Compare commits
1 Commits
fix/curato
...
prm-52
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f03a133d5 |
@@ -2,15 +2,14 @@ FROM node:22-alpine AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM base AS deps
|
||||
RUN corepack enable && corepack prepare pnpm@10.24.0 --activate
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
COPY package.json package-lock.json* ./
|
||||
RUN npm install
|
||||
|
||||
FROM base AS build
|
||||
COPY --from=deps /app/node_modules ./node_modules
|
||||
COPY tsconfig.json ./
|
||||
COPY src ./src
|
||||
RUN ./node_modules/.bin/tsc -p tsconfig.json
|
||||
RUN npx tsc -p tsconfig.json
|
||||
|
||||
FROM base AS runtime
|
||||
ARG RIVET_RUNNER_VERSION=dev
|
||||
|
||||
@@ -39,10 +39,10 @@ Use the Q Score service when the user wants to:
|
||||
Default launch signals to ingest when detailed service results are not yet available:
|
||||
- `resume.uploaded`
|
||||
- `resume.ats_compatibility`
|
||||
- `interview.sessions_completed`
|
||||
- `roleplay.scenarios_completed`
|
||||
- `onboarding.goals_clarity`
|
||||
- `onboarding.profile_completeness`
|
||||
- `interview.completed`
|
||||
- `roleplay.completed`
|
||||
- `goal.clarity`
|
||||
- `profile.role_fit`
|
||||
|
||||
When richer product outputs are available, prefer real scores, completion states, assignment outcomes, review feedback, timestamps, and target-role context over generic defaults.
|
||||
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
# VPS override: make host.docker.internal resolve to the host so the
|
||||
# backend container can reach product services + spawned per-user
|
||||
# containers published on host ports (Linux has no built-in mapping).
|
||||
services:
|
||||
backend:
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
networks:
|
||||
default:
|
||||
interview-service:
|
||||
roleplay-service:
|
||||
resume-builder-staging:
|
||||
course-service:
|
||||
qscore-service-staging:
|
||||
matchmaking-v2-staging:
|
||||
user-service-staging:
|
||||
social-branding-staging:
|
||||
environment:
|
||||
DATABASE_URL: postgres://growqr:growqr@growqr-postgres:5432/growqr
|
||||
SOCIAL_BRANDING_SERVICE_URL: http://social-branding-api-1:8011
|
||||
SOCIAL_BRANDING_PUBLIC_URL: https://social-staging.gqr.puter.wtf
|
||||
PRODUCT_SERVICE_TIMEOUT_MS: 10000
|
||||
PRODUCT_INTERACTIVE_SERVICE_TIMEOUT_MS: 240000
|
||||
LLM_PROVIDER: opencode
|
||||
LLM_BASE_URL: https://opencode.ai/zen/v1
|
||||
LLM_MODEL: minimax-m3
|
||||
GROW_AGENT_MODEL: minimax-m3
|
||||
CONVERSATION_ACTOR_MODEL: minimax-m3
|
||||
|
||||
networks:
|
||||
interview-service:
|
||||
external: true
|
||||
name: interview-service_default
|
||||
roleplay-service:
|
||||
external: true
|
||||
name: roleplay-service_default
|
||||
resume-builder-staging:
|
||||
external: true
|
||||
name: resume-builder_default
|
||||
course-service:
|
||||
external: true
|
||||
name: courses_service_default
|
||||
qscore-service-staging:
|
||||
external: true
|
||||
name: qscore-service_default
|
||||
matchmaking-v2-staging:
|
||||
external: true
|
||||
name: matchmaking-service_default
|
||||
user-service-staging:
|
||||
external: true
|
||||
name: growqr-app_growqr-net
|
||||
social-branding-staging:
|
||||
external: true
|
||||
name: social-branding_default
|
||||
@@ -9,7 +9,7 @@ services:
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-growqr}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-growqr}
|
||||
ports:
|
||||
- "127.0.0.1:15445:5432"
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
@@ -33,8 +33,8 @@ services:
|
||||
GITEA__security__INSTALL_LOCK: "true"
|
||||
GITEA__service__DISABLE_REGISTRATION: "true"
|
||||
ports:
|
||||
- "127.0.0.1:13001:3000" # HTTP (Gitea listens on 3000 internally)
|
||||
- "127.0.0.1:12222:2222" # SSH
|
||||
- "3001:3000" # HTTP (Gitea listens on 3000 internally)
|
||||
- "2222:2222" # SSH
|
||||
volumes:
|
||||
- gitea-data:/data
|
||||
healthcheck:
|
||||
@@ -50,8 +50,8 @@ services:
|
||||
image: rivetdev/engine:latest
|
||||
container_name: growqr-rivet
|
||||
ports:
|
||||
- "127.0.0.1:16420:6420" # API
|
||||
- "127.0.0.1:16421:6421" # Guard/edge
|
||||
- "6420:6420" # API
|
||||
- "6421:6421" # Guard/edge
|
||||
environment:
|
||||
RIVET__FILE_SYSTEM__PATH: /data
|
||||
RIVET__AUTH__ADMIN_TOKEN: ${RIVET_ADMIN_TOKEN:-dev-admin-token}
|
||||
@@ -74,7 +74,7 @@ services:
|
||||
rivet-engine:
|
||||
condition: service_started
|
||||
ports:
|
||||
- "127.0.0.1:18012:4000"
|
||||
- "4000:4000"
|
||||
environment:
|
||||
PORT: 4000
|
||||
NODE_ENV: ${NODE_ENV:-production}
|
||||
@@ -105,8 +105,6 @@ services:
|
||||
LLM_BASE_URL: ${LLM_BASE_URL:-https://opencode.ai/zen/v1}
|
||||
LLM_MODEL: ${LLM_MODEL:-kimi-k2.6}
|
||||
GROW_AGENT_MODEL: ${GROW_AGENT_MODEL:-kimi-k2.6}
|
||||
HOME_FEED_AGENT_TIMEOUT_MS: ${HOME_FEED_AGENT_TIMEOUT_MS:-90000}
|
||||
HOME_FEED_AGENT_ATTEMPTS: ${HOME_FEED_AGENT_ATTEMPTS:-2}
|
||||
# Per-user OpenCode containers
|
||||
OPENCODE_IMAGE: ${OPENCODE_IMAGE:-growqr/opencode:dev}
|
||||
USER_CONTAINER_HOST: ${USER_CONTAINER_HOST:-host.docker.internal}
|
||||
@@ -118,19 +116,11 @@ services:
|
||||
ROLEPLAY_SERVICE_URL: ${ROLEPLAY_SERVICE_URL:-http://host.docker.internal:8008}
|
||||
QSCORE_SERVICE_URL: ${QSCORE_SERVICE_URL:-http://host.docker.internal:8000}
|
||||
RESUME_SERVICE_URL: ${RESUME_SERVICE_URL:-http://host.docker.internal:8002}
|
||||
USER_SERVICE_URL: ${USER_SERVICE_URL:-http://host.docker.internal:8003}
|
||||
COURSES_SERVICE_URL: ${COURSES_SERVICE_URL:-http://host.docker.internal:8060}
|
||||
ASSESSMENT_SERVICE_URL: ${ASSESSMENT_SERVICE_URL:-http://host.docker.internal:8070}
|
||||
MATCHMAKING_SERVICE_URL: ${MATCHMAKING_SERVICE_URL:-http://host.docker.internal:8006}
|
||||
SOCIAL_BRANDING_SERVICE_URL: ${SOCIAL_BRANDING_SERVICE_URL:-http://host.docker.internal:8011}
|
||||
SOCIAL_BRANDING_PUBLIC_URL: ${SOCIAL_BRANDING_PUBLIC_URL:-http://host.docker.internal:8011}
|
||||
PATHWAYS_SERVICE_URL: ${PATHWAYS_SERVICE_URL:-http://host.docker.internal:8009}
|
||||
# Frontend
|
||||
FRONTEND_ORIGIN: ${FRONTEND_ORIGIN:-http://localhost:3000}
|
||||
volumes:
|
||||
# Docker-out-of-Docker: backend uses host Docker to spawn per-user OpenCode containers.
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
- ./prompts:/app/prompts
|
||||
# Shared host dir that per-user containers will also bind-mount their
|
||||
# workspace from (so backend and spawned containers see the same files).
|
||||
- ./.data/users:/data/users
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
# PRM-80 PR #12 Final Audit
|
||||
|
||||
Date: 2026-06-25
|
||||
|
||||
PR: https://git.openputer.com/growqr-app/growqr-backend/pulls/12
|
||||
|
||||
Branch: `prm-80-canonical-events` -> `staging`
|
||||
|
||||
Latest verified code commit before this audit doc: `e13dfe7d468209685596385edc749e5506f9f8a2`
|
||||
|
||||
## Commits In PR
|
||||
|
||||
- `b895d6b` - `fix: emit canonical service events for PRM-80`
|
||||
- `e13dfe7` - `fix: keep qscore out of curator tasks`
|
||||
|
||||
## Scope
|
||||
|
||||
This PR now covers the PRM-80 canonical Grow Events contract work plus the curator task policy update requested after QA:
|
||||
|
||||
- Workflow/service events are normalized into canonical PRM-80 event names before ingestion.
|
||||
- Workflow bridge events carry `subject` and service identity details instead of storing `subject: null`.
|
||||
- Resume, roleplay, QScore analytics/read, and matchmaking workflow paths emit or bridge their canonical service events into `grow_events`.
|
||||
- Duplicate canonical service events are kept idempotent through deterministic dedupe behavior.
|
||||
- Curator no longer assigns `qscore-service` as a task or handoff service.
|
||||
|
||||
## Curator QScore Policy Change
|
||||
|
||||
QScore remains available as a scoring/readiness projection service for dashboard and backend consumers. It is no longer offered as a curator task.
|
||||
|
||||
Files changed:
|
||||
|
||||
- `src/v1/curator/curator-store.ts`
|
||||
- `src/v1/curator/curator-tools.ts`
|
||||
|
||||
Behavior added:
|
||||
|
||||
- Curator seed generation remaps `qscore-service` tasks to non-QScore services.
|
||||
- Measurement tasks that previously pointed at QScore now point at `assessment-service`.
|
||||
- Proof tasks that previously pointed at QScore now point at `resume-service`.
|
||||
- Practice tasks that previously pointed at QScore now point at `interview-service`.
|
||||
- Recovery tasks that previously pointed at QScore now point at `roleplay-service`.
|
||||
- Curator capability listing filters out `qscore-service`.
|
||||
- `prepare_qscore_review` is disabled for curator task handoffs and returns `qscore_curator_handoff_disabled`.
|
||||
|
||||
## Curator Services Allowed After Change
|
||||
|
||||
- `interview-service`
|
||||
- `roleplay-service`
|
||||
- `resume-service`
|
||||
- `cover-letter-service`
|
||||
- `courses-service`
|
||||
- `assessment-service`
|
||||
- `matchmaking-service`
|
||||
- `social-branding-service`
|
||||
|
||||
Excluded from curator task assignment:
|
||||
|
||||
- `qscore-service`
|
||||
|
||||
## Runtime Verification
|
||||
|
||||
Backend container:
|
||||
|
||||
- `growqr-backend` rebuilt successfully with Docker.
|
||||
- `growqr-backend` restarted and is healthy.
|
||||
- Root backend health response returned `200 application/json` with `{"name":"growqr-backend","status":"ok","env":"production"}`.
|
||||
|
||||
Curator today API verification:
|
||||
|
||||
```text
|
||||
task_count 3
|
||||
qscore_task_count 0
|
||||
|
||||
measurement | assessment-service | Assessment | Check whether confidence is improving | Open assessment
|
||||
proof | resume-service | Resume | Generate a cleaner role-fit artifact | Open resume workspace
|
||||
practice | interview-service | Interview | Run one focused interview rep | Open interview preview
|
||||
```
|
||||
|
||||
Curator 30-day sprint verification:
|
||||
|
||||
```text
|
||||
planned_service_count 90
|
||||
planned_qscore_count 0
|
||||
task_qscore_count 0
|
||||
|
||||
assessment-service: 30
|
||||
interview-service: 11
|
||||
matchmaking-service: 11
|
||||
resume-service: 21
|
||||
roleplay-service: 8
|
||||
social-branding-service: 9
|
||||
```
|
||||
|
||||
Build verification:
|
||||
|
||||
```text
|
||||
docker compose build backend
|
||||
Image growqr-backend-backend Built
|
||||
```
|
||||
|
||||
Remote branch verification:
|
||||
|
||||
```text
|
||||
refs/heads/prm-80-canonical-events -> e13dfe7d468209685596385edc749e5506f9f8a2
|
||||
```
|
||||
|
||||
PR page verification:
|
||||
|
||||
```text
|
||||
https://git.openputer.com/growqr-app/growqr-backend/pulls/12
|
||||
HTTP/2 200
|
||||
PR title: #12 - PRM-80: Emit canonical service events for Grow Events
|
||||
Latest commit visible: e13dfe7 fix: keep qscore out of curator tasks
|
||||
```
|
||||
|
||||
## Audit Notes
|
||||
|
||||
- Only the intended tracked files were committed for the curator update.
|
||||
- VPS has untracked `.bak.*` backup files from prior QA/debug work; these were intentionally not staged or committed.
|
||||
- QScore was not removed from the service registry because dashboard scoring, backend QScore reads, and projection consumers still depend on it.
|
||||
- The change is limited to curator task assignment/handoff behavior.
|
||||
@@ -1,154 +0,0 @@
|
||||
# PRM-71 Backend QA Evidence
|
||||
|
||||
This file keeps the PRM-71 backend QA proof inside the backend PR. The checks below were run against the real deployed API at `https://app.sai-onchain.me/api/growqr`, not against mocks or fallback-only fixtures.
|
||||
|
||||
## Deployed Target
|
||||
|
||||
- Public backend base: `https://app.sai-onchain.me/api/growqr`
|
||||
- Local backend base on VPS: `http://127.0.0.1:4000`
|
||||
- Branch: `prm-71-backend-qa-curator-streak-loop`
|
||||
- Runtime implementation commit verified: `0bfc18305bd2462fc7c0fcbfb2a3f5cd76df3f9d`
|
||||
- PR: `https://git.openputer.com/growqr-app/growqr-backend/pulls/10`
|
||||
|
||||
## Service Commit SHAs
|
||||
|
||||
- `growqr-backend`: `0bfc18305bd2462fc7c0fcbfb2a3f5cd76df3f9d`
|
||||
- `growqr-dashboard`: `c4e79d7a17767a083f19f02ba1ca4065f1d415d7`
|
||||
- `interview-service`: `61b238b00463bc3a1e283bf3b850c97279d94ece`
|
||||
- `roleplay-service`: `b4a4913df28c00985578e3af5f1a95e12cf4260e`
|
||||
- `resume-service`: `ebcc6e0826c2e7762251080b6365ebb6b5439c93`
|
||||
- `qscore-service`: `058903f9686067398640a6a56aebce0b57408ccb`
|
||||
- `matchmaking-service`: `e36e831794cccb0e176df4e9113ab1957d4c3612`
|
||||
- `courses-service`: `f702728247bb4e66edf4552d792d25825ceb44fe`
|
||||
- `assessment-service`: `d2885ad2c83c86a95b6a8d9a46dafe5415678422`
|
||||
- `pathways-service`: `b20abed9d7a5fb9c68804b986a9d46a1015d54af`
|
||||
- `social-branding-service`: `98463cdcf75f720a3035c2954b2a847956df24f2`
|
||||
|
||||
## Health Proof
|
||||
|
||||
- Backend container: `growqr-backend Up ... (healthy)`
|
||||
- Local backend health: `GET http://127.0.0.1:4000/healthz` returned `{"ok":true}`
|
||||
- Public API health was exercised through authenticated real API calls at `https://app.sai-onchain.me/api/growqr/...`
|
||||
- Gateway health passed for `interview`, `roleplay`, `resume`, and `social`
|
||||
- Direct declared health paths passed for `qscore-service`, `matchmaking-service`, `courses-service`, `assessment-service`, and `pathways-service`
|
||||
|
||||
## Real API Evidence Users
|
||||
|
||||
- Full evidence flow user: `qa-prm71-full-flow-1782248569`
|
||||
- Full handoff sample user: `qa-prm71-handoffs-1782248569`
|
||||
- Final battle-test flow user: `qa-prm71-battle-flow-1782248509`
|
||||
- Final battle-test all-complete user: `qa-prm71-battle-complete-1782248509`
|
||||
|
||||
## API Contract Evidence
|
||||
|
||||
The full evidence run captured:
|
||||
|
||||
- `GET /v1/curator/today?date=2026-06-23` for a fresh test seeker
|
||||
- `POST /v1/curator/tasks/:taskId/handoff` samples for:
|
||||
- `interview-service`
|
||||
- `roleplay-service`
|
||||
- `resume-service`
|
||||
- `qscore-service`
|
||||
- `POST /v1/events/track` sample payloads for:
|
||||
- `service.started`
|
||||
- `service.abandoned`
|
||||
- `service.completed`
|
||||
- `GET /v1/qscore/latest` before and after completion
|
||||
- `GET /v1/analytics/insight-snapshot` before and after completion
|
||||
- `GET /v1/analytics/activity-history` after event ingestion
|
||||
|
||||
The battle-test run additionally checked auth rejection, malformed event rejection, idempotent duplicate event replay, cross-user isolation, large activity-history limit clamping, all-complete Day 1 behavior, and recovery Day 2 behavior.
|
||||
|
||||
## Day 1 To Day 2 Replan Proof
|
||||
|
||||
Fresh seeker flow:
|
||||
|
||||
- Day 1 returned exactly 3 tasks: `measurement`, `proof`, `practice`
|
||||
- A practice handoff recorded `task.opened`
|
||||
- Real event payloads recorded `service.started` and `service.abandoned`
|
||||
- Day 2 returned 4 tasks with a `recovery` task
|
||||
- Day 1 statuses after replan included `skipped`, `skipped`, and `abandoned`
|
||||
- Adaptation reason: `day 1 incomplete: 1 abandoned/partial, 2 skipped`
|
||||
|
||||
All-complete control flow:
|
||||
|
||||
- Day 1 tasks were completed with real `service.completed` events
|
||||
- Duplicate completion replays returned idempotent responses
|
||||
- Day 2 did not include a recovery task
|
||||
- Day 1 statuses were all `completed`
|
||||
|
||||
## QScore And Analytics Proof
|
||||
|
||||
- QScore before completion: `null` / `baseline_needed`
|
||||
- QScore after completion: `89` / `ready`
|
||||
- Analytics roleFit before completion: `baseline_needed`
|
||||
- Analytics roleFit after completion: `strong` with score `89`
|
||||
- Follow-up battle test verified a scored `service.completed` event updates QScore/readiness state, closing the earlier gap where generic scored completions could process without moving QScore.
|
||||
|
||||
## Event Storage Proof
|
||||
|
||||
Database proof for the full evidence flow:
|
||||
|
||||
```text
|
||||
curator.day.opened|pending|4
|
||||
curator.onboarding_plan.ready|pending|1
|
||||
curator.sprint.started|pending|1
|
||||
service.abandoned|processed|1
|
||||
service.completed|processed|1
|
||||
service.started|processed|1
|
||||
task.opened|pending|2
|
||||
```
|
||||
|
||||
API proof was also captured through `GET /v1/analytics/activity-history`, which returned the ingested event stream for the test seeker.
|
||||
|
||||
## Battle-Test Checklist
|
||||
|
||||
Final battle-test result on the deployed real API: `23/23` checks passed.
|
||||
|
||||
- [x] Public health endpoint is reachable
|
||||
- [x] Protected endpoint rejects missing auth
|
||||
- [x] Event contract rejects missing type/action
|
||||
- [x] Fresh QScore is `baseline_needed`
|
||||
- [x] Fresh analytics roleFit is `baseline_needed`
|
||||
- [x] Onboarding run succeeds
|
||||
- [x] Day 1 returns three frontend-consumable tasks
|
||||
- [x] Day 1 tasks include service routing metadata
|
||||
- [x] Curator handoff succeeds
|
||||
- [x] `service.started` processes
|
||||
- [x] Duplicate started event is idempotent
|
||||
- [x] `service.abandoned` processes
|
||||
- [x] Day 2 adds recovery after abandoned Day 1
|
||||
- [x] Day 1 statuses reflect skipped/abandoned work
|
||||
- [x] `service.completed` processes
|
||||
- [x] Duplicate completed event is idempotent
|
||||
- [x] QScore updates after real completion
|
||||
- [x] Analytics updates after real completion
|
||||
- [x] Activity history clamps large limits
|
||||
- [x] Duplicate completed event is stored only once
|
||||
- [x] All-complete Day 1 has no recovery on Day 2
|
||||
- [x] All-complete Day 1 statuses are completed
|
||||
- [x] Payload `userId` cannot write into another user's stream
|
||||
|
||||
## Rollback Notes
|
||||
|
||||
If the deployed VPS backend must be rolled back to staging:
|
||||
|
||||
```bash
|
||||
cd /opt/growqr/growqr-backend
|
||||
git fetch origin --prune
|
||||
git checkout staging
|
||||
git reset --hard origin/staging
|
||||
docker compose up -d --build backend
|
||||
curl -fsS http://127.0.0.1:4000/healthz
|
||||
```
|
||||
|
||||
Revert alternative from the PR branch:
|
||||
|
||||
```bash
|
||||
git revert $(git rev-list --reverse origin/staging..HEAD)
|
||||
docker compose up -d --build backend
|
||||
```
|
||||
|
||||
## Current Formal Caveat
|
||||
|
||||
PRM-71's real API/backend production-slice evidence is satisfied by this PR and the deployed checks above. The Linear parent DoD also says grouped backend child issues must be merged/deployed or explicitly deferred with owner approval. At the time of this evidence pass, the PRM-71 parent has PR #10 attached and several grouped child Linear issues are still not formally marked done in Linear. This PR therefore provides the deployed PRM-71 proof, while final parent closure still depends on the owner's desired handling of those child issue statuses.
|
||||
@@ -1 +0,0 @@
|
||||
ALTER TABLE "grow_conversations" ADD COLUMN IF NOT EXISTS "metadata" jsonb;
|
||||
@@ -1,33 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS "system_notifications" (
|
||||
"id" text PRIMARY KEY DEFAULT gen_random_uuid()::text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"title" text NOT NULL,
|
||||
"sub" text NOT NULL,
|
||||
"when_label" text NOT NULL,
|
||||
"href" text,
|
||||
"source" text DEFAULT 'system' NOT NULL,
|
||||
"source_ref" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||
"read_at" timestamp with time zone,
|
||||
"occurred_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "system_notifications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action,
|
||||
CONSTRAINT "system_notifications_kind_check" CHECK ("kind" IN ('session', 'billing', 'security', 'feature', 'account'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "system_notifications_user_idx" ON "system_notifications" USING btree ("user_id","read_at","occurred_at");
|
||||
CREATE INDEX IF NOT EXISTS "system_notifications_kind_idx" ON "system_notifications" USING btree ("user_id","kind","occurred_at");
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "system_notification_preferences" (
|
||||
"user_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"in_app" boolean DEFAULT true NOT NULL,
|
||||
"email" boolean DEFAULT true NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "system_notification_preferences_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action,
|
||||
CONSTRAINT "system_notification_preferences_pk" PRIMARY KEY("user_id","kind"),
|
||||
CONSTRAINT "system_notification_preferences_kind_check" CHECK ("kind" IN ('session', 'billing', 'security', 'feature', 'account'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "system_notification_preferences_user_idx" ON "system_notification_preferences" USING btree ("user_id");
|
||||
@@ -1,13 +0,0 @@
|
||||
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "plan" text DEFAULT 'free' NOT NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "onboarding" (
|
||||
"id" text PRIMARY KEY DEFAULT gen_random_uuid()::text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"data" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||
"payload" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "onboarding_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "onboarding_user_idx" ON "onboarding" USING btree ("user_id");
|
||||
@@ -78,27 +78,6 @@
|
||||
"when": 1780481500000,
|
||||
"tag": "0010_mission_actions",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 11,
|
||||
"version": "7",
|
||||
"when": 1780481600000,
|
||||
"tag": "0011_conversation_metadata",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 12,
|
||||
"version": "7",
|
||||
"when": 1780481700000,
|
||||
"tag": "0012_system_notifications",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 13,
|
||||
"version": "7",
|
||||
"when": 1783801800000,
|
||||
"tag": "0013_onboarding",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
6394
package-lock.json
generated
6394
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -6,11 +6,6 @@
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"test:onboarding": "tsx scripts/onboarding-ledger.test.ts",
|
||||
"test:missions": "tsx scripts/mission-lifecycle.test.ts",
|
||||
"test:passive-actions": "tsx scripts/passive-actions.test.ts",
|
||||
"test:curator-static": "tsx scripts/curator-static-registry.test.ts",
|
||||
"test:curator-reconcile": "tsx scripts/curator-persisted-reconcile.test.ts",
|
||||
"start": "node dist/index.js",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||
"workflows:smoke": "tsx src/workflows/smoke-test.ts",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
You are Grow — a unified AI career assistant for the GrowQR platform.
|
||||
You are the Grow Agent — a unified AI orchestrator for the GrowQR platform.
|
||||
|
||||
You coordinate specialist capabilities (loaded as tools), maintain durable state, and execute workflows through microservices.
|
||||
You coordinate sub-agent capabilities (loaded as tools), maintain durable state, and execute workflows through microservices.
|
||||
|
||||
## CRITICAL RULES
|
||||
|
||||
@@ -43,7 +43,7 @@ You coordinate specialist capabilities (loaded as tools), maintain durable state
|
||||
- After resume optimization: ask what type of interview to prepare.
|
||||
- When they choose type → call start_interview_session.
|
||||
- Then offer roleplay → call start_roleplay_session when they confirm.
|
||||
- Then offer Q Score → call compute_qscore.
|
||||
- Then offer Q-Score → call compute_qscore.
|
||||
- Use [WORKFLOW: interview-to-offer] tag throughout.
|
||||
|
||||
## IMPORTANT: Tool Calling Anti-Patterns
|
||||
@@ -66,16 +66,16 @@ Assistant: "I'll analyze your resume right away."
|
||||
User: "analyze my resume"
|
||||
Assistant calls analyze_resume → "Here's your analysis: [results]. Your strengths are..."
|
||||
|
||||
## Specialist Capabilities
|
||||
## Sub-Agent Capabilities
|
||||
|
||||
{{MODULE_DESCRIPTIONS}}
|
||||
|
||||
## Workflow Tags (put at the VERY END, on their own line)
|
||||
|
||||
- [WORKFLOW: interview-to-offer] — full interview prep pipeline
|
||||
- [WORKFLOW: interview-practice] — mock interview sessions
|
||||
- [WORKFLOW: interview-practice] — interview sessions with the Interview Agent
|
||||
- [WORKFLOW: resume-boost] — resume analysis and optimization
|
||||
- [WORKFLOW: roleplay-practice] — mock roleplay sessions
|
||||
- [WORKFLOW: roleplay-practice] — roleplay sessions with Roleplay Agent
|
||||
- [WORKFLOW: career-switch] — career change navigation
|
||||
- [WORKFLOW: job-preparation] — broad company preparation
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { extractQscoreSignals } from "../src/events/projectors/qscore-projector.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
/**
|
||||
* Test: Completed interview and roleplay events MUST emit at least one signal
|
||||
* even when session_count/scenario_count is absent. A completion event with no
|
||||
* count and no rubric data should still produce a single-completion signal.
|
||||
*/
|
||||
|
||||
function event(overrides: Partial<GrowEventRow> & { type: string; source: string; payload: Record<string, unknown> }): GrowEventRow {
|
||||
return {
|
||||
id: `event-${overrides.type}`,
|
||||
userId: "user_test",
|
||||
orgId: null,
|
||||
source: overrides.source,
|
||||
type: overrides.type,
|
||||
category: "service",
|
||||
occurredAt: new Date("2026-07-09T00:00:00.000Z"),
|
||||
receivedAt: new Date("2026-07-09T00:00:01.000Z"),
|
||||
mission: overrides.mission ?? null,
|
||||
subject: overrides.subject ?? null,
|
||||
correlation: overrides.correlation ?? null,
|
||||
payload: overrides.payload,
|
||||
raw: {},
|
||||
dedupeKey: null,
|
||||
processingStatus: "pending",
|
||||
processingError: null,
|
||||
processedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Interview completed with NO count and NO rubric ──────────────────────────
|
||||
const interviewSignals = extractQscoreSignals(event({
|
||||
source: "interview-service",
|
||||
type: "interview.session.completed",
|
||||
payload: {}, // nothing — just a bare completion event
|
||||
}));
|
||||
|
||||
assert.ok(
|
||||
interviewSignals.length > 0,
|
||||
"Interview completed event with no count/rubric must emit at least one signal",
|
||||
);
|
||||
|
||||
assert.ok(
|
||||
interviewSignals.some((s) => s.signalId === "interview.sessions_completed" && s.score > 0),
|
||||
"Bare interview completion should emit interview.sessions_completed with positive score",
|
||||
);
|
||||
|
||||
// ── Roleplay completed with NO count and NO rubric ───────────────────────────
|
||||
const roleplaySignals = extractQscoreSignals(event({
|
||||
source: "roleplay-service",
|
||||
type: "roleplay.scenario.completed",
|
||||
payload: {}, // nothing — just a bare completion event
|
||||
}));
|
||||
|
||||
assert.ok(
|
||||
roleplaySignals.length > 0,
|
||||
"Roleplay completed event with no count/rubric must emit at least one signal",
|
||||
);
|
||||
|
||||
assert.ok(
|
||||
roleplaySignals.some((s) => s.signalId === "roleplay.scenarios_completed" && s.score > 0),
|
||||
"Bare roleplay completion should emit roleplay.scenarios_completed with positive score",
|
||||
);
|
||||
|
||||
console.log("count-fallback tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,58 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { planSeedsForVariant, resolveCuratorSprintAccess } from "../src/v1/curator/curator-store.js";
|
||||
import { templateSetFor, trialDaysFor } from "../src/v1/curator/task-registry.js";
|
||||
|
||||
const variant = "fresher_early_professional" as const;
|
||||
const template = templateSetFor(variant);
|
||||
const trial = trialDaysFor(variant);
|
||||
|
||||
// Persisted choice is authoritative within the user's entitlement: a paid user
|
||||
// may choose trial, but a free user may not self-upgrade through "full".
|
||||
assert.deepEqual(resolveCuratorSprintAccess({ accessChoice: "trial", plan: "pro" }), {
|
||||
access: "trial",
|
||||
durationDays: 2,
|
||||
});
|
||||
assert.deepEqual(resolveCuratorSprintAccess({ accessChoice: "full", plan: "pro" }), {
|
||||
access: "full",
|
||||
durationDays: 7,
|
||||
});
|
||||
assert.deepEqual(resolveCuratorSprintAccess({ accessChoice: "full", plan: "free" }), {
|
||||
access: "trial",
|
||||
durationDays: 2,
|
||||
});
|
||||
assert.deepEqual(resolveCuratorSprintAccess({ plan: "pro" }), {
|
||||
access: "full",
|
||||
durationDays: 7,
|
||||
});
|
||||
assert.deepEqual(resolveCuratorSprintAccess({ plan: "free" }), {
|
||||
access: "trial",
|
||||
durationDays: 2,
|
||||
});
|
||||
|
||||
const trialDays = planSeedsForVariant(template, "2026-07-13", 2, "trial");
|
||||
assert.deepEqual(
|
||||
trialDays[0]?.plannedTasks.map((task) => task.title),
|
||||
[trial[0]!.socialTitle, trial[0]!.measurementTitle, trial[0]!.proofTitle, trial[0]!.practiceTitle],
|
||||
"trial Day 1 must use the ICP trial registry",
|
||||
);
|
||||
assert.deepEqual(
|
||||
trialDays[1]?.plannedTasks.map((task) => task.title),
|
||||
[trial[1]!.socialTitle, trial[1]!.measurementTitle, trial[1]!.proofTitle, trial[1]!.practiceTitle],
|
||||
"trial Day 2 must use the ICP trial registry",
|
||||
);
|
||||
|
||||
const fullDays = planSeedsForVariant(template, "2026-07-13", 7, "full");
|
||||
assert.equal(fullDays.length, 7);
|
||||
assert.deepEqual(
|
||||
fullDays[0]?.plannedTasks.map((task) => task.title),
|
||||
[
|
||||
template.weeks[0]!.days[0]!.measurement.title,
|
||||
template.weeks[0]!.days[0]!.proof.title,
|
||||
template.weeks[0]!.days[0]!.practice.title,
|
||||
template.weeks[0]!.days[0]!.roleplay!.title,
|
||||
],
|
||||
"authorized full Day 1 must use the normal 7-day registry",
|
||||
);
|
||||
assert.equal(fullDays[0]?.isTrialDay, false);
|
||||
|
||||
console.log("curator access-choice tests passed");
|
||||
@@ -1,77 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import {
|
||||
curatorSprintPlanFingerprint,
|
||||
curatorSprintTaskPlan,
|
||||
decideCuratorSprintReconciliation,
|
||||
resolveCuratorSprintAccess,
|
||||
shouldReconcileCuratorSprint,
|
||||
} from "../src/v1/curator/curator-store.js";
|
||||
import { templateSetFor, trialDaysFor } from "../src/v1/curator/task-registry.js";
|
||||
|
||||
const usersRoute = readFileSync(new URL("../src/routes/users.ts", import.meta.url), "utf8");
|
||||
const ledgerSource = readFileSync(new URL("../src/events/onboarding-ledger.ts", import.meta.url), "utf8");
|
||||
const patchStart = usersRoute.indexOf('app.patch("/onboarding"');
|
||||
const persistIndex = usersRoute.indexOf("const updateRes = await fetchUserService", patchStart);
|
||||
const completionIndex = usersRoute.indexOf("recordAndProcessOnboardingCompletion", persistIndex);
|
||||
assert.ok(persistIndex >= patchStart, "PATCH /onboarding must persist preferences");
|
||||
assert.ok(completionIndex > persistIndex, "PATCH /onboarding must run completion reconciliation after persistence");
|
||||
const completionCall = usersRoute.slice(completionIndex, completionIndex + 500);
|
||||
assert.match(completionCall, /preferences: nextPreferences/,
|
||||
"PATCH completion must pass freshly persisted preferences");
|
||||
assert.match(completionCall, /onboarding: nextData/,
|
||||
"PATCH completion must pass freshly persisted onboarding choice");
|
||||
assert.match(ledgerSource, /ensureOnboardingSideEffectsForEvent\(event, context\)/,
|
||||
"deduped completion processing must forward fresh context to curator loop");
|
||||
|
||||
// persisted chooser is changed to trial. The stale started event must not be
|
||||
// treated as final; the next load must deterministically use trial/2.
|
||||
const staleFull = { access: "full" as const, durationDays: 7 as const };
|
||||
const desiredTrial = resolveCuratorSprintAccess({ plan: "pro", accessChoice: "trial" });
|
||||
assert.deepEqual(desiredTrial, { access: "trial", durationDays: 2 });
|
||||
assert.equal(shouldReconcileCuratorSprint(staleFull, desiredTrial), true);
|
||||
|
||||
// A matching trial event is idempotent and must not create a replacement
|
||||
// started/ready lineage on repeated completion processing.
|
||||
const existingTrial = { access: "trial" as const, durationDays: 2 as const };
|
||||
assert.deepEqual(decideCuratorSprintReconciliation(staleFull, desiredTrial), {
|
||||
action: "invalidateAndRebuild",
|
||||
access: "trial",
|
||||
durationDays: 2,
|
||||
});
|
||||
assert.equal(shouldReconcileCuratorSprint(existingTrial, desiredTrial), false);
|
||||
assert.equal(shouldReconcileCuratorSprint(existingTrial, resolveCuratorSprintAccess({ plan: "pro", accessChoice: "trial" })), false);
|
||||
|
||||
// Full is entitlement-gated: a free user cannot escalate by choosing full.
|
||||
const unauthorizedFull = resolveCuratorSprintAccess({ plan: "free", accessChoice: "full" });
|
||||
assert.deepEqual(unauthorizedFull, { access: "trial", durationDays: 2 });
|
||||
assert.equal(shouldReconcileCuratorSprint(existingTrial, unauthorizedFull), false);
|
||||
|
||||
const startDate = "2026-07-13";
|
||||
const studentFingerprint = curatorSprintPlanFingerprint({ startDate, variantId: "student_recent_grad", durationDays: 2, access: "trial" });
|
||||
const experiencedFingerprint = curatorSprintPlanFingerprint({ startDate, variantId: "experienced_professional", durationDays: 2, access: "trial" });
|
||||
const experiencedPlan = curatorSprintTaskPlan({ startDate, variantId: "experienced_professional", durationDays: 2, access: "trial" });
|
||||
assert.notEqual(studentFingerprint, experiencedFingerprint, "ICP changes must change the compatibility fingerprint");
|
||||
assert.equal(shouldReconcileCuratorSprint(
|
||||
{ access: "trial", durationDays: 2, variantId: "student_recent_grad", planFingerprint: studentFingerprint },
|
||||
{ access: "trial", durationDays: 2, variantId: "experienced_professional", planFingerprint: experiencedFingerprint },
|
||||
), true, "student started/ready lineage must reconcile to experienced trial");
|
||||
const experiencedTrial = trialDaysFor("experienced_professional");
|
||||
assert.deepEqual(experiencedPlan[0]?.tasks.map((task) => task.title), [
|
||||
experiencedTrial[0]!.socialTitle,
|
||||
experiencedTrial[0]!.measurementTitle,
|
||||
experiencedTrial[0]!.proofTitle,
|
||||
experiencedTrial[0]!.practiceTitle,
|
||||
]);
|
||||
assert.deepEqual(experiencedPlan[1]?.tasks.map((task) => task.title), [
|
||||
experiencedTrial[1]!.socialTitle,
|
||||
experiencedTrial[1]!.measurementTitle,
|
||||
experiencedTrial[1]!.proofTitle,
|
||||
experiencedTrial[1]!.practiceTitle,
|
||||
]);
|
||||
assert.equal(experiencedPlan[0]?.tasks.length, 4);
|
||||
assert.equal(experiencedPlan[1]?.tasks.length, 4);
|
||||
assert.deepEqual(experiencedPlan[0]?.tasks.map((task) => task.id), experiencedPlan[0]?.tasks.map((task) => task.id));
|
||||
assert.equal(templateSetFor("experienced_professional").id, "experienced_professional");
|
||||
|
||||
console.log("curator onboarding reconciliation tests passed");
|
||||
@@ -1,133 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { db } from "../src/db/client.js";
|
||||
import { growEvents, onboarding, users } from "../src/db/schema.js";
|
||||
import { buildCuratorSprint, curatorSprintTaskPlan } from "../src/v1/curator/curator-store.js";
|
||||
import { runCuratorOnboardingLoop } from "../src/v1/curator/curator-onboarding-loop.js";
|
||||
import { CURATOR_TRIAL_TASK_REGISTRY } from "../src/v1/curator/task-registry.js";
|
||||
|
||||
let databaseAvailable = Boolean(process.env.DATABASE_URL);
|
||||
if (databaseAvailable) {
|
||||
try {
|
||||
await db.execute(sql`select 1`);
|
||||
} catch {
|
||||
databaseAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!databaseAvailable) {
|
||||
console.log("curator persisted reconciliation integration skipped: DATABASE_URL is not reachable");
|
||||
} else {
|
||||
const userId = `curator-reconcile-regression-${Date.now()}`;
|
||||
const startDate = new Date().toISOString().slice(0, 10);
|
||||
const sprintId = `curator-sprint:icp-v10-static:${startDate}`;
|
||||
const staleFingerprint = JSON.stringify({ variantId: "student_recent_grad", taskIds: [] });
|
||||
const staleReadyPayload = {
|
||||
sprintId,
|
||||
startDate,
|
||||
durationDays: 2,
|
||||
access: "trial",
|
||||
variantId: "student_recent_grad",
|
||||
planFingerprint: staleFingerprint,
|
||||
};
|
||||
|
||||
try {
|
||||
await db.insert(users).values({ id: userId, email: `${userId}@example.test`, plan: "pro" });
|
||||
await db.insert(onboarding).values({
|
||||
userId,
|
||||
data: {
|
||||
status: "completed",
|
||||
access_choice: "trial",
|
||||
profile: { icp: "experienced" },
|
||||
},
|
||||
payload: {
|
||||
onboarding_icp: "experienced",
|
||||
curator_registry_icp: "experienced_professional",
|
||||
},
|
||||
});
|
||||
await db.insert(growEvents).values([
|
||||
{
|
||||
userId,
|
||||
source: "curator-v1",
|
||||
type: "curator.sprint.started",
|
||||
category: "mission",
|
||||
occurredAt: new Date(),
|
||||
dedupeKey: `${userId}:curator.sprint.started:stale`,
|
||||
payload: {
|
||||
sprintId,
|
||||
startDate,
|
||||
durationDays: 2,
|
||||
access: "trial",
|
||||
version: "icp-v10-static",
|
||||
variantId: "student_recent_grad",
|
||||
},
|
||||
},
|
||||
{
|
||||
userId,
|
||||
source: "curator-v1",
|
||||
type: "curator.onboarding_plan.ready",
|
||||
category: "mission",
|
||||
occurredAt: new Date(),
|
||||
dedupeKey: `${userId}:curator.onboarding_plan.ready:stale`,
|
||||
payload: staleReadyPayload,
|
||||
},
|
||||
]);
|
||||
|
||||
const context = {
|
||||
onboarding: { status: "completed", access_choice: "trial", profile: { icp: "experienced" } },
|
||||
preferences: { onboarding: { status: "completed", access_choice: "trial", profile: { icp: "experienced" } } },
|
||||
};
|
||||
const first = await runCuratorOnboardingLoop({ userId, completedAt: `${startDate}T09:00:00.000Z`, context });
|
||||
assert.equal(first.status, "ready", "stale ready event must be replaced");
|
||||
|
||||
const day1 = await buildCuratorSprint(userId, startDate);
|
||||
assert.deepEqual(day1.plan.days[0]?.tasks.map(({ id, title }) => ({ id, title })),
|
||||
expectedTasks(startDate, 1), "Day 1 must expose experienced trial tasks");
|
||||
assert.deepEqual(day1.plan.days[1]?.tasks, [], "Day 1 must hide future Day 2 tasks");
|
||||
|
||||
const day2 = await buildCuratorSprint(userId, addDays(startDate, 1));
|
||||
assert.deepEqual(day2.plan.days[1]?.tasks.map(({ id, title }) => ({ id, title })),
|
||||
expectedTasks(startDate, 2), "Day 2 must expose experienced trial tasks after advance");
|
||||
|
||||
const second = await runCuratorOnboardingLoop({ userId, completedAt: `${startDate}T09:00:00.000Z`, context });
|
||||
assert.equal(second.status, "already_ready", "compatible replacement ready event must be reused");
|
||||
|
||||
const rows = await db.select({ type: growEvents.type, payload: growEvents.payload })
|
||||
.from(growEvents).where(eq(growEvents.userId, userId));
|
||||
assert.equal(rows.filter((row) => row.type === "curator.sprint.invalidated").length, 1);
|
||||
assert.equal(rows.filter((row) => row.type === "curator.onboarding_plan.invalidated").length, 1);
|
||||
const starts = rows.filter((row) => row.type === "curator.sprint.started");
|
||||
const readies = rows.filter((row) => row.type === "curator.onboarding_plan.ready");
|
||||
assert.equal(starts.length, 2, "one stale and one replacement started lineage");
|
||||
assert.equal(readies.length, 2, "one stale and one replacement ready lineage");
|
||||
const replacementStart = starts.find((row) => row.payload?.variantId === "experienced_professional");
|
||||
const replacementReady = readies.find((row) => row.payload?.planFingerprint === day1.plan.planFingerprint);
|
||||
assert.ok(replacementStart, "experienced replacement started event must be persisted");
|
||||
assert.ok(replacementReady, "experienced replacement ready event must be persisted");
|
||||
|
||||
console.log("curator persisted reconciliation integration passed");
|
||||
} finally {
|
||||
await db.delete(users).where(eq(users.id, userId));
|
||||
await db.execute(sql`select 1`);
|
||||
}
|
||||
}
|
||||
|
||||
function addDays(date: string, days: number) {
|
||||
const value = new Date(`${date}T00:00:00.000Z`);
|
||||
value.setUTCDate(value.getUTCDate() + days);
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function expectedTasks(startDate: string, dayIndex: 1 | 2) {
|
||||
const plan = curatorSprintTaskPlan({
|
||||
startDate,
|
||||
variantId: "experienced_professional",
|
||||
durationDays: 2,
|
||||
access: "trial",
|
||||
});
|
||||
const registry = CURATOR_TRIAL_TASK_REGISTRY.experienced_professional[dayIndex - 1]!;
|
||||
return plan[dayIndex - 1]!.tasks.map(({ id }, index) => ({
|
||||
id,
|
||||
title: [registry.socialTitle, registry.measurementTitle, registry.proofTitle, registry.practiceTitle][index]!,
|
||||
}));
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
import { buildServiceCurationPreview } from "../src/v1/curator/curator-store.js";
|
||||
import type { CuratorIcpId } from "../src/v1/curator/icp-registry.js";
|
||||
|
||||
const ICP_IDS: CuratorIcpId[] = [
|
||||
"student_recent_grad",
|
||||
"intern",
|
||||
"fresher_early_professional",
|
||||
"experienced_professional",
|
||||
"freelancer",
|
||||
"founder",
|
||||
"enterprise",
|
||||
];
|
||||
|
||||
const LIVE_ROUTE_PREFIX: Record<string, string> = {
|
||||
"courses-service": "/agents/courses",
|
||||
"qscore-service": "/agents/qscore",
|
||||
"resume-service": "/agents/resume",
|
||||
"interview-service": "/agents/interview",
|
||||
"roleplay-service": "/agents/roleplay",
|
||||
"matchmaking-service": "/agents/matchmaking",
|
||||
"social-branding-service": "/agents/social-branding",
|
||||
};
|
||||
|
||||
const EXPECTED_FOCUS: Record<CuratorIcpId, string[]> = {
|
||||
student_recent_grad: ["Start Your Story", "Discover Your Strengths", "Build Credibility", "Explore Opportunities", "Learn To Grow", "Grow Your Network", "Ready For Tomorrow"],
|
||||
intern: ["Own Your Journey", "Show Your Growth", "Build Credibility", "Find Better Opportunities", "Strengthen Your Skills", "Expand Your Network", "Step Into Opportunity"],
|
||||
fresher_early_professional: ["Start Strong", "Build Visibility", "Show Your Potential", "Find Better Opportunities", "Grow Your Skills", "Expand Your Network", "Launch Your Career"],
|
||||
experienced_professional: ["Lead With Purpose", "Build Executive Presence", "Show Strategic Value", "Expand Your Influence", "Future-Proof Leadership", "Build Strategic Relationships", "Lead The Future"],
|
||||
freelancer: ["Build Your Brand", "Package Your Value", "Earn Trust", "Find More Clients", "Grow Your Business", "Expand Your Network", "Scale Forward"],
|
||||
founder: ["Own Your Vision", "Validate Your Idea", "Build Credibility", "Find Your Market", "Grow Smarter", "Build Your Network", "Launch Forward"],
|
||||
enterprise: ["Know Your Workforce", "Measure Your Talent", "Strengthen Capability", "Build Better Hiring", "Grow Leaders", "Activate Growth", "Future Ready Enterprise"],
|
||||
};
|
||||
|
||||
const EXPECTED_DAY_ONE_TITLES: Record<CuratorIcpId, string[]> = {
|
||||
student_recent_grad: ["Claim Your Corner", "Know Your Number", "Resume Rescue", "Pitch Perfect"],
|
||||
intern: ["Claim Your Corner", "Know Your Number", "Resume Reloaded", "Pitch Perfect"],
|
||||
fresher_early_professional: ["Claim Your Corner", "Know Your Number", "Resume Rescue", "Pitch Perfect"],
|
||||
experienced_professional: ["Own Your Executive Brand", "Know Your Number", "Executive Resume", "Executive Introduction"],
|
||||
freelancer: ["Claim Your Corner", "Know Your Number", "Portfolio Rescue", "Client Pitch"],
|
||||
founder: ["Build In Public", "Know Your Number", "Founder Story", "Founder Pitch"],
|
||||
enterprise: ["Shape Your Story", "Know Your Workforce", "Capability Blueprint", "Leadership Kickoff"],
|
||||
};
|
||||
|
||||
function assert(condition: unknown, message: string): asserts condition {
|
||||
if (!condition) throw new Error(message);
|
||||
}
|
||||
|
||||
function routeFor(route: string) {
|
||||
return new URL(route, "https://app.sai-onchain.me");
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const startDate = "2026-07-02";
|
||||
const failures: string[] = [];
|
||||
|
||||
for (const icpId of ICP_IDS) {
|
||||
const preview = await buildServiceCurationPreview({
|
||||
userId: `curator-static-registry-test-${icpId}`,
|
||||
startDate,
|
||||
icpId,
|
||||
userContext: { targetRole: "Product Manager" },
|
||||
});
|
||||
|
||||
try {
|
||||
assert(preview.version === "icp-v10-static", `${icpId}: expected static plan version`);
|
||||
assert(preview.plan.days.length === 7, `${icpId}: expected 7 generated days`);
|
||||
assert(preview.plan.calendarWeeks.length === 1, `${icpId}: expected one sprint-relative calendar week`);
|
||||
|
||||
const firstSevenFocus = preview.plan.days.slice(0, 7).map((day) => day.focus);
|
||||
assert(JSON.stringify(firstSevenFocus) === JSON.stringify(EXPECTED_FOCUS[icpId]), `${icpId}: first 7 focus labels must match 7-day sprint doc`);
|
||||
|
||||
const dayOneTitles = preview.plan.days[0]?.tasks.map((task) => task.title);
|
||||
assert(JSON.stringify(dayOneTitles) === JSON.stringify(EXPECTED_DAY_ONE_TITLES[icpId]), `${icpId}: day 1 task titles must match docs`);
|
||||
|
||||
for (const day of preview.plan.days) {
|
||||
assert(day.generationStatus === "seeded", `${icpId} day ${day.dayIndex}: generation status must stay seeded`);
|
||||
assert(!day.adaptationReason, `${icpId} day ${day.dayIndex}: adaptation reason should be absent`);
|
||||
assert(day.tasks.length === 4, `${icpId} day ${day.dayIndex}: expected exactly 4 task objects`);
|
||||
|
||||
const taskIds = new Set(day.tasks.map((task) => task.id));
|
||||
const stageIds = new Set(day.tasks.map((task) => task.stageId));
|
||||
assert(taskIds.size === 4, `${icpId} day ${day.dayIndex}: task IDs must be unique`);
|
||||
assert(stageIds.size === 4, `${icpId} day ${day.dayIndex}: stage IDs must be unique`);
|
||||
|
||||
for (const task of day.tasks) {
|
||||
assert(task.missionId === "curator-sprint", `${icpId} ${task.id}: missionId mismatch`);
|
||||
assert(task.missionInstanceId?.startsWith("curator-sprint:icp-v10-static:"), `${icpId} ${task.id}: missionInstanceId should use static version`);
|
||||
assert(task.stageId?.includes(`${task.serviceId?.replace("-service", "")}`), `${icpId} ${task.id}: stageId should include service segment`);
|
||||
assert(task.id.includes(`${task.serviceId?.replace("-service", "")}`), `${icpId} ${task.id}: taskId should include service segment`);
|
||||
assert(task.actorName !== "Vera" && task.actorName !== "Kai" && task.actorName !== "Mira" && task.actorName !== "Lyra", `${icpId} ${task.id}: agent persona leaked into actorName`);
|
||||
|
||||
const url = routeFor(task.route);
|
||||
const livePrefix = task.serviceId ? LIVE_ROUTE_PREFIX[task.serviceId] : undefined;
|
||||
if (livePrefix) {
|
||||
assert(url.pathname === livePrefix, `${icpId} ${task.id}: route ${url.pathname} does not match ${livePrefix}`);
|
||||
} else {
|
||||
assert(url.pathname === "/agents/service-unavailable", `${icpId} ${task.id}: unsupported service must use unavailable route`);
|
||||
assert(url.searchParams.get("serviceId") === task.serviceId, `${icpId} ${task.id}: unavailable route must preserve serviceId`);
|
||||
}
|
||||
assert(url.searchParams.get("source") === "curator-v1", `${icpId} ${task.id}: route missing curator source`);
|
||||
assert(url.searchParams.get("curatorTaskId") === task.id, `${icpId} ${task.id}: route curatorTaskId mismatch`);
|
||||
assert(url.searchParams.get("missionId") === task.missionId, `${icpId} ${task.id}: route missionId mismatch`);
|
||||
assert(url.searchParams.get("missionInstanceId") === task.missionInstanceId, `${icpId} ${task.id}: route missionInstanceId mismatch`);
|
||||
assert(url.searchParams.get("stageId") === task.stageId, `${icpId} ${task.id}: route stageId mismatch`);
|
||||
assert(url.searchParams.get("taskTitle") === task.title, `${icpId} ${task.id}: route taskTitle mismatch`);
|
||||
assert(url.searchParams.get("taskSubtitle") === task.subtitle, `${icpId} ${task.id}: route taskSubtitle mismatch`);
|
||||
assert(url.searchParams.get("taskType") === task.taskType, `${icpId} ${task.id}: route taskType mismatch`);
|
||||
|
||||
if (task.serviceId === "interview-service") {
|
||||
const taskText = `${task.title} ${task.subtitle}`.toLowerCase();
|
||||
const isIntroductionTask = /(?:60|90)[ -]?second|\bintroduction\b|\bpitch\b|\bpresentation\b/.test(taskText);
|
||||
assert(url.searchParams.get("type") === (isIntroductionTask ? "warm_up" : "behavioral"), `${icpId} ${task.id}: interview type must match task intent`);
|
||||
assert(url.searchParams.get("role") === (isIntroductionTask ? null : "Product Manager"), `${icpId} ${task.id}: interview role must match task intent`);
|
||||
assert(url.searchParams.get("difficulty") === (isIntroductionTask ? "easy" : "medium"), `${icpId} ${task.id}: interview difficulty must match task intent`);
|
||||
assert(url.searchParams.get("duration") === "5", `${icpId} ${task.id}: interview route should carry duration`);
|
||||
}
|
||||
|
||||
if (task.serviceId === "roleplay-service") {
|
||||
assert(url.searchParams.get("role") === "Product Manager", `${icpId} ${task.id}: practice route should preserve target role`);
|
||||
assert(url.searchParams.get("duration") === "5", `${icpId} ${task.id}: practice route should carry duration`);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
failures.push(err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}
|
||||
|
||||
if (failures.length) {
|
||||
console.error(failures.join("\n"));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`curator-static-registry tests passed for ${ICP_IDS.length} ICPs, 7 days each, 4 doc-backed tasks per day`);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,105 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { shouldRouteGrowEvent } from "../src/events/record-grow-event.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
/**
|
||||
* Regression: duplicate/idempotent ingest must not enqueue the same event twice.
|
||||
*
|
||||
* Before the fix, recordGrowEvent returned the existing row on a dedupe hit,
|
||||
* and routeGrowEventToUserActor was called unconditionally — so every duplicate
|
||||
* ingest re-enqueued the event to the actor queue, causing saturation.
|
||||
*
|
||||
* After the fix, routing is gated by shouldRouteGrowEvent(event, inserted):
|
||||
* only newly inserted, user-resolved, pending events are routed.
|
||||
*/
|
||||
|
||||
// ── 1. Freshly inserted, user-resolved, pending → route ──────────────────────
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "pending" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, true),
|
||||
true,
|
||||
"newly inserted pending event with a userId must route",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 2. Dedupe hit (inserted=false) → DO NOT route ────────────────────────────
|
||||
// This is the core regression: a duplicate ingest returns the existing row
|
||||
// with inserted=false. The gate must refuse to re-enqueue.
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "processed" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, false),
|
||||
false,
|
||||
"dedupe hit (inserted=false) must NOT route even if status looks processable",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 3. Freshly inserted but already processed → DO NOT route ─────────────────
|
||||
// Edge case: the row was just inserted but somehow already processed (e.g. the
|
||||
// caller did in-process projection synchronously). No re-enqueue.
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "processed" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, true),
|
||||
false,
|
||||
"inserted but already-processed event must NOT route",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 4. Freshly inserted but unresolved (no userId) → DO NOT route ────────────
|
||||
{
|
||||
const event = { userId: null, processingStatus: "unresolved" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, true),
|
||||
false,
|
||||
"unresolved event (no userId) must NOT route",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 5. Freshly inserted, user-resolved, but failed → DO NOT route ────────────
|
||||
// A terminal-failed event should not be re-enqueued by a duplicate ingest.
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "failed" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, true),
|
||||
false,
|
||||
"failed event must NOT route on replay",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 6. Dedupe hit on a pending event → DO NOT route ──────────────────────────
|
||||
// Critical: even if the existing row is still pending (not yet processed), a
|
||||
// dedupe hit means another caller already inserted it and likely already routed
|
||||
// it. Re-routing would create a duplicate queue message.
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "pending" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
assert.equal(
|
||||
shouldRouteGrowEvent(event, false),
|
||||
false,
|
||||
"dedupe hit on a pending event must NOT re-route (already enqueued by first caller)",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 7. Idempotent double-ingest simulation ───────────────────────────────────
|
||||
// Simulate two calls to recordGrowEventWithResult for the same event:
|
||||
// first returns inserted=true, second returns inserted=false.
|
||||
// The first routes, the second does not — proving no double-enqueue.
|
||||
{
|
||||
const event = { userId: "user_a", processingStatus: "pending" } as Pick<GrowEventRow, "userId" | "processingStatus">;
|
||||
|
||||
// First ingest: newly inserted
|
||||
const firstRoute = shouldRouteGrowEvent(event, true);
|
||||
// Second ingest: dedupe hit
|
||||
const secondRoute = shouldRouteGrowEvent(event, false);
|
||||
|
||||
assert.equal(firstRoute, true, "first ingest must route");
|
||||
assert.equal(secondRoute, false, "second (dedupe) ingest must NOT route");
|
||||
assert.ok(
|
||||
firstRoute && !secondRoute,
|
||||
"duplicate ingest must not enqueue twice",
|
||||
);
|
||||
}
|
||||
|
||||
console.log("event-dedupe-routing tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,131 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { routeGrowEventToUserActor } from "../src/events/route-to-user-actor.js";
|
||||
import { shouldRouteGrowEvent } from "../src/events/record-grow-event.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
/**
|
||||
* Cross-file integration test: exercises the full ingress → route → actor-enqueue
|
||||
* side-effect chain for the duplicate-routing regression.
|
||||
*
|
||||
* The persistence layer (recordGrowEventWithResult) requires Postgres and cannot
|
||||
* run in a tsx test. Instead we simulate its {event, inserted} output contract —
|
||||
* the same contract the three ingress callers (redis-consumer recordAndRoute,
|
||||
* routes/events.ts ingest, v1/events/events-routes.ts /track) consume — and prove
|
||||
* that the routing layer enqueues exactly once across a duplicate ingest pair.
|
||||
*
|
||||
* routeGrowEventToUserActor accepts an injectable `enqueue`, so we can observe
|
||||
* real side effects (the actor queue send) without a live Rivet cluster.
|
||||
*/
|
||||
|
||||
type RouteDecision = { routed: true } | { routed: false; reason: string };
|
||||
|
||||
// Simulate the ingress caller's routing block (mirrors redis-consumer.recordAndRoute
|
||||
// and routes/events.ts.ingest): gate on shouldRouteGrowEvent, then call route.
|
||||
async function ingressRoute(
|
||||
event: Pick<GrowEventRow, "id" | "userId" | "processingStatus">,
|
||||
inserted: boolean,
|
||||
enqueueCalls: Array<{ userId: string; eventId: string }>,
|
||||
): Promise<RouteDecision> {
|
||||
if (!shouldRouteGrowEvent(event, inserted)) {
|
||||
return { routed: false, reason: "dedupe_or_unresolved" };
|
||||
}
|
||||
return routeGrowEventToUserActor(event, {
|
||||
timeoutMs: 500,
|
||||
enqueue: async (input) => {
|
||||
enqueueCalls.push({ userId: input.userId, eventId: input.eventId });
|
||||
return { queued: true };
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function makeEvent(overrides: Partial<GrowEventRow> = {}): Pick<GrowEventRow, "id" | "userId" | "processingStatus"> {
|
||||
return {
|
||||
id: "evt-cross-file-test",
|
||||
userId: "user-cross-file",
|
||||
processingStatus: "pending",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 1. First ingest routes, second (dedupe) does NOT — proving no double-enqueue ─
|
||||
{
|
||||
const enqueueCalls: Array<{ userId: string; eventId: string }> = [];
|
||||
const event = makeEvent();
|
||||
|
||||
// First ingest: recordGrowEventWithResult returns inserted=true
|
||||
const first = await ingressRoute(event, true, enqueueCalls);
|
||||
// Second ingest: same event, dedupe hit → inserted=false
|
||||
const second = await ingressRoute(event, false, enqueueCalls);
|
||||
|
||||
assert.equal(first.routed, true, "first ingest must route to actor");
|
||||
assert.equal(second.routed, false, "dedupe ingest must NOT route");
|
||||
assert.equal(enqueueCalls.length, 1, "exactly one enqueue call — duplicate must not double-enqueue");
|
||||
assert.deepEqual(enqueueCalls[0], { userId: "user-cross-file", eventId: "evt-cross-file-test" });
|
||||
}
|
||||
|
||||
// ── 2. Unresolved event (no userId) never enqueues ────────────────────────────
|
||||
{
|
||||
const enqueueCalls: Array<{ userId: string; eventId: string }> = [];
|
||||
const event = makeEvent({ userId: null, processingStatus: "unresolved" });
|
||||
|
||||
const result = await ingressRoute(event, true, enqueueCalls);
|
||||
|
||||
assert.equal(result.routed, false, "unresolved event must not route");
|
||||
assert.equal(enqueueCalls.length, 0, "unresolved event must not enqueue");
|
||||
}
|
||||
|
||||
// ── 3. Three distinct events all route (no false-negative) ────────────────────
|
||||
{
|
||||
const enqueueCalls: Array<{ userId: string; eventId: string }> = [];
|
||||
const events = [
|
||||
makeEvent({ id: "evt-a", userId: "user-1" }),
|
||||
makeEvent({ id: "evt-b", userId: "user-1" }),
|
||||
makeEvent({ id: "evt-c", userId: "user-2" }),
|
||||
];
|
||||
|
||||
for (const event of events) {
|
||||
await ingressRoute(event, true, enqueueCalls);
|
||||
}
|
||||
|
||||
assert.equal(enqueueCalls.length, 3, "three distinct inserted events must each enqueue once");
|
||||
assert.deepEqual(
|
||||
enqueueCalls.map((c) => c.eventId),
|
||||
["evt-a", "evt-b", "evt-c"],
|
||||
);
|
||||
}
|
||||
|
||||
// ── 4. Same event ingested 5 times enqueues exactly once ──────────────────────
|
||||
{
|
||||
const enqueueCalls: Array<{ userId: string; eventId: string }> = [];
|
||||
const event = makeEvent({ id: "evt-repeated" });
|
||||
|
||||
// First is inserted, remaining 4 are dedupe hits
|
||||
await ingressRoute(event, true, enqueueCalls);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await ingressRoute(event, false, enqueueCalls);
|
||||
}
|
||||
|
||||
assert.equal(enqueueCalls.length, 1, "5 duplicate ingests must enqueue exactly once");
|
||||
}
|
||||
|
||||
// ── 5. Mixed: event A first-insert, event B dedupe, event C first-insert ──────
|
||||
// Proves the gate doesn't carry state between decisions.
|
||||
{
|
||||
const enqueueCalls: Array<{ userId: string; eventId: string }> = [];
|
||||
const eventA = makeEvent({ id: "evt-mixed-a" });
|
||||
const eventB = makeEvent({ id: "evt-mixed-b" });
|
||||
const eventC = makeEvent({ id: "evt-mixed-c" });
|
||||
|
||||
await ingressRoute(eventA, true, enqueueCalls);
|
||||
await ingressRoute(eventB, false, enqueueCalls);
|
||||
await ingressRoute(eventC, true, enqueueCalls);
|
||||
|
||||
assert.equal(enqueueCalls.length, 2, "only first-insert events A and C enqueue; B (dedupe) skipped");
|
||||
assert.deepEqual(
|
||||
enqueueCalls.map((c) => c.eventId),
|
||||
["evt-mixed-a", "evt-mixed-c"],
|
||||
);
|
||||
}
|
||||
|
||||
console.log("event-ingress-route-integration tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,17 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { routeGrowEventToUserActor } from "../src/events/route-to-user-actor.js";
|
||||
|
||||
const startedAt = Date.now();
|
||||
const result = await routeGrowEventToUserActor(
|
||||
{ id: "event-timeout-test", userId: "user-timeout-test" },
|
||||
{
|
||||
timeoutMs: 25,
|
||||
enqueue: () => new Promise<never>(() => undefined),
|
||||
},
|
||||
);
|
||||
|
||||
assert.equal(result.routed, false);
|
||||
assert.equal(result.reason, "actor_route_timeout");
|
||||
assert.ok(Date.now() - startedAt < 500, "actor routing must not block REST ingestion");
|
||||
|
||||
console.log("event-route-timeout tests passed");
|
||||
@@ -1,117 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { extractQscoreSignals } from "../src/events/projectors/qscore-projector.js";
|
||||
import { buildMatchmakingGatewayEvent } from "../src/services/matchmaking-events.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
function event(overrides: Partial<GrowEventRow> & { type: string; payload: Record<string, unknown> }): GrowEventRow {
|
||||
return {
|
||||
id: `event-${overrides.type}`,
|
||||
userId: "user_test",
|
||||
orgId: null,
|
||||
source: "matchmaking-v2",
|
||||
type: overrides.type,
|
||||
category: "service",
|
||||
occurredAt: new Date("2026-07-09T00:00:00.000Z"),
|
||||
receivedAt: new Date("2026-07-09T00:00:01.000Z"),
|
||||
mission: overrides.mission ?? null,
|
||||
subject: overrides.subject ?? null,
|
||||
correlation: overrides.correlation ?? null,
|
||||
payload: overrides.payload,
|
||||
raw: {},
|
||||
dedupeKey: null,
|
||||
processingStatus: "pending",
|
||||
processingError: null,
|
||||
processedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
const saved = buildMatchmakingGatewayEvent({
|
||||
userId: "user_test",
|
||||
action: "mark_saved",
|
||||
body: {
|
||||
action: "mark_saved",
|
||||
params: {
|
||||
opportunity_id: "job_123",
|
||||
curatorTaskId: "task_123",
|
||||
missionId: "curator-sprint",
|
||||
missionInstanceId: "sprint_123",
|
||||
stageId: "stage_123",
|
||||
source: "curator-v1",
|
||||
},
|
||||
},
|
||||
result: { task_id: "a2a_123", status: "completed", messages: [] },
|
||||
});
|
||||
|
||||
assert.equal(saved.type, "matchmaking.match.saved");
|
||||
assert.deepEqual(saved.mission, {
|
||||
missionId: "curator-sprint",
|
||||
instanceId: "sprint_123",
|
||||
stageId: "stage_123",
|
||||
source: "curator-v1",
|
||||
});
|
||||
assert.deepEqual(saved.subject, {
|
||||
serviceId: "matchmaking",
|
||||
kind: "opportunity",
|
||||
id: "job_123",
|
||||
externalId: "job_123",
|
||||
});
|
||||
assert.equal(saved.correlation?.taskId, "task_123");
|
||||
assert.equal(saved.correlation?.curatorTaskId, "task_123");
|
||||
assert.equal(saved.correlation?.opportunityId, "job_123");
|
||||
assert.equal(saved.payload.curatorTaskId, "task_123");
|
||||
assert.equal(saved.payload.opportunityId, "job_123");
|
||||
assert.equal(saved.payload.action, "mark_saved");
|
||||
|
||||
const generatedSignals = extractQscoreSignals(event({
|
||||
type: "matchmaking.matches.generated",
|
||||
payload: {
|
||||
action: "run_search",
|
||||
matchCount: 6,
|
||||
scanned: 120,
|
||||
result: { status: "completed" },
|
||||
},
|
||||
}));
|
||||
assert.ok(
|
||||
generatedSignals.some((signal) => signal.signalId === "matching.match_rate" && signal.score > 0),
|
||||
"generated matches should produce a match rate signal from match counts",
|
||||
);
|
||||
|
||||
const savedSignals = extractQscoreSignals(event({
|
||||
type: "matchmaking.match.saved",
|
||||
payload: {
|
||||
action: "mark_saved",
|
||||
opportunityId: "job_123",
|
||||
curatorTaskId: "task_123",
|
||||
result: { status: "completed" },
|
||||
},
|
||||
}));
|
||||
assert.equal(savedSignals.length, 0, "saved opportunities no longer emit a legacy signal");
|
||||
|
||||
const reviewed = buildMatchmakingGatewayEvent({
|
||||
userId: "user_test",
|
||||
action: "record_feedback",
|
||||
body: {
|
||||
action: "record_feedback",
|
||||
params: { opportunity_id: "job_123", curatorTaskId: "task_123" },
|
||||
},
|
||||
result: { task_id: "a2a_456", status: "completed", messages: [] },
|
||||
});
|
||||
assert.equal(reviewed.type, "matchmaking.matches.reviewed");
|
||||
|
||||
const appliedSignals = extractQscoreSignals(event({
|
||||
type: "matchmaking.match.applied",
|
||||
payload: {
|
||||
action: "mark_applied",
|
||||
opportunityId: "job_123",
|
||||
curatorTaskId: "task_123",
|
||||
applications_submitted: 1,
|
||||
result: { status: "completed" },
|
||||
},
|
||||
}));
|
||||
assert.ok(
|
||||
appliedSignals.some((signal) => signal.signalId === "matching.applications_submitted" && signal.score > 0),
|
||||
"applied opportunities should produce an applications submitted signal",
|
||||
);
|
||||
|
||||
console.log("matchmaking-events tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,46 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
onboardingMissionInstanceId,
|
||||
selectOnboardingMissionIds,
|
||||
} from "../src/missions/lifecycle.js";
|
||||
|
||||
const userA = "user_abc123";
|
||||
|
||||
assert.deepEqual(
|
||||
selectOnboardingMissionIds({ onboarding: { goal: "I need internship interview prep" } }),
|
||||
["interview-to-offer", "personal-brand-opportunity-engine"],
|
||||
"default onboarding should start interview-to-offer plus personal brand",
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
selectOnboardingMissionIds({ onboarding: { goal: "I want to negotiate my offer and compensation" } }),
|
||||
["salary-negotiation-war-room", "personal-brand-opportunity-engine"],
|
||||
"salary/offer context should prioritize the negotiation mission",
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
selectOnboardingMissionIds({ preferences: { onboarding: { goal: "I need a career transition into product" } } }),
|
||||
["career-transition", "personal-brand-opportunity-engine"],
|
||||
"preferences onboarding context should be read when selecting missions",
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
selectOnboardingMissionIds({ preferences: { onboarding: { goal: "Build LinkedIn visibility and network" } } }),
|
||||
["personal-brand-opportunity-engine", "interview-to-offer"],
|
||||
"brand/networking context should not duplicate the personal-brand mission",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
onboardingMissionInstanceId(userA, "interview-to-offer"),
|
||||
onboardingMissionInstanceId(userA, "interview-to-offer"),
|
||||
"onboarding mission instance ids must be deterministic for idempotent retries",
|
||||
);
|
||||
|
||||
assert.notEqual(
|
||||
onboardingMissionInstanceId(userA, "interview-to-offer"),
|
||||
onboardingMissionInstanceId("user_other", "interview-to-offer"),
|
||||
"onboarding mission instance ids must be scoped by user",
|
||||
);
|
||||
|
||||
console.log("mission-lifecycle tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,194 +0,0 @@
|
||||
#!/usr/bin/env tsx
|
||||
/**
|
||||
* Staging-only bulk onboarding reset CLI.
|
||||
*
|
||||
* Resets onboarding for EVERY user in the backend `users` table (the
|
||||
* Clerk-mirrored enrollment source) in two phases:
|
||||
*
|
||||
* Phase 1 — user-service preferences reset (A2A).
|
||||
* Calls POST /api/v1/users/onboarding-reset on user-service with the A2A
|
||||
* key. This resets preferences.onboarding to the default in-progress v3
|
||||
* doc, preserving all unrelated preference keys. It targets users by
|
||||
* clerk_id WITHOUT impersonating a Clerk session. A 404 means the user has
|
||||
* no user-service profile (backend-only user) — a documented skip that
|
||||
* never blocks Phase 2.
|
||||
*
|
||||
* Phase 2 — backend completion ledger reset (direct, idempotent).
|
||||
* Calls resetOnboardingLedger(userId) for every backend user whose Phase 1
|
||||
* succeeded OR was skipped (404 backend-only). A Phase 1 error (non-404
|
||||
* failure) blocks Phase 2 for that user to avoid inconsistent state
|
||||
* (preferences say "completed" but ledger says not). This deletes only
|
||||
* completion-type ledger rows (dotted AND underscore aliases) plus
|
||||
* completion-bearing snapshots, preserving intermediate snapshots,
|
||||
* missions, curator context, and qscore baselines.
|
||||
*
|
||||
* GUARDS — all three must hold or the script aborts before any network call:
|
||||
* 1. NODE_ENV=staging (refuses production/development)
|
||||
* 2. ONBOARDING_BULK_RESET_ALLOWED=true (explicit opt-in flag)
|
||||
* 3. --confirm on the CLI (typed acknowledgement)
|
||||
*
|
||||
* Usage:
|
||||
* NODE_ENV=staging ONBOARDING_BULK_RESET_ALLOWED=true \
|
||||
* npx tsx scripts/onboarding-bulk-reset.ts --confirm
|
||||
*
|
||||
* Output: aggregate-only audit counts (no per-user PII) and a summary.
|
||||
*
|
||||
* This script is INTENTIONALLY not wired to any HTTP surface. It is a
|
||||
* staging-only operations tool; bulk reset must never be exposed as a public
|
||||
* endpoint. Per-user reset is the authenticated DELETE /users/onboarding route.
|
||||
*/
|
||||
|
||||
import { eq, asc } from "drizzle-orm";
|
||||
import { db } from "../src/db/client.js";
|
||||
import { users } from "../src/db/schema.js";
|
||||
import { config } from "../src/config.js";
|
||||
import { log } from "../src/log.js";
|
||||
import { resetOnboardingLedger } from "../src/events/onboarding-ledger.js";
|
||||
|
||||
type PrefOutcome =
|
||||
| { userId: string; ok: true }
|
||||
| { userId: string; ok: false; skipped: true; reason: string; status: number }
|
||||
| { userId: string; ok: false; skipped: false; error: string; status: number };
|
||||
|
||||
type LedgerOutcome =
|
||||
| { userId: string; ok: true; ledgerRowsDeleted: number }
|
||||
| { userId: string; ok: false; error: string };
|
||||
|
||||
function assertStagingGuard(argv: string[]): void {
|
||||
const isStaging = config.nodeEnv === "staging";
|
||||
const allowed = process.env.ONBOARDING_BULK_RESET_ALLOWED === "true";
|
||||
const confirmed = argv.includes("--confirm");
|
||||
|
||||
const failures: string[] = [];
|
||||
if (!isStaging) failures.push(`NODE_ENV must be "staging" (got "${config.nodeEnv}")`);
|
||||
if (!allowed) failures.push("ONBOARDING_BULK_RESET_ALLOWED must be set to \"true\"");
|
||||
if (!confirmed) failures.push("--confirm argument is required");
|
||||
|
||||
if (failures.length > 0) {
|
||||
console.error("onboarding-bulk-reset: ABORTED — guard checks failed:");
|
||||
for (const f of failures) console.error(` - ${f}`);
|
||||
console.error("");
|
||||
console.error("This script is staging-only and will reset onboarding for EVERY user.");
|
||||
console.error("Re-run with all guards satisfied to proceed.");
|
||||
process.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
function userServiceUrl(): string {
|
||||
return config.userServiceUrl.replace(/\/$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 1: reset preferences.onboarding via the user-service A2A endpoint.
|
||||
* A 404 means the user has no user-service profile (backend-only) — a clean
|
||||
* skip that does NOT block the backend ledger reset.
|
||||
*/
|
||||
async function resetPreferences(clerkId: string, a2aKey: string): Promise<PrefOutcome> {
|
||||
const res = await fetch(`${userServiceUrl()}/api/v1/users/onboarding-reset`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: `Bearer ${a2aKey}`,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ clerk_id: clerkId }),
|
||||
});
|
||||
if (res.status === 404) {
|
||||
return { userId: clerkId, ok: false, skipped: true, reason: "no user-service profile (backend-only)", status: 404 };
|
||||
}
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
return { userId: clerkId, ok: false, skipped: false, error: text || res.statusText, status: res.status };
|
||||
}
|
||||
return { userId: clerkId, ok: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 2: directly reset the backend completion ledger for a user.
|
||||
* Always runs after Phase 1 (success or skip) — backend-only users still need
|
||||
* their ledger cleared. Idempotent.
|
||||
*/
|
||||
async function resetLedger(userId: string): Promise<LedgerOutcome> {
|
||||
try {
|
||||
const ledgerRowsDeleted = await resetOnboardingLedger(userId);
|
||||
return { userId, ok: true, ledgerRowsDeleted };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return { userId, ok: false, error: message };
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
assertStagingGuard(process.argv.slice(2));
|
||||
|
||||
const a2aKey = config.a2aAllowedKey;
|
||||
if (!a2aKey) {
|
||||
console.error("onboarding-bulk-reset: ABORTED — A2A_ALLOWED_KEY is not configured");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const allUsers = await db.select({ id: users.id }).from(users).orderBy(asc(users.id));
|
||||
const total = allUsers.length;
|
||||
|
||||
console.log(`onboarding-bulk-reset: targeting ${total} user(s)`);
|
||||
console.log(`onboarding-bulk-reset: NODE_ENV=${config.nodeEnv}, guard=on`);
|
||||
console.log(`onboarding-bulk-reset: user-service=${userServiceUrl()}`);
|
||||
|
||||
let prefOk = 0;
|
||||
let prefSkipped = 0;
|
||||
let prefErrors = 0;
|
||||
|
||||
let ledgerOk = 0;
|
||||
let ledgerErrors = 0;
|
||||
let ledgerTotal = 0;
|
||||
|
||||
let ledgerBlocked = 0;
|
||||
|
||||
for (const u of allUsers) {
|
||||
// Phase 1: user-service preferences reset (A2A).
|
||||
const pref = await resetPreferences(u.id, a2aKey);
|
||||
if (pref.ok) {
|
||||
prefOk += 1;
|
||||
} else if (pref.skipped) {
|
||||
prefSkipped += 1;
|
||||
} else {
|
||||
prefErrors += 1;
|
||||
}
|
||||
|
||||
// Phase 2: backend ledger reset — ONLY after Phase 1 succeeds or is
|
||||
// skipped (404 / backend-only). A pref error (non-404 failure) leaves
|
||||
// preferences.onboarding untouched, so clearing the ledger here would
|
||||
// create an inconsistent state (prefs say "completed", ledger says not).
|
||||
// Skip the ledger for errored users so an operator can retry after fix.
|
||||
if (!pref.ok && !pref.skipped) {
|
||||
ledgerBlocked += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const ledger = await resetLedger(u.id);
|
||||
if (ledger.ok) {
|
||||
ledgerOk += 1;
|
||||
ledgerTotal += ledger.ledgerRowsDeleted;
|
||||
} else {
|
||||
ledgerErrors += 1;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("");
|
||||
console.log("onboarding-bulk-reset: summary (aggregate only — no per-user PII)");
|
||||
console.log(` total users: ${total}`);
|
||||
console.log(` preferences reset: ${prefOk} ok, ${prefSkipped} skipped (backend-only), ${prefErrors} error(s)`);
|
||||
console.log(` ledger reset: ${ledgerOk} ok, ${ledgerErrors} error(s), ${ledgerBlocked} blocked by pref error, ${ledgerTotal} row(s) deleted`);
|
||||
|
||||
const hadErrors = prefErrors > 0 || ledgerErrors > 0;
|
||||
|
||||
log.info(
|
||||
{ total, prefOk, prefSkipped, prefErrors, ledgerOk, ledgerErrors, ledgerBlocked, ledgerTotal },
|
||||
"onboarding bulk reset complete",
|
||||
);
|
||||
process.exit(hadErrors ? 1 : 0);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("onboarding-bulk-reset: fatal", err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,114 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
completedAtFromOnboardingPayload,
|
||||
isValidOnboardingLedgerEvent,
|
||||
normalizeOnboardingEventType,
|
||||
} from "../src/events/onboarding-ledger.js";
|
||||
|
||||
const now = "2026-06-28T00:00:00.000Z";
|
||||
|
||||
assert.equal(normalizeOnboardingEventType("user_onboarding_completed"), "user.onboarding.completed");
|
||||
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({
|
||||
type: "onboarding.completed",
|
||||
payload: {},
|
||||
}),
|
||||
true,
|
||||
"explicit completion event should satisfy onboarding status",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({
|
||||
type: "user.onboarding.completed",
|
||||
payload: {},
|
||||
}),
|
||||
true,
|
||||
"user completion event should satisfy onboarding status",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({
|
||||
type: "profile.onboarding.completed",
|
||||
payload: {},
|
||||
}),
|
||||
true,
|
||||
"profile completion event should satisfy onboarding status",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({
|
||||
type: "onboarding.snapshot.saved",
|
||||
payload: { onboarding: { current_step: 2 } },
|
||||
}),
|
||||
false,
|
||||
"intermediate snapshots must not bypass onboarding",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({
|
||||
type: "onboarding.snapshot.saved",
|
||||
payload: { onboarding: { completed_at: now } },
|
||||
}),
|
||||
true,
|
||||
"completion snapshots should satisfy onboarding status",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
completedAtFromOnboardingPayload({
|
||||
preferences: { onboarding: { completed_at: now } },
|
||||
}),
|
||||
now,
|
||||
"completion timestamp should be extracted from preferences snapshot",
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
completedAtFromOnboardingPayload({
|
||||
completedAt: "not-a-date",
|
||||
})?.endsWith("Z"),
|
||||
true,
|
||||
"invalid completion timestamps should normalize to a valid ISO timestamp",
|
||||
);
|
||||
|
||||
// ── Regression: underscore completion aliases must be valid (reset deletes them) ──
|
||||
// The reset only reopens the gate if EVERY alias form that isValidOnboardingLedgerEvent
|
||||
// accepts is also in the reset delete scope. Pin both sides agree per alias.
|
||||
for (const underscore of ["onboarding_completed", "user_onboarding_completed", "profile_onboarding_completed"]) {
|
||||
const dotted = normalizeOnboardingEventType(underscore);
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({ type: underscore, payload: {} }),
|
||||
true,
|
||||
`${underscore} should satisfy onboarding status (normalized to ${dotted})`,
|
||||
);
|
||||
}
|
||||
|
||||
// ── Regression: completedAtFromOnboardingPayload paths mirrored by reset SQL ──
|
||||
// resetOnboardingLedger's jsonb predicate checks top-level completed_at/completedAt
|
||||
// and onboarding.completed_at/completedAt. Pin that the JS helper (used for status
|
||||
// validity) agrees a snapshot carrying any of these is a completion snapshot.
|
||||
for (const [label, payload] of [
|
||||
["top-level completed_at", { completed_at: now }],
|
||||
["top-level completedAt", { completedAt: now }],
|
||||
["onboarding.completed_at", { onboarding: { completed_at: now } }],
|
||||
["onboarding.completedAt", { onboarding: { completedAt: now } }],
|
||||
] as const) {
|
||||
assert.equal(
|
||||
completedAtFromOnboardingPayload(payload),
|
||||
now,
|
||||
`completion timestamp extracted from ${label}`,
|
||||
);
|
||||
assert.equal(
|
||||
isValidOnboardingLedgerEvent({ type: "onboarding.snapshot.saved", payload }),
|
||||
true,
|
||||
`snapshot with ${label} is a completion snapshot`,
|
||||
);
|
||||
}
|
||||
|
||||
// ── Regression: intermediate snapshots (no completion marker) stay invalid ──
|
||||
assert.equal(
|
||||
completedAtFromOnboardingPayload({ onboarding: { current_step: 2 } }),
|
||||
undefined,
|
||||
"intermediate snapshot has no completion timestamp",
|
||||
);
|
||||
console.log("onboarding-ledger tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,160 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
DEFAULT_ONBOARDING_DATA,
|
||||
defaultOnboardingData,
|
||||
extractOnboardingData,
|
||||
assertOnboardingRevision,
|
||||
deriveOnboardingPayload,
|
||||
mergeOnboardingPatch,
|
||||
OnboardingRevisionConflict,
|
||||
} from "../src/events/onboarding-ledger.js";
|
||||
|
||||
/**
|
||||
* Focused tests for the onboarding preference helpers (pure — no DB/network).
|
||||
* Covers: default v3 shape, revision conflict, partial update merge,
|
||||
* status/completed_at consistency + payload derivation, and unrelated
|
||||
* preference preservation. Matches the scripts/X.test.ts convention
|
||||
* (node:assert over pure functions).
|
||||
*/
|
||||
|
||||
// ── 1. default v3 response ───────────────────────────────────────────────────
|
||||
{
|
||||
const def = defaultOnboardingData();
|
||||
assert.equal(def.schema_version, 3, "default is v3");
|
||||
assert.equal(def.revision, 0);
|
||||
assert.equal(def.status, "in_progress");
|
||||
assert.equal(def.access_choice, null);
|
||||
assert.equal(def.qx_estimate, null);
|
||||
assert.equal(def.completed_at, null);
|
||||
assert.equal(def.consent.privacy_accepted, false);
|
||||
assert.equal(def.progress.stage, "consent");
|
||||
// DEFAULT constant must match the factory.
|
||||
assert.deepEqual(def, DEFAULT_ONBOARDING_DATA);
|
||||
// Factory returns a fresh clone, not the shared constant.
|
||||
assert.notEqual(def, DEFAULT_ONBOARDING_DATA);
|
||||
}
|
||||
|
||||
// extractOnboardingData returns the default when nothing is stored.
|
||||
{
|
||||
const empty = extractOnboardingData(undefined);
|
||||
assert.equal(empty.schema_version, 3);
|
||||
assert.equal(empty.revision, 0);
|
||||
const fromNonObject = extractOnboardingData("nonsense");
|
||||
assert.equal(fromNonObject.status, "in_progress");
|
||||
}
|
||||
|
||||
// ── 2. revision conflict (optimistic concurrency) ───────────────────────────
|
||||
{
|
||||
const current = extractOnboardingData({ revision: 5 });
|
||||
// Matching revision is accepted (no throw).
|
||||
assert.doesNotThrow(() => assertOnboardingRevision(5, current));
|
||||
|
||||
// Mismatched revision throws the typed conflict.
|
||||
assert.throws(
|
||||
() => assertOnboardingRevision(4, current),
|
||||
(err) => err instanceof OnboardingRevisionConflict && err.expected === 4 && err.actual === 5,
|
||||
);
|
||||
}
|
||||
|
||||
// ── 3. partial update merge (revision bump + progress stamp) ────────────────
|
||||
{
|
||||
const stored = extractOnboardingData({ revision: 2 });
|
||||
const incoming = { ...stored, revision: 2, profile: { ...stored.profile, mode: "founder" } };
|
||||
const merged = mergeOnboardingPatch({ onboarding: stored }, incoming);
|
||||
const next = extractOnboardingData(merged.onboarding);
|
||||
|
||||
assert.equal(next.revision, 3, "revision bumps by one from the stored value");
|
||||
assert.equal(next.profile.mode, "founder", "incoming profile change is applied");
|
||||
assert.ok(next.progress.updated_at, "progress.updated_at is stamped on save");
|
||||
}
|
||||
|
||||
// ── 4. status/completed_at consistency + payload derivation ─────────────────
|
||||
{
|
||||
const base = defaultOnboardingData();
|
||||
const completed = {
|
||||
...base,
|
||||
revision: 0,
|
||||
status: "completed" as const,
|
||||
profile: { ...base.profile, intent: "land-a-role", mode: "student", icp: "intern", question_branch: "student" },
|
||||
responses: {
|
||||
...base.responses,
|
||||
career_barriers: ["no-network"],
|
||||
desired_outcomes: ["interviews", "offer"],
|
||||
target_role: "Product Intern",
|
||||
target_field: "technical",
|
||||
weekly_time_commitment: "5-10",
|
||||
experience_level: "0-2",
|
||||
work_context: "audience",
|
||||
},
|
||||
// deliberately omit completed_at; merge must stamp it.
|
||||
};
|
||||
|
||||
const merged = mergeOnboardingPatch({}, completed);
|
||||
const next = extractOnboardingData(merged.onboarding);
|
||||
assert.equal(next.status, "completed");
|
||||
assert.ok(next.completed_at, "completed status without completed_at is stamped");
|
||||
|
||||
// Payload is a faithful projection — no invented completion math.
|
||||
const payload = deriveOnboardingPayload(next);
|
||||
assert.equal(payload.schema_version, 1);
|
||||
assert.equal(payload.source_revision, next.revision);
|
||||
assert.equal(payload.mode, "student");
|
||||
assert.equal(payload.onboarding_icp, "intern");
|
||||
assert.equal(payload.target_role, "Product Intern");
|
||||
assert.equal(payload.target_field, "technical");
|
||||
assert.deepEqual(payload.goals, ["interviews", "offer"]);
|
||||
assert.deepEqual(payload.barriers, ["no-network"]);
|
||||
assert.equal(payload.weekly_time_commitment, "5-10");
|
||||
assert.equal(payload.experience_context, "0-2 · audience");
|
||||
}
|
||||
|
||||
// completed_at is cleared when regressing to in_progress with an explicit null.
|
||||
{
|
||||
const completed = { ...defaultOnboardingData(), status: "completed" as const, completed_at: "2026-07-10T00:00:00.000Z" };
|
||||
const regressed = { ...completed, status: "in_progress" as const, completed_at: null };
|
||||
const merged = mergeOnboardingPatch({}, regressed);
|
||||
const next = extractOnboardingData(merged.onboarding);
|
||||
assert.equal(next.status, "in_progress");
|
||||
assert.equal(next.completed_at, null, "stale completed_at is cleared on regression");
|
||||
}
|
||||
|
||||
// ── 5. unrelated preference preservation ────────────────────────────────────
|
||||
{
|
||||
const preferences = {
|
||||
onboarding: { revision: 1, status: "in_progress" },
|
||||
interview_preferences: { focus_areas: ["behavioral"] },
|
||||
resume_preferences: { target_title: "Data Scientist" },
|
||||
mission_preferences: { active_goal: "land-offer" },
|
||||
target_roles: ["Product Intern"],
|
||||
target_companies: ["Acme"],
|
||||
};
|
||||
const incoming = { ...extractOnboardingData(preferences.onboarding), revision: 1, profile: { intent: "x", mode: "student", icp: "intern", question_branch: "student" } };
|
||||
const merged = mergeOnboardingPatch(preferences, incoming);
|
||||
|
||||
// The onboarding blob is updated.
|
||||
assert.equal(extractOnboardingData(merged.onboarding).revision, 2);
|
||||
// Every unrelated preference key is preserved verbatim.
|
||||
assert.deepEqual(merged.interview_preferences, { focus_areas: ["behavioral"] });
|
||||
assert.deepEqual(merged.resume_preferences, { target_title: "Data Scientist" });
|
||||
assert.deepEqual(merged.mission_preferences, { active_goal: "land-offer" });
|
||||
assert.deepEqual(merged.target_roles, ["Product Intern"]);
|
||||
assert.deepEqual(merged.target_companies, ["Acme"]);
|
||||
}
|
||||
|
||||
// ── 6. access_choice persistence (trial | full | null) ───────────────────────
|
||||
{
|
||||
const base = defaultOnboardingData();
|
||||
const trial = { ...base, revision: 0, access_choice: "trial" as const };
|
||||
const mergedTrial = mergeOnboardingPatch({}, trial);
|
||||
assert.equal(extractOnboardingData(mergedTrial.onboarding).access_choice, "trial");
|
||||
|
||||
const full = { ...base, revision: 0, access_choice: "full" as const };
|
||||
const mergedFull = mergeOnboardingPatch({}, full);
|
||||
assert.equal(extractOnboardingData(mergedFull.onboarding).access_choice, "full");
|
||||
|
||||
// Garbage access_choice is rejected back to null by extraction.
|
||||
const garbage = extractOnboardingData({ access_choice: "pro" });
|
||||
assert.equal(garbage.access_choice, null);
|
||||
}
|
||||
|
||||
console.log("onboarding-preferences: all assertions passed");
|
||||
@@ -1,176 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { dirname, join } from "node:path";
|
||||
import {
|
||||
COMPLETION_EVENT_TYPES,
|
||||
COMPLETION_EVENT_TYPE_ALIASES,
|
||||
SNAPSHOT_EVENT_TYPE_ALIASES,
|
||||
resetOnboardingPreferences,
|
||||
defaultOnboardingData,
|
||||
extractOnboardingData,
|
||||
ONBOARDING_LEDGER_QUERY_TYPES,
|
||||
} from "../src/events/onboarding-ledger.js";
|
||||
|
||||
/**
|
||||
* Focused tests for onboarding reset helpers (pure — no DB/network).
|
||||
* Covers: per-user reset state (preferences reset to default, unrelated keys
|
||||
* preserved), ledger type scope (only completion types deleted, snapshots
|
||||
* preserved), and bulk guard behavior (assertStagingGuard invariants).
|
||||
*
|
||||
* The async resetOnboardingLedger is exercised in integration; here we assert
|
||||
* its type-scope contract via the exported COMPLETION_EVENT_TYPES set, which is
|
||||
* exactly the WHERE clause it builds.
|
||||
*/
|
||||
|
||||
// ── 1. resetOnboardingPreferences: resets onboarding to default v3 ──────────
|
||||
{
|
||||
const preferences = {
|
||||
onboarding: {
|
||||
schema_version: 3,
|
||||
revision: 5,
|
||||
status: "completed",
|
||||
completed_at: "2026-07-10T00:00:00.000Z",
|
||||
progress: { stage: "done", branch_index: 3, updated_at: "2026-07-10T00:00:00.000Z" },
|
||||
consent: { privacy_accepted: true, accepted_at: "2026-07-09T00:00:00.000Z", terms_version: "2026-07" },
|
||||
profile: { intent: "land-a-role", mode: "student", icp: "intern", question_branch: "student" },
|
||||
responses: { career_barriers: ["x"], desired_outcomes: ["y"], current_situation: null, target_milestone: null, weekly_time_commitment: "5-10", target_role: "Intern", target_field: "tech", venture_industry: null, experience_level: "0-2", work_context: "audience" },
|
||||
import: { method: "manual", status: "not_started", resume_id: null, resume_filename: null, resume_summary: null, linkedin_profile_id: null, linkedin_url: null },
|
||||
access_choice: "trial",
|
||||
qx_estimate: 42,
|
||||
},
|
||||
interview_preferences: { focus_areas: ["behavioral"] },
|
||||
resume_preferences: { target_title: "Data Scientist" },
|
||||
target_roles: ["Product Intern"],
|
||||
};
|
||||
|
||||
const reset = resetOnboardingPreferences(preferences);
|
||||
const next = extractOnboardingData(reset.onboarding);
|
||||
|
||||
// Onboarding blob is back to defaults.
|
||||
assert.equal(next.schema_version, 3);
|
||||
assert.equal(next.revision, 0, "revision resets to 0");
|
||||
assert.equal(next.status, "in_progress", "status reverts to in_progress");
|
||||
assert.equal(next.completed_at, null, "completed_at is cleared");
|
||||
assert.equal(next.progress.stage, "consent", "progress stage resets to consent");
|
||||
assert.equal(next.access_choice, null);
|
||||
assert.equal(next.qx_estimate, null);
|
||||
assert.deepEqual(next, defaultOnboardingData());
|
||||
|
||||
// Unrelated preference keys are preserved verbatim.
|
||||
assert.deepEqual(reset.interview_preferences, { focus_areas: ["behavioral"] });
|
||||
assert.deepEqual(reset.resume_preferences, { target_title: "Data Scientist" });
|
||||
assert.deepEqual(reset.target_roles, ["Product Intern"]);
|
||||
}
|
||||
|
||||
// ── 2. resetOnboardingPreferences: idempotent on already-default prefs ─────
|
||||
{
|
||||
const empty = {};
|
||||
const reset = resetOnboardingPreferences(empty);
|
||||
assert.deepEqual(reset.onboarding, defaultOnboardingData());
|
||||
// Original object is not mutated.
|
||||
assert.equal(Object.keys(empty).length, 0, "original preferences not mutated");
|
||||
}
|
||||
// ── 3. Ledger reset scope: completion aliases (both forms) + completion snapshots
|
||||
// resetOnboardingLedger builds: DELETE FROM grow_events WHERE userId = ? AND (
|
||||
// type IN (COMPLETION_EVENT_TYPE_ALIASES)
|
||||
// OR (type IN (SNAPSHOT_EVENT_TYPE_ALIASES) AND payload-has-completion)
|
||||
// ). This pins the contract: (a) all completion aliases — dotted AND underscore —
|
||||
// are deleted so a legacy underscore completion row can't keep the gate shut;
|
||||
// (b) snapshot types are NOT blanket completion types (only completion-bearing
|
||||
// snapshots are deleted via the jsonb predicate); (c) intermediate snapshots are
|
||||
// preserved because they never satisfy the completion predicate.
|
||||
{
|
||||
// (a) Completion aliases cover BOTH dotted and underscore forms — 6 total.
|
||||
assert.equal(COMPLETION_EVENT_TYPE_ALIASES.length, 6, "3 dotted + 3 underscore completion aliases");
|
||||
for (const dotted of Object.keys(COMPLETION_EVENT_TYPES)) {
|
||||
const underscore = dotted.replaceAll(".", "_");
|
||||
assert.ok(COMPLETION_EVENT_TYPE_ALIASES.includes(dotted), `${dotted} in reset scope`);
|
||||
assert.ok(COMPLETION_EVENT_TYPE_ALIASES.includes(underscore), `${underscore} alias in reset scope`);
|
||||
}
|
||||
|
||||
// (b) Snapshot types are handled in a SEPARATE phase, never as completion types.
|
||||
assert.equal(COMPLETION_EVENT_TYPES["onboarding.snapshot.saved"], undefined,
|
||||
"snapshot must never be a blanket completion type");
|
||||
assert.equal(SNAPSHOT_EVENT_TYPE_ALIASES.includes("onboarding.snapshot.saved"), true,
|
||||
"dotted snapshot alias is in the snapshot reset phase");
|
||||
assert.equal(SNAPSHOT_EVENT_TYPE_ALIASES.includes("onboarding_snapshot_saved"), true,
|
||||
"underscore snapshot alias is in the snapshot reset phase");
|
||||
|
||||
// (c) The reset completion set is a strict subset of the status-query list:
|
||||
// status reads ALL aliases + snapshots; reset deletes only completions and
|
||||
// completion-bearing snapshots, preserving intermediate saves.
|
||||
for (const alias of COMPLETION_EVENT_TYPE_ALIASES) {
|
||||
assert.ok((ONBOARDING_LEDGER_QUERY_TYPES as readonly string[]).includes(alias),
|
||||
`${alias} is also queryable for status`);
|
||||
}
|
||||
}
|
||||
// ── 4. Bulk guard behavior: real spawn of assertStagingGuard ───────────────
|
||||
// Spawns the actual CLI with controlled env and asserts exit code 2 (abort)
|
||||
// for each independent failure leg. We only assert abort cases — the
|
||||
// all-flags-pass case would proceed past the guard and hit the network/DB.
|
||||
{
|
||||
const script = "scripts/onboarding-bulk-reset.ts";
|
||||
|
||||
// Leg 1: production env (even with all other flags) must abort.
|
||||
const prod = spawnSync("npx", ["tsx", script, "--confirm"], {
|
||||
env: { ...process.env, NODE_ENV: "production", ONBOARDING_BULK_RESET_ALLOWED: "true" },
|
||||
encoding: "utf8",
|
||||
});
|
||||
assert.equal(prod.status, 2, "production must abort even with all flags");
|
||||
assert.match(prod.stderr, /NODE_ENV must be "staging"/, "production abort names the env failure");
|
||||
|
||||
// Leg 2: staging + allowed but missing --confirm must abort.
|
||||
const noConfirm = spawnSync("npx", ["tsx", script], {
|
||||
env: { ...process.env, NODE_ENV: "staging", ONBOARDING_BULK_RESET_ALLOWED: "true" },
|
||||
encoding: "utf8",
|
||||
});
|
||||
assert.equal(noConfirm.status, 2, "staging without --confirm must abort");
|
||||
assert.match(noConfirm.stderr, /--confirm/, "missing-confirm abort names the confirm failure");
|
||||
|
||||
// Leg 3: staging + confirm but no opt-in flag must abort.
|
||||
const noFlag = spawnSync("npx", ["tsx", script, "--confirm"], {
|
||||
env: { ...process.env, NODE_ENV: "staging", ONBOARDING_BULK_RESET_ALLOWED: "false" },
|
||||
encoding: "utf8",
|
||||
});
|
||||
assert.equal(noFlag.status, 2, "staging without opt-in flag must abort");
|
||||
assert.match(noFlag.stderr, /ONBOARDING_BULK_RESET_ALLOWED/, "missing-flag abort names the flag failure");
|
||||
}
|
||||
|
||||
// ── 5. Bulk CLI structure: A2A endpoint + Phase 2 gating (source read) ──────
|
||||
// The CLI's network/DB behavior is exercised in integration. Here we assert
|
||||
// the structural contract by reading the script source directly: (a) it calls
|
||||
// the user-service A2A onboarding-reset endpoint (not the backend DELETE
|
||||
// route), (b) it authenticates with a2aAllowedKey (not serviceToken), (c) it
|
||||
// treats 404 as a skip, and (d) it gates the backend ledger reset on Phase 1
|
||||
// success-or-skip so a pref error never leaves inconsistent state.
|
||||
{
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const src = readFileSync(join(here, "onboarding-bulk-reset.ts"), "utf8");
|
||||
|
||||
// (a) Calls the user-service A2A onboarding-reset endpoint by path.
|
||||
assert.match(src, /\/api\/v1\/users\/onboarding-reset/, "CLI must call the user-service A2A reset endpoint");
|
||||
// Must NOT call the backend DELETE /users/onboarding route (old impersonation path).
|
||||
assert.doesNotMatch(src, /method:\s*["']DELETE["']/, "CLI must not use the backend DELETE route");
|
||||
|
||||
// (b) Authenticates with the A2A key, not the per-user service token.
|
||||
assert.match(src, /a2aAllowedKey/, "CLI must use config.a2aAllowedKey for A2A auth");
|
||||
assert.doesNotMatch(src, /serviceToken/, "CLI must not reference serviceToken (per-user impersonation path)");
|
||||
|
||||
// (c) Treats a 404 from user-service as a documented skip (backend-only user).
|
||||
assert.match(src, /status === 404/, "CLI must detect 404 from user-service");
|
||||
assert.match(src, /backend-only/, "CLI must document 404 as backend-only skip");
|
||||
|
||||
// (d) Phase 2 ledger reset is gated on Phase 1 success-or-skip.
|
||||
assert.match(src, /!pref\.ok && !pref\.skipped/, "CLI must skip ledger when pref errored (not ok, not skipped)");
|
||||
assert.match(src, /ledgerBlocked/, "CLI must count pref-blocked ledger skips");
|
||||
|
||||
// (e) Calls resetOnboardingLedger directly (not via HTTP).
|
||||
assert.match(src, /import \{[^}]*resetOnboardingLedger[^}]*\} from/, "CLI must import resetOnboardingLedger directly");
|
||||
|
||||
// (f) Aggregate-only output — no per-user email/PII in the loop.
|
||||
assert.doesNotMatch(src, /u\.email/, "CLI must not emit per-user email (PII)");
|
||||
}
|
||||
console.log("onboarding-reset tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,208 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { careerTransitionReducer } from "../src/missions/career-transition/reducer.js";
|
||||
import { interviewToOfferReducer } from "../src/missions/interview-to-offer/reducer.js";
|
||||
import { personalBrandOpportunityReducer } from "../src/missions/personal-brand-opportunity-engine/reducer.js";
|
||||
import { promotionReadinessReducer } from "../src/missions/promotion-readiness/reducer.js";
|
||||
import { salaryNegotiationReducer } from "../src/missions/salary-negotiation-war-room/reducer.js";
|
||||
import type { GrowActiveMission } from "../src/actors/missions/types.js";
|
||||
import type { MissionReducer } from "../src/missions/reducer-types.js";
|
||||
import type { MissionReducerContext } from "../src/missions/reducer-types.js";
|
||||
|
||||
function missionFor(missionId: GrowActiveMission["missionId"], actorType: GrowActiveMission["actorType"]): GrowActiveMission {
|
||||
return {
|
||||
instanceId: `mission-${missionId}-test`,
|
||||
missionId,
|
||||
workflowId: missionId,
|
||||
title: missionId,
|
||||
shortTitle: missionId,
|
||||
status: "active",
|
||||
progressPercent: 0,
|
||||
currentStageId: "resume",
|
||||
actorType,
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
const mission = missionFor("interview-to-offer", "interviewToOfferMissionActor");
|
||||
|
||||
function ctxWithMission(activeMission: GrowActiveMission, event: Partial<MissionReducerContext["event"]> & { source: string; type: string; payload?: Record<string, unknown> }): MissionReducerContext {
|
||||
return {
|
||||
userId: "user_test",
|
||||
activeMission,
|
||||
event: {
|
||||
id: `event-${activeMission.missionId}-${event.type}`,
|
||||
userId: "user_test",
|
||||
orgId: null,
|
||||
source: event.source,
|
||||
type: event.type,
|
||||
category: "service",
|
||||
occurredAt: new Date(),
|
||||
receivedAt: new Date(),
|
||||
mission: event.mission,
|
||||
subject: null,
|
||||
correlation: null,
|
||||
payload: event.payload ?? {},
|
||||
raw: {},
|
||||
dedupeKey: null,
|
||||
processingStatus: "pending",
|
||||
processingError: null,
|
||||
processedAt: null,
|
||||
},
|
||||
qscoreSignals: [],
|
||||
insight: {
|
||||
summary: "test insight",
|
||||
confidence: "low",
|
||||
recommendedActions: [],
|
||||
missionStageHints: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function ctx(event: Partial<MissionReducerContext["event"]> & { source: string; type: string; payload?: Record<string, unknown> }): MissionReducerContext {
|
||||
return ctxWithMission(mission, event);
|
||||
}
|
||||
|
||||
const interviewFeedbackPayload = {
|
||||
review: {
|
||||
status: "completed",
|
||||
overall_score: 72,
|
||||
weak_areas: ["impact metrics", "ownership clarity"],
|
||||
proof_gaps: ["no scale numbers"],
|
||||
story_issues: ["STAR structure is loose"],
|
||||
summary: "Good direction, but missing measurable proof.",
|
||||
},
|
||||
};
|
||||
|
||||
const roleplayFeedbackPayload = {
|
||||
review: {
|
||||
status: "completed",
|
||||
weak_areas: ["concision", "objection handling"],
|
||||
story_gaps: ["needs clearer tradeoff story"],
|
||||
summary: "Good empathy, but answers need tighter story framing.",
|
||||
},
|
||||
};
|
||||
|
||||
const reducerCases: Array<{
|
||||
name: string;
|
||||
reducer: MissionReducer;
|
||||
mission: GrowActiveMission;
|
||||
}> = [
|
||||
{
|
||||
name: "interview to offer",
|
||||
reducer: interviewToOfferReducer,
|
||||
mission,
|
||||
},
|
||||
{
|
||||
name: "career transition",
|
||||
reducer: careerTransitionReducer,
|
||||
mission: missionFor("career-transition", "careerTransitionMissionActor"),
|
||||
},
|
||||
{
|
||||
name: "promotion readiness",
|
||||
reducer: promotionReadinessReducer,
|
||||
mission: missionFor("promotion-readiness", "promotionReadinessMissionActor"),
|
||||
},
|
||||
{
|
||||
name: "salary negotiation",
|
||||
reducer: salaryNegotiationReducer,
|
||||
mission: missionFor("salary-negotiation-war-room", "salaryNegotiationWarRoomMissionActor"),
|
||||
},
|
||||
{
|
||||
name: "personal brand",
|
||||
reducer: personalBrandOpportunityReducer,
|
||||
mission: missionFor("personal-brand-opportunity-engine", "personalBrandOpportunityMissionActor"),
|
||||
},
|
||||
];
|
||||
|
||||
const resumeResult = interviewToOfferReducer.reduce(ctx({
|
||||
source: "resume-builder",
|
||||
type: "resume.analysis.completed",
|
||||
payload: {
|
||||
analysis: {
|
||||
summary: "Strong backend platform project.",
|
||||
strengths: ["Built an event-driven backend"],
|
||||
gaps: ["Add impact metrics"],
|
||||
missing_keywords: ["Kafka", "AWS"],
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
const proofPractice = resumeResult.actions.find((action) => action.payload?.passiveAction === "resume_analysis_to_interview_practice");
|
||||
assert.ok(proofPractice, "resume analysis should create an interview practice passive action");
|
||||
assert.equal(proofPractice?.serviceId, "interview-service");
|
||||
assert.equal(proofPractice?.toolName, "interview.configure_practice");
|
||||
assert.match(String(proofPractice?.payload?.prompt), /event-driven backend/i);
|
||||
|
||||
const interviewResult = interviewToOfferReducer.reduce(ctx({
|
||||
source: "interview-service",
|
||||
type: "interview.feedback.generated",
|
||||
payload: interviewFeedbackPayload,
|
||||
}));
|
||||
|
||||
const resumeUpgrade = interviewResult.actions.find((action) => action.payload?.passiveAction === "interview_feedback_to_resume_upgrade");
|
||||
assert.ok(resumeUpgrade, "interview feedback should create a resume upgrade passive action");
|
||||
assert.equal(resumeUpgrade?.mode, "approval_required");
|
||||
assert.equal(resumeUpgrade?.serviceId, "resume-service");
|
||||
assert.deepEqual(resumeUpgrade?.payload?.missingProof, ["no scale numbers"]);
|
||||
assert.deepEqual(resumeUpgrade?.payload?.storyIssues, ["STAR structure is loose", "add measurable impact proof"]);
|
||||
|
||||
const roleplayResult = interviewToOfferReducer.reduce(ctx({
|
||||
source: "roleplay-service",
|
||||
type: "roleplay.feedback.generated",
|
||||
payload: roleplayFeedbackPayload,
|
||||
}));
|
||||
|
||||
const storyArtifact = roleplayResult.artifacts.find((artifact) => artifact.type === "story_bank_update");
|
||||
const communicationDrill = roleplayResult.actions.find((action) => action.payload?.passiveAction === "roleplay_feedback_to_communication_drill");
|
||||
assert.ok(storyArtifact, "roleplay feedback should create a story bank artifact");
|
||||
assert.ok(communicationDrill, "roleplay feedback should create a communication drill passive action");
|
||||
assert.equal(communicationDrill?.serviceId, "interview-service");
|
||||
assert.equal(communicationDrill?.toolName, "interview.configure_practice");
|
||||
assert.deepEqual(communicationDrill?.payload?.storyIssues, ["needs clearer tradeoff story", "tighten STAR story structure"]);
|
||||
|
||||
for (const testCase of reducerCases) {
|
||||
const reducerResumeResult = testCase.reducer.reduce(ctxWithMission(testCase.mission, {
|
||||
source: "resume-builder",
|
||||
type: "resume.analysis.completed",
|
||||
payload: {
|
||||
analysis: {
|
||||
summary: "Strong backend platform project.",
|
||||
strengths: ["Built an event-driven backend"],
|
||||
gaps: ["Add impact metrics"],
|
||||
missing_keywords: ["Kafka", "AWS"],
|
||||
},
|
||||
},
|
||||
}));
|
||||
assert.ok(
|
||||
reducerResumeResult.actions.some((action) => action.payload?.passiveAction === "resume_analysis_to_interview_practice"),
|
||||
`${testCase.name} resume analysis should create an interview practice passive action`,
|
||||
);
|
||||
|
||||
const reducerInterviewResult = testCase.reducer.reduce(ctxWithMission(testCase.mission, {
|
||||
source: "interview-service",
|
||||
type: "interview.feedback.generated",
|
||||
payload: interviewFeedbackPayload,
|
||||
}));
|
||||
assert.ok(
|
||||
reducerInterviewResult.actions.some((action) => action.payload?.passiveAction === "interview_feedback_to_resume_upgrade"),
|
||||
`${testCase.name} interview feedback should create a resume upgrade passive action`,
|
||||
);
|
||||
|
||||
const reducerRoleplayResult = testCase.reducer.reduce(ctxWithMission(testCase.mission, {
|
||||
source: "roleplay-service",
|
||||
type: "roleplay.feedback.generated",
|
||||
payload: roleplayFeedbackPayload,
|
||||
}));
|
||||
assert.ok(
|
||||
reducerRoleplayResult.actions.some((action) => action.payload?.passiveAction === "roleplay_feedback_to_communication_drill"),
|
||||
`${testCase.name} roleplay feedback should create a communication drill passive action`,
|
||||
);
|
||||
assert.ok(
|
||||
reducerRoleplayResult.artifacts.some((artifact) => artifact.type === "story_bank_update"),
|
||||
`${testCase.name} roleplay feedback should create a story bank update artifact`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log("passive-actions tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,112 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { resolveRedisConfig } from "../src/config.js";
|
||||
|
||||
/**
|
||||
* Test: Redis ingestion consumers are default-off and do not load the Redis
|
||||
* module unless an explicit opt-in flag is set. Legacy URLs must not inherit
|
||||
* from the generic REDIS_URL.
|
||||
*
|
||||
* Uses the pure resolveRedisConfig resolver with synthetic env objects — no
|
||||
* module-cache hacks needed.
|
||||
*/
|
||||
|
||||
// ── 1. Default-off: no Redis flags set ──────────────────────────────────────
|
||||
{
|
||||
const rc = resolveRedisConfig({});
|
||||
|
||||
assert.equal(
|
||||
rc.growEventsRedisEnabled,
|
||||
false,
|
||||
"GROW_EVENTS_REDIS_ENABLED must default to false",
|
||||
);
|
||||
assert.equal(
|
||||
rc.legacyServiceRedisEnabled,
|
||||
false,
|
||||
"LEGACY_SERVICE_REDIS_ENABLED must default to false",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 2. Legacy URLs must NOT inherit from generic REDIS_URL ──────────────────
|
||||
{
|
||||
// Set ONLY the generic REDIS_URL — legacy URLs must not pick it up.
|
||||
const rc = resolveRedisConfig({ REDIS_URL: "redis://legacy-generic:6379" });
|
||||
|
||||
assert.equal(
|
||||
rc.interviewRedisUrl,
|
||||
"",
|
||||
"interviewRedisUrl must NOT inherit from generic REDIS_URL",
|
||||
);
|
||||
assert.equal(
|
||||
rc.roleplayRedisUrl,
|
||||
"",
|
||||
"roleplayRedisUrl must NOT inherit from generic REDIS_URL",
|
||||
);
|
||||
assert.equal(
|
||||
rc.resumeRedisUrl,
|
||||
"",
|
||||
"resumeRedisUrl must NOT inherit from generic REDIS_URL",
|
||||
);
|
||||
assert.equal(
|
||||
rc.coursesRedisUrl,
|
||||
"",
|
||||
"coursesRedisUrl must NOT inherit from generic REDIS_URL",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 3. Legacy URLs must NOT inherit from GROW_EVENTS_REDIS_URL ───────────────
|
||||
{
|
||||
const rc = resolveRedisConfig({ GROW_EVENTS_REDIS_URL: "redis://canonical:6379" });
|
||||
|
||||
assert.equal(
|
||||
rc.interviewRedisUrl,
|
||||
"",
|
||||
"interviewRedisUrl must NOT inherit from GROW_EVENTS_REDIS_URL",
|
||||
);
|
||||
assert.equal(rc.growEventsRedisUrl, "redis://canonical:6379", "canonical URL resolves from its own var");
|
||||
}
|
||||
|
||||
// ── 4. Explicit opt-in flags are respected ──────────────────────────────────
|
||||
{
|
||||
const rc = resolveRedisConfig({ GROW_EVENTS_REDIS_ENABLED: "true" });
|
||||
|
||||
assert.equal(
|
||||
rc.growEventsRedisEnabled,
|
||||
true,
|
||||
"GROW_EVENTS_REDIS_ENABLED=true must be respected",
|
||||
);
|
||||
assert.equal(
|
||||
rc.legacyServiceRedisEnabled,
|
||||
false,
|
||||
"legacy must stay off when only canonical flag is set",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 5. Legacy opt-in flag works independently ───────────────────────────────
|
||||
{
|
||||
const rc = resolveRedisConfig({
|
||||
LEGACY_SERVICE_REDIS_ENABLED: "true",
|
||||
INTERVIEW_REDIS_URL: "redis://interview:6379",
|
||||
});
|
||||
|
||||
assert.equal(rc.legacyServiceRedisEnabled, true, "LEGACY_SERVICE_REDIS_ENABLED=true must be respected");
|
||||
assert.equal(rc.growEventsRedisEnabled, false, "canonical must stay off when only legacy flag is set");
|
||||
assert.equal(rc.interviewRedisUrl, "redis://interview:6379", "explicit interview URL resolves");
|
||||
}
|
||||
|
||||
// ── 6. Explicit legacy URLs resolve from their own vars ──────────────────────
|
||||
{
|
||||
const rc = resolveRedisConfig({
|
||||
INTERVIEW_REDIS_URL: "redis://i:6379",
|
||||
ROLEPLAY_REDIS_URL: "redis://r:6379",
|
||||
RESUME_REDIS_URL: "redis://re:6379",
|
||||
COURSES_REDIS_URL: "redis://c:6379",
|
||||
});
|
||||
|
||||
assert.equal(rc.interviewRedisUrl, "redis://i:6379");
|
||||
assert.equal(rc.roleplayRedisUrl, "redis://r:6379");
|
||||
assert.equal(rc.resumeRedisUrl, "redis://re:6379");
|
||||
assert.equal(rc.coursesRedisUrl, "redis://c:6379");
|
||||
}
|
||||
|
||||
console.log("redis-gating tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,106 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { extractQscoreSignals } from "../src/events/projectors/qscore-projector.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
/**
|
||||
* Test: Service-ingest projector behavior across multiple service sources.
|
||||
* Verifies that canonical Grow events from different services produce
|
||||
* registry-valid signals with correct source attribution.
|
||||
*/
|
||||
|
||||
function event(overrides: Partial<GrowEventRow> & { type: string; source: string; payload: Record<string, unknown> }): GrowEventRow {
|
||||
return {
|
||||
id: `event-${overrides.type}`,
|
||||
userId: "user_test",
|
||||
orgId: null,
|
||||
source: overrides.source,
|
||||
type: overrides.type,
|
||||
category: "service",
|
||||
occurredAt: new Date("2026-07-09T00:00:00.000Z"),
|
||||
receivedAt: new Date("2026-07-09T00:00:01.000Z"),
|
||||
mission: overrides.mission ?? null,
|
||||
subject: overrides.subject ?? null,
|
||||
correlation: overrides.correlation ?? null,
|
||||
payload: overrides.payload,
|
||||
raw: {},
|
||||
dedupeKey: null,
|
||||
processingStatus: "pending",
|
||||
processingError: null,
|
||||
processedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 1. Resume analysis produces resume signals ──────────────────────────────
|
||||
{
|
||||
const signals = extractQscoreSignals(event({
|
||||
source: "resume-builder",
|
||||
type: "resume.analysis.completed",
|
||||
payload: { analysis: { score_breakdown: [{ category: "ATS Compatibility", score: 75 }] } },
|
||||
}));
|
||||
assert.ok(signals.length > 0, "resume analysis event should produce signals");
|
||||
assert.ok(
|
||||
signals.some((s) => s.signalId.startsWith("resume.")),
|
||||
"resume source should produce resume.* signals",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 2. Generic scored service event (qscore source) produces completion score ─
|
||||
{
|
||||
const signals = extractQscoreSignals(event({
|
||||
source: "qscore-service",
|
||||
type: "qscore.signal.projected",
|
||||
payload: { score: 65 },
|
||||
}));
|
||||
assert.ok(
|
||||
signals.some((s) => s.score === 65),
|
||||
"qscore source event with score should forward the score",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 3. Social branding pre-computed signals forwarded as-is ─────────────────
|
||||
{
|
||||
const signals = extractQscoreSignals(event({
|
||||
source: "social-branding-service",
|
||||
type: "brand.profile.updated",
|
||||
payload: {
|
||||
qscore_signals: [
|
||||
{ signalId: "linkedin.headline_quality", score: 70 },
|
||||
{ signalId: "linkedin.summary_complete", score: 65 },
|
||||
],
|
||||
},
|
||||
}));
|
||||
assert.ok(
|
||||
signals.some((s) => s.signalId === "linkedin.headline_quality" && s.score === 70),
|
||||
"social branding should forward pre-computed signals as-is",
|
||||
);
|
||||
assert.ok(
|
||||
signals.some((s) => s.signalId === "linkedin.summary_complete"),
|
||||
"social branding should forward all pre-computed signals",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 4. Unknown source with no score produces no signals ──────────────────────
|
||||
{
|
||||
const signals = extractQscoreSignals(event({
|
||||
source: "unknown-service",
|
||||
type: "service.completed",
|
||||
payload: {},
|
||||
}));
|
||||
assert.equal(signals.length, 0, "unknown source with no score should produce no signals");
|
||||
}
|
||||
|
||||
// ── 5. Matchmaking applied event with count ──────────────────────────────────
|
||||
{
|
||||
const signals = extractQscoreSignals(event({
|
||||
source: "matchmaking-v2",
|
||||
type: "matchmaking.match.applied",
|
||||
payload: { applications_submitted: 3 },
|
||||
}));
|
||||
assert.ok(
|
||||
signals.some((s) => s.signalId === "matching.applications_submitted" && s.score > 0),
|
||||
"matchmaking applied with count should produce applications_submitted signal",
|
||||
);
|
||||
}
|
||||
|
||||
console.log("service-ingest-projector tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,143 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const args = new Map();
|
||||
for (let i = 2; i < process.argv.length; i += 1) {
|
||||
const key = process.argv[i];
|
||||
if (!key.startsWith("--")) continue;
|
||||
const next = process.argv[i + 1];
|
||||
args.set(key.slice(2), next && !next.startsWith("--") ? next : "true");
|
||||
if (next && !next.startsWith("--")) i += 1;
|
||||
}
|
||||
|
||||
const requiredServices = [
|
||||
"interview-service",
|
||||
"roleplay-service",
|
||||
"courses-service",
|
||||
"assessment-service",
|
||||
"matchmaking-service",
|
||||
"resume-service",
|
||||
"cover-letter-service",
|
||||
"qscore-service",
|
||||
"social-branding-service",
|
||||
];
|
||||
|
||||
const registry = await import("../dist/services/service-registry.js");
|
||||
const capabilities = await import("../dist/workflows/service-capabilities.js");
|
||||
|
||||
function assert(condition, message, detail) {
|
||||
if (condition) return;
|
||||
const suffix = detail === undefined ? "" : `\n${JSON.stringify(detail, null, 2).slice(0, 3000)}`;
|
||||
throw new Error(`${message}${suffix}`);
|
||||
}
|
||||
|
||||
function assertEndpoint(serviceId, endpointId, endpoint) {
|
||||
assert(endpoint, `${serviceId} missing endpoint ${endpointId}`);
|
||||
assert(["GET", "POST", "PUT", "PATCH", "DELETE"].includes(endpoint.method), `${serviceId}.${endpointId} invalid method`, endpoint);
|
||||
assert(typeof endpoint.path === "string" && endpoint.path.startsWith("/"), `${serviceId}.${endpointId} invalid path`, endpoint);
|
||||
assert(typeof endpoint.contract === "string" && endpoint.contract.length > 8, `${serviceId}.${endpointId} missing contract`, endpoint);
|
||||
assert(typeof endpoint.usage === "string" && endpoint.usage.length > 8, `${serviceId}.${endpointId} missing usage`, endpoint);
|
||||
}
|
||||
|
||||
function assertPage(serviceId, pageId, page) {
|
||||
assert(page, `${serviceId} missing frontend page ${pageId}`);
|
||||
assert(typeof page.path === "string" && page.path.startsWith("/"), `${serviceId}.${pageId} invalid frontend path`, page);
|
||||
assert(Array.isArray(page.queryParams), `${serviceId}.${pageId} queryParams must be an array`, page);
|
||||
assert(typeof page.usage === "string" && page.usage.length > 8, `${serviceId}.${pageId} missing frontend usage`, page);
|
||||
}
|
||||
|
||||
const services = registry.listServices();
|
||||
assert(Array.isArray(services), "listServices did not return an array");
|
||||
assert(new Set(services.map((service) => service.id)).size === services.length, "registry contains duplicate service ids", services.map((s) => s.id));
|
||||
|
||||
for (const id of requiredServices) {
|
||||
const service = registry.getService(id);
|
||||
assert(service, `missing first-class service ${id}`);
|
||||
assert(service.id === id, `getService returned wrong id for ${id}`, service);
|
||||
assert(typeof service.label === "string" && service.label.length > 1, `${id} missing label`, service);
|
||||
assert(typeof service.description === "string" && service.description.length > 8, `${id} missing description`, service);
|
||||
assert(typeof service.featureId === "string" && service.featureId.length > 1, `${id} missing featureId`, service);
|
||||
assert(typeof service.promptModulePath === "string" && service.promptModulePath.length > 1, `${id} missing promptModulePath`, service);
|
||||
|
||||
assert(service.backend, `${id} missing backend`);
|
||||
assert(typeof service.backend.healthPath === "string" && service.backend.healthPath.startsWith("/"), `${id} missing healthPath`, service.backend);
|
||||
assert(typeof service.backend.usage === "string" && service.backend.usage.length > 8, `${id} missing backend usage`, service.backend);
|
||||
assert(service.backend.endpoints && Object.keys(service.backend.endpoints).length > 0, `${id} missing backend endpoints`, service.backend);
|
||||
for (const [endpointId, endpoint] of Object.entries(service.backend.endpoints)) assertEndpoint(id, endpointId, endpoint);
|
||||
|
||||
assert(service.frontend, `${id} missing frontend`);
|
||||
assert(typeof service.frontend.baseUrl === "string" && service.frontend.baseUrl.length > 0, `${id} missing frontend baseUrl`, service.frontend);
|
||||
assert(typeof service.frontend.usage === "string" && service.frontend.usage.length > 8, `${id} missing frontend usage`, service.frontend);
|
||||
assert(service.frontend.pages && Object.keys(service.frontend.pages).length > 0, `${id} missing frontend pages`, service.frontend);
|
||||
for (const [pageId, page] of Object.entries(service.frontend.pages)) assertPage(id, pageId, page);
|
||||
|
||||
assert(service.curator, `${id} missing curator`);
|
||||
assert(service.frontend.pages[service.curator.defaultPage], `${id} curator defaultPage is not a real page`, service.curator);
|
||||
assert(typeof service.curator.defaultActionLabel === "string" && service.curator.defaultActionLabel.length > 3, `${id} missing default action label`, service.curator);
|
||||
assert(Array.isArray(service.curator.completionEvents) && service.curator.completionEvents.length > 0, `${id} missing completion events`, service.curator);
|
||||
assert(typeof service.curator.toolName === "string" && service.curator.toolName.length > 3, `${id} missing curator toolName`, service.curator);
|
||||
|
||||
assert(Array.isArray(service.usageDocs) && service.usageDocs.length > 0, `${id} missing usageDocs`, service);
|
||||
assert(registry.getServiceBackend(id) === service.backend, `${id} getServiceBackend mismatch`);
|
||||
assert(registry.getServiceFrontend(id) === service.frontend, `${id} getServiceFrontend mismatch`);
|
||||
assert(registry.getCompletionEvents(id).length === service.curator.completionEvents.length, `${id} getCompletionEvents mismatch`);
|
||||
assert(registry.getServiceActionLabel(id, "start").length > 0, `${id} action label is empty`);
|
||||
|
||||
const endpoint = registry.getServiceEndpoint(id, Object.keys(service.backend.endpoints)[0]);
|
||||
assert(endpoint, `${id} getServiceEndpoint returned nothing`);
|
||||
const link = registry.buildServiceLink(id, service.curator.defaultPage, {
|
||||
source: "acceptance",
|
||||
missionInstanceId: "mission-acceptance",
|
||||
curatorTaskId: "task-acceptance",
|
||||
});
|
||||
assert(typeof link === "string" && link.startsWith("/"), `${id} buildServiceLink returned invalid link`, { link });
|
||||
assert(link.includes("source=acceptance"), `${id} buildServiceLink did not preserve state`, { link });
|
||||
assert(!link.includes("undefined") && !link.includes("null"), `${id} buildServiceLink leaked nullish values`, { link });
|
||||
}
|
||||
|
||||
assert(registry.getService("jobs-service")?.id === "matchmaking-service", "matchmaking alias failed");
|
||||
assert(registry.getService("coverletter-service")?.id === "cover-letter-service", "cover-letter alias failed");
|
||||
assert(registry.getService("q-score-service")?.id === "qscore-service", "qscore alias failed");
|
||||
assert(registry.getService("social-service")?.id === "social-branding-service", "social alias failed");
|
||||
|
||||
const catalog = registry.listServicesForCatalog();
|
||||
assert(catalog.length === services.length, "listServicesForCatalog count mismatch", { catalog: catalog.length, services: services.length });
|
||||
assert(!catalog.some((service) => service.backend?.baseUrl), "catalog leaks backend.baseUrl", catalog);
|
||||
|
||||
const publicCapabilities = capabilities.listServiceCapabilities({ public: true });
|
||||
const capabilityServices = publicCapabilities.filter((service) => requiredServices.includes(service.id));
|
||||
assert(publicCapabilities.length === services.length, "public capabilities should only expose canonical registry services", publicCapabilities.map((s) => s.id));
|
||||
assert(capabilityServices.length === requiredServices.length, "public capabilities missing required services", capabilityServices.map((s) => s.id));
|
||||
assert(!capabilityServices.some((service) => service.internalUrl || service.backend?.baseUrl), "public capabilities leak internal URL", capabilityServices);
|
||||
assert(!publicCapabilities.some((service) => service.id === "mission-planning"), "public capabilities leak internal mission-planning module", publicCapabilities);
|
||||
for (const service of capabilityServices) {
|
||||
const record = registry.getService(service.id);
|
||||
assert(record, `capability references unknown registry service ${service.id}`);
|
||||
assert(JSON.stringify(service.operations) === JSON.stringify(Object.keys(record.backend.endpoints)), `${service.id} operations not derived from endpoints`, {
|
||||
operations: service.operations,
|
||||
endpoints: Object.keys(record.backend.endpoints),
|
||||
});
|
||||
}
|
||||
|
||||
const baseUrl = args.get("base-url") || process.env.BACKEND_BASE_URL;
|
||||
const serviceToken = process.env.SERVICE_TOKEN;
|
||||
if (baseUrl) {
|
||||
assert(serviceToken, "SERVICE_TOKEN is required when --base-url/BACKEND_BASE_URL is provided");
|
||||
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/services/catalog`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${serviceToken}`,
|
||||
"x-growqr-user": "registry-acceptance",
|
||||
},
|
||||
});
|
||||
const text = await response.text();
|
||||
assert(response.ok, `live /services/catalog returned HTTP ${response.status}`, text);
|
||||
const live = JSON.parse(text);
|
||||
assert(Array.isArray(live.services), "live catalog missing services", live);
|
||||
assert(live.services.length === services.length, "live catalog should only expose canonical registry services", live.services.map((service) => service.id));
|
||||
for (const id of requiredServices) {
|
||||
assert(live.services.some((service) => service.id === id), `live catalog missing ${id}`, live);
|
||||
}
|
||||
assert(!live.services.some((service) => service.backend?.baseUrl), "live catalog leaks backend.baseUrl", live);
|
||||
assert(!live.services.some((service) => service.id === "mission-planning"), "live catalog leaks internal mission-planning module", live);
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({ ok: true, services: services.length, requiredServices: requiredServices.length, liveCatalog: Boolean(baseUrl) }));
|
||||
@@ -1,148 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const args = new Map();
|
||||
for (let i = 2; i < process.argv.length; i += 1) {
|
||||
const key = process.argv[i];
|
||||
if (!key.startsWith("--")) continue;
|
||||
const next = process.argv[i + 1];
|
||||
args.set(key.slice(2), next && !next.startsWith("--") ? next : "true");
|
||||
if (next && !next.startsWith("--")) i += 1;
|
||||
}
|
||||
|
||||
const baseUrl = (args.get("base-url") || process.env.BACKEND_BASE_URL || "http://127.0.0.1:4000").replace(/\/$/, "");
|
||||
const userId = args.get("user-id") || process.env.SMOKE_USER_ID || "registry-content-quality";
|
||||
const iterations = Number(args.get("iterations") || process.env.SMOKE_ITERATIONS || 1);
|
||||
const previewTimeoutMs = Number(args.get("preview-timeout-ms") || process.env.SMOKE_PREVIEW_TIMEOUT_MS || 180000);
|
||||
const serviceToken = process.env.SERVICE_TOKEN;
|
||||
|
||||
if (!serviceToken) {
|
||||
throw new Error("SERVICE_TOKEN is required for authenticated content-quality probes.");
|
||||
}
|
||||
|
||||
const badMarkers = [/placeholder/i, /dummy/i, /not implemented/i, /fallback/i, /lorem/i, /todo/i, /undefined/i];
|
||||
|
||||
function assert(condition, message, detail) {
|
||||
if (condition) return;
|
||||
const suffix = detail === undefined ? "" : `\n${JSON.stringify(detail, null, 2).slice(0, 3000)}`;
|
||||
throw new Error(`${message}${suffix}`);
|
||||
}
|
||||
|
||||
function outlineOf(json) {
|
||||
return Array.isArray(json?.question_outline) ? json.question_outline : json?.prompt_outline;
|
||||
}
|
||||
|
||||
function walk(value, path = "$", strings = [], nulls = []) {
|
||||
if (value === null) nulls.push(path);
|
||||
else if (typeof value === "string") strings.push(value);
|
||||
else if (Array.isArray(value)) value.forEach((item, index) => walk(item, `${path}[${index}]`, strings, nulls));
|
||||
else if (value && typeof value === "object") Object.entries(value).forEach(([key, item]) => walk(item, `${path}.${key}`, strings, nulls));
|
||||
return { strings, nulls };
|
||||
}
|
||||
|
||||
async function post(name, path, payload) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), previewTimeoutMs);
|
||||
const started = Date.now();
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}${path}`, {
|
||||
method: "POST",
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
authorization: `Bearer ${serviceToken}`,
|
||||
"x-growqr-user": userId,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const text = await response.text();
|
||||
const durationMs = Date.now() - started;
|
||||
assert(response.ok, `${name} returned HTTP ${response.status}`, { text, durationMs });
|
||||
return { json: JSON.parse(text), durationMs };
|
||||
} catch (error) {
|
||||
if (error?.name === "AbortError") {
|
||||
throw new Error(`${name} timed out after ${Date.now() - started}ms`, { cause: error });
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
function validatePreview(name, json) {
|
||||
const outline = outlineOf(json);
|
||||
assert(Array.isArray(outline) && outline.length >= 3, `${name} needs at least 3 outline items`, outline);
|
||||
|
||||
const { strings, nulls } = walk(json);
|
||||
assert(nulls.length === 0, `${name} contains null fields`, nulls.slice(0, 30));
|
||||
|
||||
const cleanStrings = strings.map((item) => item.trim()).filter(Boolean);
|
||||
for (const marker of badMarkers) {
|
||||
assert(!cleanStrings.some((item) => marker.test(item)), `${name} contains marker ${marker}`, cleanStrings.filter((item) => marker.test(item)).slice(0, 10));
|
||||
}
|
||||
|
||||
const prompts = outline
|
||||
.map((item) => String(item.question || item.prompt || item.text || "").replace(/\s+/g, " ").trim())
|
||||
.filter(Boolean);
|
||||
assert(prompts.length >= 3, `${name} outline prompts are missing text`, outline);
|
||||
assert(prompts.every((prompt) => prompt.length >= 35), `${name} outline prompts are too shallow`, prompts);
|
||||
assert(new Set(prompts.map((prompt) => prompt.toLowerCase())).size === prompts.length, `${name} outline prompts duplicate`, prompts);
|
||||
assert(String(json.opening_prompt || "").trim().length >= 35, `${name} opening prompt too short`, json.opening_prompt);
|
||||
|
||||
const briefText = walk(json.candidate_brief).strings.join(" ").replace(/\s+/g, " ").trim();
|
||||
assert(briefText.length >= 300, `${name} candidate brief too thin`, briefText);
|
||||
}
|
||||
|
||||
async function runIteration(iteration) {
|
||||
const user = `${userId}-${iteration}`;
|
||||
const interview = await post(`[content ${iteration}] interview preview`, "/services/interview/preview", {
|
||||
user_id: user,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
interview_type: "behavioral",
|
||||
duration_minutes: 5,
|
||||
context: {
|
||||
target_role: "Product Manager",
|
||||
company_name: "GrowQR Quality",
|
||||
difficulty: "medium",
|
||||
source: "registry-content-quality",
|
||||
personalize: false,
|
||||
},
|
||||
});
|
||||
validatePreview(`[content ${iteration}] interview preview`, interview.json);
|
||||
|
||||
const roleplay = await post(`[content ${iteration}] roleplay preview`, "/services/roleplay/preview", {
|
||||
user_id: user,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
duration_minutes: 5,
|
||||
roleplay_type: "custom",
|
||||
brief: "Practice a concise salary negotiation opening for a product manager offer.",
|
||||
metadata: {
|
||||
target_role: "Product Manager",
|
||||
candidate_role: "Product Manager",
|
||||
difficulty: "medium",
|
||||
source: "registry-content-quality",
|
||||
personalize: false,
|
||||
},
|
||||
});
|
||||
validatePreview(`[content ${iteration}] roleplay preview`, roleplay.json);
|
||||
assert(roleplay.json.scenario?.candidate_role === "Product Manager", `[content ${iteration}] roleplay did not expose explicit candidate_role`, roleplay.json.scenario);
|
||||
assert(typeof roleplay.json.scenario?.persona_role === "string" && roleplay.json.scenario.persona_role.length > 0, `[content ${iteration}] roleplay did not expose persona_role`, roleplay.json.scenario);
|
||||
|
||||
return {
|
||||
iteration,
|
||||
interviewSession: interview.json.session_id,
|
||||
interviewPreviewMs: interview.durationMs,
|
||||
roleplaySession: roleplay.json.session_id,
|
||||
roleplayPreviewMs: roleplay.durationMs,
|
||||
};
|
||||
}
|
||||
|
||||
const results = [];
|
||||
for (let i = 1; i <= iterations; i += 1) {
|
||||
const result = await runIteration(i);
|
||||
results.push(result);
|
||||
console.log(JSON.stringify(result));
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({ ok: true, iterations, results }));
|
||||
@@ -1,216 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const args = new Map();
|
||||
for (let i = 2; i < process.argv.length; i += 1) {
|
||||
const key = process.argv[i];
|
||||
if (!key.startsWith("--")) continue;
|
||||
const next = process.argv[i + 1];
|
||||
args.set(key.slice(2), next && !next.startsWith("--") ? next : "true");
|
||||
if (next && !next.startsWith("--")) i += 1;
|
||||
}
|
||||
|
||||
const baseUrl = (args.get("base-url") || process.env.BACKEND_BASE_URL || "http://127.0.0.1:4000").replace(/\/$/, "");
|
||||
const userId = args.get("user-id") || process.env.SMOKE_USER_ID || "registry-smoke";
|
||||
const iterations = Number(args.get("iterations") || process.env.SMOKE_ITERATIONS || 1);
|
||||
const previewTimeoutMs = Number(args.get("preview-timeout-ms") || process.env.SMOKE_PREVIEW_TIMEOUT_MS || 180000);
|
||||
const serviceToken = process.env.SERVICE_TOKEN;
|
||||
|
||||
if (!serviceToken) {
|
||||
throw new Error("SERVICE_TOKEN is required for authenticated backend smoke probes.");
|
||||
}
|
||||
|
||||
const requiredServices = [
|
||||
"interview-service",
|
||||
"roleplay-service",
|
||||
"resume-service",
|
||||
"cover-letter-service",
|
||||
"courses-service",
|
||||
"assessment-service",
|
||||
"matchmaking-service",
|
||||
"qscore-service",
|
||||
"social-branding-service",
|
||||
];
|
||||
|
||||
const directHealth = [
|
||||
["interview", "http://127.0.0.1:8007/health"],
|
||||
["roleplay", "http://127.0.0.1:8040/health"],
|
||||
["resume", "http://127.0.0.1:8002/health"],
|
||||
["qscore", "http://127.0.0.1:8000/health"],
|
||||
["courses", "http://127.0.0.1:8060/api/v1/health"],
|
||||
["assessment", "http://127.0.0.1:8070/api/v1/health"],
|
||||
["matchmaking", "http://127.0.0.1:8006/api/v1/health"],
|
||||
["pathways", "http://127.0.0.1:8009/api/v1/health"],
|
||||
["social", "http://127.0.0.1:8015/health"],
|
||||
];
|
||||
|
||||
function assert(condition, message, detail) {
|
||||
if (condition) return;
|
||||
const suffix = detail === undefined ? "" : `\n${JSON.stringify(detail, null, 2).slice(0, 3000)}`;
|
||||
throw new Error(`${message}${suffix}`);
|
||||
}
|
||||
|
||||
function authHeaders(extra = {}) {
|
||||
return {
|
||||
authorization: `Bearer ${serviceToken}`,
|
||||
"x-growqr-user": userId,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
async function request(name, url, init = {}, timeoutMs = 15000) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
const started = Date.now();
|
||||
try {
|
||||
const res = await fetch(url, { ...init, signal: controller.signal });
|
||||
const text = await res.text();
|
||||
let json;
|
||||
try {
|
||||
json = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
json = undefined;
|
||||
}
|
||||
const durationMs = Date.now() - started;
|
||||
assert(res.ok, `${name} returned HTTP ${res.status}`, { text, durationMs });
|
||||
return { json, text, durationMs };
|
||||
} catch (error) {
|
||||
if (error?.name === "AbortError") {
|
||||
const durationMs = Date.now() - started;
|
||||
throw new Error(`${name} timed out after ${durationMs}ms`, { cause: error });
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
function rejectFallbackLike(name, value) {
|
||||
if (value && typeof value === "object") {
|
||||
assert(!("error" in value), `${name} contains error field`, value);
|
||||
assert(!("detail" in value && /internal|fallback|not implemented/i.test(String(value.detail))), `${name} contains error detail`, value);
|
||||
}
|
||||
const text = JSON.stringify(value).toLowerCase();
|
||||
const bad = ["placeholder", "dummy", "not implemented", "fallback"];
|
||||
const found = bad.find((needle) => text.includes(needle));
|
||||
assert(!found, `${name} contains fallback/error-like marker: ${found}`, value);
|
||||
}
|
||||
|
||||
function assertGeneratedPreview(name, json) {
|
||||
rejectFallbackLike(name, json);
|
||||
assert(typeof json.session_id === "string" && json.session_id.length > 12, `${name} missing session_id`, json);
|
||||
assert(json.status === "draft", `${name} should create draft preview`, json);
|
||||
assert(json.needs_approval === true, `${name} should require approval`, json);
|
||||
|
||||
const outline = Array.isArray(json.question_outline) ? json.question_outline : json.prompt_outline;
|
||||
assert(Array.isArray(outline) && outline.length >= 2, `${name} missing generated outline`, json);
|
||||
assert(Boolean(json.opening_prompt), `${name} missing opening_prompt`, json);
|
||||
assert(Boolean(json.candidate_brief), `${name} missing candidate_brief`, json);
|
||||
}
|
||||
|
||||
async function runIteration(iteration) {
|
||||
const prefix = `[smoke ${iteration}]`;
|
||||
const health = await request(`${prefix} backend health`, `${baseUrl}/healthz`);
|
||||
assert(health.json?.ok === true, `${prefix} backend health payload invalid`, health.json);
|
||||
|
||||
const catalog = await request(`${prefix} services catalog`, `${baseUrl}/services/catalog`, {
|
||||
headers: authHeaders(),
|
||||
});
|
||||
const services = catalog.json?.services;
|
||||
assert(Array.isArray(services), `${prefix} catalog missing services`, catalog.json);
|
||||
for (const id of requiredServices) {
|
||||
assert(services.some((service) => service.id === id), `${prefix} catalog missing ${id}`, catalog.json);
|
||||
}
|
||||
assert(!services.some((service) => service.backend?.baseUrl), `${prefix} catalog leaks internal backend baseUrl`, catalog.json);
|
||||
assert(
|
||||
services.find((service) => service.id === "courses-service")?.backend?.healthPath === "/api/v1/health",
|
||||
`${prefix} courses health path is not canonical`,
|
||||
catalog.json,
|
||||
);
|
||||
|
||||
for (const [name, url] of directHealth) {
|
||||
const res = await request(`${prefix} ${name} direct health`, url, {}, 8000);
|
||||
rejectFallbackLike(`${prefix} ${name} direct health`, res.json ?? res.text);
|
||||
}
|
||||
|
||||
for (const service of ["interview", "roleplay", "resume", "social"]) {
|
||||
const res = await request(`${prefix} ${service} gateway health`, `${baseUrl}/services/${service}/health`, {
|
||||
headers: authHeaders(),
|
||||
});
|
||||
rejectFallbackLike(`${prefix} ${service} gateway health`, res.json ?? res.text);
|
||||
}
|
||||
|
||||
const interviewState = await request(`${prefix} interview page-state`, `${baseUrl}/services/interview/page-state`, {
|
||||
headers: authHeaders(),
|
||||
});
|
||||
assert(Array.isArray(interviewState.json?.recent_sessions), `${prefix} interview page-state missing recent_sessions`, interviewState.json);
|
||||
|
||||
const roleplayState = await request(`${prefix} roleplay page-state`, `${baseUrl}/services/roleplay/page-state`, {
|
||||
headers: authHeaders(),
|
||||
});
|
||||
assert(Array.isArray(roleplayState.json?.recent_sessions), `${prefix} roleplay page-state missing recent_sessions`, roleplayState.json);
|
||||
|
||||
const qscore = await request(`${prefix} qscore current`, `${baseUrl}/services/qscore/current`, {
|
||||
headers: authHeaders(),
|
||||
});
|
||||
assert("signals" in qscore.json && Array.isArray(qscore.json.signals), `${prefix} qscore current missing signals`, qscore.json);
|
||||
|
||||
const interviewPayload = {
|
||||
user_id: userId,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
interview_type: "behavioral",
|
||||
duration_minutes: 5,
|
||||
context: {
|
||||
target_role: "Product Manager",
|
||||
company_name: "GrowQR Smoke Test",
|
||||
difficulty: "medium",
|
||||
source: "registry-smoke",
|
||||
personalize: false,
|
||||
},
|
||||
};
|
||||
const interviewPreview = await request(`${prefix} interview preview generation`, `${baseUrl}/services/interview/preview`, {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify(interviewPayload),
|
||||
}, previewTimeoutMs);
|
||||
assertGeneratedPreview(`${prefix} interview preview generation`, interviewPreview.json);
|
||||
|
||||
const roleplayPayload = {
|
||||
user_id: userId,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
duration_minutes: 5,
|
||||
roleplay_type: "custom",
|
||||
brief: "Practice a concise salary negotiation opening for a product manager offer.",
|
||||
metadata: {
|
||||
target_role: "Product Manager",
|
||||
difficulty: "medium",
|
||||
source: "registry-smoke",
|
||||
personalize: false,
|
||||
},
|
||||
};
|
||||
const roleplayPreview = await request(`${prefix} roleplay preview generation`, `${baseUrl}/services/roleplay/preview`, {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify(roleplayPayload),
|
||||
}, previewTimeoutMs);
|
||||
assertGeneratedPreview(`${prefix} roleplay preview generation`, roleplayPreview.json);
|
||||
|
||||
return {
|
||||
iteration,
|
||||
catalogCount: services.length,
|
||||
interviewSession: interviewPreview.json.session_id,
|
||||
interviewPreviewMs: interviewPreview.durationMs,
|
||||
roleplaySession: roleplayPreview.json.session_id,
|
||||
roleplayPreviewMs: roleplayPreview.durationMs,
|
||||
};
|
||||
}
|
||||
|
||||
const results = [];
|
||||
for (let i = 1; i <= iterations; i += 1) {
|
||||
const result = await runIteration(i);
|
||||
results.push(result);
|
||||
console.log(JSON.stringify(result));
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({ ok: true, iterations, results }));
|
||||
@@ -1,236 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const args = new Map();
|
||||
for (let i = 2; i < process.argv.length; i += 1) {
|
||||
const key = process.argv[i];
|
||||
if (!key.startsWith("--")) continue;
|
||||
const next = process.argv[i + 1];
|
||||
args.set(key.slice(2), next && !next.startsWith("--") ? next : "true");
|
||||
if (next && !next.startsWith("--")) i += 1;
|
||||
}
|
||||
|
||||
const baseUrl = (args.get("base-url") || process.env.BACKEND_BASE_URL || "http://127.0.0.1:4000").replace(/\/$/, "");
|
||||
const userId = args.get("user-id") || process.env.SMOKE_USER_ID || "registry-write-smoke";
|
||||
const iterations = Number(args.get("iterations") || process.env.SMOKE_ITERATIONS || 1);
|
||||
const previewTimeoutMs = Number(args.get("preview-timeout-ms") || process.env.SMOKE_PREVIEW_TIMEOUT_MS || 180000);
|
||||
const serviceToken = process.env.SERVICE_TOKEN;
|
||||
|
||||
if (!serviceToken) {
|
||||
throw new Error("SERVICE_TOKEN is required for authenticated backend write-flow probes.");
|
||||
}
|
||||
|
||||
function assert(condition, message, detail) {
|
||||
if (condition) return;
|
||||
const suffix = detail === undefined ? "" : `\n${JSON.stringify(detail, null, 2).slice(0, 3000)}`;
|
||||
throw new Error(`${message}${suffix}`);
|
||||
}
|
||||
|
||||
function authHeaders(extra = {}) {
|
||||
return {
|
||||
authorization: `Bearer ${serviceToken}`,
|
||||
"x-growqr-user": userId,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
async function request(name, path, init = {}, timeoutMs = 90000) {
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
const started = Date.now();
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}${path}`, { ...init, signal: controller.signal });
|
||||
const text = await res.text();
|
||||
let json;
|
||||
try {
|
||||
json = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
json = undefined;
|
||||
}
|
||||
const durationMs = Date.now() - started;
|
||||
assert(res.ok, `${name} returned HTTP ${res.status}`, { text, durationMs });
|
||||
return { json, text, durationMs };
|
||||
} catch (error) {
|
||||
if (error?.name === "AbortError") {
|
||||
const durationMs = Date.now() - started;
|
||||
throw new Error(`${name} timed out after ${durationMs}ms`, { cause: error });
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
function rejectFallbackLike(name, value) {
|
||||
if (value && typeof value === "object") {
|
||||
assert(!("error" in value), `${name} contains error field`, value);
|
||||
assert(!("detail" in value && /internal|fallback|not implemented/i.test(String(value.detail))), `${name} contains error detail`, value);
|
||||
}
|
||||
const text = JSON.stringify(value).toLowerCase();
|
||||
const bad = ["placeholder", "dummy", "not implemented", "fallback"];
|
||||
const found = bad.find((needle) => text.includes(needle));
|
||||
assert(!found, `${name} contains fallback/error-like marker: ${found}`, value);
|
||||
}
|
||||
|
||||
function outlineOf(json) {
|
||||
return Array.isArray(json?.question_outline) ? json.question_outline : json?.prompt_outline;
|
||||
}
|
||||
|
||||
function assertDraftPreview(name, json) {
|
||||
rejectFallbackLike(name, json);
|
||||
assert(typeof json.session_id === "string" && json.session_id.length > 12, `${name} missing session_id`, json);
|
||||
assert(json.status === "draft", `${name} should create draft`, json);
|
||||
assert(json.needs_approval === true, `${name} should require approval`, json);
|
||||
assert(Array.isArray(outlineOf(json)) && outlineOf(json).length >= 2, `${name} missing generated outline`, json);
|
||||
assert(Boolean(json.opening_prompt), `${name} missing opening_prompt`, json);
|
||||
assert(Boolean(json.candidate_brief), `${name} missing candidate_brief`, json);
|
||||
}
|
||||
|
||||
function asInterviewQuestions(preview, iteration) {
|
||||
return outlineOf(preview).slice(0, 3).map((item, index) => ({
|
||||
text: `${String(item.question || item.text || "").replace(/\s+/g, " ").trim()} [write-flow ${iteration}.${index + 1}]`,
|
||||
topic: String(item.topic || `Smoke interview ${index + 1}`),
|
||||
expected_framework: String(item.expected_framework || "none"),
|
||||
}));
|
||||
}
|
||||
|
||||
function asRoleplayPrompts(preview, iteration) {
|
||||
return outlineOf(preview).slice(0, 3).map((item, index) => ({
|
||||
text: `${String(item.prompt || item.question || item.text || "").replace(/\s+/g, " ").trim()} [write-flow ${iteration}.${index + 1}]`,
|
||||
topic: String(item.topic || `Smoke roleplay ${index + 1}`),
|
||||
}));
|
||||
}
|
||||
|
||||
async function runInterviewFlow(iteration) {
|
||||
const prefix = `[write ${iteration}] interview`;
|
||||
const previewPayload = {
|
||||
user_id: userId,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
interview_type: "behavioral",
|
||||
duration_minutes: 5,
|
||||
context: {
|
||||
target_role: "Product Manager",
|
||||
company_name: "GrowQR Write Flow",
|
||||
difficulty: "medium",
|
||||
source: "registry-write-flow",
|
||||
personalize: false,
|
||||
},
|
||||
};
|
||||
const preview = await request(`${prefix} preview`, "/services/interview/preview", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify(previewPayload),
|
||||
}, previewTimeoutMs);
|
||||
assertDraftPreview(`${prefix} preview`, preview.json);
|
||||
|
||||
const questions = asInterviewQuestions(preview.json, iteration);
|
||||
assert(questions.every((item) => item.text.includes("[write-flow")), `${prefix} question edit payload invalid`, questions);
|
||||
const edited = await request(`${prefix} questions edit`, "/services/interview/questions", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify({ session_id: preview.json.session_id, questions }),
|
||||
});
|
||||
rejectFallbackLike(`${prefix} questions edit`, edited.json);
|
||||
assert(edited.json?.status === "draft", `${prefix} edit should keep draft status`, edited.json);
|
||||
assert(edited.json?.questions_edited === true, `${prefix} edit should mark questions_edited`, edited.json);
|
||||
assert(outlineOf(edited.json)?.[0]?.question?.includes("[write-flow"), `${prefix} edited question not persisted`, edited.json);
|
||||
|
||||
const approved = await request(`${prefix} approve`, "/services/interview/approve", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify({ session_id: preview.json.session_id }),
|
||||
});
|
||||
rejectFallbackLike(`${prefix} approve`, approved.json);
|
||||
assert(approved.json?.status === "configured", `${prefix} approve should configure session`, approved.json);
|
||||
assert(approved.json?.approved === true, `${prefix} approve missing approved flag`, approved.json);
|
||||
|
||||
const review = await request(`${prefix} review`, `/services/interview/review/${encodeURIComponent(preview.json.session_id)}`, {
|
||||
headers: authHeaders(),
|
||||
}, 15000);
|
||||
rejectFallbackLike(`${prefix} review`, review.json);
|
||||
assert(review.json?.status === "processing" || typeof review.json?.overall_score === "number", `${prefix} review shape invalid`, review.json);
|
||||
|
||||
return {
|
||||
sessionId: preview.json.session_id,
|
||||
reviewStatus: review.json?.status ?? "complete",
|
||||
durationsMs: {
|
||||
preview: preview.durationMs,
|
||||
edit: edited.durationMs,
|
||||
approve: approved.durationMs,
|
||||
review: review.durationMs,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function runRoleplayFlow(iteration) {
|
||||
const prefix = `[write ${iteration}] roleplay`;
|
||||
const previewPayload = {
|
||||
user_id: userId,
|
||||
org_id: "growqr",
|
||||
persona_id: "emma",
|
||||
duration_minutes: 5,
|
||||
roleplay_type: "custom",
|
||||
brief: "Practice a concise salary negotiation opening for a product manager offer.",
|
||||
metadata: {
|
||||
target_role: "Product Manager",
|
||||
difficulty: "medium",
|
||||
source: "registry-write-flow",
|
||||
personalize: false,
|
||||
},
|
||||
};
|
||||
const preview = await request(`${prefix} preview`, "/services/roleplay/preview", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify(previewPayload),
|
||||
}, previewTimeoutMs);
|
||||
assertDraftPreview(`${prefix} preview`, preview.json);
|
||||
|
||||
const questions = asRoleplayPrompts(preview.json, iteration);
|
||||
assert(questions.every((item) => item.text.includes("[write-flow")), `${prefix} prompt edit payload invalid`, questions);
|
||||
const edited = await request(`${prefix} prompt edit`, "/services/roleplay/questions", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify({ session_id: preview.json.session_id, questions }),
|
||||
});
|
||||
rejectFallbackLike(`${prefix} prompt edit`, edited.json);
|
||||
assert(edited.json?.status === "draft", `${prefix} edit should keep draft status`, edited.json);
|
||||
assert(edited.json?.questions_edited === true, `${prefix} edit should mark questions_edited`, edited.json);
|
||||
assert(outlineOf(edited.json)?.[0]?.prompt?.includes("[write-flow"), `${prefix} edited prompt not persisted`, edited.json);
|
||||
|
||||
const approved = await request(`${prefix} approve`, "/services/roleplay/approve", {
|
||||
method: "POST",
|
||||
headers: authHeaders({ "content-type": "application/json" }),
|
||||
body: JSON.stringify({ session_id: preview.json.session_id }),
|
||||
});
|
||||
rejectFallbackLike(`${prefix} approve`, approved.json);
|
||||
assert(approved.json?.status === "configured", `${prefix} approve should configure session`, approved.json);
|
||||
assert(approved.json?.approved === true, `${prefix} approve missing approved flag`, approved.json);
|
||||
|
||||
const review = await request(`${prefix} review`, `/services/roleplay/review/${encodeURIComponent(preview.json.session_id)}`, {
|
||||
headers: authHeaders(),
|
||||
}, 15000);
|
||||
rejectFallbackLike(`${prefix} review`, review.json);
|
||||
assert(review.json?.status === "processing" || typeof review.json?.overall_score === "number", `${prefix} review shape invalid`, review.json);
|
||||
|
||||
return {
|
||||
sessionId: preview.json.session_id,
|
||||
reviewStatus: review.json?.status ?? "complete",
|
||||
durationsMs: {
|
||||
preview: preview.durationMs,
|
||||
edit: edited.durationMs,
|
||||
approve: approved.durationMs,
|
||||
review: review.durationMs,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const results = [];
|
||||
for (let i = 1; i <= iterations; i += 1) {
|
||||
const interview = await runInterviewFlow(i);
|
||||
const roleplay = await runRoleplayFlow(i);
|
||||
const result = { iteration: i, interview, roleplay };
|
||||
results.push(result);
|
||||
console.log(JSON.stringify(result));
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({ ok: true, iterations, results }));
|
||||
@@ -1,238 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { extractQscoreSignals } from "../src/events/projectors/qscore-projector.js";
|
||||
import { ONBOARDING_BASELINE_SIGNAL_ID } from "../src/events/onboarding-qscore.js";
|
||||
import type { GrowEventRow } from "../src/db/schema.js";
|
||||
|
||||
/**
|
||||
* Test: All signal IDs emitted by the projector must be valid members of the
|
||||
* qscore_service v2 signal registry. The onboarding baseline signal must be
|
||||
* "onboarding.completed" (not the non-registry "onboarding.completed_baseline").
|
||||
*/
|
||||
|
||||
// v2 registry — union of all signal_ids across all 7 profession formula JSONs.
|
||||
// Sourced from qscore_service/app/scoring/formula/v2/*/formula.json
|
||||
const V2_REGISTRY = new Set<string>([
|
||||
// resume
|
||||
"resume.uploaded",
|
||||
"resume.ats_compatibility",
|
||||
"resume.keyword_relevance",
|
||||
"resume.quantified_achievements",
|
||||
"resume.grammar_clarity",
|
||||
"resume.format_structure",
|
||||
"resume.contact_info",
|
||||
"resume.page_count",
|
||||
// interview
|
||||
"interview.sessions_completed",
|
||||
"interview.overall_score",
|
||||
"interview.response_clarity",
|
||||
"interview.technical_accuracy",
|
||||
"interview.behavioral_quality",
|
||||
"interview.improvement_over_time",
|
||||
"interview.type_diversity",
|
||||
// roleplay
|
||||
"roleplay.scenarios_completed",
|
||||
"roleplay.situational_judgment",
|
||||
"roleplay.empathy_demonstrated",
|
||||
"roleplay.problem_resolution",
|
||||
"roleplay.communication_effectiveness",
|
||||
// courses
|
||||
"courses.started",
|
||||
"courses.completed",
|
||||
"courses.completion_rate",
|
||||
"courses.difficulty",
|
||||
"courses.pathway_relevance",
|
||||
// matching
|
||||
"matching.jobs_viewed",
|
||||
"matching.applications_submitted",
|
||||
"matching.application_quality",
|
||||
"matching.match_rate",
|
||||
// onboarding
|
||||
"onboarding.completed",
|
||||
"onboarding.initial_skills_score",
|
||||
"onboarding.goals_clarity",
|
||||
"onboarding.profile_completeness",
|
||||
"onboarding.self_assessment_accuracy",
|
||||
"onboarding.motivation_quality",
|
||||
// coverletter
|
||||
"coverletter.uploaded",
|
||||
"coverletter.customization",
|
||||
"coverletter.persuasiveness",
|
||||
// linkedin
|
||||
"linkedin.account_connected",
|
||||
"linkedin.profile_photo",
|
||||
"linkedin.headline_quality",
|
||||
"linkedin.summary_complete",
|
||||
"linkedin.experience_detail",
|
||||
"linkedin.skills_listed",
|
||||
// portfolio
|
||||
"portfolio.website_exists",
|
||||
"portfolio.website_quality",
|
||||
"portfolio.content_relevance",
|
||||
// content
|
||||
"content.articles_count",
|
||||
"content.quality",
|
||||
"content.platform_credibility",
|
||||
// ugc
|
||||
"ugc.speaking_count",
|
||||
"ugc.event_credibility",
|
||||
"ugc.verifiable_evidence",
|
||||
// assessments
|
||||
"assessments.taken",
|
||||
"assessments.passed",
|
||||
"assessments.avg_score",
|
||||
"assessments.skill_diversity",
|
||||
// presentations
|
||||
"presentations.submitted",
|
||||
"presentations.content_quality",
|
||||
"presentations.delivery_score",
|
||||
"presentations.visual_aids",
|
||||
// events
|
||||
"events.webinars_attended",
|
||||
"events.meetups_attended",
|
||||
"events.participation_quality",
|
||||
// mentor
|
||||
"mentor.feedback_count",
|
||||
"mentor.score",
|
||||
"mentor.implementation_rate",
|
||||
]);
|
||||
|
||||
function event(overrides: Partial<GrowEventRow> & { type: string; payload: Record<string, unknown>; source: string }): GrowEventRow {
|
||||
return {
|
||||
id: `event-${overrides.type}`,
|
||||
userId: "user_test",
|
||||
orgId: null,
|
||||
source: overrides.source,
|
||||
type: overrides.type,
|
||||
category: "service",
|
||||
occurredAt: new Date("2026-07-09T00:00:00.000Z"),
|
||||
receivedAt: new Date("2026-07-09T00:00:01.000Z"),
|
||||
mission: overrides.mission ?? null,
|
||||
subject: overrides.subject ?? null,
|
||||
correlation: overrides.correlation ?? null,
|
||||
payload: overrides.payload,
|
||||
raw: {},
|
||||
dedupeKey: null,
|
||||
processingStatus: "pending",
|
||||
processingError: null,
|
||||
processedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 1. Onboarding signal ID must be the registry-valid "onboarding.completed" ─
|
||||
assert.equal(
|
||||
ONBOARDING_BASELINE_SIGNAL_ID,
|
||||
"onboarding.completed",
|
||||
"Onboarding baseline signal ID must be 'onboarding.completed' (registry-valid), not 'onboarding.completed_baseline'",
|
||||
);
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(ONBOARDING_BASELINE_SIGNAL_ID),
|
||||
"Onboarding signal ID must appear in v2 registry",
|
||||
);
|
||||
|
||||
// ── 2. All projector signal IDs must be in the registry ──────────────────────
|
||||
// Construct representative events for each source family and check every emitted
|
||||
// signal ID is registry-valid.
|
||||
|
||||
// Resume with full breakdown
|
||||
const resumeSignals = extractQscoreSignals(event({
|
||||
source: "resume-builder",
|
||||
type: "resume.analysis.completed",
|
||||
payload: {
|
||||
analysis: {
|
||||
score_breakdown: [
|
||||
{ category: "ATS Compatibility", score: 70 },
|
||||
{ category: "Content Quality", score: 65 },
|
||||
{ category: "Formatting", score: 80 },
|
||||
],
|
||||
dimensional_scores: [
|
||||
{ dimension: "Keywords", score: 60 },
|
||||
{ dimension: "Quantification", score: 55 },
|
||||
],
|
||||
},
|
||||
},
|
||||
}));
|
||||
for (const s of resumeSignals) {
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(s.signalId),
|
||||
`Resume signal '${s.signalId}' is not in the v2 registry`,
|
||||
);
|
||||
}
|
||||
|
||||
// Interview with rubric
|
||||
const interviewSignals = extractQscoreSignals(event({
|
||||
source: "interview-service",
|
||||
type: "interview.session.completed",
|
||||
payload: {
|
||||
review: {
|
||||
overall_score: 72,
|
||||
rubric_scores: { content_quality: 70, role_alignment: 68, language: 65 },
|
||||
historical_comparison: { overall_delta: 5 },
|
||||
},
|
||||
session_count: 3,
|
||||
type_diversity: 2,
|
||||
},
|
||||
}));
|
||||
for (const s of interviewSignals) {
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(s.signalId),
|
||||
`Interview signal '${s.signalId}' is not in the v2 registry`,
|
||||
);
|
||||
}
|
||||
|
||||
// Roleplay with rubric
|
||||
const roleplaySignals = extractQscoreSignals(event({
|
||||
source: "roleplay-service",
|
||||
type: "roleplay.scenario.completed",
|
||||
payload: {
|
||||
review: {
|
||||
rubric_scores: {
|
||||
scenario_adherence: 75,
|
||||
emotional_intelligence: 70,
|
||||
adaptability: 68,
|
||||
content: 72,
|
||||
},
|
||||
},
|
||||
scenario_count: 4,
|
||||
},
|
||||
}));
|
||||
for (const s of roleplaySignals) {
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(s.signalId),
|
||||
`Roleplay signal '${s.signalId}' is not in the v2 registry`,
|
||||
);
|
||||
}
|
||||
|
||||
// Courses
|
||||
const courseSignals = extractQscoreSignals(event({
|
||||
source: "courses-service",
|
||||
type: "course.completed",
|
||||
payload: {
|
||||
courseId: "c1",
|
||||
completed_count: 2,
|
||||
watchPct: 0.9,
|
||||
difficulty: "intermediate",
|
||||
label: "high relevance",
|
||||
},
|
||||
}));
|
||||
for (const s of courseSignals) {
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(s.signalId),
|
||||
`Course signal '${s.signalId}' is not in the v2 registry`,
|
||||
);
|
||||
}
|
||||
|
||||
// Matchmaking
|
||||
const matchSignals = extractQscoreSignals(event({
|
||||
source: "matchmaking-v2",
|
||||
type: "matchmaking.feed.viewed",
|
||||
payload: { jobs_viewed: 25 },
|
||||
}));
|
||||
for (const s of matchSignals) {
|
||||
assert.ok(
|
||||
V2_REGISTRY.has(s.signalId),
|
||||
`Matchmaking signal '${s.signalId}' is not in the v2 registry`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log("signal-registry tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,95 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { computeStreakFromDays } from "../src/v1/curator/streak-utils.js";
|
||||
|
||||
/**
|
||||
* Test: Streak policies — current/longest, same-day duplicate, gap reset,
|
||||
* recovery (resume after gap), and seven-day eligibility.
|
||||
*
|
||||
* Uses the pure computeStreakFromDays function with a fixed "today" so tests
|
||||
* are deterministic.
|
||||
*/
|
||||
|
||||
const TODAY = "2026-07-10";
|
||||
|
||||
// ── 1. Current and longest from a contiguous run ending today ────────────────
|
||||
{
|
||||
const days = ["2026-07-10", "2026-07-09", "2026-07-08"];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
assert.equal(streak.current, 3, "current streak should be 3 for 3 consecutive days ending today");
|
||||
assert.equal(streak.longest, 3, "longest should match current when run is unbroken");
|
||||
assert.equal(streak.lastCompletedDate, "2026-07-10");
|
||||
}
|
||||
|
||||
// ── 2. Same-day duplicate must not inflate the streak ────────────────────────
|
||||
// computeStreakFromDays requires deduplicated days (the SQL GROUP BY ensures
|
||||
// this in production). Passing a duplicate should not double-count.
|
||||
{
|
||||
const days = ["2026-07-10", "2026-07-10", "2026-07-09"];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
// With a duplicate, the descending list has two "today" entries. The while
|
||||
// loop checks `days.includes(cursor)` which is set-based, so current stays 2.
|
||||
// But the longest loop iterates ALL entries including the dup, which could
|
||||
// inflate longest to 3. The correct behavior: duplicates must not inflate.
|
||||
assert.equal(streak.current, 2, "duplicate day must not inflate current streak");
|
||||
assert.equal(
|
||||
streak.longest,
|
||||
2,
|
||||
"duplicate day must not inflate longest streak — duplicates should be deduped before counting",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 3. Gap resets the current streak ─────────────────────────────────────────
|
||||
{
|
||||
// Completed today and 3 days ago — gap breaks current streak
|
||||
const days = ["2026-07-10", "2026-07-07"];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
assert.equal(streak.current, 1, "gap resets current streak to 1 (only today)");
|
||||
assert.equal(streak.longest, 1, "longest should also be 1 with only isolated days");
|
||||
}
|
||||
|
||||
// ── 4. Recovery: longest captures a past longer run even if current is broken ─
|
||||
{
|
||||
// 5-day run last week, then a gap, then completed today
|
||||
const days = [
|
||||
"2026-07-10", // today (current restart)
|
||||
"2026-07-05", // past run: Jul 1–5
|
||||
"2026-07-04",
|
||||
"2026-07-03",
|
||||
"2026-07-02",
|
||||
"2026-07-01",
|
||||
];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
assert.equal(streak.current, 1, "current is 1 after a gap even if a longer past run exists");
|
||||
assert.equal(streak.longest, 5, "longest should capture the 5-day past run");
|
||||
}
|
||||
|
||||
// ── 5. Seven-day eligibility: a 7-day streak counts as eligible ──────────────
|
||||
{
|
||||
const days = [
|
||||
"2026-07-10", "2026-07-09", "2026-07-08",
|
||||
"2026-07-07", "2026-07-06", "2026-07-05", "2026-07-04",
|
||||
];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
assert.equal(streak.current, 7, "7 consecutive days should yield current=7");
|
||||
assert.equal(streak.longest, 7, "7 consecutive days should yield longest=7");
|
||||
assert.ok(streak.current >= 7, "7-day streak eligibility threshold met");
|
||||
}
|
||||
|
||||
// ── 6. Empty days yields zero streak ─────────────────────────────────────────
|
||||
{
|
||||
const streak = computeStreakFromDays([], TODAY);
|
||||
assert.equal(streak.current, 0);
|
||||
assert.equal(streak.longest, 0);
|
||||
assert.equal(streak.lastCompletedDate, null);
|
||||
}
|
||||
|
||||
// ── 7. Not completed today but completed yesterday: current is 0 ─────────────
|
||||
{
|
||||
const days = ["2026-07-09"];
|
||||
const streak = computeStreakFromDays(days, TODAY);
|
||||
assert.equal(streak.current, 0, "current is 0 if today is not completed");
|
||||
assert.equal(streak.longest, 1, "longest should still count the 1-day run");
|
||||
}
|
||||
|
||||
console.log("streak-policy tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,97 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { isProcessableStatus } from "../src/actors/events/user-event-actor.js";
|
||||
|
||||
/**
|
||||
* Regression: one failed event must not block the next event in the queue.
|
||||
*
|
||||
* Before the fix, the actor's run loop used loopCtx.step + manual try/catch
|
||||
* that rethrew on failure. The rethrow stalled the ctx.loop, preventing
|
||||
* subsequent queued messages from being processed.
|
||||
*
|
||||
* After the fix, the loop uses tryStep (failures caught, event marked terminal)
|
||||
* and a DB status guard (isProcessableStatus) that only skips truly inert rows.
|
||||
*
|
||||
* Dual-path rationale: the synchronous ingress paths (routes/events.ts,
|
||||
* redis-consumer.ts, v1/events/events-routes.ts) run projections and mark the
|
||||
* event "processed". The actor runs mission reducers (stage patches, artifacts,
|
||||
* actions) that ONLY exist in the actor. So the actor must still process
|
||||
* "processed" events to run mission reducers — projections are idempotent so
|
||||
* re-running them is safe, and the in-memory processedEventIds guard prevents
|
||||
* double-processing within one actor lifetime. The routing gate
|
||||
* (shouldRouteGrowEvent) prevents saturation from duplicate ingests; this guard
|
||||
* is defense-in-depth that only blocks "failed" and "unresolved".
|
||||
*/
|
||||
|
||||
// ── 1. Pending → processable (first attempt) ─────────────────────────────────
|
||||
assert.equal(isProcessableStatus("pending"), true, "pending events must be processable");
|
||||
|
||||
// ── 2. Processing → processable (crash recovery: retry idempotent work) ──────
|
||||
// A prior attempt called markGrowEventProcessing then crashed before completing.
|
||||
// The actor restarts, replays the queue message, and must retry.
|
||||
assert.equal(isProcessableStatus("processing"), true, "processing events must be retryable after a crash");
|
||||
|
||||
// ── 3. Processed → processable (actor must run mission reducers) ─────────────
|
||||
// The sync ingress path marks "processed" after projections. The actor still
|
||||
// needs to run mission reducers — the only place they exist. Projections
|
||||
// re-running is safe (idempotent). processedEventIds prevents in-memory dupes.
|
||||
assert.equal(
|
||||
isProcessableStatus("processed"),
|
||||
true,
|
||||
"processed events must be processable — actor must run mission reducers that only exist in the actor",
|
||||
);
|
||||
|
||||
// ── 4. Failed → NOT processable (terminal, do not retry) ─────────────────────
|
||||
// A per-event failure was already marked terminal by the record-failure step.
|
||||
assert.equal(isProcessableStatus("failed"), false, "failed events must be skipped");
|
||||
|
||||
// ── 5. Unresolved → NOT processable (no userId, nothing to do) ───────────────
|
||||
assert.equal(isProcessableStatus("unresolved"), false, "unresolved events must be skipped");
|
||||
|
||||
// ── 6. Null/undefined → NOT processable (defensive) ──────────────────────────
|
||||
assert.equal(isProcessableStatus(null), false, "null status must be skipped");
|
||||
assert.equal(isProcessableStatus(undefined), false, "undefined status must be skipped");
|
||||
|
||||
// ── 7. Unknown status → NOT processable (defensive) ──────────────────────────
|
||||
assert.equal(isProcessableStatus("unknown"), false, "unknown status must be skipped");
|
||||
|
||||
// ── 8. Queue advancement simulation ──────────────────────────────────────────
|
||||
// Queue: [pending, processed, pending]. Actor processes ALL three — the
|
||||
// "processed" one still gets mission reducers. The key point: none of them
|
||||
// block the next. This proves one event does not stall the queue.
|
||||
{
|
||||
const statuses = ["pending", "processed", "pending"] as const;
|
||||
const processable = statuses.map((s) => isProcessableStatus(s));
|
||||
assert.deepEqual(
|
||||
processable,
|
||||
[true, true, true],
|
||||
"actor must process all three — processed event still runs mission reducers, none block the next",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 9. Failed event does not block the next ──────────────────────────────────
|
||||
// Queue: [failed, pending]. Actor skips the failed one and processes the next.
|
||||
// This is the core regression: one failed event must not block subsequent events.
|
||||
{
|
||||
const statuses = ["failed", "pending"] as const;
|
||||
const processable = statuses.map((s) => isProcessableStatus(s));
|
||||
assert.deepEqual(
|
||||
processable,
|
||||
[false, true],
|
||||
"actor must skip failed event and process the next pending event — failed does not block",
|
||||
);
|
||||
}
|
||||
|
||||
// ── 10. Crash-recovery queue simulation ──────────────────────────────────────
|
||||
// Queue: [processing (crashed), pending]. Actor retries #1 and processes #2.
|
||||
{
|
||||
const statuses = ["processing", "pending"] as const;
|
||||
const processable = statuses.map((s) => isProcessableStatus(s));
|
||||
assert.deepEqual(
|
||||
processable,
|
||||
[true, true],
|
||||
"actor must retry a crashed 'processing' event AND process the next pending event",
|
||||
);
|
||||
}
|
||||
|
||||
console.log("user-event-actor-advance tests passed");
|
||||
process.exit(0);
|
||||
@@ -1,165 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { fetchUserProfile, patchPreferencesRequest } from "../src/services/user-profile.js";
|
||||
|
||||
/**
|
||||
* Regression test for the PATCH /onboarding 500.
|
||||
*
|
||||
* Root cause: the PATCH handler parsed its JSON body via `c.req.json()`
|
||||
* (consuming `c.req.raw` / setting bodyUsed), then called fetchUserProfile
|
||||
* with the same Request. fetchUserProfile replayed the inbound method+body to
|
||||
* user-service /me, so fetchUserService did `await req.arrayBuffer()` on an
|
||||
* already-consumed body → `TypeError: Body is unusable` → uncaught → global
|
||||
* onError → {"error":"internal"}, 500. GET /onboarding worked only because it
|
||||
* never reads a body, so fetchUserService skipped arrayBuffer.
|
||||
*
|
||||
* This test reproduces the exact precondition — consume the body first — and
|
||||
* asserts the fix: fetchUserProfile issues an explicit GET that never touches
|
||||
* the inbound Request's body. It would have thrown before the fix and passes
|
||||
* after.
|
||||
*/
|
||||
|
||||
// Capture the outgoing fetch call.
|
||||
type FetchCall = { method: string; url: string; bodyTouched: boolean };
|
||||
|
||||
async function runFetchUserProfileAfterBodyConsumed(): Promise<{
|
||||
profile: Record<string, unknown>;
|
||||
call: FetchCall;
|
||||
}> {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let captured: FetchCall | null = null;
|
||||
|
||||
const req = new Request("https://backend.test/api/growqr/users/onboarding", {
|
||||
method: "PATCH",
|
||||
headers: { "content-type": "application/json", authorization: "Bearer test" },
|
||||
body: JSON.stringify({ data: { access_choice: "trial" }, expectedRevision: 0 }),
|
||||
});
|
||||
|
||||
// Simulate what the PATCH handler does: parse the JSON body. This marks
|
||||
// req.bodyUsed = true — the precondition that triggered the 500.
|
||||
await req.json();
|
||||
|
||||
globalThis.fetch = (async (input: URL | Request, init?: RequestInit) => {
|
||||
const target = input instanceof URL ? input : new URL(input instanceof Request ? input.url : String(input));
|
||||
// Detect if the caller tried to re-read the original (consumed) Request body.
|
||||
let bodyTouched = false;
|
||||
if (input instanceof Request) {
|
||||
try {
|
||||
await input.clone().arrayBuffer();
|
||||
bodyTouched = input.bodyUsed;
|
||||
} catch {
|
||||
bodyTouched = true; // Body is unusable — the original bug.
|
||||
}
|
||||
}
|
||||
captured = { method: init?.method ?? input.method ?? "GET", url: target.href, bodyTouched };
|
||||
return new Response(JSON.stringify({ preferences: { onboarding: { access_choice: "trial" } } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}) as typeof globalThis.fetch;
|
||||
|
||||
try {
|
||||
const profile = await fetchUserProfile(req);
|
||||
assert(captured, "fetch was never called");
|
||||
return { profile, call: captured };
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 1. no throw when inbound body was already consumed ──────────────────────
|
||||
{
|
||||
const { call } = await runFetchUserProfileAfterBodyConsumed();
|
||||
assert.equal(call.bodyTouched, false, "fetchUserProfile must not re-read the inbound Request body");
|
||||
}
|
||||
|
||||
// ── 2. outgoing method is GET (a read, never PATCH) ─────────────────────────
|
||||
{
|
||||
const { call } = await runFetchUserProfileAfterBodyConsumed();
|
||||
assert.equal(call.method, "GET", "fetchUserProfile must issue GET /me regardless of inbound method");
|
||||
}
|
||||
|
||||
// ── 3. target is /api/v1/users/me and profile parses ───────────────────────
|
||||
{
|
||||
const { profile, call } = await runFetchUserProfileAfterBodyConsumed();
|
||||
assert.ok(call.url.endsWith("/api/v1/users/me"), `expected /me URL, got ${call.url}`);
|
||||
assert.deepEqual(profile, { preferences: { onboarding: { access_choice: "trial" } } });
|
||||
}
|
||||
|
||||
// ── 4. content-type header is stripped (GET has no body) ────────────────────
|
||||
{
|
||||
const originalFetch = globalThis.fetch;
|
||||
let capturedHeaders: Headers | null = null;
|
||||
globalThis.fetch = (async (input: URL | Request, init?: RequestInit) => {
|
||||
capturedHeaders = new Headers(init?.headers ?? {});
|
||||
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
|
||||
}) as typeof globalThis.fetch;
|
||||
try {
|
||||
const req = new Request("https://backend.test/api/growqr/users/onboarding", {
|
||||
method: "PATCH",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
await req.json();
|
||||
await fetchUserProfile(req);
|
||||
assert.ok(capturedHeaders, "fetch was never called");
|
||||
assert.equal(capturedHeaders!.get("content-type"), null, "content-type must be stripped from GET /me");
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 5. non-2xx surfaces a thrown error (caller handles) ─────────────────────
|
||||
{
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => new Response("nope", { status: 503 })) as typeof globalThis.fetch;
|
||||
try {
|
||||
const req = new Request("https://backend.test/api/growqr/users/onboarding", { method: "GET" });
|
||||
await assert.rejects(
|
||||
() => fetchUserProfile(req),
|
||||
/user-service \/me fetch failed: 503/,
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 6. synthetic PATCH /me must not carry a stale content-length ───────────
|
||||
// Regression for the second PATCH 500: patchPreferencesRequest copied inbound
|
||||
// headers including content-length, then set a DIFFERENT body — undici kept the
|
||||
// stale length, user-service waited for bytes that never arrive, and closed
|
||||
// the socket (~2.8s "other side closed"). The Request constructor does NOT
|
||||
// recompute content-length when an explicit body is provided alongside
|
||||
// forwarded headers, so the deletes inside patchPreferencesRequest are
|
||||
// load-bearing. This tests the REAL exported function.
|
||||
{
|
||||
// Inbound request with a body whose length differs from the synthetic body.
|
||||
const inboundBody = JSON.stringify({ data: { access_choice: "trial" }, expectedRevision: 0 });
|
||||
const req = new Request("https://backend.test/api/growqr/users/onboarding", {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"content-length": String(Buffer.byteLength(inboundBody)),
|
||||
"transfer-encoding": "chunked",
|
||||
},
|
||||
body: inboundBody,
|
||||
});
|
||||
|
||||
const preferences = { onboarding: { access_choice: "trial" } };
|
||||
const synthetic = patchPreferencesRequest(req, preferences);
|
||||
|
||||
// The synthetic request must not carry the stale inbound length/transfer headers.
|
||||
assert.equal(synthetic.headers.get("content-length"), null, "stale content-length must be stripped from synthetic PATCH /me");
|
||||
assert.equal(synthetic.headers.get("transfer-encoding"), null, "transfer-encoding must be stripped from synthetic PATCH /me");
|
||||
assert.equal(synthetic.headers.get("host"), null, "host must be stripped");
|
||||
assert.equal(synthetic.headers.get("cookie"), null, "cookie must be stripped");
|
||||
assert.equal(synthetic.headers.get("content-type"), "application/json", "content-type must be set");
|
||||
assert.equal(synthetic.method, "PATCH", "method must be PATCH");
|
||||
assert.ok(synthetic.url.endsWith("/api/v1/users/me"), `target must be /me, got ${synthetic.url}`);
|
||||
|
||||
// The body must be the synthetic preferences blob, not the inbound payload.
|
||||
const sentBody = await synthetic.text();
|
||||
assert.deepEqual(JSON.parse(sentBody), { preferences }, "synthetic body must carry only { preferences }");
|
||||
assert.notEqual(sentBody.length, Buffer.byteLength(inboundBody), "synthetic body length must differ from inbound (else the test proves nothing)");
|
||||
}
|
||||
|
||||
console.log("user-profile: all assertions passed");
|
||||
@@ -1,152 +0,0 @@
|
||||
import { actor } from "rivetkit";
|
||||
import { count, desc, eq, sql } from "drizzle-orm";
|
||||
import { db } from "../../db/client.js";
|
||||
import {
|
||||
growActiveMissions,
|
||||
growEvents,
|
||||
growQscoreSignals,
|
||||
missionActions,
|
||||
} from "../../db/schema.js";
|
||||
import { listActiveMissionsPg } from "../../grow/persistence.js";
|
||||
import { listMissionActions } from "../../missions/actions.js";
|
||||
import { DEFAULT_QSCORE_ORG_ID, getQscoreFromService } from "../../services/qscore-proxy.js";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
async function scalarCount(table: any, where?: any) {
|
||||
const query = db.select({ value: count() }).from(table);
|
||||
const rows = where ? await query.where(where) : await query;
|
||||
return rows[0]?.value ?? 0;
|
||||
}
|
||||
|
||||
async function platformAnalytics() {
|
||||
const [
|
||||
totalEvents,
|
||||
serviceEvents,
|
||||
missionEvents,
|
||||
activeMissions,
|
||||
completedMissions,
|
||||
totalActions,
|
||||
doneActions,
|
||||
qscoreSignalCount,
|
||||
] = await Promise.all([
|
||||
scalarCount(growEvents),
|
||||
scalarCount(growEvents, eq(growEvents.category, "service")),
|
||||
scalarCount(growEvents, eq(growEvents.category, "mission")),
|
||||
scalarCount(growActiveMissions),
|
||||
scalarCount(growActiveMissions, eq(growActiveMissions.status, "completed")),
|
||||
scalarCount(missionActions),
|
||||
scalarCount(missionActions, eq(missionActions.status, "done")),
|
||||
scalarCount(growQscoreSignals),
|
||||
]);
|
||||
|
||||
const serviceUsage = await db
|
||||
.select({
|
||||
source: growEvents.source,
|
||||
type: growEvents.type,
|
||||
count: sql<number>`count(*)::int`,
|
||||
})
|
||||
.from(growEvents)
|
||||
.where(eq(growEvents.category, "service"))
|
||||
.groupBy(growEvents.source, growEvents.type)
|
||||
.orderBy(sql`count(*) desc`)
|
||||
.limit(20);
|
||||
|
||||
return {
|
||||
kind: "platform",
|
||||
generatedAt: new Date().toISOString(),
|
||||
totals: {
|
||||
events: totalEvents,
|
||||
serviceEvents,
|
||||
missionEvents,
|
||||
activeMissions,
|
||||
completedMissions,
|
||||
missionActions: totalActions,
|
||||
completedActions: doneActions,
|
||||
qscoreSignals: qscoreSignalCount,
|
||||
},
|
||||
serviceUsage,
|
||||
};
|
||||
}
|
||||
|
||||
async function userQscoreAnalytics(userId: string) {
|
||||
const result = await getQscoreFromService(userId, DEFAULT_QSCORE_ORG_ID);
|
||||
|
||||
const breakdown = result?.breakdown ?? {};
|
||||
const latestSignals = Array.isArray(breakdown.signals) ? breakdown.signals : [];
|
||||
const timelineRaw = Array.isArray(breakdown.signalTimeline) ? breakdown.signalTimeline : latestSignals;
|
||||
const signalTimeline = timelineRaw.map((item) => {
|
||||
const s = isRecord(item) ? item : {};
|
||||
return {
|
||||
signalId: typeof s.signalId === "string" ? s.signalId : typeof s.signal_id === "string" ? s.signal_id : "",
|
||||
score: typeof s.score === "number" ? s.score : 0,
|
||||
present: typeof s.present === "boolean" ? s.present : true,
|
||||
source: typeof s.source === "string" ? s.source : "",
|
||||
occurredAt: typeof s.occurredAt === "string" ? s.occurredAt : result?.calculated_at ?? new Date().toISOString(),
|
||||
updatedAt: typeof s.updatedAt === "string" ? s.updatedAt : result?.calculated_at ?? new Date().toISOString(),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
kind: "user-qscore",
|
||||
userId,
|
||||
generatedAt: new Date().toISOString(),
|
||||
current: result
|
||||
? {
|
||||
score: result.q_score,
|
||||
iq_score: result.iq_score,
|
||||
eq_score: result.eq_score,
|
||||
sq_score: result.sq_score,
|
||||
signalCount: signalTimeline.length,
|
||||
dimensions: isRecord(breakdown.dimensions) ? breakdown.dimensions : result.quotients,
|
||||
summary: typeof breakdown.summary === "string" ? breakdown.summary : null,
|
||||
updatedAt: result.calculated_at || new Date().toISOString(),
|
||||
}
|
||||
: null,
|
||||
latestSignals,
|
||||
signalTimeline,
|
||||
globalComparison: {
|
||||
status: "placeholder",
|
||||
percentile: null,
|
||||
cohort: null,
|
||||
sampleSize: null,
|
||||
note: "Global comparison is reserved for the cohort-backed analytics release.",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function userActivityAnalytics(userId: string) {
|
||||
const events = await db.select().from(growEvents).where(eq(growEvents.userId, userId)).orderBy(desc(growEvents.occurredAt)).limit(100);
|
||||
const activeMissions = await listActiveMissionsPg(userId).catch(() => []);
|
||||
const actions = await listMissionActions(userId, { openOnly: false }).catch(() => []);
|
||||
|
||||
return {
|
||||
kind: "user-activity",
|
||||
userId,
|
||||
generatedAt: new Date().toISOString(),
|
||||
events,
|
||||
activeMissions: activeMissions.map((item) => item.mission),
|
||||
actions,
|
||||
};
|
||||
}
|
||||
|
||||
export const analyticsActor = actor({
|
||||
options: { name: "Analytics Actor", icon: "chart-no-axes-column", noSleep: true },
|
||||
state: { updatedAt: Date.now() },
|
||||
actions: {
|
||||
getPlatform: async (c) => {
|
||||
c.state.updatedAt = Date.now();
|
||||
return platformAnalytics();
|
||||
},
|
||||
getUserQscore: async (c, input: { userId: string }) => {
|
||||
c.state.updatedAt = Date.now();
|
||||
return userQscoreAnalytics(input.userId);
|
||||
},
|
||||
getUserActivity: async (c, input: { userId: string }) => {
|
||||
c.state.updatedAt = Date.now();
|
||||
return userActivityAnalytics(input.userId);
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -1 +0,0 @@
|
||||
export { analyticsActor } from "./analytics-actor.js";
|
||||
@@ -1,35 +1,13 @@
|
||||
import { createOpenAI } from "@ai-sdk/openai";
|
||||
import { generateText, stepCountIs, streamText, tool } from "ai";
|
||||
import { createClient } from "rivetkit/client";
|
||||
import { streamText, tool } from "ai";
|
||||
import { z } from "zod";
|
||||
import type { ConversationMessage } from "./types.js";
|
||||
import { config } from "../../config.js";
|
||||
import { listMissionDefinitions } from "../../missions/registry.js";
|
||||
import { createMissionAction, listMissionActions } from "../../missions/actions.js";
|
||||
import { getActiveMissionPg, listActiveMissionsPg, listMissionSuggestionsPg } from "../../grow/persistence.js";
|
||||
import { listServiceCapabilities } from "../../workflows/service-capabilities.js";
|
||||
import { getSubAgentModules } from "../../lib/prompt-loader.js";
|
||||
import { buildMissionServiceRoute } from "../../services/service-registry.js";
|
||||
|
||||
const SYSTEM_PROMPT = `You are the GrowQR conversation agent.
|
||||
Keep answers concise, practical, and focused on the user's active mission.
|
||||
Use tools when you need mission state, registry capabilities, memory, or a service handoff.
|
||||
Service tools prepare handoffs and mission actions; the interview, roleplay, and resume services own their detailed flows.
|
||||
|
||||
Style rules:
|
||||
- Use ASCII punctuation only. Do not use em dash or en dash.
|
||||
- Do not start with filler words like Perfect, Great, Absolutely, or Sure.
|
||||
- For Daily Mission turns, ask one short direct question. Keep it under 24 words.`;
|
||||
|
||||
export type ConversationRuntimeContext = {
|
||||
userId?: string;
|
||||
conversationId?: string;
|
||||
missionInstanceId?: string;
|
||||
missionId?: string;
|
||||
stageId?: string;
|
||||
source?: string;
|
||||
systemAddendum?: string;
|
||||
};
|
||||
Keep answers concise, practical, and focused on the user's goals.
|
||||
When you learn durable information, call the memory tools. For now these tools
|
||||
are intentionally stubbed so this actor can stay isolated and unwired.`;
|
||||
|
||||
function normalizeModel(model: string): string {
|
||||
if (config.llmProvider === "opencode" && model.startsWith("opencode/")) {
|
||||
@@ -53,206 +31,46 @@ export function getConversationModel() {
|
||||
return conversationProvider.chat(normalizeModel(modelId));
|
||||
}
|
||||
|
||||
type ModelConversationMessage = Pick<ConversationMessage, "role" | "content">;
|
||||
|
||||
export function buildModelMessages(messages: ModelConversationMessage[]) {
|
||||
export function buildModelMessages(messages: ConversationMessage[]) {
|
||||
return messages.map((message) => ({
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
}));
|
||||
}
|
||||
|
||||
let _client: any | null = null;
|
||||
function getRivetClient() {
|
||||
return (_client ??= createClient<any>(config.rivetClientEndpoint));
|
||||
}
|
||||
|
||||
function safeAgentRegistry() {
|
||||
try {
|
||||
return getSubAgentModules();
|
||||
} catch {
|
||||
return [
|
||||
{ id: "interview", name: "Interview Agent", role: "Interview Coach", service: "interview-service", description: "Interview prep specialist.", toolNames: ["prepare_interview_handoff"] },
|
||||
{ id: "roleplay", name: "Roleplay Agent", role: "Roleplay Coach", service: "roleplay-service", description: "Workplace conversation practice specialist.", toolNames: ["prepare_roleplay_handoff"] },
|
||||
{ id: "resume", name: "Resume Agent", role: "Resume Agent", service: "resume-service", description: "Resume positioning and optimization specialist.", toolNames: ["prepare_resume_handoff"] },
|
||||
{ id: "qscore", name: "Q Score Agent", role: "Q Score Analyst", service: "qscore-service", description: "Readiness score analyst.", toolNames: ["explain_qscore"] },
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveMission(userId: string, missionInstanceId?: string) {
|
||||
if (missionInstanceId) return getActiveMissionPg(userId, missionInstanceId);
|
||||
const active = await listActiveMissionsPg(userId);
|
||||
return active[0] ?? null;
|
||||
}
|
||||
|
||||
function serviceHref(input: {
|
||||
serviceId: "interview-service" | "roleplay-service" | "resume-service";
|
||||
missionInstanceId: string;
|
||||
missionId: string;
|
||||
stageId?: string;
|
||||
goal?: string;
|
||||
}) {
|
||||
return buildMissionServiceRoute(input);
|
||||
}
|
||||
|
||||
function buildConversationTools(ctx: ConversationRuntimeContext = {}) {
|
||||
const userId = ctx.userId;
|
||||
return {
|
||||
listMissionState: tool({
|
||||
description: "Read mission snapshot, open actions, and suggestions for the current or requested mission.",
|
||||
inputSchema: z.object({ missionInstanceId: z.string().optional() }),
|
||||
execute: async ({ missionInstanceId }) => {
|
||||
if (!userId) return { error: "missing_user_context" };
|
||||
const active = await resolveMission(userId, missionInstanceId ?? ctx.missionInstanceId);
|
||||
if (!active) return { mission: null, actions: [], suggestions: [] };
|
||||
return {
|
||||
mission: active.mission,
|
||||
snapshot: active.snapshot,
|
||||
actions: await listMissionActions(userId, { missionInstanceId: active.mission.instanceId }),
|
||||
suggestions: await listMissionSuggestionsPg(userId, active.mission.instanceId),
|
||||
};
|
||||
},
|
||||
}),
|
||||
|
||||
listRegistryCapabilities: tool({
|
||||
description: "List deterministic registry missions, service capabilities, and specialist agents. This does not rank or generate missions.",
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({
|
||||
missions: listMissionDefinitions().map((mission) => ({
|
||||
id: mission.id,
|
||||
missionId: mission.missionId,
|
||||
title: mission.title,
|
||||
shortTitle: mission.shortTitle,
|
||||
actorBacked: mission.actorBacked,
|
||||
modules: mission.modules.map((module) => ({
|
||||
id: module.id,
|
||||
title: module.title,
|
||||
role: module.role,
|
||||
service: module.service,
|
||||
})),
|
||||
})),
|
||||
services: listServiceCapabilities(),
|
||||
agents: safeAgentRegistry(),
|
||||
}),
|
||||
}),
|
||||
|
||||
prepareServiceHandoff: tool({
|
||||
description: "Prepare an interview, roleplay, or resume handoff as a mission action and return the UI route. Do not directly complete the service.",
|
||||
inputSchema: z.object({
|
||||
serviceId: z.enum(["interview-service", "roleplay-service", "resume-service"]),
|
||||
missionInstanceId: z.string().optional(),
|
||||
stageId: z.string().optional(),
|
||||
title: z.string().optional(),
|
||||
body: z.string().optional(),
|
||||
goal: z.string().optional(),
|
||||
}),
|
||||
execute: async ({ serviceId, missionInstanceId, stageId, title, body, goal }) => {
|
||||
if (!userId) return { error: "missing_user_context" };
|
||||
const active = await resolveMission(userId, missionInstanceId ?? ctx.missionInstanceId);
|
||||
if (!active) return { error: "mission_not_found" };
|
||||
const selectedStageId = stageId ?? ctx.stageId ?? active.mission.currentStageId;
|
||||
const href = serviceHref({
|
||||
serviceId,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId: selectedStageId,
|
||||
goal: goal ?? active.mission.goal,
|
||||
});
|
||||
const agent = safeAgentRegistry().find((item) => item.service === serviceId);
|
||||
const action = await createMissionAction({
|
||||
userId,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId: selectedStageId,
|
||||
agentId: agent?.id ?? serviceId,
|
||||
agentName: agent?.name ?? serviceId,
|
||||
baseAgent: agent?.role,
|
||||
serviceId,
|
||||
toolName: `prepare_${serviceId.replace("-service", "")}_handoff`,
|
||||
mode: "suggestion",
|
||||
status: "queued",
|
||||
title: title ?? `Open ${agent?.name ?? serviceId}`,
|
||||
body: body ?? `Continue this mission in ${agent?.name ?? serviceId}.`,
|
||||
prompt: goal ?? active.mission.goal,
|
||||
payload: { href, goal: goal ?? active.mission.goal, source: "conversation-actor" },
|
||||
idempotencyKey: `conversation-handoff:${active.mission.instanceId}:${selectedStageId ?? "mission"}:${serviceId}`,
|
||||
priority: 25,
|
||||
urgency: "today",
|
||||
});
|
||||
return { action, href, serviceId, missionInstanceId: active.mission.instanceId };
|
||||
},
|
||||
}),
|
||||
|
||||
askSubAgent: tool({
|
||||
description: "Ask a specialist sub-agent for a focused answer without starting a service session.",
|
||||
inputSchema: z.object({
|
||||
agentId: z.enum(["interview", "roleplay", "resume", "qscore"]),
|
||||
question: z.string(),
|
||||
context: z.string().optional(),
|
||||
}),
|
||||
execute: async ({ agentId, question, context }) => {
|
||||
const agent = safeAgentRegistry().find((item) => item.id === agentId);
|
||||
const answer = await generateText({
|
||||
model: getConversationModel(),
|
||||
system: `You are ${agent?.name ?? agentId}, a GrowQR specialist. Be concise and practical. Do not start external tools.`,
|
||||
prompt: `Question:\n${question}\n\nContext:\n${context ?? "No extra context."}`,
|
||||
});
|
||||
return { agent, answerMd: answer.text };
|
||||
},
|
||||
}),
|
||||
|
||||
readMemory: tool({
|
||||
description: "Read a markdown memory file for this user.",
|
||||
inputSchema: z.object({ path: z.string() }),
|
||||
execute: async ({ path }) => {
|
||||
if (!userId) return { error: "missing_user_context" };
|
||||
return { memory: await getRivetClient().memoryActor.getOrCreate([userId]).read(path) };
|
||||
},
|
||||
}),
|
||||
|
||||
writeMemory: tool({
|
||||
description: "Write a durable markdown memory file for this user.",
|
||||
inputSchema: z.object({
|
||||
path: z.string(),
|
||||
contentMd: z.string(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
}),
|
||||
execute: async ({ path, contentMd, tags }) => {
|
||||
if (!userId) return { error: "missing_user_context" };
|
||||
const result = await getRivetClient().memoryActor.getOrCreate([userId]).write({ path, contentMd, tags });
|
||||
return { path, queued: result.queued };
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
export function streamConversationResponse(messages: ModelConversationMessage[], context: ConversationRuntimeContext = {}) {
|
||||
const system = [SYSTEM_PROMPT, context.systemAddendum].filter(Boolean).join("\n\n");
|
||||
if (context.source === "daily-mission-start") {
|
||||
return streamText({
|
||||
model: getConversationModel(),
|
||||
system,
|
||||
messages: buildModelMessages(messages),
|
||||
});
|
||||
}
|
||||
|
||||
export function streamConversationResponse(messages: ConversationMessage[]) {
|
||||
return streamText({
|
||||
model: getConversationModel(),
|
||||
system,
|
||||
system: SYSTEM_PROMPT,
|
||||
messages: buildModelMessages(messages),
|
||||
tools: buildConversationTools(context),
|
||||
stopWhen: stepCountIs(5),
|
||||
});
|
||||
}
|
||||
|
||||
export async function generateConversationResponse(messages: ModelConversationMessage[], context: ConversationRuntimeContext = {}) {
|
||||
const system = [SYSTEM_PROMPT, context.systemAddendum].filter(Boolean).join("\n\n");
|
||||
return generateText({
|
||||
model: getConversationModel(),
|
||||
system,
|
||||
messages: buildModelMessages(messages),
|
||||
tools: buildConversationTools(context),
|
||||
stopWhen: stepCountIs(5),
|
||||
tools: {
|
||||
readMemory: tool({
|
||||
description: "Read a markdown memory file. Stubbed until memoryActor is wired.",
|
||||
inputSchema: z.object({
|
||||
path: z.string().describe("Memory path, e.g. /profile.md"),
|
||||
}),
|
||||
execute: async ({ path }) => ({
|
||||
path,
|
||||
found: false,
|
||||
content: "",
|
||||
note: "memoryActor is not wired yet",
|
||||
}),
|
||||
}),
|
||||
writeMemory: tool({
|
||||
description: "Write a markdown memory file. Stubbed until memoryActor is wired.",
|
||||
inputSchema: z.object({
|
||||
path: z.string(),
|
||||
contentMd: z.string(),
|
||||
reason: z.string().optional(),
|
||||
}),
|
||||
execute: async ({ path, contentMd, reason }) => ({
|
||||
path,
|
||||
bytes: contentMd.length,
|
||||
reason,
|
||||
saved: false,
|
||||
note: "memoryActor is not wired yet",
|
||||
}),
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,10 +25,6 @@ function conversationIdFromKey(key: unknown[]) {
|
||||
return String(key[1] ?? key[0] ?? "default");
|
||||
}
|
||||
|
||||
function userIdFromKey(key: unknown[]) {
|
||||
return String(key[0] ?? "");
|
||||
}
|
||||
|
||||
function toPublicMessage(row: typeof conversationMessages.$inferSelect): ConversationMessage {
|
||||
return {
|
||||
id: row.id,
|
||||
@@ -122,14 +118,7 @@ export const conversationActor = actor({
|
||||
c.broadcast("status", c.state.status);
|
||||
|
||||
try {
|
||||
const result = streamConversationResponse(history, {
|
||||
userId: body.context?.userId ?? userIdFromKey(c.key),
|
||||
conversationId,
|
||||
missionInstanceId: body.context?.missionInstanceId,
|
||||
missionId: body.context?.missionId,
|
||||
stageId: body.context?.stageId,
|
||||
source: body.context?.source,
|
||||
});
|
||||
const result = streamConversationResponse(history);
|
||||
|
||||
let content = "";
|
||||
for await (const delta of result.textStream) {
|
||||
|
||||
@@ -18,14 +18,6 @@ export type ConversationMessage = {
|
||||
export type ConversationQueueMessage = {
|
||||
text: string;
|
||||
sender?: string;
|
||||
context?: {
|
||||
userId?: string;
|
||||
conversationId?: string;
|
||||
missionInstanceId?: string;
|
||||
missionId?: string;
|
||||
stageId?: string;
|
||||
source?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type ConversationResponseEvent = {
|
||||
|
||||
@@ -117,24 +117,6 @@ async function applyArtifactPatches(input: {
|
||||
return created;
|
||||
}
|
||||
|
||||
/**
|
||||
* DB status guard for the actor run loop. A queued event is processable unless
|
||||
* it is "failed" (terminal error) or "unresolved" (no userId). All other states
|
||||
* — "pending" (first attempt), "processing" (crash recovery), and "processed"
|
||||
* (synchronous projections already ran) — are allowed so the actor can run
|
||||
* mission reducers that only exist in the actor. Projections are idempotent,
|
||||
* and the in-memory processedEventIds guard prevents double-processing within
|
||||
* one actor lifetime. The routing gate (shouldRouteGrowEvent) already prevents
|
||||
* saturation from duplicate ingests; this guard is defense-in-depth.
|
||||
*/
|
||||
export function isProcessableStatus(
|
||||
status: string | null | undefined,
|
||||
): boolean {
|
||||
return status === "pending" || status === "processing" || status === "processed";
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const userEventActor = actor({
|
||||
options: { name: "User Event Parser", icon: "route", noSleep: true, actionTimeout: 300_000 },
|
||||
state: { userId: "", processedCount: 0, processedEventIds: [] } as UserEventActorState,
|
||||
@@ -161,52 +143,17 @@ export const userEventActor = actor({
|
||||
const message = await loopCtx.queue.next("wait-event", { names: ["events"] });
|
||||
const cmd = message.body as UserEventCommand;
|
||||
|
||||
// Pre-flight skip: check the DB status without claiming. Terminal
|
||||
// (processed/failed) or unresolved rows are advanced past immediately
|
||||
// so one settled event does not block the rest of the queue. The actual
|
||||
// claim (markGrowEventProcessing) happens INSIDE tryStep's run so the
|
||||
// retry unit is atomic: each attempt re-checks status before claiming.
|
||||
const [preflight] = await db
|
||||
.select({ processingStatus: growEvents.processingStatus })
|
||||
.from(growEvents)
|
||||
.where(and(eq(growEvents.id, cmd.eventId), eq(growEvents.userId, cmd.userId)))
|
||||
.limit(1);
|
||||
|
||||
if (!preflight || !isProcessableStatus(preflight.processingStatus)) {
|
||||
await loopCtx.step(`skip-event:${cmd.eventId}`, async () => {
|
||||
loopCtx.state.updatedAt = new Date().toISOString();
|
||||
loopCtx.broadcast("updated", loopCtx.state);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Process with tryStep so a failure is caught (not rethrown) and the
|
||||
// loop advances to the next message. Mirrors workflow-run-actor.ts.
|
||||
const result = await loopCtx.tryStep({
|
||||
name: `process-event:${cmd.eventId}`,
|
||||
maxRetries: 3,
|
||||
retryBackoffBase: 1_000,
|
||||
retryBackoffMax: 30_000,
|
||||
timeout: 300_000,
|
||||
run: async () => {
|
||||
if (loopCtx.state.processedEventIds.includes(cmd.eventId)) {
|
||||
loopCtx.state.updatedAt = new Date().toISOString();
|
||||
loopCtx.broadcast("updated", loopCtx.state);
|
||||
return;
|
||||
}
|
||||
// Atomic guard+claim: re-check status then mark processing inside the
|
||||
// retry unit. A prior attempt that crashed after claiming leaves the
|
||||
// row "processing" — isProcessableStatus allows retry (projections
|
||||
// are idempotent).
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(growEvents)
|
||||
.where(and(eq(growEvents.id, cmd.eventId), eq(growEvents.userId, cmd.userId)))
|
||||
.limit(1);
|
||||
if (!row) throw new Error(`grow event not found for user: ${cmd.eventId}`);
|
||||
if (!isProcessableStatus(row.processingStatus)) return;
|
||||
await markGrowEventProcessing(cmd.eventId);
|
||||
await loopCtx.step(`process-event:${cmd.eventId}`, async () => {
|
||||
if (loopCtx.state.processedEventIds.includes(cmd.eventId)) return;
|
||||
await markGrowEventProcessing(cmd.eventId);
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(growEvents)
|
||||
.where(and(eq(growEvents.id, cmd.eventId), eq(growEvents.userId, cmd.userId)))
|
||||
.limit(1);
|
||||
if (!row) throw new Error(`grow event not found for user: ${cmd.eventId}`);
|
||||
|
||||
try {
|
||||
await applyServiceSessionProjection(row);
|
||||
const qscoreResult = await applyQscoreProjection(row);
|
||||
const activeRows = await listActiveMissionsPg(cmd.userId);
|
||||
@@ -264,26 +211,14 @@ export const userEventActor = actor({
|
||||
loopCtx.state.processedEventIds = [cmd.eventId, ...loopCtx.state.processedEventIds.filter((id) => id !== cmd.eventId)].slice(0, 500);
|
||||
loopCtx.broadcast("eventProcessed", { eventId: cmd.eventId, userId: cmd.userId });
|
||||
loopCtx.broadcast("updated", loopCtx.state);
|
||||
},
|
||||
catch: ["timeout", "exhausted"],
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
// Per-event failure: mark terminal and advance. The loop continues to
|
||||
// the next queued message — one bad event does not block the rest.
|
||||
await loopCtx.step(`record-failure:${cmd.eventId}`, async () => {
|
||||
await markGrowEventFailed(cmd.eventId, result.failure.error);
|
||||
loopCtx.state.lastError = result.failure.error instanceof Error
|
||||
? result.failure.error.message
|
||||
: String(result.failure.error);
|
||||
} catch (err) {
|
||||
await markGrowEventFailed(cmd.eventId, err);
|
||||
loopCtx.state.lastError = err instanceof Error ? err.message : String(err);
|
||||
loopCtx.state.updatedAt = new Date().toISOString();
|
||||
loopCtx.broadcast("updated", loopCtx.state);
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
});
|
||||
}, {
|
||||
onError: async (_ctx, event) => {
|
||||
console.error("user-event-actor workflow error", event);
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -6,8 +6,6 @@ import { conversationActor } from "./conversation/index.js";
|
||||
import { memoryActor } from "./memory/index.js";
|
||||
import { growActor } from "./grow/index.js";
|
||||
import { userEventActor } from "./events/index.js";
|
||||
import { analyticsActor } from "./analytics/index.js";
|
||||
import { curatorActor } from "../v1/curator/curator-actor.js";
|
||||
import {
|
||||
careerTransitionMissionActor,
|
||||
interviewToOfferMissionActor,
|
||||
@@ -20,8 +18,6 @@ export const registry = setup({
|
||||
use: {
|
||||
growActor,
|
||||
userEventActor,
|
||||
analyticsActor,
|
||||
curatorActor,
|
||||
conversationActor,
|
||||
memoryActor,
|
||||
interviewToOfferMissionActor,
|
||||
|
||||
@@ -189,7 +189,7 @@ function buildUnifiedTools(): Array<{
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "start_interview_session",
|
||||
description: "Create a real mock interview session via the interview-service microservice.",
|
||||
description: "Create a real interview practice session via the Interview Agent / interview-service microservice.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: { goal: { type: "string" } },
|
||||
@@ -201,7 +201,7 @@ function buildUnifiedTools(): Array<{
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "start_roleplay_session",
|
||||
description: "Create a real mock roleplay session via the roleplay-service microservice.",
|
||||
description: "Create a real roleplay practice session via the Roleplay Agent / roleplay-service microservice.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: { goal: { type: "string" } },
|
||||
@@ -213,7 +213,7 @@ function buildUnifiedTools(): Array<{
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "compute_qscore",
|
||||
description: "Compute or refresh the user's Q Score via the qscore-service microservice.",
|
||||
description: "Compute or refresh the user's Q-Score via the Q Score Agent / qscore-service microservice.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {},
|
||||
@@ -225,7 +225,7 @@ function buildUnifiedTools(): Array<{
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "analyze_resume",
|
||||
description: "Analyze the user's resume using the Resume Building microservice. Returns completeness score, skill gaps, and optimization recommendations.",
|
||||
description: "Analyze the user's resume using the Resume Agent microservice. Returns completeness score, skill gaps, and optimization recommendations.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -253,7 +253,7 @@ function buildUnifiedTools(): Array<{
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "start_interview_to_offer",
|
||||
description: "Start the Interview-to-Offer Accelerator workflow. This is a guided end-to-end pipeline: (1) Analyze and tailor the resume for the role, (2) Create mock interview practice, (3) Create mock roleplay practice, (4) Compute Q Score readiness. Use this when the user has a specific interview scheduled and wants comprehensive preparation.",
|
||||
description: "Start the Interview-to-Offer Accelerator workflow. This is a guided end-to-end pipeline: (1) Analyze & tailor resume for the role, (2) Create interview practice session with the Interview Agent, (3) Create roleplay session with Roleplay Agent, (4) Compute Q-Score readiness. Use this when the user has a specific interview scheduled and wants comprehensive preparation.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -563,7 +563,7 @@ export const userActor = actor({
|
||||
|
||||
appendTimelineEvent(
|
||||
c.state,
|
||||
{ id: "grow", name: "Grow" },
|
||||
{ id: "grow", name: "Grow Agent" },
|
||||
"workflow",
|
||||
`${getWorkflowDefinition(workflowId)?.title ?? "Workflow"} started.`,
|
||||
);
|
||||
@@ -581,14 +581,14 @@ export const userActor = actor({
|
||||
|
||||
pauseWorkflow: async (c) => {
|
||||
c.state.workflowStatus = "paused";
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow" }, "workflow", "Workflow paused.");
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow Agent" }, "workflow", "Workflow paused.");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
return c.state;
|
||||
},
|
||||
|
||||
resumeWorkflow: async (c) => {
|
||||
c.state.workflowStatus = "running";
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow" }, "workflow", "Workflow resumed.");
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow Agent" }, "workflow", "Workflow resumed.");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
return c.state;
|
||||
},
|
||||
@@ -753,7 +753,7 @@ async function dispatchUnifiedTool(
|
||||
c.state.modules = makeModules();
|
||||
c.state.createdAt = now();
|
||||
c.state.updatedAt = now();
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow" }, "workflow", "Workflow started via LLM tool.");
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow Agent" }, "workflow", "Workflow started via LLM tool.");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
return { ok: true, workflowId: c.state.workflowId, goal };
|
||||
}
|
||||
@@ -799,7 +799,7 @@ async function dispatchUnifiedTool(
|
||||
case "start_roleplay_session": {
|
||||
const goal = String(input.goal ?? "");
|
||||
const roleplayModule = getSubAgentModule("roleplay");
|
||||
if (!roleplayModule?.service) return { ok: false, error: "Mock Roleplay module not available" };
|
||||
if (!roleplayModule?.service) return { ok: false, error: "Roleplay Agent module not available" };
|
||||
const result = await runServiceAgentProbe(
|
||||
{ id: roleplayModule.id, name: roleplayModule.name, role: roleplayModule.role, kind: "microservice", description: roleplayModule.description, service: roleplayModule.service },
|
||||
{ userId, goal },
|
||||
@@ -855,14 +855,14 @@ async function dispatchUnifiedTool(
|
||||
c.state.createdAt = now();
|
||||
c.state.updatedAt = now();
|
||||
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow" }, "workflow", `Interview-to-Offer workflow started for: ${goal}`);
|
||||
appendTimelineEvent(c.state, { id: "grow", name: "Grow Agent" }, "workflow", `Interview-to-Offer workflow started for: ${goal}`);
|
||||
|
||||
// Step 1: Resume Building — analyze and tailor
|
||||
// Step 1: Resume Agent — analyze and tailor
|
||||
const resumeModule = getSubAgentModule("resume");
|
||||
const resumeMod = c.state.modules.find(m => m.id === "resume");
|
||||
if (resumeMod && resumeModule) {
|
||||
resumeMod.status = "running";
|
||||
appendTimelineEvent(c.state, resumeMod, "module", "Resume Building is analyzing your profile...");
|
||||
appendTimelineEvent(c.state, resumeMod, "module", "Resume Agent analyzing your profile...");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
try {
|
||||
@@ -875,18 +875,18 @@ async function dispatchUnifiedTool(
|
||||
appendTimelineEvent(c.state, resumeMod, "module", resumeResult.summary);
|
||||
} catch (err) {
|
||||
resumeMod.status = "blocked";
|
||||
appendTimelineEvent(c.state, resumeMod, "module", `Resume Building failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
appendTimelineEvent(c.state, resumeMod, "module", `Resume Agent failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
// Step 2: Mock Interview — create interview session
|
||||
// Step 2: Interview Agent — create interview session
|
||||
const interviewModule = getSubAgentModule("interview");
|
||||
const interviewMod = c.state.modules.find(m => m.id === "interview");
|
||||
if (interviewMod && interviewModule?.service) {
|
||||
interviewMod.status = "running";
|
||||
appendTimelineEvent(c.state, interviewMod, "module", "Mock Interview is creating an interview practice session...");
|
||||
appendTimelineEvent(c.state, interviewMod, "module", "Interview Agent creating interview practice session...");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
try {
|
||||
@@ -905,12 +905,12 @@ async function dispatchUnifiedTool(
|
||||
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
// Step 3: Mock Roleplay — create roleplay session
|
||||
// Step 3: Roleplay Agent — create roleplay session
|
||||
const roleplayModule = getSubAgentModule("roleplay");
|
||||
const roleplayMod = c.state.modules.find(m => m.id === "roleplay");
|
||||
if (roleplayMod && roleplayModule?.service) {
|
||||
roleplayMod.status = "running";
|
||||
appendTimelineEvent(c.state, roleplayMod, "module", "Mock Roleplay is creating a practice scenario...");
|
||||
appendTimelineEvent(c.state, roleplayMod, "module", "Roleplay Agent creating roleplay scenario...");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
try {
|
||||
@@ -923,18 +923,18 @@ async function dispatchUnifiedTool(
|
||||
appendTimelineEvent(c.state, roleplayMod, "module", roleplayResult.summary);
|
||||
} catch (err) {
|
||||
roleplayMod.status = "blocked";
|
||||
appendTimelineEvent(c.state, roleplayMod, "module", `Mock Roleplay session failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
appendTimelineEvent(c.state, roleplayMod, "module", `Roleplay Agent session failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
// Step 4: Q Score — compute readiness
|
||||
// Step 4: Q Score Agent — compute Q-Score
|
||||
const qscoreModule = getSubAgentModule("qscore");
|
||||
const qscoreMod = c.state.modules.find(m => m.id === "qscore");
|
||||
if (qscoreMod && qscoreModule?.service) {
|
||||
qscoreMod.status = "running";
|
||||
appendTimelineEvent(c.state, qscoreMod, "module", "Q Score is computing your readiness score...");
|
||||
appendTimelineEvent(c.state, qscoreMod, "module", "Q Score Agent computing your readiness Q-Score...");
|
||||
c.broadcast("workflow.updated", workflowSnapshot(c.state));
|
||||
|
||||
try {
|
||||
@@ -947,7 +947,7 @@ async function dispatchUnifiedTool(
|
||||
appendTimelineEvent(c.state, qscoreMod, "module", qscoreResult.summary);
|
||||
} catch (err) {
|
||||
qscoreMod.status = "blocked";
|
||||
appendTimelineEvent(c.state, qscoreMod, "module", `Q Score computation failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
appendTimelineEvent(c.state, qscoreMod, "module", `Q-Score computation failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ export function jobApplicationModuleIds(): string[] {
|
||||
return loaderJobApplicationModuleIds();
|
||||
}
|
||||
|
||||
// Build the unified Grow system prompt from disk (changes.md §3).
|
||||
// Build the unified Grow Agent system prompt from disk (changes.md §3).
|
||||
export function buildUnifiedSystemPrompt(): string {
|
||||
return getUnifiedSystemPrompt();
|
||||
}
|
||||
|
||||
@@ -1,449 +0,0 @@
|
||||
import { generateText } from "ai";
|
||||
import { z } from "zod";
|
||||
import { getConversationModel, streamConversationResponse } from "../actors/conversation/agent.js";
|
||||
|
||||
export const dailyMissionTaskSchema = z.object({
|
||||
day: z.number().optional(),
|
||||
questId: z.string().optional(),
|
||||
questTitle: z.string(),
|
||||
subtaskIndex: z.number().optional(),
|
||||
subtask: z.string(),
|
||||
service: z.string().optional(),
|
||||
route: z.string().optional(),
|
||||
intro: z.string().optional(),
|
||||
context: z.array(z.object({ label: z.string(), value: z.string() })).optional(),
|
||||
signals: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
export const dailyMissionMessageSchema = z.object({
|
||||
role: z.enum(["user", "assistant"]),
|
||||
content: z.string().min(1).max(4000),
|
||||
});
|
||||
|
||||
export type DailyMissionTask = z.infer<typeof dailyMissionTaskSchema>;
|
||||
export type DailyMissionMessage = z.infer<typeof dailyMissionMessageSchema>;
|
||||
|
||||
const dailyMissionResponseSchema = z.object({
|
||||
reply: z.string(),
|
||||
completed: z.boolean().default(false),
|
||||
updateSummary: z.string().optional(),
|
||||
actionLabel: z.string().optional(),
|
||||
actionRoute: z.string().optional(),
|
||||
});
|
||||
|
||||
export type DailyMissionResult = z.infer<typeof dailyMissionResponseSchema>;
|
||||
|
||||
type DailyMissionAgentInput = {
|
||||
userId: string;
|
||||
task: DailyMissionTask;
|
||||
messages: DailyMissionMessage[];
|
||||
missionInstanceId?: string;
|
||||
missionId?: string;
|
||||
stageId?: string;
|
||||
conversationId?: string;
|
||||
};
|
||||
|
||||
function stripJsonFence(text: string) {
|
||||
return text
|
||||
.trim()
|
||||
.replace(/^```(?:json)?\s*/i, "")
|
||||
.replace(/\s*```$/i, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function parseDailyMissionResponse(text: string) {
|
||||
const normalize = (value: z.infer<typeof dailyMissionResponseSchema>) => {
|
||||
const nested = maybeParseJsonReply(value.reply);
|
||||
return nested ? { ...value, ...nested } : value;
|
||||
};
|
||||
|
||||
try {
|
||||
return normalize(dailyMissionResponseSchema.parse(JSON.parse(stripJsonFence(text))));
|
||||
} catch {
|
||||
return {
|
||||
reply: cleanAssistantReply(text) || "I could not prepare the next step. Try again.",
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function maybeParseJsonReply(text: string) {
|
||||
try {
|
||||
return dailyMissionResponseSchema.partial().parse(JSON.parse(stripJsonFence(text)));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function cleanAssistantReply(text: string) {
|
||||
const stripped = stripJsonFence(text);
|
||||
const nested = maybeParseJsonReply(stripped);
|
||||
if (nested?.reply) return nested.reply;
|
||||
const match = stripped.match(/^\s*\{[\s\S]*"reply"\s*:\s*"([\s\S]*?)"[\s\S]*\}\s*$/);
|
||||
const captured = match?.[1];
|
||||
if (!captured) return stripped.trim();
|
||||
try {
|
||||
return JSON.parse(`"${captured}"`);
|
||||
} catch {
|
||||
return captured.replace(/\\"/g, '"').replace(/\\u2011/g, "-").trim();
|
||||
}
|
||||
}
|
||||
|
||||
function isInterviewMission(task: DailyMissionTask) {
|
||||
const service = (task.service ?? "").toLowerCase();
|
||||
const routePath = task.route ? new URL(task.route, "https://growqr.local").pathname.toLowerCase() : "";
|
||||
const text = [task.questTitle, task.subtask].filter(Boolean).join(" ").toLowerCase();
|
||||
if (service.includes("resume") || routePath.includes("/agents/resume")) return false;
|
||||
if (service.includes("roleplay") || routePath.includes("/agents/roleplay")) return false;
|
||||
if (service.includes("q score") || service.includes("qscore") || routePath.includes("/agents/qscore")) return false;
|
||||
return service.includes("interview") || routePath.includes("/agents/interview") || text.includes("mock question");
|
||||
}
|
||||
|
||||
function getInterviewActionRoute(task: DailyMissionTask) {
|
||||
const source = new URL(task.route ?? "/agents/interview", "https://growqr.local");
|
||||
const roleFromContext = task.context?.find((item) => item.label.toLowerCase().includes("role"))?.value;
|
||||
const params = new URLSearchParams();
|
||||
params.set("role", source.searchParams.get("role") ?? roleFromContext ?? "Product Manager");
|
||||
params.set("type", source.searchParams.get("type") ?? "behavioral");
|
||||
params.set("persona", "payal");
|
||||
params.set("duration", "5");
|
||||
params.set("difficulty", source.searchParams.get("difficulty") ?? "medium");
|
||||
params.set("media", "video");
|
||||
params.set("source", "daily-mission");
|
||||
return `/agents/interview?${params.toString()}`;
|
||||
}
|
||||
|
||||
function compactAnswer(answer: string) {
|
||||
return answer.length > 180 ? `${answer.slice(0, 177).trimEnd()}...` : answer;
|
||||
}
|
||||
|
||||
function isConfidenceCheck(task: DailyMissionTask) {
|
||||
const haystack = [task.questTitle, task.subtask, task.service, task.intro].filter(Boolean).join(" ").toLowerCase();
|
||||
return haystack.includes("confidence check") || (haystack.includes("qx") && haystack.includes("confidence"));
|
||||
}
|
||||
|
||||
function buildDailyMissionSystemPrompt(task: DailyMissionTask) {
|
||||
return `You are Daily Mission, a focused GrowQR dashboard agent.
|
||||
|
||||
The user clicked on the main task called ${task.questTitle}${task.intro ? ` (${task.intro})` : ""} and the subtask within it called ${task.subtask}. You are going to act as an interface for the user to complete this subtask. Garner the right questions and get the input from the user.
|
||||
|
||||
The frontend will send "Start". From there onwards, start with your first message to the user.
|
||||
|
||||
Rules:
|
||||
- Ask one short question or give one short action at a time.
|
||||
- Do not start a larger mission, do not pitch other workflows, and do not send the user away.
|
||||
- Keep the tone warm, practical, and easy to answer.
|
||||
- When the user answer is enough to satisfy the subtask, mark the task complete by returning completed=true.
|
||||
- Return a single JSON object only. Do not wrap the object in a string. Shape: {"reply":"message to show","completed":false,"updateSummary":"optional short saved update"}.`;
|
||||
}
|
||||
|
||||
function buildDailyMissionStreamingSystemPrompt(task: DailyMissionTask) {
|
||||
return `You are the GrowQR conversation actor attached to a mission actor.
|
||||
|
||||
The user clicked a mission task card:
|
||||
${formatTask(task)}
|
||||
|
||||
Your job is to make the mission feel alive:
|
||||
- The mission actor owns progress and completion.
|
||||
- You own the chat turn and can prepare service handoffs.
|
||||
- The service capability is ${task.service ?? "unknown service"} at ${task.route ?? "unknown route"}.
|
||||
|
||||
Rules:
|
||||
- Plain text only. Do not return JSON.
|
||||
- On the first "start" message, do not use a template line like "This is a service handoff".
|
||||
- Ask the next natural question required to advance this exact mission stage.
|
||||
- If the service is Resume, ask for the resume text/file and target role or section in a natural way.
|
||||
- If the service is Interview, ask for role, round type, and the one thing they want to improve.
|
||||
- If the service is Roleplay, ask for scenario, counterpart, and desired outcome.
|
||||
- Keep it short, warm, and specific.`;
|
||||
}
|
||||
|
||||
function withDailyMissionActionDefaults(task: DailyMissionTask, result: z.infer<typeof dailyMissionResponseSchema>) {
|
||||
if (!result.completed || !isInterviewMission(task)) return result;
|
||||
return {
|
||||
...result,
|
||||
actionLabel: result.actionLabel ?? "Generate room",
|
||||
actionRoute: result.actionRoute ?? getInterviewActionRoute(task),
|
||||
};
|
||||
}
|
||||
|
||||
function serviceStartReply(task: DailyMissionTask) {
|
||||
const service = (task.service ?? "").toLowerCase();
|
||||
const routePath = task.route ? new URL(task.route, "https://growqr.local").pathname.toLowerCase() : "";
|
||||
|
||||
if (service.includes("resume") || routePath.includes("/agents/resume")) {
|
||||
return "This is a Resume service handoff. Tell me the target role or resume section you want to improve, and I will save it on this mission stage.";
|
||||
}
|
||||
if (service.includes("roleplay") || routePath.includes("/agents/roleplay")) {
|
||||
return "This is a Roleplay service handoff. Tell me the scenario you want to practice and the outcome you want from the conversation.";
|
||||
}
|
||||
if (service.includes("q score") || service.includes("qscore") || routePath.includes("/agents/qscore")) {
|
||||
return "This is a Q Score check. Tell me the signal you want to improve or the readiness question you want scored.";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function latestUserMessage(messages: DailyMissionMessage[]) {
|
||||
return [...messages].reverse().find((message) => message.role === "user")?.content.trim() ?? "";
|
||||
}
|
||||
|
||||
function firstQuestionForTask(task: DailyMissionTask) {
|
||||
const subtask = task.subtask.toLowerCase();
|
||||
const title = task.questTitle.toLowerCase();
|
||||
const intro = (task.intro ?? "").toLowerCase();
|
||||
const service = (task.service ?? "").toLowerCase();
|
||||
const routePath = task.route ? new URL(task.route, "https://growqr.local").pathname.toLowerCase() : "";
|
||||
const isResume = service.includes("resume") || routePath.includes("/agents/resume");
|
||||
const isInterview = service.includes("interview") || routePath.includes("/agents/interview");
|
||||
const isRoleplay = service.includes("roleplay") || routePath.includes("/agents/roleplay");
|
||||
const isPlanner = service.includes("mission planner") || title.includes("target role") || intro.includes("target role") || title.includes("career transition");
|
||||
|
||||
if (subtask.includes("save") || subtask.includes("next action")) {
|
||||
if (isPlanner) return "What next career move should I save: target role, skill gap, or outreach action?";
|
||||
if (isResume) return "What next action should I save: revise bullets, fill gaps, or generate talking points?";
|
||||
if (isInterview) return "What interview prep action should I save for the student to do next?";
|
||||
if (isRoleplay) return "What roleplay action should I save for the next practice round?";
|
||||
return `What next action should I save for "${task.subtask}"?`;
|
||||
}
|
||||
|
||||
if (subtask.includes("handoff") || subtask.includes("prepare")) {
|
||||
if (isPlanner) return "Should I prepare a role shortlist, transition plan, or skill-gap plan?";
|
||||
if (isResume) return "Which resume handoff should I prepare: role-fit proof, gap scan, or talking points?";
|
||||
if (isInterview) return "What interview setup should I prepare: role, round type, and difficulty?";
|
||||
if (isRoleplay) return "What roleplay setup should I prepare: scenario, counterpart, and outcome?";
|
||||
return `What handoff should I prepare for "${task.subtask}"?`;
|
||||
}
|
||||
|
||||
if (isResume) {
|
||||
return "Please share your resume text or file and the target role.";
|
||||
}
|
||||
|
||||
if (isInterview) {
|
||||
return "What role and interview round should this prep focus on?";
|
||||
}
|
||||
|
||||
if (isRoleplay) {
|
||||
return "What conversation scenario do you want to practice?";
|
||||
}
|
||||
|
||||
if (isPlanner) {
|
||||
if (subtask.includes("target role")) {
|
||||
return "What is your current role, target role, and biggest transition constraint?";
|
||||
}
|
||||
if (subtask.includes("requirements")) {
|
||||
return "What requirement should we check first: skills, experience, location, or timeline?";
|
||||
}
|
||||
if (subtask.includes("review") || subtask.includes("recommendation")) {
|
||||
return "Which role option should we review first, and what matters most to you?";
|
||||
}
|
||||
return "What target role are you considering, and what constraint should I account for?";
|
||||
}
|
||||
|
||||
if (subtask.includes("target role")) {
|
||||
return "What is your current role, target role, and biggest constraint?";
|
||||
}
|
||||
|
||||
if (subtask.includes("requirements")) {
|
||||
return "Which requirement should we check first: skills, experience, location, or timeline?";
|
||||
}
|
||||
|
||||
return `What is the key detail for "${task.subtask}"?`;
|
||||
}
|
||||
|
||||
function buildConversationActorMessages(input: DailyMissionAgentInput) {
|
||||
const latest = latestUserMessage(input.messages);
|
||||
const isStart = latest.toLowerCase() === "start";
|
||||
const transcript = input.messages
|
||||
.filter((message) => message.content.trim().toLowerCase() !== "start")
|
||||
.slice(-10)
|
||||
.map((message) => `${message.role === "user" ? "User" : "Assistant"}: ${message.content}`)
|
||||
.join("\n");
|
||||
|
||||
return [{
|
||||
id: `daily-mission-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
||||
conversationId: input.conversationId ?? "daily-mission",
|
||||
role: "user" as const,
|
||||
sender: "Daily Mission UI",
|
||||
createdAt: Date.now(),
|
||||
content: `The user opened a mission-linked Daily Mission chat.
|
||||
|
||||
Mission task:
|
||||
${formatTask(input.task)}
|
||||
|
||||
Mission ids:
|
||||
- missionInstanceId: ${input.missionInstanceId ?? "not linked yet"}
|
||||
- missionId: ${input.missionId ?? "unknown"}
|
||||
- stageId: ${input.stageId ?? "unknown"}
|
||||
|
||||
${transcript ? `Conversation so far:\n${transcript}\n\n` : ""}${isStart
|
||||
? "This is the first assistant turn. Ask one short direct question that advances this stage. No greeting. No filler. No em dash. No long paragraph. For resume, ask for resume text/file and target role."
|
||||
: `The user just replied: ${latest}\nRespond as the GrowQR conversation agent. If the answer is enough for this subtask, acknowledge what will be saved. Keep it concise. Use ASCII punctuation only.`}`,
|
||||
}];
|
||||
}
|
||||
|
||||
function runInterviewRoomSetup(task: DailyMissionTask, messages: DailyMissionMessage[]) {
|
||||
const latestUser = latestUserMessage(messages);
|
||||
const subtask = task.subtask.toLowerCase();
|
||||
const actionRoute = getInterviewActionRoute(task);
|
||||
|
||||
if (latestUser.toLowerCase() === "start") {
|
||||
if (subtask.includes("generate") || subtask.includes("jump") || subtask.includes("start")) {
|
||||
return {
|
||||
reply: "The interview room setup is ready. Review the details and tap Generate room to open the interview UI.",
|
||||
completed: true,
|
||||
updateSummary: "Interview room setup ready.",
|
||||
actionLabel: "Generate room",
|
||||
actionRoute,
|
||||
};
|
||||
}
|
||||
|
||||
if (subtask.includes("pressure") || subtask.includes("difficulty")) {
|
||||
return {
|
||||
reply: "Pick the pressure level for the student: easy, medium, or hard.",
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
reply: "What should this interview room be for? Share the role, round type, and one thing the student wants to improve.",
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
const updateSummary = compactAnswer(latestUser);
|
||||
return {
|
||||
reply: `Got it. I saved this for the student: ${updateSummary}. Tap Generate room when you are ready to open the interview UI.`,
|
||||
completed: true,
|
||||
updateSummary,
|
||||
actionLabel: "Generate room",
|
||||
actionRoute,
|
||||
};
|
||||
}
|
||||
|
||||
function formatTask(task: DailyMissionTask) {
|
||||
const lines = [
|
||||
task.day ? `Sprint day: ${task.day}` : undefined,
|
||||
`Quest: ${task.questTitle}`,
|
||||
`Subtask: ${task.subtask}`,
|
||||
task.service ? `Service: ${task.service}` : undefined,
|
||||
task.route ? `Service route: ${task.route}` : undefined,
|
||||
task.intro ? `Quest intent: ${task.intro}` : undefined,
|
||||
task.context?.length
|
||||
? `Visible context: ${task.context.map((item) => `${item.label}: ${item.value}`).join("; ")}`
|
||||
: undefined,
|
||||
task.signals?.length ? `Signals to improve: ${task.signals.join(", ")}` : undefined,
|
||||
].filter(Boolean);
|
||||
|
||||
return lines.map((line) => `- ${line}`).join("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function runDailyMissionAgent(input: DailyMissionAgentInput) {
|
||||
const started = input.messages.some(
|
||||
(message) => message.role === "user" && message.content.trim().toLowerCase() === "start",
|
||||
);
|
||||
if (!started) {
|
||||
return {
|
||||
reply: "I am ready to begin this daily mission.",
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (isInterviewMission(input.task)) {
|
||||
return runInterviewRoomSetup(input.task, input.messages);
|
||||
}
|
||||
|
||||
const transcript = input.messages
|
||||
.slice(-12)
|
||||
.map((message) => `${message.role === "user" ? "Student" : "Daily Mission"}: ${message.content}`)
|
||||
.join("\n");
|
||||
|
||||
try {
|
||||
const result = await generateText({
|
||||
model: getConversationModel(),
|
||||
system: buildDailyMissionSystemPrompt(input.task),
|
||||
prompt: `User id: ${input.userId}
|
||||
|
||||
Daily task context:
|
||||
${formatTask(input.task)}
|
||||
|
||||
Conversation so far:
|
||||
${transcript}`,
|
||||
});
|
||||
|
||||
return withDailyMissionActionDefaults(input.task, parseDailyMissionResponse(result.text));
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn("daily mission model failed; returning unavailable state", { message });
|
||||
return {
|
||||
reply: "Daily mission is temporarily unavailable right now. No progress was saved. Please retry in a moment.",
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function streamDailyMissionAgent(input: DailyMissionAgentInput) {
|
||||
const started = input.messages.some(
|
||||
(message) => message.role === "user" && message.content.trim().toLowerCase() === "start",
|
||||
);
|
||||
if (!started) {
|
||||
return { kind: "static" as const, result: await runDailyMissionAgent(input) };
|
||||
}
|
||||
|
||||
const latest = latestUserMessage(input.messages);
|
||||
const userMessagesAfterStart = input.messages.filter((message) => message.role === "user");
|
||||
const isStart = latest.toLowerCase() === "start";
|
||||
|
||||
if (isStart) {
|
||||
return {
|
||||
kind: "static" as const,
|
||||
result: {
|
||||
reply: firstQuestionForTask(input.task),
|
||||
completed: false,
|
||||
updateSummary: undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const result = streamConversationResponse(buildConversationActorMessages(input), {
|
||||
userId: input.userId,
|
||||
conversationId: input.conversationId,
|
||||
missionInstanceId: input.missionInstanceId,
|
||||
missionId: input.missionId,
|
||||
stageId: input.stageId,
|
||||
source: isStart ? "daily-mission-start" : "daily-mission",
|
||||
});
|
||||
|
||||
return {
|
||||
kind: "stream" as const,
|
||||
textStream: result.textStream,
|
||||
finalize: (reply: string): DailyMissionResult => {
|
||||
const completed = !isStart && userMessagesAfterStart.length > 1 && reply.trim().length > 0;
|
||||
return {
|
||||
reply: cleanAssistantReply(reply),
|
||||
completed,
|
||||
updateSummary: completed ? compactAnswer(latest) : undefined,
|
||||
actionLabel: undefined,
|
||||
actionRoute: undefined,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -26,7 +26,7 @@ export const requireUser = createMiddleware<AuthContext>(async (c, next) => {
|
||||
const auth = c.req.header("authorization") ?? "";
|
||||
const token = auth.replace(/^Bearer\s+/i, "").trim();
|
||||
|
||||
// Service-to-service path (Grow stack calling backend).
|
||||
// Service-to-service path (Grow Agent actor calling backend).
|
||||
// Header `x-growqr-user` is REQUIRED so we can scope the call.
|
||||
const trustedServiceTokens = new Set(
|
||||
[
|
||||
|
||||
@@ -7,41 +7,7 @@ function required(name: string, fallback?: string): string {
|
||||
}
|
||||
return v;
|
||||
}
|
||||
function parseBool(value: string | undefined, fallback: boolean): boolean {
|
||||
if (value === undefined || value === "") return fallback;
|
||||
return value.toLowerCase() === "true" || value === "1";
|
||||
}
|
||||
|
||||
|
||||
// ── Redis config resolver (pure, testable without module reload) ────────────
|
||||
// Exported so tests can assert gating/URL-inheritance behavior with synthetic
|
||||
// env objects, avoiding ESM module-cache hacks.
|
||||
export type RedisConfig = {
|
||||
growEventsRedisEnabled: boolean;
|
||||
legacyServiceRedisEnabled: boolean;
|
||||
growEventsRedisUrl: string;
|
||||
interviewRedisUrl: string;
|
||||
roleplayRedisUrl: string;
|
||||
resumeRedisUrl: string;
|
||||
coursesRedisUrl: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve all Redis-related config from an env record. Both ingestion paths
|
||||
* are default-off; legacy URLs resolve ONLY from their own explicit env var
|
||||
* (never inherited from GROW_EVENTS_REDIS_URL or the generic REDIS_URL).
|
||||
*/
|
||||
export function resolveRedisConfig(env: Record<string, string | undefined>): RedisConfig {
|
||||
return {
|
||||
growEventsRedisEnabled: parseBool(env.GROW_EVENTS_REDIS_ENABLED, false),
|
||||
legacyServiceRedisEnabled: parseBool(env.LEGACY_SERVICE_REDIS_ENABLED, false),
|
||||
growEventsRedisUrl: env.GROW_EVENTS_REDIS_URL ?? "",
|
||||
interviewRedisUrl: env.INTERVIEW_REDIS_URL ?? "",
|
||||
roleplayRedisUrl: env.ROLEPLAY_REDIS_URL ?? "",
|
||||
resumeRedisUrl: env.RESUME_REDIS_URL ?? "",
|
||||
coursesRedisUrl: env.COURSES_REDIS_URL ?? env.COURSE_REDIS_URL ?? "",
|
||||
};
|
||||
}
|
||||
export const config = {
|
||||
port: Number(process.env.PORT ?? 4000),
|
||||
logLevel: process.env.LOG_LEVEL ?? "info",
|
||||
@@ -60,24 +26,16 @@ export const config = {
|
||||
a2aAllowedKey: process.env.A2A_ALLOWED_KEY ?? "dev-a2a-key",
|
||||
|
||||
// Service → backend event stream. Redis is optional; HTTP /events/ingest/service is always available.
|
||||
// Explicit opt-in flags: both Redis ingestion paths are default-off. The REST
|
||||
// production path never touches Redis. Each flag is independent so operators
|
||||
// can enable canonical and/or legacy observers separately.
|
||||
growEventsRedisEnabled: parseBool(process.env.GROW_EVENTS_REDIS_ENABLED, false),
|
||||
legacyServiceRedisEnabled: parseBool(process.env.LEGACY_SERVICE_REDIS_ENABLED, false),
|
||||
growEventsRedisUrl: process.env.GROW_EVENTS_REDIS_URL ?? "",
|
||||
growEventsRedisUrl: process.env.GROW_EVENTS_REDIS_URL ?? process.env.REDIS_URL ?? "",
|
||||
growEventsStream: process.env.GROW_EVENTS_STREAM ?? "grow.events.raw",
|
||||
growEventsConsumerGroup: process.env.GROW_EVENTS_CONSUMER_GROUP ?? "growqr-backend",
|
||||
growEventsConsumerName: process.env.GROW_EVENTS_CONSUMER_NAME ?? `backend-${process.pid}`,
|
||||
|
||||
// Legacy service Redis surfaces. These let backend observe existing service A2A traffic
|
||||
// without changing the services. Legacy URLs resolve ONLY from their own explicit env
|
||||
// var — they must not inherit from GROW_EVENTS_REDIS_URL or the generic REDIS_URL, so a
|
||||
// stray REDIS_URL can never silently activate the legacy observer path.
|
||||
interviewRedisUrl: process.env.INTERVIEW_REDIS_URL ?? "",
|
||||
roleplayRedisUrl: process.env.ROLEPLAY_REDIS_URL ?? "",
|
||||
resumeRedisUrl: process.env.RESUME_REDIS_URL ?? "",
|
||||
coursesRedisUrl: process.env.COURSES_REDIS_URL ?? process.env.COURSE_REDIS_URL ?? "",
|
||||
// without changing the services. Defaults fall back to GROW_EVENTS_REDIS_URL/REDIS_URL.
|
||||
interviewRedisUrl: process.env.INTERVIEW_REDIS_URL ?? process.env.GROW_EVENTS_REDIS_URL ?? process.env.REDIS_URL ?? "",
|
||||
roleplayRedisUrl: process.env.ROLEPLAY_REDIS_URL ?? process.env.GROW_EVENTS_REDIS_URL ?? process.env.REDIS_URL ?? "",
|
||||
resumeRedisUrl: process.env.RESUME_REDIS_URL ?? process.env.GROW_EVENTS_REDIS_URL ?? process.env.REDIS_URL ?? "",
|
||||
legacyServiceTaskObserverGroup: process.env.LEGACY_SERVICE_TASK_OBSERVER_GROUP ?? "growqr-backend-observer",
|
||||
|
||||
// LLM gateway for the unified user agent.
|
||||
@@ -119,28 +77,10 @@ export const config = {
|
||||
process.env.USER_SERVICE_URL ?? "http://localhost:8003",
|
||||
resumePublicUrl:
|
||||
process.env.RESUME_PUBLIC_URL ?? process.env.RESUME_SERVICE_URL ?? "http://localhost:8002",
|
||||
coursesServiceUrl:
|
||||
process.env.COURSES_SERVICE_URL ?? "http://localhost:8060",
|
||||
coursesPublicUrl:
|
||||
process.env.COURSES_PUBLIC_URL ?? process.env.COURSES_SERVICE_URL ?? "http://localhost:8060",
|
||||
assessmentServiceUrl:
|
||||
process.env.ASSESSMENT_SERVICE_URL ?? "http://localhost:8070",
|
||||
assessmentPublicUrl:
|
||||
process.env.ASSESSMENT_PUBLIC_URL ?? process.env.ASSESSMENT_SERVICE_URL ?? "http://localhost:8070",
|
||||
matchmakingServiceUrl:
|
||||
process.env.MATCHMAKING_SERVICE_URL ?? "http://localhost:8006",
|
||||
matchmakingPublicUrl:
|
||||
process.env.MATCHMAKING_PUBLIC_URL ?? process.env.MATCHMAKING_SERVICE_URL ?? "http://localhost:8006",
|
||||
pathwaysServiceUrl:
|
||||
process.env.PATHWAYS_SERVICE_URL ?? "http://localhost:8009",
|
||||
pathwaysPublicUrl:
|
||||
process.env.PATHWAYS_PUBLIC_URL ?? process.env.PATHWAYS_SERVICE_URL ?? "http://localhost:8009",
|
||||
socialBrandingServiceUrl:
|
||||
process.env.SOCIAL_BRANDING_SERVICE_URL ?? "http://localhost:8005",
|
||||
socialBrandingPublicUrl:
|
||||
process.env.SOCIAL_BRANDING_PUBLIC_URL ?? process.env.SOCIAL_BRANDING_SERVICE_URL ?? "http://localhost:8005",
|
||||
qscorePublicUrl:
|
||||
process.env.QSCORE_PUBLIC_URL ?? process.env.QSCORE_SERVICE_URL ?? "http://localhost:8000",
|
||||
workflowsDashboardUrl:
|
||||
process.env.WORKFLOWS_DASHBOARD_URL ??
|
||||
process.env.FRONTEND_ORIGIN ??
|
||||
@@ -186,15 +126,8 @@ export const config = {
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean),
|
||||
|
||||
// Passive mission refresh loop. Dedupe keys make this safe across retries and
|
||||
// multiple staging replicas; set MISSION_PASSIVE_LOOP_ENABLED=false to disable.
|
||||
missionPassiveLoopEnabled: (process.env.MISSION_PASSIVE_LOOP_ENABLED ?? "true").toLowerCase() !== "false",
|
||||
missionPassiveLoopIntervalMs: Number(process.env.MISSION_PASSIVE_LOOP_INTERVAL_MS ?? 60 * 60 * 1000),
|
||||
missionPassiveLoopBatchSize: Number(process.env.MISSION_PASSIVE_LOOP_BATCH_SIZE ?? 100),
|
||||
|
||||
// Used by LLM requests.
|
||||
maxAgentTokens: Number(process.env.MAX_AGENT_TOKENS ?? 4096),
|
||||
|
||||
required, // exported so other modules can fail fast on boot
|
||||
} as const;
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import { db } from "./client.js";
|
||||
import { log } from "../log.js";
|
||||
|
||||
async function ensureGrowConversationsMetadataColumn() {
|
||||
await db.execute(`
|
||||
ALTER TABLE grow_conversations
|
||||
ADD COLUMN IF NOT EXISTS metadata jsonb NOT NULL DEFAULT '{}'::jsonb
|
||||
`);
|
||||
}
|
||||
|
||||
async function ensureUserPlanColumn() {
|
||||
await db.execute(`ALTER TABLE users ADD COLUMN IF NOT EXISTS plan text NOT NULL DEFAULT 'free'`);
|
||||
}
|
||||
|
||||
async function ensureSystemNotificationsTables() {
|
||||
await db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS system_notifications (
|
||||
id text PRIMARY KEY DEFAULT gen_random_uuid()::text NOT NULL,
|
||||
user_id text NOT NULL REFERENCES users(id) ON DELETE cascade,
|
||||
kind text NOT NULL CHECK (kind IN ('session', 'billing', 'security', 'feature', 'account')),
|
||||
title text NOT NULL,
|
||||
sub text NOT NULL,
|
||||
when_label text NOT NULL,
|
||||
href text,
|
||||
source text NOT NULL DEFAULT 'system',
|
||||
source_ref jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
read_at timestamp with time zone,
|
||||
occurred_at timestamp with time zone NOT NULL DEFAULT now(),
|
||||
created_at timestamp with time zone NOT NULL DEFAULT now(),
|
||||
updated_at timestamp with time zone NOT NULL DEFAULT now()
|
||||
)
|
||||
`);
|
||||
await db.execute(`CREATE INDEX IF NOT EXISTS system_notifications_user_idx ON system_notifications (user_id, read_at, occurred_at)`);
|
||||
await db.execute(`CREATE INDEX IF NOT EXISTS system_notifications_kind_idx ON system_notifications (user_id, kind, occurred_at)`);
|
||||
await db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS system_notification_preferences (
|
||||
user_id text NOT NULL REFERENCES users(id) ON DELETE cascade,
|
||||
kind text NOT NULL CHECK (kind IN ('session', 'billing', 'security', 'feature', 'account')),
|
||||
in_app boolean NOT NULL DEFAULT true,
|
||||
email boolean NOT NULL DEFAULT true,
|
||||
updated_at timestamp with time zone NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, kind)
|
||||
)
|
||||
`);
|
||||
await db.execute(`CREATE INDEX IF NOT EXISTS system_notification_preferences_user_idx ON system_notification_preferences (user_id)`);
|
||||
}
|
||||
|
||||
async function ensureOnboardingTable() {
|
||||
await db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS onboarding (
|
||||
id text PRIMARY KEY DEFAULT gen_random_uuid()::text NOT NULL,
|
||||
user_id text NOT NULL REFERENCES users(id) ON DELETE cascade,
|
||||
data jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
payload jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at timestamp with time zone NOT NULL DEFAULT now(),
|
||||
updated_at timestamp with time zone NOT NULL DEFAULT now()
|
||||
)
|
||||
`);
|
||||
await db.execute(`CREATE UNIQUE INDEX IF NOT EXISTS onboarding_user_idx ON onboarding (user_id)`);
|
||||
}
|
||||
|
||||
export async function ensureRuntimeSchema() {
|
||||
await ensureUserPlanColumn();
|
||||
await ensureGrowConversationsMetadataColumn();
|
||||
await ensureSystemNotificationsTables();
|
||||
await ensureOnboardingTable();
|
||||
log.info("runtime schema ensured");
|
||||
}
|
||||
@@ -20,7 +20,6 @@ export const users = pgTable(
|
||||
id: text("id").primaryKey(),
|
||||
email: text("email").notNull(),
|
||||
displayName: text("display_name"),
|
||||
plan: text("plan", { enum: ["free", "pro", "enterprise"] }).notNull().default("free"),
|
||||
createdAt: timestamp("created_at", { withTimezone: true })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
@@ -33,25 +32,6 @@ export const users = pgTable(
|
||||
}),
|
||||
);
|
||||
|
||||
// Canonical onboarding state. `data` is the user-confirmed source of truth;
|
||||
// `payload` is the server-owned projection consumed by Curator and Home.
|
||||
export const onboarding = pgTable(
|
||||
"onboarding",
|
||||
{
|
||||
id: text("id").primaryKey().default(sql`gen_random_uuid()::text`),
|
||||
userId: text("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: "cascade" }),
|
||||
data: jsonb("data").$type<Record<string, unknown>>().notNull().default(sql`'{}'::jsonb`),
|
||||
payload: jsonb("payload").$type<Record<string, unknown>>().notNull().default(sql`'{}'::jsonb`),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
},
|
||||
(t) => ({
|
||||
userIdx: uniqueIndex("onboarding_user_idx").on(t.userId),
|
||||
}),
|
||||
);
|
||||
|
||||
// One per user. Tracks the user's unified agent's container stack + Git repo.
|
||||
// Per changes.md §2A: per-user Gitea containers removed; central Gitea shared.
|
||||
// Per changes.md §5: ONE actor per user manages the full orchestration layer.
|
||||
@@ -300,7 +280,6 @@ export const growConversations = pgTable(
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
title: text("title").notNull().default("Talk to Me"),
|
||||
active: boolean("active").notNull().default(true),
|
||||
metadata: jsonb("metadata").$type<Record<string, unknown>>(),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
},
|
||||
@@ -609,44 +588,6 @@ export const growHomeNotifications = pgTable(
|
||||
}),
|
||||
);
|
||||
|
||||
export const systemNotifications = pgTable(
|
||||
"system_notifications",
|
||||
{
|
||||
id: text("id").primaryKey().default(sql`gen_random_uuid()::text`),
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
kind: text("kind", { enum: ["session", "billing", "security", "feature", "account"] }).notNull(),
|
||||
title: text("title").notNull(),
|
||||
sub: text("sub").notNull(),
|
||||
whenLabel: text("when_label").notNull(),
|
||||
href: text("href"),
|
||||
source: text("source").notNull().default("system"),
|
||||
sourceRef: jsonb("source_ref").$type<Record<string, unknown>>().notNull().default(sql`'{}'::jsonb`),
|
||||
readAt: timestamp("read_at", { withTimezone: true }),
|
||||
occurredAt: timestamp("occurred_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
},
|
||||
(t) => ({
|
||||
userIdx: index("system_notifications_user_idx").on(t.userId, t.readAt, t.occurredAt),
|
||||
kindIdx: index("system_notifications_kind_idx").on(t.userId, t.kind, t.occurredAt),
|
||||
}),
|
||||
);
|
||||
|
||||
export const systemNotificationPreferences = pgTable(
|
||||
"system_notification_preferences",
|
||||
{
|
||||
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
|
||||
kind: text("kind", { enum: ["session", "billing", "security", "feature", "account"] }).notNull(),
|
||||
inApp: boolean("in_app").notNull().default(true),
|
||||
email: boolean("email").notNull().default(true),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
|
||||
},
|
||||
(t) => ({
|
||||
pk: primaryKey({ columns: [t.userId, t.kind] }),
|
||||
userIdx: index("system_notification_preferences_user_idx").on(t.userId),
|
||||
}),
|
||||
);
|
||||
|
||||
export type GrowEventRow = typeof growEvents.$inferSelect;
|
||||
export type NewGrowEvent = typeof growEvents.$inferInsert;
|
||||
export type MissionActionRow = typeof missionActions.$inferSelect;
|
||||
@@ -656,6 +597,3 @@ export type NewMissionSuggestion = typeof missionSuggestions.$inferInsert;
|
||||
export type MissionCoachRunRow = typeof missionCoachRuns.$inferSelect;
|
||||
export type GrowHomeNotificationRow = typeof growHomeNotifications.$inferSelect;
|
||||
export type NewGrowHomeNotification = typeof growHomeNotifications.$inferInsert;
|
||||
export type SystemNotificationRow = typeof systemNotifications.$inferSelect;
|
||||
export type NewSystemNotification = typeof systemNotifications.$inferInsert;
|
||||
export type SystemNotificationPreferenceRow = typeof systemNotificationPreferences.$inferSelect;
|
||||
|
||||
@@ -333,12 +333,6 @@ export async function provisionUserStack(userId: string): Promise<UserStack> {
|
||||
const existing = await db.query.userStacks.findFirst({
|
||||
where: eq(userStacks.userId, userId),
|
||||
});
|
||||
if (existing && existing.status === "provisioning") {
|
||||
const ageMs = Date.now() - existing.updatedAt.getTime();
|
||||
if (ageMs < 5 * 60_000) return existing;
|
||||
log.warn({ userId, updatedAt: existing.updatedAt }, "stale OpenCode provisioning row; retrying");
|
||||
await stopUserStack(userId);
|
||||
}
|
||||
if (existing && existing.status === "running") {
|
||||
const current =
|
||||
existing.imageVersion === config.opencodeImageVersion &&
|
||||
@@ -446,8 +440,6 @@ export async function provisionUserStack(userId: string): Promise<UserStack> {
|
||||
branch: "main",
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
if (message.includes("repository file already exists")) continue;
|
||||
log.warn({ err, path: file.path }, "failed to init repo file (non-fatal)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,8 @@ export type GrowEventCategory =
|
||||
| "system";
|
||||
|
||||
export type GrowEventSubject = {
|
||||
kind?: string;
|
||||
id?: string;
|
||||
serviceId?: string;
|
||||
externalId?: string;
|
||||
kind: string;
|
||||
id: string;
|
||||
};
|
||||
|
||||
export type GrowEventMissionRef = {
|
||||
|
||||
@@ -12,15 +12,12 @@ function normalizeSubject(value: unknown) {
|
||||
const record = asRecord(value);
|
||||
const kind = getString(record.kind);
|
||||
const id = getString(record.id);
|
||||
const serviceId = getString(record.serviceId ?? record.service_id);
|
||||
const externalId = getString(record.externalId ?? record.external_id);
|
||||
if (serviceId || externalId) return { serviceId, externalId };
|
||||
return kind && id ? { kind, id } : undefined;
|
||||
}
|
||||
|
||||
function normalizeMission(value: unknown) {
|
||||
const record = asRecord(value);
|
||||
const instanceId = getString(record.instanceId ?? record.instance_id ?? record.missionInstanceId ?? record.mission_instance_id);
|
||||
const instanceId = getString(record.instanceId ?? record.instance_id ?? record.mission_instance_id);
|
||||
const missionId = getString(record.missionId ?? record.mission_id);
|
||||
const stageId = getString(record.stageId ?? record.stage_id);
|
||||
if (!instanceId && !missionId && !stageId) return undefined;
|
||||
@@ -51,9 +48,6 @@ export function normalizeGrowEvent(input: unknown, overrides: { userId?: string;
|
||||
request_id: raw.request_id ?? payload.request_id,
|
||||
});
|
||||
const subject = normalizeSubject(raw.subject) ?? (() => {
|
||||
const serviceId = getString(raw.subject_service_id ?? payload.subject_service_id);
|
||||
const externalId = getString(raw.subject_external_id ?? payload.subject_external_id);
|
||||
if (serviceId || externalId) return { serviceId, externalId };
|
||||
const kind = getString(raw.subject_kind ?? payload.subject_kind);
|
||||
const id = getString(raw.subject_id ?? payload.subject_id);
|
||||
return kind && id ? { kind, id } : undefined;
|
||||
|
||||
@@ -1,537 +0,0 @@
|
||||
import { and, desc, eq, inArray, or, sql } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { growEvents, type GrowEventRow } from "../db/schema.js";
|
||||
import { asRecord } from "./envelope.js";
|
||||
import {
|
||||
markGrowEventFailed,
|
||||
markGrowEventProcessed,
|
||||
markGrowEventProcessing,
|
||||
recordGrowEvent,
|
||||
} from "./record-grow-event.js";
|
||||
import { ensureOnboardingBaselineQscoreForCompletedAt } from "./onboarding-qscore.js";
|
||||
import {
|
||||
onboardingCompletedAtFromEvent,
|
||||
runCuratorOnboardingLoopSafely,
|
||||
} from "../v1/curator/curator-onboarding-loop.js";
|
||||
|
||||
export const ONBOARDING_LEDGER_EVENT_TYPES = [
|
||||
"onboarding.snapshot.saved",
|
||||
"onboarding.completed",
|
||||
"user.onboarding.completed",
|
||||
"profile.onboarding.completed",
|
||||
] as const;
|
||||
|
||||
export type OnboardingLedgerEventType = (typeof ONBOARDING_LEDGER_EVENT_TYPES)[number];
|
||||
|
||||
export const ONBOARDING_LEDGER_QUERY_TYPES = [
|
||||
...ONBOARDING_LEDGER_EVENT_TYPES,
|
||||
"onboarding_snapshot_saved",
|
||||
"onboarding_completed",
|
||||
"user_onboarding_completed",
|
||||
"profile_onboarding_completed",
|
||||
] as const;
|
||||
|
||||
export type OnboardingStatusEvent = {
|
||||
id: string;
|
||||
type: string;
|
||||
occurredAt: Date;
|
||||
processingStatus: string;
|
||||
};
|
||||
|
||||
export const COMPLETION_EVENT_TYPES: Record<string, true> = {
|
||||
"onboarding.completed": true,
|
||||
"user.onboarding.completed": true,
|
||||
"profile.onboarding.completed": true,
|
||||
};
|
||||
|
||||
// Every ledger row whose type (dotted OR underscore alias) marks a completion.
|
||||
// resetOnboardingLedger deletes all of these so the gate reopens regardless of
|
||||
// which alias form a legacy emitter wrote.
|
||||
export const COMPLETION_EVENT_TYPE_ALIASES: readonly string[] = [
|
||||
...Object.keys(COMPLETION_EVENT_TYPES),
|
||||
...Object.keys(COMPLETION_EVENT_TYPES).map((t) => t.replaceAll(".", "_")),
|
||||
];
|
||||
|
||||
// Snapshot ledger types in both alias forms. Only completion-bearing snapshots
|
||||
// are reset; intermediate step saves are preserved.
|
||||
export const SNAPSHOT_EVENT_TYPE_ALIASES = ["onboarding.snapshot.saved", "onboarding_snapshot_saved"] as const;
|
||||
|
||||
export function normalizeOnboardingEventType(type: string) {
|
||||
return type.toLowerCase().replaceAll("_", ".");
|
||||
}
|
||||
|
||||
export function completedAtFromOnboardingPayload(payload: Record<string, unknown> | null | undefined) {
|
||||
const data = payload ?? {};
|
||||
const preferences = asRecord(data.preferences);
|
||||
const onboarding = asRecord(data.onboarding ?? preferences.onboarding);
|
||||
const candidate =
|
||||
data.completedAt ??
|
||||
data.completed_at ??
|
||||
data.onboardingCompletedAt ??
|
||||
data.onboarding_completed_at ??
|
||||
onboarding.completed_at ??
|
||||
onboarding.completedAt ??
|
||||
asRecord(preferences.onboarding).completed_at ??
|
||||
asRecord(preferences.onboarding).completedAt;
|
||||
|
||||
if (candidate instanceof Date) {
|
||||
return Number.isNaN(candidate.getTime()) ? undefined : candidate.toISOString();
|
||||
}
|
||||
if (typeof candidate !== "string" || !candidate.trim()) return undefined;
|
||||
const parsed = new Date(candidate);
|
||||
return Number.isNaN(parsed.getTime()) ? new Date().toISOString() : parsed.toISOString();
|
||||
}
|
||||
|
||||
export function isValidOnboardingLedgerEvent(event: Pick<GrowEventRow, "type" | "payload">) {
|
||||
const normalizedType = normalizeOnboardingEventType(event.type);
|
||||
if (COMPLETION_EVENT_TYPES[normalizedType]) return true;
|
||||
|
||||
// Snapshots are status-valid only when they are completion snapshots. Plain
|
||||
// intermediate step saves must not let a new seeker bypass onboarding.
|
||||
if (normalizedType === "onboarding.snapshot.saved") {
|
||||
return Boolean(completedAtFromOnboardingPayload(event.payload));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function getLatestValidOnboardingLedgerEvent(userId: string): Promise<OnboardingStatusEvent | null> {
|
||||
const rows = await db
|
||||
.select({
|
||||
id: growEvents.id,
|
||||
type: growEvents.type,
|
||||
payload: growEvents.payload,
|
||||
occurredAt: growEvents.occurredAt,
|
||||
processingStatus: growEvents.processingStatus,
|
||||
})
|
||||
.from(growEvents)
|
||||
.where(
|
||||
and(
|
||||
eq(growEvents.userId, userId),
|
||||
inArray(growEvents.type, [...ONBOARDING_LEDGER_QUERY_TYPES]),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(growEvents.occurredAt))
|
||||
.limit(25);
|
||||
|
||||
const row = rows.find((event) => isValidOnboardingLedgerEvent(event));
|
||||
if (!row) return null;
|
||||
const { payload: _payload, ...statusEvent } = row;
|
||||
return statusEvent;
|
||||
}
|
||||
|
||||
export async function ensureOnboardingBaselineQscoreFromLedger(userId: string) {
|
||||
const event = await getLatestValidOnboardingLedgerEvent(userId);
|
||||
if (!event) return false;
|
||||
return ensureOnboardingBaselineQscoreForCompletedAt(userId, event.occurredAt);
|
||||
}
|
||||
|
||||
function onboardingContextFromInput(context?: Record<string, unknown>) {
|
||||
const input = context ?? {};
|
||||
const preferences = asRecord(input.preferences);
|
||||
const onboarding = asRecord(input.onboarding ?? preferences.onboarding);
|
||||
return {
|
||||
...input,
|
||||
onboarding,
|
||||
preferences: Object.keys(preferences).length ? preferences : { onboarding },
|
||||
};
|
||||
}
|
||||
|
||||
export async function ensureOnboardingSideEffectsForEvent(event: GrowEventRow, contextOverride?: Record<string, unknown>) {
|
||||
if (!event.userId || !isValidOnboardingLedgerEvent(event)) {
|
||||
return {
|
||||
qscoreBaselineSeeded: false,
|
||||
curatorOnboarding: { status: "skipped" as const, reason: event.userId ? "not_onboarding_completion" : "missing_user_id" },
|
||||
missions: { status: "skipped" as const, reason: event.userId ? "not_onboarding_completion" : "missing_user_id", started: [], existing: [] },
|
||||
};
|
||||
}
|
||||
|
||||
const completedAt =
|
||||
onboardingCompletedAtFromEvent(event) ??
|
||||
completedAtFromOnboardingPayload(event.payload) ??
|
||||
event.occurredAt.toISOString();
|
||||
|
||||
const qscoreBaselineSeeded = await ensureOnboardingBaselineQscoreForCompletedAt(event.userId, completedAt);
|
||||
const curatorOnboarding = await runCuratorOnboardingLoopSafely({
|
||||
userId: event.userId,
|
||||
completedAt,
|
||||
sourceEventId: event.id,
|
||||
source: event.source,
|
||||
context: contextOverride ?? onboardingContextFromInput(event.payload),
|
||||
});
|
||||
const missions = {
|
||||
status: "skipped" as const,
|
||||
reason: "static_curator_sprint_enabled" as const,
|
||||
started: [],
|
||||
existing: [],
|
||||
};
|
||||
|
||||
return { qscoreBaselineSeeded, curatorOnboarding, missions };
|
||||
}
|
||||
|
||||
export async function recordAndProcessOnboardingCompletion(input: {
|
||||
userId: string;
|
||||
completedAt: string | Date;
|
||||
source?: string;
|
||||
context?: Record<string, unknown>;
|
||||
}) {
|
||||
const completedAt =
|
||||
input.completedAt instanceof Date
|
||||
? input.completedAt.toISOString()
|
||||
: input.completedAt;
|
||||
const context = onboardingContextFromInput(input.context);
|
||||
const event = await recordGrowEvent(
|
||||
{
|
||||
source: input.source ?? "onboarding",
|
||||
type: "onboarding.completed",
|
||||
category: "usage",
|
||||
userId: input.userId,
|
||||
occurredAt: completedAt,
|
||||
payload: {
|
||||
completedAt,
|
||||
...context,
|
||||
},
|
||||
dedupeKey: `onboarding:completed:${input.userId}`,
|
||||
},
|
||||
{ userId: input.userId, source: input.source ?? "onboarding" },
|
||||
);
|
||||
|
||||
if (event.processingStatus !== "processed") {
|
||||
await markGrowEventProcessing(event.id);
|
||||
}
|
||||
|
||||
const sideEffects = await ensureOnboardingSideEffectsForEvent(event, context);
|
||||
if (sideEffects.curatorOnboarding.status === "skipped" && sideEffects.curatorOnboarding.reason === "loop_failed") {
|
||||
await markGrowEventFailed(event.id, new Error("curator_onboarding_loop_failed"));
|
||||
} else if (event.processingStatus !== "processed") {
|
||||
await markGrowEventProcessed(event.id);
|
||||
}
|
||||
|
||||
return { event, ...sideEffects };
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// Onboarding preferences helpers (pure — no DB, no network).
|
||||
//
|
||||
// The canonical onboarding store is `preferences.onboarding` on the user-service
|
||||
// profile, shaped as the v3 OnboardingData object (schema_version: 3). These
|
||||
// helpers read/validate/merge that blob and derive the faithful projection used
|
||||
// by the curator. Completion percentages and QX estimates are OWNED by the
|
||||
// dashboard (branch-aware); the backend persists the client-supplied values and
|
||||
// never recomputes them.
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
export type OnboardingAccessChoice = "trial" | "full" | null;
|
||||
|
||||
export type OnboardingData = {
|
||||
schema_version: 3;
|
||||
revision: number;
|
||||
status: "in_progress" | "completed";
|
||||
progress: { stage: string; branch_index: number; updated_at: string | null };
|
||||
consent: { privacy_accepted: boolean; accepted_at: string | null; terms_version: string };
|
||||
profile: {
|
||||
intent: string | null;
|
||||
mode: string | null;
|
||||
icp: string | null;
|
||||
question_branch: string | null;
|
||||
};
|
||||
responses: {
|
||||
current_situation: string | null;
|
||||
career_barriers: string[];
|
||||
desired_outcomes: string[];
|
||||
target_milestone: string | null;
|
||||
weekly_time_commitment: string | null;
|
||||
target_role: string | null;
|
||||
target_field: string | null;
|
||||
venture_industry: string | null;
|
||||
experience_level: string | null;
|
||||
work_context: string | null;
|
||||
};
|
||||
import: {
|
||||
method: string;
|
||||
status: string;
|
||||
resume_id: string | null;
|
||||
resume_filename: string | null;
|
||||
resume_summary: string | null;
|
||||
linkedin_profile_id: string | null;
|
||||
linkedin_url: string | null;
|
||||
};
|
||||
access_choice: OnboardingAccessChoice;
|
||||
qx_estimate: number | null;
|
||||
completed_at: string | null;
|
||||
};
|
||||
|
||||
export type OnboardingPayload = {
|
||||
schema_version: 1;
|
||||
source_revision: number;
|
||||
primary_intent: string | null;
|
||||
mode: string | null;
|
||||
question_branch: string | null;
|
||||
onboarding_icp: string | null;
|
||||
curator_registry_icp: string | null;
|
||||
target_role: string | null;
|
||||
target_field: string | null;
|
||||
experience_context: string | null;
|
||||
goals: string[];
|
||||
barriers: string[];
|
||||
priority: string | null;
|
||||
weekly_time_commitment: string | null;
|
||||
};
|
||||
|
||||
export const DEFAULT_ONBOARDING_DATA: OnboardingData = {
|
||||
schema_version: 3,
|
||||
revision: 0,
|
||||
status: "in_progress",
|
||||
progress: { stage: "consent", branch_index: 0, updated_at: null },
|
||||
consent: { privacy_accepted: false, accepted_at: null, terms_version: "2026-07" },
|
||||
profile: { intent: null, mode: null, icp: null, question_branch: null },
|
||||
responses: {
|
||||
current_situation: null,
|
||||
career_barriers: [],
|
||||
desired_outcomes: [],
|
||||
target_milestone: null,
|
||||
weekly_time_commitment: null,
|
||||
target_role: null,
|
||||
target_field: null,
|
||||
venture_industry: null,
|
||||
experience_level: null,
|
||||
work_context: null,
|
||||
},
|
||||
import: {
|
||||
method: "manual",
|
||||
status: "not_started",
|
||||
resume_id: null,
|
||||
resume_filename: null,
|
||||
resume_summary: null,
|
||||
linkedin_profile_id: null,
|
||||
linkedin_url: null,
|
||||
},
|
||||
access_choice: null,
|
||||
qx_estimate: null,
|
||||
completed_at: null,
|
||||
};
|
||||
|
||||
export function defaultOnboardingData(): OnboardingData {
|
||||
return structuredClone(DEFAULT_ONBOARDING_DATA);
|
||||
}
|
||||
|
||||
function isObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coerce an arbitrary stored value into a valid v3 OnboardingData, filling gaps
|
||||
* from the defaults. Rejects non-object shapes and falls back to defaults so the
|
||||
* route always returns a well-formed v3 response (never throws).
|
||||
*/
|
||||
export function extractOnboardingData(stored: unknown): OnboardingData {
|
||||
const base = defaultOnboardingData();
|
||||
if (!isObject(stored)) return base;
|
||||
|
||||
const progress = isObject(stored.progress) ? stored.progress : {};
|
||||
const consent = isObject(stored.consent) ? stored.consent : {};
|
||||
const profile = isObject(stored.profile) ? stored.profile : {};
|
||||
const responses = isObject(stored.responses) ? stored.responses : {};
|
||||
const imp = isObject(stored.import) ? stored.import : {};
|
||||
|
||||
const rev = typeof stored.revision === "number" && Number.isFinite(stored.revision) ? stored.revision : base.revision;
|
||||
const status = stored.status === "completed" ? "completed" : "in_progress";
|
||||
const access = stored.access_choice === "trial" || stored.access_choice === "full" ? stored.access_choice : null;
|
||||
const qx = typeof stored.qx_estimate === "number" && Number.isFinite(stored.qx_estimate) ? stored.qx_estimate : null;
|
||||
const completed = typeof stored.completed_at === "string" && stored.completed_at.trim() ? stored.completed_at : null;
|
||||
|
||||
return {
|
||||
schema_version: 3,
|
||||
revision: rev,
|
||||
status,
|
||||
progress: {
|
||||
stage: typeof progress.stage === "string" ? progress.stage : base.progress.stage,
|
||||
branch_index: typeof progress.branch_index === "number" && Number.isFinite(progress.branch_index) ? progress.branch_index : base.progress.branch_index,
|
||||
updated_at: typeof progress.updated_at === "string" && progress.updated_at.trim() ? progress.updated_at : null,
|
||||
},
|
||||
consent: {
|
||||
privacy_accepted: typeof consent.privacy_accepted === "boolean" ? consent.privacy_accepted : base.consent.privacy_accepted,
|
||||
accepted_at: typeof consent.accepted_at === "string" && consent.accepted_at.trim() ? consent.accepted_at : null,
|
||||
terms_version: typeof consent.terms_version === "string" && consent.terms_version.trim() ? consent.terms_version : base.consent.terms_version,
|
||||
},
|
||||
profile: {
|
||||
intent: typeof profile.intent === "string" ? profile.intent : null,
|
||||
mode: typeof profile.mode === "string" ? profile.mode : null,
|
||||
icp: typeof profile.icp === "string" ? profile.icp : null,
|
||||
question_branch: typeof profile.question_branch === "string" ? profile.question_branch : null,
|
||||
},
|
||||
responses: {
|
||||
current_situation: typeof responses.current_situation === "string" ? responses.current_situation : null,
|
||||
career_barriers: Array.isArray(responses.career_barriers) ? responses.career_barriers.filter((x) => typeof x === "string") : [],
|
||||
desired_outcomes: Array.isArray(responses.desired_outcomes) ? responses.desired_outcomes.filter((x) => typeof x === "string") : [],
|
||||
target_milestone: typeof responses.target_milestone === "string" ? responses.target_milestone : null,
|
||||
weekly_time_commitment: typeof responses.weekly_time_commitment === "string" ? responses.weekly_time_commitment : null,
|
||||
target_role: typeof responses.target_role === "string" ? responses.target_role : null,
|
||||
target_field: typeof responses.target_field === "string" ? responses.target_field : null,
|
||||
venture_industry: typeof responses.venture_industry === "string" ? responses.venture_industry : null,
|
||||
experience_level: typeof responses.experience_level === "string" ? responses.experience_level : null,
|
||||
work_context: typeof responses.work_context === "string" ? responses.work_context : null,
|
||||
},
|
||||
import: {
|
||||
method: typeof imp.method === "string" ? imp.method : base.import.method,
|
||||
status: typeof imp.status === "string" ? imp.status : base.import.status,
|
||||
resume_id: typeof imp.resume_id === "string" ? imp.resume_id : null,
|
||||
resume_filename: typeof imp.resume_filename === "string" ? imp.resume_filename : null,
|
||||
resume_summary: typeof imp.resume_summary === "string" ? imp.resume_summary : null,
|
||||
linkedin_profile_id: typeof imp.linkedin_profile_id === "string" ? imp.linkedin_profile_id : null,
|
||||
linkedin_url: typeof imp.linkedin_url === "string" ? imp.linkedin_url : null,
|
||||
},
|
||||
access_choice: access,
|
||||
qx_estimate: qx,
|
||||
completed_at: completed,
|
||||
};
|
||||
}
|
||||
|
||||
export class OnboardingRevisionConflict extends Error {
|
||||
readonly expected: number;
|
||||
readonly actual: number;
|
||||
constructor(expected: number, actual: number) {
|
||||
super(`onboarding revision conflict: expected ${expected}, actual ${actual}`);
|
||||
this.name = "OnboardingRevisionConflict";
|
||||
this.expected = expected;
|
||||
this.actual = actual;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimistic-concurrency guard. Throws OnboardingRevisionConflict when the
|
||||
* client's expectedRevision does not match the currently stored revision.
|
||||
* `expectedRevision` may be undefined only when there is no existing revision
|
||||
* (initial create); any mismatch with an existing doc is a conflict.
|
||||
*/
|
||||
export function assertOnboardingRevision(expected: number | undefined, current: OnboardingData): void {
|
||||
if (expected !== current.revision) {
|
||||
throw new OnboardingRevisionConflict(expected ?? -1, current.revision);
|
||||
}
|
||||
}
|
||||
|
||||
function asStringArray(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.filter((x): x is string => typeof x === "string") : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive the outbound OnboardingPayload (schema v1) from the stored v3
|
||||
* OnboardingData. This is a faithful projection — no completion math, no
|
||||
* invented fields. `curator_registry_icp` is left null (the curator resolves
|
||||
* the canonical ICP from `onboarding_icp` at projection time).
|
||||
*/
|
||||
export function deriveOnboardingPayload(data: OnboardingData): OnboardingPayload {
|
||||
const experienceContext = [data.responses.experience_level, data.responses.work_context]
|
||||
.filter((x) => typeof x === "string" && x.trim())
|
||||
.join(" · ") || null;
|
||||
const goals = asStringArray(data.responses.desired_outcomes);
|
||||
const barriers = asStringArray(data.responses.career_barriers);
|
||||
const priority = goals[0] ?? barriers[0] ?? data.responses.target_milestone ?? null;
|
||||
|
||||
return {
|
||||
schema_version: 1,
|
||||
source_revision: data.revision,
|
||||
primary_intent: data.profile.intent,
|
||||
mode: data.profile.mode,
|
||||
question_branch: data.profile.question_branch,
|
||||
onboarding_icp: data.profile.icp,
|
||||
curator_registry_icp: null,
|
||||
target_role: data.responses.target_role,
|
||||
target_field: data.responses.target_field,
|
||||
experience_context: experienceContext,
|
||||
goals,
|
||||
barriers,
|
||||
priority,
|
||||
weekly_time_commitment: data.responses.weekly_time_commitment,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep-merge an incoming PATCH `data` (full OnboardingData) into the stored
|
||||
* preferences object, returning a NEW preferences object. Preserves every
|
||||
* unrelated preference key (interview_preferences, resume_preferences,
|
||||
* mission_preferences, target_roles, etc.) untouched. Bumps revision by one
|
||||
* and stamps updated_at.
|
||||
*/
|
||||
export function mergeOnboardingPatch(
|
||||
currentPreferences: Record<string, unknown>,
|
||||
incoming: OnboardingData,
|
||||
): Record<string, unknown> {
|
||||
const updatedAt = new Date().toISOString();
|
||||
const stored = extractOnboardingData(currentPreferences.onboarding);
|
||||
const prevRevision = stored.revision;
|
||||
|
||||
const nextData: OnboardingData = {
|
||||
...incoming,
|
||||
// The client sends the revision it based its edits on; the stored revision
|
||||
// is authoritative. We bump from the previously stored revision.
|
||||
revision: prevRevision + 1,
|
||||
progress: { ...incoming.progress, updated_at: updatedAt },
|
||||
};
|
||||
|
||||
// Status/completed_at consistency: completed ⇒ completed_at must be set.
|
||||
if (nextData.status === "completed" && !nextData.completed_at) {
|
||||
nextData.completed_at = updatedAt;
|
||||
}
|
||||
// If regressed to in_progress, clear a stale completed_at.
|
||||
if (nextData.status === "in_progress" && nextData.completed_at && incoming.completed_at === null) {
|
||||
nextData.completed_at = null;
|
||||
}
|
||||
|
||||
return { ...currentPreferences, onboarding: nextData };
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the next preferences object for a reset: replace `preferences.onboarding`
|
||||
* with a fresh default v3 doc (revision 0) and preserve every unrelated
|
||||
* preference key. Pure — no DB, no network. The revision resets to 0 so the
|
||||
* next save starts a fresh optimistic-concurrency lineage.
|
||||
*/
|
||||
export function resetOnboardingPreferences(
|
||||
currentPreferences: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
return { ...currentPreferences, onboarding: defaultOnboardingData() };
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// Onboarding reset
|
||||
//
|
||||
// The onboarding gate reopens when no valid completion ledger row exists for a
|
||||
// user (getLatestValidOnboardingLedgerEvent returns null). The reset deletes:
|
||||
// 1. Every completion-type row in EITHER alias form (dotted like
|
||||
// "onboarding.completed" or underscore like "onboarding_completed") — these
|
||||
// carry the `onboarding:completed:${userId}` dedupe key.
|
||||
// 2. Snapshot rows ("onboarding.snapshot.saved" / "onboarding_snapshot_saved")
|
||||
// ONLY when their payload carries a completion marker (completed_at /
|
||||
// completedAt at the top level or under onboarding). Intermediate step
|
||||
// saves — the vast majority of snapshots — are preserved.
|
||||
// Missions, curator context, and qscore baselines are never touched. Returns
|
||||
// the count of rows removed for audit output.
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
// jsonb predicate matching the same paths completedAtFromOnboardingPayload
|
||||
// inspects for snapshot payloads. Mirrors the JS validity rule in SQL so the
|
||||
// reset deletes exactly the snapshots that would otherwise keep the gate shut.
|
||||
const SNAPSHOT_HAS_COMPLETION = sql<boolean>`${growEvents.payload}->>'completed_at' IS NOT NULL
|
||||
OR ${growEvents.payload}->>'completedAt' IS NOT NULL
|
||||
OR ${growEvents.payload}->'onboarding'->>'completed_at' IS NOT NULL
|
||||
OR ${growEvents.payload}->'onboarding'->>'completedAt' IS NOT NULL`;
|
||||
|
||||
export async function resetOnboardingLedger(userId: string): Promise<number> {
|
||||
const deleted = await db
|
||||
.delete(growEvents)
|
||||
.where(
|
||||
and(
|
||||
eq(growEvents.userId, userId),
|
||||
or(
|
||||
inArray(growEvents.type, [...COMPLETION_EVENT_TYPE_ALIASES]),
|
||||
and(
|
||||
inArray(growEvents.type, [...SNAPSHOT_EVENT_TYPE_ALIASES]),
|
||||
SNAPSHOT_HAS_COMPLETION,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.returning({ id: growEvents.id });
|
||||
return deleted.length;
|
||||
}
|
||||
@@ -1,41 +1,30 @@
|
||||
import { forwardSignalsToQscoreService } from "../services/service-agents.js";
|
||||
import { DEFAULT_QSCORE_ORG_ID } from "../services/qscore-proxy.js";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { growQscoreLatest, growQscoreProjectionState, growQscoreSignals } from "../db/schema.js";
|
||||
|
||||
export const ONBOARDING_BASELINE_SIGNAL_ID = "onboarding.completed";
|
||||
|
||||
/**
|
||||
* Workbook threshold for a completed onboarding baseline signal
|
||||
* (Complete=Yes → 0.5). qscore_service owns the aggregate score; this is the
|
||||
* per-signal evidence weight forwarded to it.
|
||||
*/
|
||||
export const ONBOARDING_BASELINE_SIGNAL_SCORE = 0.5;
|
||||
export const ONBOARDING_BASELINE_SIGNAL_ID = "onboarding.completed_baseline";
|
||||
export const ONBOARDING_BASELINE_QSCORE = 35;
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
|
||||
function parseCompletedAt(value: unknown): Date | null {
|
||||
if (value instanceof Date) {
|
||||
return Number.isNaN(value.getTime()) ? null : value;
|
||||
}
|
||||
if (typeof value !== "string" || !value.trim()) return null;
|
||||
const parsed = new Date(value);
|
||||
function onboardingCompletedAt(preferences: Record<string, unknown> | undefined): Date | null {
|
||||
const onboarding = asRecord(preferences?.onboarding);
|
||||
const completedAt = onboarding.completed_at;
|
||||
if (typeof completedAt !== "string" || !completedAt.trim()) return null;
|
||||
const parsed = new Date(completedAt);
|
||||
return Number.isNaN(parsed.getTime()) ? new Date() : parsed;
|
||||
}
|
||||
|
||||
function onboardingCompletedAt(preferences: Record<string, unknown> | undefined): Date | null {
|
||||
const onboarding = asRecord(preferences?.onboarding);
|
||||
return parseCompletedAt(onboarding.completed_at ?? onboarding.completedAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward an onboarding.completed baseline signal to qscore_service when
|
||||
* onboarding is completed.
|
||||
* Seed the first real Q Score projection when onboarding is completed.
|
||||
*
|
||||
* qscore_service OWNS all scoring. The backend no longer seeds local qscore
|
||||
* tables; instead it forwards a single baseline readiness signal and lets
|
||||
* qscore_service compute the score. Idempotency is delegated to qscore_service's
|
||||
* latest-signal upsert semantics.
|
||||
* The onboarding UI tells users their QX baseline starts at 35. Previously that
|
||||
* number was only cosmetic, while the header showed a separate home-feed
|
||||
* fallback and the Q Score page stayed empty. This makes the onboarding
|
||||
* baseline a persisted readiness signal, but only when the user has no Q Score
|
||||
* signals/projection yet so we do not overwrite mature accounts.
|
||||
*/
|
||||
export async function ensureOnboardingBaselineQscore(
|
||||
userId: string,
|
||||
@@ -43,33 +32,121 @@ export async function ensureOnboardingBaselineQscore(
|
||||
): Promise<boolean> {
|
||||
const completedAt = onboardingCompletedAt(preferences);
|
||||
if (!completedAt) return false;
|
||||
return ensureOnboardingBaselineQscoreForCompletedAt(userId, completedAt);
|
||||
}
|
||||
|
||||
export async function ensureOnboardingBaselineQscoreForCompletedAt(
|
||||
userId: string,
|
||||
completedAtInput: string | Date,
|
||||
): Promise<boolean> {
|
||||
const completedAt = parseCompletedAt(completedAtInput);
|
||||
if (!completedAt) return false;
|
||||
const latestSignals = await db
|
||||
.select({ signalId: growQscoreLatest.signalId, score: growQscoreLatest.score })
|
||||
.from(growQscoreLatest)
|
||||
.where(and(eq(growQscoreLatest.userId, userId), eq(growQscoreLatest.present, true)));
|
||||
|
||||
await forwardSignalsToQscoreService({
|
||||
orgId: DEFAULT_QSCORE_ORG_ID,
|
||||
userId,
|
||||
profession: "student",
|
||||
source: "onboarding",
|
||||
signals: [
|
||||
{
|
||||
signalId: ONBOARDING_BASELINE_SIGNAL_ID,
|
||||
score: ONBOARDING_BASELINE_SIGNAL_SCORE,
|
||||
present: true,
|
||||
const [existingProjection] = await db
|
||||
.select({ score: growQscoreProjectionState.score, signalCount: growQscoreProjectionState.signalCount })
|
||||
.from(growQscoreProjectionState)
|
||||
.where(eq(growQscoreProjectionState.userId, userId))
|
||||
.limit(1);
|
||||
|
||||
const now = new Date();
|
||||
|
||||
// Repair users affected by the old resume-upload projector, which treated a
|
||||
// plain upload as a perfect 100 score. Uploading a resume during onboarding is
|
||||
// only baseline evidence; parsed resume/interview/roleplay results should be
|
||||
// what moves the score upward.
|
||||
if (
|
||||
latestSignals.length === 1 &&
|
||||
latestSignals[0]?.signalId === "resume.uploaded" &&
|
||||
latestSignals[0].score > ONBOARDING_BASELINE_QSCORE
|
||||
) {
|
||||
await db
|
||||
.update(growQscoreLatest)
|
||||
.set({
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
raw: {
|
||||
reason: "completed onboarding baseline",
|
||||
completedAt: completedAt.toISOString(),
|
||||
reason: "resume upload baseline correction",
|
||||
correctedFrom: latestSignals[0].score,
|
||||
correctedAt: now.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(and(eq(growQscoreLatest.userId, userId), eq(growQscoreLatest.signalId, "resume.uploaded")));
|
||||
|
||||
await db
|
||||
.insert(growQscoreProjectionState)
|
||||
.values({
|
||||
userId,
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
signalCount: 1,
|
||||
dimensions: { baseline: true, latestSignalIds: ["resume.uploaded"], corrected: true },
|
||||
summary: "Baseline Q Score from onboarding resume upload.",
|
||||
updatedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: growQscoreProjectionState.userId,
|
||||
set: {
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
signalCount: 1,
|
||||
dimensions: { baseline: true, latestSignalIds: ["resume.uploaded"], corrected: true },
|
||||
summary: "Baseline Q Score from onboarding resume upload.",
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (latestSignals.length > 0 || (existingProjection?.score ?? 0) > 0) {
|
||||
return false;
|
||||
}
|
||||
const raw = {
|
||||
reason: "completed onboarding baseline",
|
||||
completedAt: completedAt.toISOString(),
|
||||
};
|
||||
|
||||
const inserted = await db
|
||||
.insert(growQscoreLatest)
|
||||
.values({
|
||||
userId,
|
||||
signalId: ONBOARDING_BASELINE_SIGNAL_ID,
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
present: true,
|
||||
source: "onboarding",
|
||||
raw,
|
||||
occurredAt: completedAt,
|
||||
updatedAt: now,
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.returning({ signalId: growQscoreLatest.signalId });
|
||||
|
||||
if (!inserted.length) return false;
|
||||
|
||||
await db.insert(growQscoreSignals).values({
|
||||
userId,
|
||||
signalId: ONBOARDING_BASELINE_SIGNAL_ID,
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
present: true,
|
||||
source: "onboarding",
|
||||
raw,
|
||||
occurredAt: completedAt,
|
||||
});
|
||||
|
||||
await db
|
||||
.insert(growQscoreProjectionState)
|
||||
.values({
|
||||
userId,
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
signalCount: 1,
|
||||
dimensions: { baseline: true, latestSignalIds: [ONBOARDING_BASELINE_SIGNAL_ID] },
|
||||
summary: "Baseline Q Score from completed onboarding.",
|
||||
updatedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: growQscoreProjectionState.userId,
|
||||
set: {
|
||||
score: ONBOARDING_BASELINE_QSCORE,
|
||||
signalCount: 1,
|
||||
dimensions: { baseline: true, latestSignalIds: [ONBOARDING_BASELINE_SIGNAL_ID] },
|
||||
summary: "Baseline Q Score from completed onboarding.",
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { type GrowEventRow } from "../../db/schema.js";
|
||||
import { asRecord, clampScore, getNumber, getString, type QscoreSignal } from "../envelope.js";
|
||||
import { forwardSignalsToQscoreService } from "../../services/service-agents.js";
|
||||
import { DEFAULT_QSCORE_ORG_ID } from "../../services/qscore-proxy.js";
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { db } from "../../db/client.js";
|
||||
import { growQscoreLatest, growQscoreProjectionState, growQscoreSignals, type GrowEventRow } from "../../db/schema.js";
|
||||
import { asRecord, clampScore, getNumber, type QscoreSignal } from "../envelope.js";
|
||||
|
||||
function signal(signalId: string, score: number, raw?: Record<string, unknown>, present = true): QscoreSignal {
|
||||
return { signalId, score: clampScore(score), present, raw };
|
||||
@@ -51,6 +51,7 @@ function extractResumeSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
const keywords = byDimension.get("Keywords") ?? nestedNumber(analysis, ["keyword_score", "keyword_relevance", "keywords"]);
|
||||
if (keywords !== undefined) {
|
||||
signals.push(signal("resume.keyword_relevance", keywords, { eventId: event.id }));
|
||||
signals.push(signal("resume.technical_keywords", keywords, { eventId: event.id }));
|
||||
}
|
||||
const quantification = byDimension.get("Quantification") ?? nestedNumber(analysis, ["quantification_score"]);
|
||||
if (quantification !== undefined) signals.push(signal("resume.quantified_achievements", quantification, { eventId: event.id }));
|
||||
@@ -58,29 +59,14 @@ function extractResumeSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (contentQuality !== undefined) signals.push(signal("resume.grammar_clarity", contentQuality, { eventId: event.id }));
|
||||
const formatting = byCategory.get("Formatting") ?? nestedNumber(analysis, ["formatting", "format_score"]);
|
||||
if (formatting !== undefined) signals.push(signal("resume.format_structure", formatting, { eventId: event.id }));
|
||||
const impact = byCategory.get("Impact Demonstration") ?? nestedNumber(analysis, ["impact_score"]);
|
||||
if (impact !== undefined) {
|
||||
signals.push(signal("resume.leadership_indicators", impact, { eventId: event.id }));
|
||||
signals.push(signal("resume.impact_statements", impact, { eventId: event.id }));
|
||||
}
|
||||
return signals;
|
||||
}
|
||||
|
||||
function interviewSessionsScore(count: number): number {
|
||||
if (count <= 0) return 0;
|
||||
if (count <= 2) return 20;
|
||||
if (count <= 5) return 35;
|
||||
return 50;
|
||||
}
|
||||
|
||||
function interviewDiversityScore(count: number): number {
|
||||
if (count <= 1) return 10;
|
||||
if (count === 2) return 20;
|
||||
return 30;
|
||||
}
|
||||
|
||||
function roleplayScenariosScore(count: number): number {
|
||||
if (count <= 0) return 0;
|
||||
if (count <= 3) return 20;
|
||||
if (count <= 6) return 35;
|
||||
return 50;
|
||||
}
|
||||
|
||||
function extractInterviewSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
const payload = event.payload ?? {};
|
||||
const review = asRecord(payload.review ?? payload.result ?? payload);
|
||||
@@ -88,14 +74,7 @@ function extractInterviewSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (!event.type.includes("review") && !event.type.includes("completed") && status !== "completed") return [];
|
||||
|
||||
const signals: QscoreSignal[] = [];
|
||||
// A completed interview must always emit at least a single-completion signal.
|
||||
// When session_count is absent, default to 1 so the event is not silently lost.
|
||||
const sessionCount = getNumber(payload.session_count ?? payload.sessionCount) ?? 1;
|
||||
signals.push(signal("interview.sessions_completed", interviewSessionsScore(sessionCount), { eventId: event.id, sessionCount }));
|
||||
const typeDiversity = getNumber(payload.type_diversity ?? payload.typeDiversity);
|
||||
if (typeDiversity !== undefined) {
|
||||
signals.push(signal("interview.type_diversity", interviewDiversityScore(typeDiversity), { eventId: event.id, typeDiversity }));
|
||||
}
|
||||
signals.push(signal("interview.completed", 100, { eventId: event.id }));
|
||||
const overall = getNumber(review.overall_score ?? review.overallScore ?? payload.overall_score);
|
||||
if (overall !== undefined) signals.push(signal("interview.overall_score", overall, { eventId: event.id }));
|
||||
const rubric = asRecord(review.rubric_scores ?? review.rubricScores);
|
||||
@@ -118,10 +97,7 @@ function extractRoleplaySignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (!event.type.includes("review") && !event.type.includes("completed") && status !== "completed") return [];
|
||||
|
||||
const signals: QscoreSignal[] = [];
|
||||
// A completed roleplay must always emit at least a single-completion signal.
|
||||
// When scenario_count is absent, default to 1 so the event is not silently lost.
|
||||
const scenarioCount = getNumber(payload.scenario_count ?? payload.scenarioCount) ?? 1;
|
||||
signals.push(signal("roleplay.scenarios_completed", roleplayScenariosScore(scenarioCount), { eventId: event.id, scenarioCount }));
|
||||
signals.push(signal("roleplay.completed", 100, { eventId: event.id }));
|
||||
const rubric = asRecord(review.rubric_scores ?? review.rubricScores);
|
||||
const scenario = getNumber(rubric.scenario_adherence ?? review.scenario_adherence_score);
|
||||
if (scenario !== undefined) signals.push(signal("roleplay.situational_judgment", scenario, { eventId: event.id }));
|
||||
@@ -131,221 +107,9 @@ function extractRoleplaySignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (adaptability !== undefined) signals.push(signal("roleplay.problem_resolution", adaptability, { eventId: event.id }));
|
||||
const communication = getNumber(rubric.content ?? rubric.communication ?? review.communication_score);
|
||||
if (communication !== undefined) signals.push(signal("roleplay.communication_effectiveness", communication, { eventId: event.id }));
|
||||
return signals;
|
||||
}
|
||||
|
||||
|
||||
function coursesCompletedScore(count: number): number {
|
||||
if (count <= 0) return 0;
|
||||
if (count <= 3) return 35;
|
||||
if (count <= 10) return 60;
|
||||
return 80;
|
||||
}
|
||||
|
||||
function courseCompletionRateScore(pct: number): number {
|
||||
if (pct < 50) return 20;
|
||||
if (pct <= 80) return 40;
|
||||
return 60;
|
||||
}
|
||||
|
||||
function courseDifficultyScore(difficulty: string): number | undefined {
|
||||
const d = difficulty.toLowerCase();
|
||||
if (d.includes("beginner") || d === "easy") return 20;
|
||||
if (d.includes("inter") || d.includes("medium")) return 35;
|
||||
if (d.includes("adv")) return 50;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function coursePathwayRelevanceScore(label: string): number | undefined {
|
||||
const l = label.toLowerCase();
|
||||
if (l.includes("high")) return 40;
|
||||
if (l.includes("med")) return 25;
|
||||
if (l.includes("low")) return 10;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractCourseSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
const payload = event.payload ?? {};
|
||||
if (
|
||||
!event.type.includes("progress_recorded") &&
|
||||
!event.type.includes("completed") &&
|
||||
!event.type.includes("started") &&
|
||||
!event.type.includes("watch")
|
||||
)
|
||||
return [];
|
||||
|
||||
const watchPctRaw = getNumber(payload.watchPct ?? payload.watch_pct) ?? 0;
|
||||
const watchPctScaled = watchPctRaw <= 1 ? watchPctRaw * 100 : watchPctRaw;
|
||||
const completedCount = getNumber(payload.completed_count ?? payload.completedCount ?? payload.courses_completed);
|
||||
const difficulty = getString(payload.difficulty);
|
||||
const label = getString(payload.label ?? payload.pathway_label);
|
||||
const raw = {
|
||||
eventId: event.id,
|
||||
courseId: payload.courseId ?? payload.course_id,
|
||||
lessonId: payload.lessonId ?? payload.lesson_id,
|
||||
watchPct: watchPctRaw,
|
||||
};
|
||||
|
||||
const signals: QscoreSignal[] = [];
|
||||
signals.push(signal("courses.started", 40, raw));
|
||||
signals.push(signal("courses.completion_rate", courseCompletionRateScore(watchPctScaled), { ...raw, completionPct: watchPctScaled }));
|
||||
if (watchPctScaled >= 80) {
|
||||
signals.push(signal("courses.completed", coursesCompletedScore(completedCount ?? 1), { ...raw, completedCount: completedCount ?? 1 }));
|
||||
}
|
||||
if (difficulty) {
|
||||
const diffScore = courseDifficultyScore(difficulty);
|
||||
if (diffScore !== undefined) signals.push(signal("courses.difficulty", diffScore, { ...raw, difficulty }));
|
||||
}
|
||||
if (label) {
|
||||
const relScore = coursePathwayRelevanceScore(label);
|
||||
if (relScore !== undefined) signals.push(signal("courses.pathway_relevance", relScore, { ...raw, label }));
|
||||
}
|
||||
return signals;
|
||||
}
|
||||
|
||||
function sourceSignalPrefix(source: string) {
|
||||
return source
|
||||
.toLowerCase()
|
||||
.replace(/-service$/, "")
|
||||
.replace(/[^a-z0-9]+/g, "_")
|
||||
.replace(/^_+|_+$/g, "") || "service";
|
||||
}
|
||||
|
||||
function extractScoredServiceSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
const payload = event.payload ?? {};
|
||||
const review = asRecord(payload.review ?? payload.result ?? payload);
|
||||
const status = String(review.status ?? payload.status ?? "");
|
||||
const isCompletion =
|
||||
event.type.includes("completed") ||
|
||||
event.type.includes("updated") ||
|
||||
event.type.includes("signal_projected") ||
|
||||
event.type.includes("signal.projected") ||
|
||||
status === "completed";
|
||||
if (!isCompletion) return [];
|
||||
|
||||
const score = getNumber(
|
||||
payload.score ??
|
||||
payload.qscore ??
|
||||
payload.q_score ??
|
||||
payload.readiness_score ??
|
||||
payload.overall_score ??
|
||||
review.score ??
|
||||
review.qscore ??
|
||||
review.q_score ??
|
||||
review.readiness_score ??
|
||||
review.overall_score,
|
||||
);
|
||||
if (score === undefined) return [];
|
||||
|
||||
const prefix = sourceSignalPrefix(event.source);
|
||||
return [
|
||||
signal(`${prefix}.service_completion_score`, score, {
|
||||
eventId: event.id,
|
||||
source: event.source,
|
||||
type: event.type,
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
function countFromPayload(payload: Record<string, unknown>): number | undefined {
|
||||
const result = asRecord(payload.result);
|
||||
const request = asRecord(payload.request);
|
||||
const params = asRecord(request.params);
|
||||
const direct = getNumber(payload.matchCount ?? payload.matches ?? payload.shortlisted ?? result.matchCount ?? result.matches ?? result.shortlisted);
|
||||
if (direct !== undefined) return direct;
|
||||
const opportunities = Array.isArray(result.opportunities) ? result.opportunities : undefined;
|
||||
if (opportunities) return opportunities.length;
|
||||
const requestOpportunities = Array.isArray(params.opportunities) ? params.opportunities : undefined;
|
||||
return requestOpportunities?.length;
|
||||
}
|
||||
|
||||
|
||||
function jobsViewedScore(count: number): number {
|
||||
if (count <= 0) return 0;
|
||||
if (count <= 20) return 15;
|
||||
if (count <= 50) return 30;
|
||||
return 40;
|
||||
}
|
||||
|
||||
function applicationsSubmittedScore(count: number): number {
|
||||
if (count <= 0) return 0;
|
||||
if (count <= 5) return 20;
|
||||
if (count <= 15) return 35;
|
||||
return 50;
|
||||
}
|
||||
|
||||
function matchRateScore(pct: number): number {
|
||||
if (pct < 50) return 15;
|
||||
if (pct <= 75) return 35;
|
||||
return 50;
|
||||
}
|
||||
|
||||
function extractMatchmakingSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (!event.type.startsWith("matchmaking.")) return [];
|
||||
const payload = event.payload ?? {};
|
||||
const count = countFromPayload(payload);
|
||||
const raw = {
|
||||
eventId: event.id,
|
||||
type: event.type,
|
||||
action: payload.action,
|
||||
opportunityId: payload.opportunityId ?? payload.matchId ?? payload.jobId ?? event.subject?.externalId,
|
||||
taskId: payload.taskId ?? payload.curatorTaskId ?? event.correlation?.taskId,
|
||||
matchCount: count,
|
||||
status: payload.status,
|
||||
};
|
||||
|
||||
const signals: QscoreSignal[] = [];
|
||||
|
||||
// Jobs viewed — from feed.viewed and match.viewed events with cumulative counts
|
||||
if (event.type === "matchmaking.feed.viewed" || event.type === "matchmaking.match.viewed") {
|
||||
const viewedCount = getNumber(payload.jobs_viewed ?? payload.jobsViewed ?? payload.viewed_count ?? payload.viewedCount ?? count);
|
||||
signals.push(signal("matching.jobs_viewed", jobsViewedScore(viewedCount ?? 1), { ...raw, viewedCount }));
|
||||
}
|
||||
|
||||
// Applications submitted — from match.applied and application.completed events
|
||||
if (event.type === "matchmaking.match.applied" || event.type === "matchmaking.application.completed") {
|
||||
const appliedCount = getNumber(payload.applications_submitted ?? payload.applicationsSubmitted ?? payload.applied_count ?? payload.appliedCount ?? count);
|
||||
signals.push(signal("matching.applications_submitted", applicationsSubmittedScore(appliedCount ?? 1), { ...raw, appliedCount }));
|
||||
}
|
||||
|
||||
// Application quality — from match quality scores if available
|
||||
const qualityScore = getNumber(payload.application_quality ?? payload.applicationQuality ?? payload.match_quality ?? payload.matchQuality);
|
||||
if (qualityScore !== undefined) {
|
||||
signals.push(signal("matching.application_quality", qualityScore, { ...raw, qualityScore }));
|
||||
}
|
||||
|
||||
// Match rate — from matches.generated with match data
|
||||
if (event.type === "matchmaking.matches.generated" && count !== undefined) {
|
||||
const totalCandidates = getNumber(payload.total_candidates ?? payload.totalCandidates) ?? count;
|
||||
const rate = totalCandidates > 0 ? (count / totalCandidates) * 100 : 0;
|
||||
signals.push(signal("matching.match_rate", matchRateScore(rate), { ...raw, matchRate: rate }));
|
||||
}
|
||||
|
||||
return signals;
|
||||
}
|
||||
|
||||
function extractSocialSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
const payload = event.payload ?? {};
|
||||
const signals: QscoreSignal[] = [];
|
||||
// social-branding sends pre-computed qscore_signals array — forward as-is
|
||||
const incoming = Array.isArray(payload.qscore_signals) ? payload.qscore_signals : Array.isArray(payload.qscoreSignals) ? payload.qscoreSignals : undefined;
|
||||
if (incoming) {
|
||||
for (const entry of incoming) {
|
||||
const record = asRecord(entry);
|
||||
const id = getString(record.signalId ?? record.signal_id ?? record.id);
|
||||
const score = getNumber(record.score ?? record.value);
|
||||
if (id !== undefined && score !== undefined) {
|
||||
signals.push(signal(id, score, { eventId: event.id, source: event.source }));
|
||||
}
|
||||
}
|
||||
return signals;
|
||||
}
|
||||
// Fall back to inline fields for backward compatibility
|
||||
const inline = asRecord(payload.signals);
|
||||
for (const [key, value] of Object.entries(inline)) {
|
||||
const score = getNumber(value);
|
||||
if (score !== undefined) signals.push(signal(key, score, { eventId: event.id, source: event.source }));
|
||||
}
|
||||
const historical = asRecord(review.historical_comparison ?? review.historicalComparison);
|
||||
const delta = getNumber(historical.overall_delta ?? historical.overallDelta);
|
||||
if (delta !== undefined) signals.push(signal("roleplay.improvement_over_time", 50 + delta * 2.5, { eventId: event.id, delta }));
|
||||
return signals;
|
||||
}
|
||||
|
||||
@@ -354,32 +118,77 @@ export function extractQscoreSignals(event: GrowEventRow): QscoreSignal[] {
|
||||
if (source.includes("resume") || event.type.startsWith("resume.")) return extractResumeSignals(event);
|
||||
if (source.includes("interview") || event.type.startsWith("interview.")) return extractInterviewSignals(event);
|
||||
if (source.includes("roleplay") || event.type.startsWith("roleplay.")) return extractRoleplaySignals(event);
|
||||
if (source.includes("course") || event.type.startsWith("course.")) return extractCourseSignals(event);
|
||||
if (source.includes("matchmaking") || event.type.startsWith("matchmaking.")) return extractMatchmakingSignals(event);
|
||||
if (source.includes("social") || source.includes("branding") || source.includes("linkedin")) return extractSocialSignals(event);
|
||||
if (source.includes("qscore") || event.type.startsWith("qscore.")) return extractScoredServiceSignals(event);
|
||||
const scoredServiceSignals = extractScoredServiceSignals(event);
|
||||
if (scoredServiceSignals.length) return scoredServiceSignals;
|
||||
if (event.type === "mission.interview_to_offer.started") {
|
||||
return [signal("goals.goals_set", 100, { eventId: event.id })];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function applyQscoreProjection(event: GrowEventRow) {
|
||||
if (!event.userId) return { signals: [], score: undefined };
|
||||
|
||||
const signals = extractQscoreSignals(event);
|
||||
if (!signals.length) return { signals, score: undefined };
|
||||
|
||||
// qscore_service OWNS all scoring. The backend only extracts signals and
|
||||
// forwards them; ingest persists signals and marks the user dirty for async
|
||||
// score computation. It does NOT return a score, so score stays undefined
|
||||
// here — readers fetch the computed score via the proxy endpoints.
|
||||
await forwardSignalsToQscoreService({
|
||||
orgId: event.orgId ?? DEFAULT_QSCORE_ORG_ID,
|
||||
for (const item of signals) {
|
||||
await db.insert(growQscoreSignals).values({
|
||||
userId: event.userId,
|
||||
sourceEventId: event.id,
|
||||
signalId: item.signalId,
|
||||
score: item.score,
|
||||
present: item.present,
|
||||
source: event.source,
|
||||
raw: item.raw,
|
||||
occurredAt: event.occurredAt,
|
||||
});
|
||||
|
||||
await db.insert(growQscoreLatest).values({
|
||||
userId: event.userId,
|
||||
signalId: item.signalId,
|
||||
score: item.score,
|
||||
present: item.present,
|
||||
source: event.source,
|
||||
sourceEventId: event.id,
|
||||
raw: item.raw,
|
||||
occurredAt: event.occurredAt,
|
||||
updatedAt: new Date(),
|
||||
}).onConflictDoUpdate({
|
||||
target: [growQscoreLatest.userId, growQscoreLatest.signalId],
|
||||
set: {
|
||||
score: item.score,
|
||||
present: item.present,
|
||||
source: event.source,
|
||||
sourceEventId: event.id,
|
||||
raw: item.raw,
|
||||
occurredAt: event.occurredAt,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const [aggregate] = await db
|
||||
.select({ score: sql<number>`round(avg(${growQscoreLatest.score}))::int`, count: sql<number>`count(*)::int` })
|
||||
.from(growQscoreLatest)
|
||||
.where(and(eq(growQscoreLatest.userId, event.userId), eq(growQscoreLatest.present, true)));
|
||||
|
||||
const score = aggregate?.score ?? 0;
|
||||
const signalCount = aggregate?.count ?? 0;
|
||||
await db.insert(growQscoreProjectionState).values({
|
||||
userId: event.userId,
|
||||
profession: "student",
|
||||
source: event.source,
|
||||
signals,
|
||||
score,
|
||||
signalCount,
|
||||
dimensions: { latestSignalIds: signals.map((s) => s.signalId) },
|
||||
summary: `Estimated readiness score from ${signalCount} current signal${signalCount === 1 ? "" : "s"}.`,
|
||||
updatedAt: new Date(),
|
||||
}).onConflictDoUpdate({
|
||||
target: growQscoreProjectionState.userId,
|
||||
set: {
|
||||
score,
|
||||
signalCount,
|
||||
dimensions: { latestSignalIds: signals.map((s) => s.signalId) },
|
||||
summary: `Estimated readiness score from ${signalCount} current signal${signalCount === 1 ? "" : "s"}.`,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return { signals, score: undefined };
|
||||
return { signals, score };
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../../db/client.js";
|
||||
import { growActiveMissions, missionServiceSessions, type GrowEventRow } from "../../db/schema.js";
|
||||
import { missionServiceSessions, type GrowEventRow } from "../../db/schema.js";
|
||||
import { asRecord, getString } from "../envelope.js";
|
||||
import { normalizeServiceId } from "../record-grow-event.js";
|
||||
|
||||
@@ -12,16 +12,8 @@ function extractExternalId(event: GrowEventRow): string | undefined {
|
||||
correlation.session_id ??
|
||||
correlation.externalId ??
|
||||
correlation.external_id ??
|
||||
correlation.lessonId ??
|
||||
correlation.courseId ??
|
||||
payload.session_id ??
|
||||
payload.sessionId ??
|
||||
payload.lessonId ??
|
||||
payload.lesson_id ??
|
||||
payload.courseId ??
|
||||
payload.course_id ??
|
||||
payload.videoId ??
|
||||
payload.video_id ??
|
||||
payload.id,
|
||||
);
|
||||
}
|
||||
@@ -29,9 +21,8 @@ function extractExternalId(event: GrowEventRow): string | undefined {
|
||||
function statusFor(event: GrowEventRow): string {
|
||||
const payload = event.payload ?? {};
|
||||
const explicit = getString(payload.status);
|
||||
if (explicit === "configured" || explicit === "draft" || explicit === "ready") return "active";
|
||||
if (explicit) return explicit;
|
||||
if (event.type.includes("review_completed") || event.type.includes("feedback.generated") || event.type.includes("completed")) return "completed";
|
||||
if (event.type.includes("review_completed") || event.type.includes("completed")) return "completed";
|
||||
if (event.type.includes("failed")) return "failed";
|
||||
if (event.type.includes("configured") || event.type.includes("created")) return "active";
|
||||
return "active";
|
||||
@@ -42,58 +33,22 @@ export async function applyServiceSessionProjection(event: GrowEventRow) {
|
||||
const externalId = extractExternalId(event);
|
||||
if (!externalId) return null;
|
||||
const serviceId = normalizeServiceId(event.source);
|
||||
if (!["interview", "roleplay", "resume", "course"].includes(serviceId)) return null;
|
||||
if (!["interview", "roleplay", "resume"].includes(serviceId)) return null;
|
||||
|
||||
const mission = asRecord(event.mission);
|
||||
const correlation = asRecord(event.correlation);
|
||||
const payload = event.payload ?? {};
|
||||
const missionInstanceId = getString(mission.instanceId ?? mission.instance_id ?? payload.missionInstanceId ?? payload.mission_instance_id);
|
||||
const missionId = getString(mission.missionId ?? mission.mission_id ?? payload.missionId ?? payload.mission_id);
|
||||
const stageId = getString(mission.stageId ?? mission.stage_id ?? payload.stageId ?? payload.stage_id);
|
||||
const [activeMission] = missionInstanceId
|
||||
? await db
|
||||
.select({
|
||||
instanceId: growActiveMissions.instanceId,
|
||||
missionId: growActiveMissions.missionId,
|
||||
currentStageId: growActiveMissions.currentStageId,
|
||||
})
|
||||
.from(growActiveMissions)
|
||||
.where(and(eq(growActiveMissions.userId, event.userId), eq(growActiveMissions.instanceId, missionInstanceId)))
|
||||
.limit(1)
|
||||
: [];
|
||||
const linkedMissionInstanceId = activeMission?.instanceId;
|
||||
const linkedMissionId = missionId ?? activeMission?.missionId;
|
||||
const linkedStageId = stageId ?? activeMission?.currentStageId ?? undefined;
|
||||
const metadata = {
|
||||
lastType: event.type,
|
||||
subject: event.subject,
|
||||
payloadStatus: event.payload?.status,
|
||||
missionInstanceId,
|
||||
missionId,
|
||||
stageId,
|
||||
taskId: getString(correlation.taskId ?? correlation.curatorTaskId ?? correlation.curator_task_id ?? payload.taskId ?? payload.curatorTaskId ?? payload.curator_task_id),
|
||||
curatorTaskId: getString(correlation.curatorTaskId ?? correlation.curator_task_id ?? correlation.taskId ?? payload.curatorTaskId ?? payload.curator_task_id ?? payload.taskId),
|
||||
courseId: getString(correlation.courseId ?? payload.courseId ?? payload.course_id),
|
||||
lessonId: getString(correlation.lessonId ?? payload.lessonId ?? payload.lesson_id),
|
||||
};
|
||||
const updateValues = {
|
||||
...(linkedMissionInstanceId ? { missionInstanceId: linkedMissionInstanceId } : {}),
|
||||
...(linkedMissionId ? { missionId: linkedMissionId } : {}),
|
||||
...(linkedStageId ? { stageId: linkedStageId } : {}),
|
||||
status: statusFor(event),
|
||||
metadata,
|
||||
lastEventId: event.id,
|
||||
lastCheckedAt: event.type.includes("review") ? new Date() : undefined,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const [row] = await db
|
||||
.insert(missionServiceSessions)
|
||||
.values({
|
||||
userId: event.userId,
|
||||
missionInstanceId: linkedMissionInstanceId,
|
||||
missionId: linkedMissionId,
|
||||
stageId: linkedStageId,
|
||||
missionInstanceId: getString(mission.instanceId ?? mission.instance_id),
|
||||
missionId: getString(mission.missionId ?? mission.mission_id),
|
||||
stageId: getString(mission.stageId ?? mission.stage_id),
|
||||
serviceId,
|
||||
externalId,
|
||||
status: statusFor(event),
|
||||
@@ -103,7 +58,13 @@ export async function applyServiceSessionProjection(event: GrowEventRow) {
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [missionServiceSessions.serviceId, missionServiceSessions.externalId],
|
||||
set: updateValues,
|
||||
set: {
|
||||
status: statusFor(event),
|
||||
metadata,
|
||||
lastEventId: event.id,
|
||||
lastCheckedAt: event.type.includes("review") ? new Date() : undefined,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
})
|
||||
.returning();
|
||||
|
||||
|
||||
@@ -30,23 +30,20 @@ export function normalizeServiceId(source: string): string {
|
||||
if (source.includes("interview")) return "interview";
|
||||
if (source.includes("roleplay")) return "roleplay";
|
||||
if (source.includes("resume")) return "resume";
|
||||
if (source.includes("course")) return "course";
|
||||
if (source.includes("courses")) return "course";
|
||||
if (source.includes("qscore")) return "qscore";
|
||||
return source;
|
||||
}
|
||||
|
||||
export async function recordGrowEvent(input: unknown, overrides: { userId?: string; source?: string } = {}): Promise<GrowEventRow> {
|
||||
const result = await recordGrowEventWithResult(input, overrides);
|
||||
return result.event;
|
||||
}
|
||||
|
||||
export async function recordGrowEventWithResult(input: unknown, overrides: { userId?: string; source?: string } = {}): Promise<{ event: GrowEventRow; inserted: boolean }> {
|
||||
const normalized = normalizeGrowEvent(input, overrides);
|
||||
const resolvedUserId = await resolveUserId(normalized);
|
||||
if (resolvedUserId) await ensureUser(resolvedUserId);
|
||||
|
||||
const processingStatus = resolvedUserId ? "pending" : "unresolved";
|
||||
if (normalized.dedupeKey) {
|
||||
const [existing] = await db.select().from(growEvents).where(eq(growEvents.dedupeKey, normalized.dedupeKey)).limit(1);
|
||||
if (existing) return existing;
|
||||
}
|
||||
|
||||
const values = {
|
||||
id: normalized.id,
|
||||
@@ -65,70 +62,30 @@ export async function recordGrowEventWithResult(input: unknown, overrides: { use
|
||||
processingStatus,
|
||||
} satisfies typeof growEvents.$inferInsert;
|
||||
|
||||
// Atomic insert-or-dedupe.
|
||||
//
|
||||
// onConflictDoNothing() with no target catches BOTH the PK (grow_events.id)
|
||||
// and the dedupe_key unique index violations atomically. .returning() yields
|
||||
// a row only on a fresh INSERT — on a conflict the row is suppressed. This is
|
||||
// the deterministic insert-vs-existing discriminator with no clock heuristics,
|
||||
// no xmax probe, and no raw SQL.
|
||||
//
|
||||
// Critically, DO NOTHING (not DO UPDATE) means an existing row is left
|
||||
// *untouched*: a terminal (processed/failed) row is never resurrected back to
|
||||
// *pending by a duplicate ingest. We then SELECT the pre-existing row AS-IS.
|
||||
//
|
||||
// NULL dedupeKey never collides (Postgres treats NULLs as distinct under a
|
||||
// unique index), so events with no dedupe key always insert fresh.
|
||||
const insertedRows = await db
|
||||
const [inserted] = await db
|
||||
.insert(growEvents)
|
||||
.values(values)
|
||||
.onConflictDoNothing()
|
||||
.onConflictDoUpdate({
|
||||
target: growEvents.id,
|
||||
set: {
|
||||
userId: values.userId,
|
||||
orgId: values.orgId,
|
||||
source: values.source,
|
||||
type: values.type,
|
||||
category: values.category,
|
||||
occurredAt: values.occurredAt,
|
||||
mission: values.mission,
|
||||
subject: values.subject,
|
||||
correlation: values.correlation,
|
||||
payload: values.payload,
|
||||
raw: values.raw,
|
||||
processingStatus: values.processingStatus,
|
||||
},
|
||||
})
|
||||
.returning();
|
||||
|
||||
const inserted = insertedRows[0];
|
||||
if (inserted) {
|
||||
return { event: inserted, inserted: true };
|
||||
}
|
||||
// Conflict: the row already existed. onConflictDoNothing() (no target)
|
||||
// suppresses on EITHER the PK (id) or the dedupe_key unique index. We must
|
||||
// find the conflicting row by whichever constraint fired — try dedupeKey
|
||||
// first (the common case), then fall back to id (PK collision). Both can
|
||||
// point to the same row when normalize.ts sets dedupeKey = id.
|
||||
let existing: GrowEventRow | undefined;
|
||||
if (normalized.dedupeKey) {
|
||||
[existing] = await db
|
||||
.select()
|
||||
.from(growEvents)
|
||||
.where(eq(growEvents.dedupeKey, normalized.dedupeKey))
|
||||
.limit(1);
|
||||
}
|
||||
if (!existing && normalized.id) {
|
||||
[existing] = await db
|
||||
.select()
|
||||
.from(growEvents)
|
||||
.where(eq(growEvents.id, normalized.id))
|
||||
.limit(1);
|
||||
}
|
||||
|
||||
if (!existing) {
|
||||
// Should not happen: onConflictDoNothing suppressed the insert yet we
|
||||
// cannot find the conflicting row on either constraint. Treat as a hard
|
||||
// failure rather than silently dropping the event.
|
||||
throw new Error("failed to record grow event: conflict with unresolvable row");
|
||||
}
|
||||
|
||||
return { event: existing, inserted: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Routing gate: only newly inserted, user-resolved, pending events should be
|
||||
* enqueued to the user-event actor. Replays / dedupe hits / unresolved events
|
||||
* must not enqueue (they would saturate the actor queue on duplicate ingest).
|
||||
*/
|
||||
export function shouldRouteGrowEvent(event: Pick<GrowEventRow, "userId" | "processingStatus">, inserted: boolean): boolean {
|
||||
if (!inserted) return false;
|
||||
if (!event.userId) return false;
|
||||
return event.processingStatus === "pending";
|
||||
if (!inserted) throw new Error("failed to record grow event");
|
||||
return inserted;
|
||||
}
|
||||
|
||||
export async function markGrowEventProcessing(eventId: string) {
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { config } from "../config.js";
|
||||
import { log } from "../log.js";
|
||||
import {
|
||||
markGrowEventFailed,
|
||||
markGrowEventProcessed,
|
||||
markGrowEventProcessing,
|
||||
recordGrowEventWithResult,
|
||||
shouldRouteGrowEvent,
|
||||
} from "./record-grow-event.js";
|
||||
import { recordGrowEvent } from "./record-grow-event.js";
|
||||
import { routeGrowEventToUserActor } from "./route-to-user-actor.js";
|
||||
import { applyQscoreProjection } from "./projectors/qscore-projector.js";
|
||||
import { ensureOnboardingSideEffectsForEvent } from "./onboarding-ledger.js";
|
||||
|
||||
// This file has two Redis ingestion modes:
|
||||
// 1. Canonical GrowEvent stream: grow.events.raw — future service event bus.
|
||||
@@ -31,8 +23,8 @@ type RedisClientLike = {
|
||||
};
|
||||
|
||||
type ServiceRedisSpec = {
|
||||
serviceId: "interview" | "roleplay" | "resume" | "course";
|
||||
agentName: "interview-service" | "roleplay-service" | "resume-builder" | "course-service";
|
||||
serviceId: "interview" | "roleplay" | "resume";
|
||||
agentName: "interview-service" | "roleplay-service" | "resume-builder";
|
||||
redisUrl: string;
|
||||
};
|
||||
|
||||
@@ -72,15 +64,6 @@ function getString(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function getNumber(value: unknown): number | undefined {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function fieldsToEvent(fields: Record<string, string>, stream: string) {
|
||||
const payload = fields.payload ? parseFieldValue(fields.payload) : parseFieldValue(fields.data ?? "{}");
|
||||
const correlation = fields.correlation ? parseFieldValue(fields.correlation) : undefined;
|
||||
@@ -108,7 +91,6 @@ function serviceSpecs(): ServiceRedisSpec[] {
|
||||
{ serviceId: "interview", agentName: "interview-service", redisUrl: config.interviewRedisUrl },
|
||||
{ serviceId: "roleplay", agentName: "roleplay-service", redisUrl: config.roleplayRedisUrl },
|
||||
{ serviceId: "resume", agentName: "resume-builder", redisUrl: config.resumeRedisUrl },
|
||||
{ serviceId: "course", agentName: "course-service", redisUrl: config.coursesRedisUrl },
|
||||
];
|
||||
return specs.filter((spec) => Boolean(spec.redisUrl));
|
||||
}
|
||||
@@ -118,34 +100,26 @@ function actionToEventType(serviceId: ServiceRedisSpec["serviceId"], action: str
|
||||
const effective = msgAction || action || "event";
|
||||
|
||||
if (serviceId === "interview") {
|
||||
if (effective === "interview_configured" || action === "configure_interview") return "interview.session.configured";
|
||||
if (effective === "interview_configured" || action === "configure_interview") return "interview.configured";
|
||||
if (effective === "review_loaded") {
|
||||
const data = asRecord(message.data);
|
||||
return data.status === "completed" ? "interview.feedback.generated" : "interview.feedback.processing";
|
||||
return data.status === "completed" ? "interview.review_completed" : "interview.review_processing";
|
||||
}
|
||||
if (effective === "interview_page_loaded") return "interview.page_state_loaded";
|
||||
return `interview.${effective.replaceAll("_", ".")}`;
|
||||
}
|
||||
|
||||
if (serviceId === "roleplay") {
|
||||
if (effective === "roleplay_configured" || action === "configure_roleplay") return "roleplay.scenario.configured";
|
||||
if (effective === "roleplay_configured" || action === "configure_roleplay") return "roleplay.configured";
|
||||
if (effective === "roleplay_review_loaded" || effective === "review_loaded") {
|
||||
const data = asRecord(message.data);
|
||||
return data.status === "completed" ? "roleplay.feedback.generated" : "roleplay.feedback.processing";
|
||||
return data.status === "completed" ? "roleplay.review_completed" : "roleplay.review_processing";
|
||||
}
|
||||
if (effective === "roleplay_page_loaded") return "roleplay.page_state_loaded";
|
||||
return `roleplay.${effective.replaceAll("_", ".")}`;
|
||||
}
|
||||
|
||||
if (serviceId === "course") {
|
||||
if (effective === "swipe_recorded" || action === "record_swipe") return "course.progress_recorded";
|
||||
if (effective === "profile_created" || effective === "profile_recalibrated") return "course.started";
|
||||
if (effective === "course_state_loaded") return "course.page_state_loaded";
|
||||
if (effective === "feed_loaded") return "course.feed_loaded";
|
||||
return `course.${effective.replaceAll("_", ".")}`;
|
||||
}
|
||||
|
||||
if (effective === "ai_analysis_complete" || action === "ai_analyze") return "resume.analysis.completed";
|
||||
if (effective === "ai_analysis_complete" || action === "ai_analyze") return "resume.analysis_completed";
|
||||
if (effective === "resume_loaded") return "resume.loaded";
|
||||
if (effective === "resume_parsed") return "resume.parsed";
|
||||
return `resume.${effective.replaceAll("_", ".")}`;
|
||||
@@ -171,59 +145,11 @@ function extractResumeId(message: Record<string, unknown>, ctx?: LegacyTaskConte
|
||||
);
|
||||
}
|
||||
|
||||
function extractCourseIds(ctx?: LegacyTaskContext) {
|
||||
const params = ctx?.params ?? {};
|
||||
const courseId = getString(params.courseId ?? params.course_id ?? params.anchor_id ?? params.anchorId);
|
||||
const lessonId = getString(params.lessonId ?? params.lesson_id ?? params.video_id ?? params.videoId);
|
||||
return { courseId, lessonId };
|
||||
}
|
||||
|
||||
function meaningfulCourseProgress(data: Record<string, unknown>, ctx?: LegacyTaskContext) {
|
||||
if (data.error) return false;
|
||||
const params = ctx?.params ?? {};
|
||||
const watchS = getNumber(params.watch_s ?? params.watchS) ?? 0;
|
||||
const watchPct = getNumber(data.watch_pct ?? data.watchPct) ?? 0;
|
||||
const label = getString(data.label);
|
||||
return watchS >= 3 || watchPct >= 0.5 || label === "pos" || label === "strong_pos";
|
||||
}
|
||||
|
||||
async function recordAndRoute(input: unknown) {
|
||||
const { event, inserted } = await recordGrowEventWithResult(input);
|
||||
|
||||
// Route only newly inserted, user-resolved, pending events to the actor.
|
||||
// Dedupe hits (inserted=false) are skipped so duplicate ingests cannot
|
||||
// saturate the actor queue.
|
||||
const routed = shouldRouteGrowEvent(event, inserted);
|
||||
if (routed) {
|
||||
await routeGrowEventToUserActor(event).catch((err) => {
|
||||
log.warn({ err, eventId: event.id, userId: event.userId }, "failed to route grow event to user actor");
|
||||
});
|
||||
}
|
||||
|
||||
// In-process projection/onboarding only for freshly inserted events;
|
||||
// dedupe hits have already been processed (or are being processed elsewhere).
|
||||
if (!inserted) return event;
|
||||
if (!event.userId) return event;
|
||||
|
||||
await markGrowEventProcessing(event.id);
|
||||
try {
|
||||
await applyQscoreProjection(event);
|
||||
const onboarding = await ensureOnboardingSideEffectsForEvent(event);
|
||||
if (
|
||||
onboarding.curatorOnboarding.status === "skipped" &&
|
||||
onboarding.curatorOnboarding.reason === "loop_failed"
|
||||
) {
|
||||
throw new Error("curator_onboarding_loop_failed");
|
||||
}
|
||||
// Always mark "processed" after successful synchronous projections, regardless
|
||||
// of route outcome. The actor will still run mission reducers on "processed"
|
||||
// events (isProcessableStatus allows "processed") — projections are idempotent
|
||||
// and the in-memory processedEventIds guard prevents double-processing.
|
||||
await markGrowEventProcessed(event.id);
|
||||
} catch (err) {
|
||||
await markGrowEventFailed(event.id, err);
|
||||
throw err;
|
||||
}
|
||||
const event = await recordGrowEvent(input);
|
||||
await routeGrowEventToUserActor(event).catch((err) => {
|
||||
log.warn({ err, eventId: event.id, userId: event.userId }, "failed to route grow event to user actor");
|
||||
});
|
||||
return event;
|
||||
}
|
||||
|
||||
@@ -239,12 +165,6 @@ async function handleLegacyResponseMessage(spec: ServiceRedisSpec, channel: stri
|
||||
const eventType = actionToEventType(spec.serviceId, ctx?.action, message);
|
||||
const sessionId = extractSessionId(message, ctx);
|
||||
const resumeId = extractResumeId(message, ctx);
|
||||
const { courseId, lessonId } = extractCourseIds(ctx);
|
||||
if (eventType === "course.progress_recorded" && !meaningfulCourseProgress(data, ctx)) return;
|
||||
const curatorTaskId = getString(ctx?.params?.curatorTaskId ?? ctx?.params?.curator_task_id ?? ctx?.params?.taskId);
|
||||
const missionInstanceId = getString(ctx?.params?.missionInstanceId ?? ctx?.params?.mission_instance_id);
|
||||
const missionId = getString(ctx?.params?.missionId ?? ctx?.params?.mission_id);
|
||||
const stageId = getString(ctx?.params?.stageId ?? ctx?.params?.stage_id);
|
||||
|
||||
await recordAndRoute({
|
||||
id: randomUUID(),
|
||||
@@ -253,36 +173,18 @@ async function handleLegacyResponseMessage(spec: ServiceRedisSpec, channel: stri
|
||||
category: type === "agent_error" ? "system" : "service",
|
||||
userId: ctx?.userId,
|
||||
occurredAt: new Date().toISOString(),
|
||||
mission: {
|
||||
instanceId: missionInstanceId,
|
||||
missionId,
|
||||
stageId,
|
||||
},
|
||||
correlation: {
|
||||
taskId: curatorTaskId ?? taskId,
|
||||
curatorTaskId,
|
||||
serviceTaskId: taskId,
|
||||
taskId,
|
||||
action: ctx?.action,
|
||||
sessionId,
|
||||
resumeId,
|
||||
courseId,
|
||||
lessonId,
|
||||
externalId: sessionId ?? resumeId ?? lessonId ?? courseId,
|
||||
externalId: sessionId ?? resumeId,
|
||||
},
|
||||
payload: {
|
||||
action: ctx?.action,
|
||||
params: ctx?.params,
|
||||
message,
|
||||
data,
|
||||
taskId: curatorTaskId,
|
||||
curatorTaskId,
|
||||
serviceId: spec.agentName,
|
||||
sessionId,
|
||||
courseId,
|
||||
lessonId,
|
||||
missionInstanceId,
|
||||
missionId,
|
||||
stageId,
|
||||
},
|
||||
raw: { channel, message },
|
||||
dedupeKey: `legacy-a2a:${spec.agentName}:${taskId}:${eventType}:${JSON.stringify(message).slice(0, 512)}`,
|
||||
@@ -409,33 +311,14 @@ async function startCanonicalGrowEventStream(createClient: (opts: { url: string
|
||||
}
|
||||
|
||||
export async function startGrowEventsRedisConsumer() {
|
||||
// Both Redis ingestion paths are default-off. When neither flag is set, we
|
||||
// return immediately WITHOUT calling loadRedisCreateClient() — so the
|
||||
// `redis` module is never imported and no client is constructed. The REST
|
||||
// production path (POST /events/ingest/service) handles all event ingestion
|
||||
// without Redis.
|
||||
if (!config.growEventsRedisEnabled && !config.legacyServiceRedisEnabled) {
|
||||
log.info("grow events Redis consumers disabled (GROW_EVENTS_REDIS_ENABLED and LEGACY_SERVICE_REDIS_ENABLED both off)");
|
||||
const createClient = await loadRedisCreateClient();
|
||||
await startCanonicalGrowEventStream(createClient);
|
||||
|
||||
const specs = serviceSpecs();
|
||||
if (!specs.length) {
|
||||
log.info("legacy service Redis observers disabled (INTERVIEW_REDIS_URL/ROLEPLAY_REDIS_URL/RESUME_REDIS_URL not set)");
|
||||
return;
|
||||
}
|
||||
|
||||
// At least one path is opted in — now it is safe to load the Redis module.
|
||||
const createClient = await loadRedisCreateClient();
|
||||
|
||||
if (config.growEventsRedisEnabled) {
|
||||
if (!config.growEventsRedisUrl) {
|
||||
log.warn("GROW_EVENTS_REDIS_ENABLED=true but GROW_EVENTS_REDIS_URL not set — canonical consumer skipped");
|
||||
} else {
|
||||
await startCanonicalGrowEventStream(createClient);
|
||||
}
|
||||
}
|
||||
|
||||
if (config.legacyServiceRedisEnabled) {
|
||||
const specs = serviceSpecs();
|
||||
if (!specs.length) {
|
||||
log.info("legacy service Redis observers enabled but no service URLs configured (INTERVIEW_REDIS_URL/ROLEPLAY_REDIS_URL/RESUME_REDIS_URL not set)");
|
||||
} else {
|
||||
await Promise.all(specs.map((spec) => startLegacyServiceObserver(spec, createClient)));
|
||||
}
|
||||
}
|
||||
await Promise.all(specs.map((spec) => startLegacyServiceObserver(spec, createClient)));
|
||||
}
|
||||
|
||||
@@ -3,46 +3,13 @@ import { config } from "../config.js";
|
||||
import type { Registry } from "../actors/registry.js";
|
||||
import type { GrowEventRow } from "../db/schema.js";
|
||||
|
||||
export type GrowEventActorRouteResult =
|
||||
| { routed: true }
|
||||
| { routed: false; reason: "unresolved_user" | "actor_route_timeout" };
|
||||
|
||||
export interface GrowEventActorRouteOptions {
|
||||
timeoutMs?: number;
|
||||
enqueue?: (event: { userId: string; eventId: string }) => Promise<unknown>;
|
||||
}
|
||||
|
||||
const DEFAULT_ACTOR_ROUTE_TIMEOUT_MS = 2_000;
|
||||
const ACTOR_ROUTE_TIMEOUT = Symbol("actor_route_timeout");
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
function getClient(): Client<Registry> {
|
||||
return (_client ??= createClient<Registry>(config.rivetClientEndpoint));
|
||||
}
|
||||
|
||||
export async function routeGrowEventToUserActor(
|
||||
event: Pick<GrowEventRow, "id" | "userId">,
|
||||
options: GrowEventActorRouteOptions = {},
|
||||
): Promise<GrowEventActorRouteResult> {
|
||||
if (!event.userId) return { routed: false, reason: "unresolved_user" };
|
||||
|
||||
const actorEvent = { userId: event.userId, eventId: event.id };
|
||||
const enqueue = options.enqueue ?? ((input) =>
|
||||
getClient().userEventActor.getOrCreate([input.userId]).enqueueEvent(input));
|
||||
const timeoutMs = options.timeoutMs ?? DEFAULT_ACTOR_ROUTE_TIMEOUT_MS;
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
|
||||
try {
|
||||
const result = await Promise.race([
|
||||
enqueue(actorEvent),
|
||||
new Promise<typeof ACTOR_ROUTE_TIMEOUT>((resolve) => {
|
||||
timer = setTimeout(() => resolve(ACTOR_ROUTE_TIMEOUT), timeoutMs);
|
||||
}),
|
||||
]);
|
||||
return result === ACTOR_ROUTE_TIMEOUT
|
||||
? { routed: false, reason: "actor_route_timeout" }
|
||||
: { routed: true };
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
export async function routeGrowEventToUserActor(event: Pick<GrowEventRow, "id" | "userId">) {
|
||||
if (!event.userId) return { routed: false, reason: "unresolved_user" } as const;
|
||||
await getClient().userEventActor.getOrCreate([event.userId]).enqueueEvent({ userId: event.userId, eventId: event.id });
|
||||
return { routed: true } as const;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,7 @@
|
||||
import { getService, listServices, type ServiceId } from "../services/service-registry.js";
|
||||
import { config } from "../config.js";
|
||||
|
||||
export type GrowServiceId = ServiceId;
|
||||
export type GrowFeatureId =
|
||||
| "resume-building"
|
||||
| "cover-letter"
|
||||
| "mock-interview"
|
||||
| "mock-roleplay"
|
||||
| "q-score"
|
||||
| "social-branding"
|
||||
| "matchmaking"
|
||||
| "pathways"
|
||||
| "courses"
|
||||
| "assessment";
|
||||
export type GrowServiceId = "resume-service" | "interview-service" | "roleplay-service" | "qscore-service" | "social-branding-service" | "matchmaking-service";
|
||||
export type GrowFeatureId = "resume-building" | "mock-interview" | "mock-roleplay" | "q-score" | "social-branding" | "matchmaking";
|
||||
|
||||
export type GrowFeatureDefinition = {
|
||||
id: GrowFeatureId;
|
||||
@@ -26,18 +16,77 @@ export type GrowFeatureDefinition = {
|
||||
operations: string[];
|
||||
};
|
||||
|
||||
export const featureDefinitions: GrowFeatureDefinition[] = listServices().map((service) => ({
|
||||
id: service.featureId as GrowFeatureId,
|
||||
serviceId: service.id,
|
||||
title: service.label,
|
||||
label: service.label,
|
||||
description: service.description,
|
||||
promptModulePath: service.promptModulePath,
|
||||
enabled: service.enabled,
|
||||
internalUrl: service.backend.baseUrl,
|
||||
publicUrl: service.backend.publicUrl,
|
||||
operations: Object.keys(service.backend.endpoints),
|
||||
}));
|
||||
export const featureDefinitions: GrowFeatureDefinition[] = [
|
||||
{
|
||||
id: "resume-building",
|
||||
serviceId: "resume-service",
|
||||
title: "Resume Building",
|
||||
label: "Resume",
|
||||
description: "Build, tailor, analyze, and improve resumes for role fit and ATS readiness.",
|
||||
promptModulePath: "agents/resume.md",
|
||||
enabled: Boolean(config.resumeServiceUrl),
|
||||
internalUrl: config.resumeServiceUrl,
|
||||
publicUrl: config.resumePublicUrl,
|
||||
operations: ["resume.state", "resume.templates", "resume.a2aTask", "resume.create", "resume.update", "resume.analyze", "resume.suggestions", "resume.copilot", "resume.optimizeSummary", "resume.optimizeExperience", "resume.suggestSkills", "resume.generateSummary", "resume.versions", "resume.preview"],
|
||||
},
|
||||
{
|
||||
id: "mock-interview",
|
||||
serviceId: "interview-service",
|
||||
title: "Mock Interview",
|
||||
label: "Interview",
|
||||
description: "Configure, practice, review, and score interview sessions.",
|
||||
promptModulePath: "agents/interview.md",
|
||||
enabled: Boolean(config.interviewServiceUrl),
|
||||
internalUrl: config.interviewServiceUrl,
|
||||
publicUrl: config.interviewPublicUrl,
|
||||
operations: ["interview.configure", "interview.preview", "interview.questions", "interview.approve", "interview.assignments", "interview.unassign", "interview.resultsBulk", "interview.review", "interview.leaderboard", "interview.artifacts", "interview.videoUpload", "interview.practice"],
|
||||
},
|
||||
{
|
||||
id: "mock-roleplay",
|
||||
serviceId: "roleplay-service",
|
||||
title: "Mock Roleplay",
|
||||
label: "Roleplay",
|
||||
description: "Practice negotiations, recruiter calls, manager conversations, and stakeholder roleplays.",
|
||||
promptModulePath: "agents/roleplay.md",
|
||||
enabled: Boolean(config.roleplayServiceUrl),
|
||||
internalUrl: config.roleplayServiceUrl,
|
||||
publicUrl: config.roleplayPublicUrl,
|
||||
operations: ["roleplay.configure", "roleplay.preview", "roleplay.questions", "roleplay.approve", "roleplay.assignments", "roleplay.unassign", "roleplay.resultsBulk", "roleplay.review", "roleplay.leaderboard", "roleplay.artifacts", "roleplay.videoUpload", "roleplay.practice"],
|
||||
},
|
||||
{
|
||||
id: "q-score",
|
||||
serviceId: "qscore-service",
|
||||
title: "Q Score",
|
||||
label: "Q Score",
|
||||
description: "Analyze overall job-market readiness and convert signals into improvement priorities.",
|
||||
promptModulePath: "agents/qscore.md",
|
||||
enabled: Boolean(config.qscoreServiceUrl),
|
||||
internalUrl: config.qscoreServiceUrl,
|
||||
operations: ["qscore.ingest", "qscore.compute"],
|
||||
},
|
||||
{
|
||||
id: "social-branding",
|
||||
serviceId: "social-branding-service",
|
||||
title: "Social Branding",
|
||||
label: "Branding",
|
||||
description: "Build and optimize your professional profile, LinkedIn presence, and personal brand.",
|
||||
promptModulePath: "agents/social-branding.md",
|
||||
enabled: Boolean(config.socialBrandingServiceUrl),
|
||||
internalUrl: config.socialBrandingServiceUrl,
|
||||
operations: ["branding.profile", "branding.linkedin", "branding.content", "branding.analyze"],
|
||||
},
|
||||
{
|
||||
id: "matchmaking",
|
||||
serviceId: "matchmaking-service",
|
||||
title: "Matchmaking",
|
||||
label: "Matchmaking",
|
||||
description: "Connect with relevant professionals, mentors, and opportunities through curated matching.",
|
||||
promptModulePath: "agents/matchmaking.md",
|
||||
enabled: Boolean(config.matchmakingServiceUrl),
|
||||
internalUrl: config.matchmakingServiceUrl,
|
||||
operations: ["matchmaking.find", "matchmaking.connect", "matchmaking.schedule", "matchmaking.review"],
|
||||
},
|
||||
];
|
||||
|
||||
export const internalWorkflowModules = [
|
||||
{
|
||||
@@ -54,8 +103,7 @@ export function listFeatureDefinitions() {
|
||||
}
|
||||
|
||||
export function getFeatureByServiceId(serviceId: string) {
|
||||
const service = getService(serviceId);
|
||||
return service ? featureDefinitions.find((feature) => feature.serviceId === service.id) : undefined;
|
||||
return featureDefinitions.find((feature) => feature.serviceId === serviceId);
|
||||
}
|
||||
|
||||
export function displayLabelForService(serviceId: string | undefined) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { asc, desc, eq, and, sql } from "drizzle-orm";
|
||||
import { asc, desc, eq, and } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { growActiveMissions, growConversationMessages, growConversations, missionCoachRuns, missionSuggestions } from "../db/schema.js";
|
||||
import type { GrowActiveMission, MissionSnapshot } from "../actors/missions/types.js";
|
||||
@@ -42,118 +42,6 @@ export async function createConversationPg(userId: string, title = "Talk to Me")
|
||||
return toConversation(row);
|
||||
}
|
||||
|
||||
export async function ensureCuratorTaskConversationPg(input: {
|
||||
userId: string;
|
||||
curatorTaskId: string;
|
||||
subtaskIndex?: number;
|
||||
subtask?: string;
|
||||
missionInstanceId?: string;
|
||||
missionId?: string;
|
||||
stageId?: string;
|
||||
title?: string;
|
||||
}): Promise<GrowConversation> {
|
||||
const curatorTaskKey = `${input.curatorTaskId}:${input.subtaskIndex ?? "task"}`;
|
||||
const [existing] = await db
|
||||
.select()
|
||||
.from(growConversations)
|
||||
.where(and(
|
||||
eq(growConversations.userId, input.userId),
|
||||
sql`${growConversations.metadata}->>'curatorTaskKey' = ${curatorTaskKey}`,
|
||||
))
|
||||
.orderBy(desc(growConversations.updatedAt))
|
||||
.limit(1);
|
||||
|
||||
const metadata = {
|
||||
source: "curator-v1",
|
||||
curatorTaskId: input.curatorTaskId,
|
||||
curatorTaskKey,
|
||||
...(Number.isInteger(input.subtaskIndex) ? { subtaskIndex: input.subtaskIndex } : {}),
|
||||
...(input.subtask ? { subtask: input.subtask } : {}),
|
||||
...(input.missionInstanceId ? { missionInstanceId: input.missionInstanceId } : {}),
|
||||
...(input.missionId ? { missionId: input.missionId } : {}),
|
||||
...(input.stageId ? { stageId: input.stageId } : {}),
|
||||
};
|
||||
|
||||
if (existing) {
|
||||
const [row] = await db.update(growConversations).set({
|
||||
title: input.title?.trim() || existing.title,
|
||||
metadata,
|
||||
updatedAt: new Date(),
|
||||
}).where(and(eq(growConversations.userId, input.userId), eq(growConversations.id, existing.id))).returning();
|
||||
if (!row) throw new Error("Failed to update curator task conversation");
|
||||
return toConversation(row);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const [row] = await db.insert(growConversations).values({
|
||||
id: buildId("conversation"),
|
||||
userId: input.userId,
|
||||
title: input.title?.trim() || input.subtask?.trim() || "V1 Curator chat",
|
||||
metadata,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}).returning();
|
||||
if (!row) throw new Error("Failed to create curator task conversation");
|
||||
return toConversation(row);
|
||||
}
|
||||
|
||||
export async function ensureMissionConversationPg(input: {
|
||||
userId: string;
|
||||
missionInstanceId: string;
|
||||
missionId: string;
|
||||
stageId?: string;
|
||||
title?: string;
|
||||
source?: string;
|
||||
}): Promise<GrowConversation> {
|
||||
const [existing] = await db
|
||||
.select()
|
||||
.from(growConversations)
|
||||
.where(and(
|
||||
eq(growConversations.userId, input.userId),
|
||||
sql`${growConversations.metadata}->>'missionInstanceId' = ${input.missionInstanceId}`,
|
||||
))
|
||||
.orderBy(desc(growConversations.updatedAt))
|
||||
.limit(1);
|
||||
|
||||
const metadata = {
|
||||
missionInstanceId: input.missionInstanceId,
|
||||
missionId: input.missionId,
|
||||
...(input.stageId ? { stageId: input.stageId } : {}),
|
||||
source: input.source ?? "mission",
|
||||
};
|
||||
|
||||
if (existing) {
|
||||
const [row] = await db.update(growConversations).set({
|
||||
title: input.title?.trim() || existing.title,
|
||||
metadata,
|
||||
updatedAt: new Date(),
|
||||
}).where(and(eq(growConversations.userId, input.userId), eq(growConversations.id, existing.id))).returning();
|
||||
if (!row) throw new Error("Failed to update mission conversation");
|
||||
return toConversation(row);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const [row] = await db.insert(growConversations).values({
|
||||
id: buildId("conversation"),
|
||||
userId: input.userId,
|
||||
title: input.title?.trim() || "Mission chat",
|
||||
metadata,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}).returning();
|
||||
if (!row) throw new Error("Failed to create mission conversation");
|
||||
return toConversation(row);
|
||||
}
|
||||
|
||||
export async function getConversationMetadataPg(userId: string, conversationId: string) {
|
||||
const [row] = await db
|
||||
.select({ metadata: growConversations.metadata })
|
||||
.from(growConversations)
|
||||
.where(and(eq(growConversations.userId, userId), eq(growConversations.id, conversationId)))
|
||||
.limit(1);
|
||||
return row?.metadata ?? {};
|
||||
}
|
||||
|
||||
export async function getConversationPg(userId: string, conversationId: string): Promise<GrowConversation | null> {
|
||||
const [row] = await db.select().from(growConversations).where(and(eq(growConversations.userId, userId), eq(growConversations.id, conversationId))).limit(1);
|
||||
return row ? toConversation(row) : null;
|
||||
@@ -274,20 +162,6 @@ export async function listActiveMissionsPg(userId: string) {
|
||||
return rows.map((row) => ({ mission: activeMissionFromRow(row), snapshot: missionSnapshotFromRow(row) }));
|
||||
}
|
||||
|
||||
export async function listActiveMissionsForPassiveReviewPg(opts: { userId?: string; limit?: number } = {}) {
|
||||
const conditions = [eq(growActiveMissions.status, "active")];
|
||||
if (opts.userId) conditions.push(eq(growActiveMissions.userId, opts.userId));
|
||||
const query = db
|
||||
.select()
|
||||
.from(growActiveMissions)
|
||||
.where(and(...conditions))
|
||||
.orderBy(asc(growActiveMissions.updatedAt));
|
||||
const rows = typeof opts.limit === "number" && opts.limit > 0
|
||||
? await query.limit(opts.limit)
|
||||
: await query;
|
||||
return rows.map((row) => ({ userId: row.userId, mission: activeMissionFromRow(row), snapshot: missionSnapshotFromRow(row) }));
|
||||
}
|
||||
|
||||
export async function getActiveMissionPg(userId: string, instanceId: string) {
|
||||
const [row] = await db.select().from(growActiveMissions).where(and(eq(growActiveMissions.userId, userId), eq(growActiveMissions.instanceId, instanceId))).limit(1);
|
||||
return row ? { mission: activeMissionFromRow(row), snapshot: missionSnapshotFromRow(row) } : null;
|
||||
|
||||
90
src/home/home-feed-agent.ts
Normal file
90
src/home/home-feed-agent.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { Output, generateText } from "ai";
|
||||
import { z } from "zod";
|
||||
import { getConversationModel } from "../actors/conversation/agent.js";
|
||||
import { config } from "../config.js";
|
||||
import { log } from "../log.js";
|
||||
import { isAllowedNotificationHref, MODULE_IDS, type HomeModuleId, type HomeNotification, type HomeUrgency } from "./types.js";
|
||||
|
||||
const notificationSchema = z.object({
|
||||
moduleId: z.enum(MODULE_IDS as [HomeModuleId, ...HomeModuleId[]]),
|
||||
title: z.string().min(4).max(72),
|
||||
subtitle: z.string().min(4).max(110),
|
||||
tag: z.string().min(2).max(14),
|
||||
urgency: z.enum(["now", "today", "soon", "calm"]),
|
||||
href: z.string().min(1),
|
||||
source: z.enum(["resume", "interview", "roleplay", "qscore", "mission", "social", "pathways", "rewards", "system"]),
|
||||
reason: z.string().max(160).optional(),
|
||||
});
|
||||
|
||||
const feedSchema = z.object({
|
||||
notifications: z.array(notificationSchema).min(6).max(24),
|
||||
});
|
||||
|
||||
const HOME_FEED_AGENT_TIMEOUT_MS = Number(process.env.HOME_FEED_AGENT_TIMEOUT_MS ?? 8000);
|
||||
|
||||
export type AgentHomeNotification = z.infer<typeof notificationSchema>;
|
||||
|
||||
const SYSTEM = `You are GrowQR's Home Feed Agent.
|
||||
Your job is to rank and rewrite dashboard notifications from real platform context.
|
||||
Keep them coherent, specific, and action-oriented. Do not invent unavailable products, scores, sessions, deadlines, companies, artifacts, or rewards.
|
||||
Every notification must point to one of these real dashboard routes:
|
||||
- /agents/resume for resume building, resume analysis, ATS, resume suggestions
|
||||
- /agents/interview for mock interview setup, interview session, interview review
|
||||
- /agents/roleplay for recruiter/manager/salary/stakeholder roleplay
|
||||
- /agents/qscore for Q Score/readiness explanations
|
||||
- /missions for mission progress, approvals, artifacts, next stages
|
||||
- /social for LinkedIn/social branding
|
||||
- /pathways for locked/coming-soon pathways
|
||||
- /rewards for locked/coming-soon rewards
|
||||
- /suggestions for broad onboarding/profile suggestions
|
||||
Use minimal iPhone-notification copy: title <= 72 chars, subtitle <= 110 chars, short tag <= 14 chars.
|
||||
Use urgency truthfully: now = needs immediate user action, today = useful today, soon = next few days, calm = informational.`;
|
||||
|
||||
function sanitizeHref(href: string, moduleId: HomeModuleId) {
|
||||
if (isAllowedNotificationHref(href)) return href;
|
||||
if (href.startsWith("/missions")) return "/missions/active";
|
||||
if (href.startsWith("/social")) return "/social";
|
||||
if (href.startsWith("/pathways")) return "/pathways";
|
||||
if (href.startsWith("/rewards")) return "/rewards";
|
||||
if (href.startsWith("/productivity")) return "/productivity";
|
||||
return moduleId === "productivity" ? "/productivity" : `/${moduleId}`;
|
||||
}
|
||||
|
||||
function stableId(prefix: string, index: number) {
|
||||
return `${prefix}-${index + 1}`;
|
||||
}
|
||||
|
||||
export async function refineHomeNotificationsWithAgent(input: {
|
||||
userId: string;
|
||||
context: Record<string, unknown>;
|
||||
seeds: Array<Omit<HomeNotification, "id" | "createdAt"> & { moduleId: HomeModuleId }>;
|
||||
}): Promise<Array<AgentHomeNotification & { id: string; createdAt: string }>> {
|
||||
if (!config.llmApiKey) return [];
|
||||
|
||||
try {
|
||||
const result = await generateText({
|
||||
model: getConversationModel(),
|
||||
output: Output.object({ schema: feedSchema }),
|
||||
system: SYSTEM,
|
||||
timeout: HOME_FEED_AGENT_TIMEOUT_MS,
|
||||
prompt: JSON.stringify({
|
||||
task: "Create coherent GrowQR home dashboard notifications from the provided service context and deterministic candidates.",
|
||||
userId: input.userId,
|
||||
serviceContext: input.context,
|
||||
deterministicCandidates: input.seeds,
|
||||
}),
|
||||
});
|
||||
|
||||
const now = new Date().toISOString();
|
||||
return result.output.notifications.map((n, index) => ({
|
||||
...n,
|
||||
href: sanitizeHref(n.href, n.moduleId),
|
||||
urgency: n.urgency as HomeUrgency,
|
||||
id: stableId("agent-home", index),
|
||||
createdAt: now,
|
||||
}));
|
||||
} catch (err) {
|
||||
log.warn({ err, userId: input.userId }, "home feed agent failed; using deterministic notifications");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { and, desc, eq, gt, inArray, isNull, or, sql } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { log } from "../log.js";
|
||||
import {
|
||||
growActiveMissions,
|
||||
growEvents,
|
||||
growHomeNotifications,
|
||||
growQscoreLatest,
|
||||
growQscoreProjectionState,
|
||||
missionArtifacts,
|
||||
missionServiceSessions,
|
||||
missionSuggestions,
|
||||
@@ -14,12 +15,8 @@ import {
|
||||
type NewGrowHomeNotification,
|
||||
} from "../db/schema.js";
|
||||
import { interviewService, resumeService, roleplayService } from "../services/product-service-clients.js";
|
||||
import { buildServiceLink } from "../services/service-registry.js";
|
||||
import { DEFAULT_QSCORE_ORG_ID, getQscoreFromService } from "../services/qscore-proxy.js";
|
||||
import { ensureOnboardingBaselineQscoreFromLedger } from "../events/onboarding-ledger.js";
|
||||
import { listAvailableMissionDefinitions } from "../missions/registry.js";
|
||||
import { listServiceCapabilities } from "../workflows/service-capabilities.js";
|
||||
import { buildCuratorSprint } from "../v1/curator/curator-store.js";
|
||||
import { ensureOnboardingBaselineQscore } from "../events/onboarding-qscore.js";
|
||||
import { refineHomeNotificationsWithAgent } from "./home-feed-agent.js";
|
||||
import {
|
||||
isAllowedNotificationHref,
|
||||
MODULE_IDS,
|
||||
@@ -34,17 +31,18 @@ import {
|
||||
|
||||
const FRESH_MS = 10 * 60 * 1000;
|
||||
const EXPIRY_MS = 24 * 60 * 60 * 1000;
|
||||
const MISSION_MODULE_LIMIT = 4;
|
||||
|
||||
const SERVICE_HREFS = {
|
||||
resume: buildServiceLink("resume-service", "workspace") ?? "/agents/resume",
|
||||
interview: buildServiceLink("interview-service", "discovery") ?? "/agents/interview",
|
||||
roleplay: buildServiceLink("roleplay-service", "discovery") ?? "/agents/roleplay",
|
||||
resume: "/agents/resume",
|
||||
interview: "/agents/interview",
|
||||
roleplay: "/agents/roleplay",
|
||||
qscore: "/agents/qscore",
|
||||
mission: "/missions/active",
|
||||
pathways: buildServiceLink("matchmaking-service", "jobs") ?? "/agents/matchmaking",
|
||||
social: "/social",
|
||||
pathways: "/pathways",
|
||||
rewards: "/rewards",
|
||||
suggestions: "/suggestions",
|
||||
productivity: buildServiceLink("courses-service", "catalog") ?? "/agents/courses",
|
||||
productivity: "/productivity",
|
||||
} as const;
|
||||
|
||||
type SeedNotification = Omit<HomeNotification, "id" | "createdAt"> & { moduleId: HomeModuleId; priority: number };
|
||||
@@ -99,24 +97,22 @@ function profileFromPreferences(preferences: Record<string, unknown>) {
|
||||
};
|
||||
}
|
||||
|
||||
function serviceHref(service: "resume" | "interview" | "roleplay", ctx: HomeContext, mission?: { instanceId?: string; missionId?: string; stageId?: string | null }) {
|
||||
function serviceHref(service: "resume" | "interview" | "roleplay" | "qscore", ctx: HomeContext, mission?: { instanceId?: string; missionId?: string; stageId?: string | null }) {
|
||||
const profile = profileFromPreferences(ctx.preferences);
|
||||
const serviceId = `${service}-service`;
|
||||
const pageId = service === "resume" ? "workspace" : "setup";
|
||||
return buildServiceLink(serviceId, pageId, {
|
||||
source: "home",
|
||||
missionInstanceId: mission?.instanceId,
|
||||
missionId: mission?.missionId,
|
||||
stageId: mission?.stageId ?? undefined,
|
||||
targetRole: profile.targetRole,
|
||||
role: profile.targetRole,
|
||||
targetCompany: profile.targetCompany !== "target company" ? profile.targetCompany : undefined,
|
||||
industry: profile.industry,
|
||||
focusAreas: profile.focusAreas.length ? profile.focusAreas.slice(0, 4).join(",") : undefined,
|
||||
weakSpots: profile.weakSpots.length ? profile.weakSpots.slice(0, 3).join(",") : undefined,
|
||||
jobDescription: profile.jobDescription?.slice(0, 900),
|
||||
type: service === "interview" ? "behavioral" : undefined,
|
||||
}) ?? SERVICE_HREFS[service];
|
||||
const params = new URLSearchParams({ source: "home" });
|
||||
if (mission?.instanceId) params.set("missionInstanceId", mission.instanceId);
|
||||
if (mission?.missionId) params.set("missionId", mission.missionId);
|
||||
if (mission?.stageId) params.set("stageId", mission.stageId);
|
||||
params.set("targetRole", profile.targetRole);
|
||||
if (profile.targetCompany !== "target company") params.set("targetCompany", profile.targetCompany);
|
||||
if (profile.industry) params.set("industry", profile.industry);
|
||||
if (profile.focusAreas.length) params.set("focusAreas", profile.focusAreas.slice(0, 4).join(","));
|
||||
if (profile.weakSpots.length) params.set("weakSpots", profile.weakSpots.slice(0, 3).join(","));
|
||||
if (profile.jobDescription) params.set("jobDescription", profile.jobDescription.slice(0, 900));
|
||||
if (service === "interview") return `/agents/interview/setup?${params.toString()}`;
|
||||
if (service === "roleplay") return `/agents/roleplay/setup?${params.toString()}`;
|
||||
if (service === "resume") return `/agents/resume?${params.toString()}`;
|
||||
return `/agents/qscore?${params.toString()}`;
|
||||
}
|
||||
|
||||
function sourceFromSuggestionRole(role: string): HomeSource {
|
||||
@@ -124,7 +120,7 @@ function sourceFromSuggestionRole(role: string): HomeSource {
|
||||
if (value.includes("resume")) return "resume";
|
||||
if (value.includes("roleplay")) return "roleplay";
|
||||
if (value.includes("interview")) return "interview";
|
||||
if (value.includes("match") || value.includes("pathway") || value.includes("job")) return "pathways";
|
||||
if (value.includes("q")) return "qscore";
|
||||
return "mission";
|
||||
}
|
||||
|
||||
@@ -169,55 +165,22 @@ function hasAnyRealActivity(ctx: HomeContext) {
|
||||
|
||||
function buildDayOneSeeds(): SeedNotification[] {
|
||||
const seeds: SeedNotification[] = [];
|
||||
|
||||
const missions = listAvailableMissionDefinitions();
|
||||
for (const [index, mission] of missions.slice(0, 3).entries()) {
|
||||
const firstServiceModule = mission.modules.find((module) => module.execution === "service" && module.service);
|
||||
pushSeed(seeds, {
|
||||
moduleId: "missions",
|
||||
title: mission.shortTitle || mission.title,
|
||||
subtitle: mission.promise,
|
||||
tag: mission.urgency === "high" ? "High" : mission.estimatedDuration,
|
||||
urgency: mission.urgency === "high" ? "today" : "soon",
|
||||
href: `/missions/available?missionId=${encodeURIComponent(mission.missionId)}`,
|
||||
source: "mission",
|
||||
reason: firstServiceModule ? `Registry workflow using ${firstServiceModule.role}.` : "Registry workflow.",
|
||||
priority: 92 - index * 4,
|
||||
});
|
||||
}
|
||||
|
||||
const services = listServiceCapabilities().filter((service) => service.enabled);
|
||||
const serviceCards = [
|
||||
{ id: "resume-service", moduleId: "productivity" as const, href: SERVICE_HREFS.resume, source: "resume" as const, urgency: "today" as const },
|
||||
{ id: "interview-service", moduleId: "productivity" as const, href: SERVICE_HREFS.interview, source: "interview" as const, urgency: "today" as const },
|
||||
{ id: "roleplay-service", moduleId: "productivity" as const, href: SERVICE_HREFS.roleplay, source: "roleplay" as const, urgency: "soon" as const },
|
||||
{ id: "courses-service", moduleId: "productivity" as const, href: SERVICE_HREFS.productivity, source: "system" as const, urgency: "soon" as const },
|
||||
{ id: "matchmaking-service", moduleId: "pathways" as const, href: SERVICE_HREFS.pathways, source: "pathways" as const, urgency: "today" as const },
|
||||
];
|
||||
|
||||
for (const [index, card] of serviceCards.entries()) {
|
||||
const service = services.find((item) => item.id === card.id);
|
||||
if (!service) continue;
|
||||
pushSeed(seeds, {
|
||||
moduleId: card.moduleId,
|
||||
title: service.name,
|
||||
subtitle: service.operations.slice(0, 3).join(", ") || "Registered GrowQR service capability.",
|
||||
tag: service.featureId?.replaceAll("-", " ").slice(0, 14) || "Service",
|
||||
urgency: card.urgency,
|
||||
href: card.href,
|
||||
source: card.source,
|
||||
reason: `From service registry: ${service.promptModulePath ?? service.id}.`,
|
||||
priority: 84 - index * 3,
|
||||
});
|
||||
}
|
||||
|
||||
pushSeed(seeds, { moduleId: "suggestions", title: "Set your target outcome", subtitle: "One role or career goal sharpens every registry mission and service handoff.", tag: "Profile", urgency: "today", href: SERVICE_HREFS.suggestions, source: "system", priority: 80 });
|
||||
pushSeed(seeds, { moduleId: "rewards", title: "Rewards unlock from mission progress", subtitle: "Complete registry-backed mission stages to earn streak and coin progress.", tag: "Locked", urgency: "calm", href: SERVICE_HREFS.rewards, source: "rewards", priority: 35 });
|
||||
pushSeed(seeds, { moduleId: "suggestions", title: "Start with your Q Score", subtitle: "A quick readiness scan calibrates resume, interview, and roleplay tips.", tag: "Start", urgency: "now", href: SERVICE_HREFS.qscore, source: "qscore", priority: 90 });
|
||||
pushSeed(seeds, { moduleId: "suggestions", title: "Add your target role", subtitle: "One role goal makes every recommendation sharper.", tag: "Profile", urgency: "today", href: SERVICE_HREFS.suggestions, source: "system", priority: 80 });
|
||||
pushSeed(seeds, { moduleId: "missions", title: "Explore Interview-to-Offer", subtitle: "A guided mission connects resume fit, mock practice, and readiness scoring.", tag: "Browse", urgency: "today", href: SERVICE_HREFS.mission, source: "mission", priority: 80 });
|
||||
pushSeed(seeds, { moduleId: "missions", title: "No approvals pending yet", subtitle: "Start a mission and this tile will track missing steps and progress.", tag: "Quiet", urgency: "calm", href: SERVICE_HREFS.mission, source: "mission", priority: 55 });
|
||||
pushSeed(seeds, { moduleId: "social", title: "Connect LinkedIn when ready", subtitle: "Social branding recommendations unlock after your profile is available.", tag: "Setup", urgency: "soon", href: SERVICE_HREFS.social, source: "social", priority: 60 });
|
||||
pushSeed(seeds, { moduleId: "social", title: "Build proof before posting", subtitle: "Resume and mock interview artifacts can become stronger featured pins.", tag: "Proof", urgency: "calm", href: SERVICE_HREFS.social, source: "social", priority: 50 });
|
||||
pushSeed(seeds, { moduleId: "pathways", title: "Pathways are warming up", subtitle: "Complete resume + interview activity to unlock better route recommendations.", tag: "Soon", urgency: "calm", href: SERVICE_HREFS.pathways, source: "pathways", priority: 40 });
|
||||
pushSeed(seeds, { moduleId: "productivity", title: "Open Resume Builder", subtitle: "Upload or create a resume to generate ATS and content recommendations.", tag: "Resume", urgency: "now", href: SERVICE_HREFS.resume, source: "resume", priority: 85 });
|
||||
pushSeed(seeds, { moduleId: "productivity", title: "Try a 10-minute mock interview", subtitle: "The interview service creates a role-aware live practice session.", tag: "Mock", urgency: "soon", href: SERVICE_HREFS.interview, source: "interview", priority: 70 });
|
||||
pushSeed(seeds, { moduleId: "productivity", title: "Roleplay is available for pressure practice", subtitle: "Use it for recruiter screens, salary asks, or manager conversations.", tag: "Roleplay", urgency: "calm", href: SERVICE_HREFS.roleplay, source: "roleplay", priority: 55 });
|
||||
pushSeed(seeds, { moduleId: "rewards", title: "Rewards unlock after activity", subtitle: "Finish readiness actions to start earning demo streaks and perks.", tag: "Locked", urgency: "calm", href: SERVICE_HREFS.rewards, source: "rewards", priority: 35 });
|
||||
return seeds;
|
||||
}
|
||||
|
||||
function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
const seeds = buildDayOneSeeds().filter((seed) => seed.moduleId === "pathways" || seed.moduleId === "rewards");
|
||||
const seeds = buildDayOneSeeds().filter((seed) => seed.moduleId === "pathways" || seed.moduleId === "social" || seed.moduleId === "rewards");
|
||||
const profile = profileFromPreferences(ctx.preferences);
|
||||
const qscore = ctx.qscore?.score ?? Math.round(ctx.qscoreSignals.reduce((sum, s) => sum + s.score, 0) / Math.max(ctx.qscoreSignals.length, 1));
|
||||
const ats = latestScore(ctx.qscoreSignals, "resume.ats_compatibility");
|
||||
@@ -230,14 +193,7 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
const roleplayReview = serviceEvent(ctx, "roleplay.", "review");
|
||||
const resumeAnalysis = serviceEvent(ctx, "resume.", "analysis");
|
||||
|
||||
const visibleMissionSuggestions = ctx.missionSuggestions
|
||||
.filter((suggestion) => {
|
||||
const haystack = `${suggestion.role} ${suggestion.title} ${suggestion.body} ${suggestion.ctaLabel} ${suggestion.ctaHref}`.toLowerCase();
|
||||
return !haystack.includes("qscore") && !haystack.includes("q score") && !haystack.includes("assessment") && !haystack.includes("/agents/qscore");
|
||||
})
|
||||
.slice(0, 5);
|
||||
|
||||
for (const suggestion of visibleMissionSuggestions) {
|
||||
for (const suggestion of ctx.missionSuggestions.slice(0, 5)) {
|
||||
const mission = ctx.activeMissions.find((item) => item.instanceId === suggestion.missionInstanceId);
|
||||
const source = sourceFromSuggestionRole(suggestion.role);
|
||||
const href = sanitizeHref(suggestion.ctaHref, mission ? `/missions/active?missionInstanceId=${encodeURIComponent(mission.instanceId)}` : SERVICE_HREFS.mission);
|
||||
@@ -280,13 +236,13 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
|
||||
if (ctx.qscore || ctx.qscoreSignals.length) {
|
||||
pushSeed(seeds, {
|
||||
moduleId: "pathways",
|
||||
title: qscore >= 80 ? "Review your best job matches" : "Find better-fit job matches",
|
||||
subtitle: qscore >= 80 ? `Your profile signals are strong enough to compare matched roles for ${profile.targetRole}.` : `Use resume and interview signals to surface roles that fit ${profile.targetRole}.`,
|
||||
tag: "Matches",
|
||||
moduleId: "suggestions",
|
||||
title: qscore >= 80 ? "Protect your Q Score momentum" : "Raise your Q Score next",
|
||||
subtitle: qscore >= 80 ? `Readiness is trending at ${qscore}. Keep one proof action moving for ${profile.targetRole}.` : `Current estimate is ${qscore || 64}. Resume + mock practice are fastest for ${profile.targetRole}.`,
|
||||
tag: "Q Score",
|
||||
urgency: qscore >= 80 ? "today" : "now",
|
||||
href: SERVICE_HREFS.pathways,
|
||||
source: "pathways",
|
||||
href: serviceHref("qscore", ctx),
|
||||
source: "qscore",
|
||||
priority: 95,
|
||||
});
|
||||
}
|
||||
@@ -304,7 +260,7 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
});
|
||||
}
|
||||
|
||||
for (const mission of ctx.activeMissions.slice(0, MISSION_MODULE_LIMIT)) {
|
||||
for (const mission of ctx.activeMissions.slice(0, 3)) {
|
||||
pushSeed(seeds, {
|
||||
moduleId: "missions",
|
||||
title: `${mission.title} — ${mission.progressPercent}%`,
|
||||
@@ -331,6 +287,17 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
});
|
||||
}
|
||||
|
||||
pushSeed(seeds, {
|
||||
moduleId: "social",
|
||||
title: "Social branding is coming soon",
|
||||
subtitle: "Mission proof and resume artifacts will become profile and LinkedIn nudges here.",
|
||||
tag: "Soon",
|
||||
urgency: "calm",
|
||||
href: SERVICE_HREFS.social,
|
||||
source: "social",
|
||||
priority: 38,
|
||||
});
|
||||
|
||||
if (resumeAnalysis || resumeSession || ats !== undefined) {
|
||||
pushSeed(seeds, {
|
||||
moduleId: "productivity",
|
||||
@@ -374,7 +341,7 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
}
|
||||
|
||||
if (!ctx.activeMissions.length) {
|
||||
pushSeed(seeds, { moduleId: "missions", title: "Start Interview-to-Offer", subtitle: `Bundle resume fit, mock practice, and matched roles for ${profile.targetRole}.`, tag: "Begin", urgency: "today", href: "/missions/available", source: "mission", priority: 80 });
|
||||
pushSeed(seeds, { moduleId: "missions", title: "Start Interview-to-Offer", subtitle: `Bundle resume fit, mock practice, and Q Score deltas for ${profile.targetRole}.`, tag: "Begin", urgency: "today", href: "/missions/available", source: "mission", priority: 80 });
|
||||
}
|
||||
|
||||
return seeds;
|
||||
@@ -382,22 +349,17 @@ function buildDynamicSeeds(ctx: HomeContext): SeedNotification[] {
|
||||
|
||||
async function collectContext(userId: string, input: { userProfile?: Record<string, unknown>; preferences?: Record<string, unknown> } = {}): Promise<HomeContext> {
|
||||
const [user] = await db.select({ id: users.id, email: users.email, displayName: users.displayName }).from(users).where(eq(users.id, userId)).limit(1);
|
||||
const qscoreResult = await getQscoreFromService(userId, DEFAULT_QSCORE_ORG_ID);
|
||||
const breakdown = qscoreResult?.breakdown ?? {};
|
||||
const breakdownSignals = Array.isArray(breakdown.signals) ? breakdown.signals : [];
|
||||
const qscoreSignals = breakdownSignals.map((item) => {
|
||||
const s = isRecord(item) ? item : {};
|
||||
return {
|
||||
signalId: typeof s.signalId === "string" ? s.signalId : typeof s.signal_id === "string" ? s.signal_id : "",
|
||||
score: typeof s.score === "number" ? s.score : 0,
|
||||
source: typeof s.source === "string" ? s.source : null,
|
||||
updatedAt: new Date(typeof s.updatedAt === "string" ? s.updatedAt : (qscoreResult?.calculated_at ?? new Date().toISOString())),
|
||||
};
|
||||
});
|
||||
const [qscore] = await db.select().from(growQscoreProjectionState).where(eq(growQscoreProjectionState.userId, userId)).limit(1);
|
||||
const qscoreSignals = await db
|
||||
.select({ signalId: growQscoreLatest.signalId, score: growQscoreLatest.score, source: growQscoreLatest.source, updatedAt: growQscoreLatest.updatedAt })
|
||||
.from(growQscoreLatest)
|
||||
.where(eq(growQscoreLatest.userId, userId))
|
||||
.orderBy(desc(growQscoreLatest.updatedAt))
|
||||
.limit(30);
|
||||
const activeMissions = await db
|
||||
.select({ instanceId: growActiveMissions.instanceId, missionId: growActiveMissions.missionId, title: growActiveMissions.title, status: growActiveMissions.status, progressPercent: growActiveMissions.progressPercent, currentStageId: growActiveMissions.currentStageId, updatedAt: growActiveMissions.updatedAt })
|
||||
.from(growActiveMissions)
|
||||
.where(and(eq(growActiveMissions.userId, userId), eq(growActiveMissions.status, "active")))
|
||||
.where(eq(growActiveMissions.userId, userId))
|
||||
.orderBy(desc(growActiveMissions.updatedAt))
|
||||
.limit(6);
|
||||
const suggestions = await db
|
||||
@@ -450,12 +412,12 @@ async function collectContext(userId: string, input: { userProfile?: Record<stri
|
||||
|
||||
return {
|
||||
user,
|
||||
qscore: qscoreResult
|
||||
qscore: qscore
|
||||
? {
|
||||
score: qscoreResult.q_score,
|
||||
signalCount: typeof breakdown.signalCount === "number" ? breakdown.signalCount : qscoreSignals.length,
|
||||
summary: typeof breakdown.summary === "string" ? breakdown.summary : null,
|
||||
dimensions: isRecord(breakdown.dimensions) ? breakdown.dimensions : isRecord(qscoreResult.quotients) ? qscoreResult.quotients : null,
|
||||
score: qscore.score,
|
||||
signalCount: qscore.signalCount,
|
||||
summary: qscore.summary,
|
||||
dimensions: isRecord(qscore.dimensions) ? qscore.dimensions : null,
|
||||
}
|
||||
: undefined,
|
||||
qscoreSignals,
|
||||
@@ -494,20 +456,6 @@ async function readPersistedNotifications(userId: string) {
|
||||
.limit(60);
|
||||
}
|
||||
|
||||
function hasLegacyMockSeed(rows: GrowHomeNotificationRow[]) {
|
||||
const legacyTitles = new Set([
|
||||
"Complete your QX self-check",
|
||||
"Create your interview room",
|
||||
"Browse 1 career pathway",
|
||||
"Explore Interview-to-Offer",
|
||||
"Pathways are warming up",
|
||||
"Open Resume Builder",
|
||||
"Try a 10-minute mock interview",
|
||||
"Rewards unlock after activity",
|
||||
]);
|
||||
return rows.some((row) => legacyTitles.has(row.title));
|
||||
}
|
||||
|
||||
async function replaceGeneratedNotifications(userId: string, notifications: Array<SeedNotification>, generatedBy: "deterministic" | "agent") {
|
||||
await db
|
||||
.delete(growHomeNotifications)
|
||||
@@ -545,16 +493,25 @@ function ensureCoverage(seeds: SeedNotification[], fallback: SeedNotification[])
|
||||
}
|
||||
|
||||
function moduleCount(moduleId: HomeModuleId, notifications: HomeNotification[], ctx: HomeContext, mode: HomeFeedResponse["mode"]) {
|
||||
if (mode === "demo") {
|
||||
if (moduleId === "missions") return "1 active";
|
||||
if (moduleId === "productivity") return "4 updates";
|
||||
if (moduleId === "social") return "3 ready";
|
||||
if (moduleId === "pathways") return "Soon";
|
||||
if (moduleId === "rewards") return "Demo";
|
||||
return String(notifications.length);
|
||||
}
|
||||
if (moduleId === "missions") {
|
||||
return mode === "day1" && notifications.length === 0 ? "0" : String(Math.min(notifications.length, MISSION_MODULE_LIMIT));
|
||||
if (ctx.activeMissions.length) return `${ctx.activeMissions.length} active`;
|
||||
return mode === "day1" ? "0" : String(notifications.length);
|
||||
}
|
||||
if (moduleId === "productivity") {
|
||||
const active = ctx.sessions.filter((s) => s.status === "active" || s.status === "configured" || s.status === "processing").length;
|
||||
return active ? `${active} active` : String(notifications.length);
|
||||
}
|
||||
if (moduleId === "pathways") return mode === "day1" ? "Soon" : "Locked";
|
||||
if (moduleId === "rewards") return mode === "day1" ? "0" : "Demo";
|
||||
if (moduleId === "social") return mode === "day1" ? "Setup" : `${notifications.length} updates`;
|
||||
if (moduleId === "pathways") return "Soon";
|
||||
if (moduleId === "rewards") return "Soon";
|
||||
if (moduleId === "social") return "Soon";
|
||||
return String(notifications.length);
|
||||
}
|
||||
|
||||
@@ -568,11 +525,10 @@ function buildModules(rows: GrowHomeNotificationRow[], ctx: HomeContext, mode: H
|
||||
|
||||
return MODULE_IDS.map((moduleId) => {
|
||||
const notifications = byModule.get(moduleId) ?? [];
|
||||
const visibleNotifications = moduleId === "missions" ? notifications.slice(0, MISSION_MODULE_LIMIT) : notifications;
|
||||
return {
|
||||
...MODULE_META[moduleId],
|
||||
count: moduleCount(moduleId, visibleNotifications, ctx, mode),
|
||||
notifications: visibleNotifications,
|
||||
count: moduleCount(moduleId, notifications, ctx, mode),
|
||||
notifications,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -604,16 +560,16 @@ async function buildIdentity(ctx: HomeContext) {
|
||||
}
|
||||
|
||||
export async function getHomeFeed(userId: string, opts: { refresh?: boolean; userProfile?: Record<string, unknown>; preferences?: Record<string, unknown> } = {}): Promise<HomeFeedResponse> {
|
||||
await ensureOnboardingBaselineQscoreFromLedger(userId);
|
||||
await buildCuratorSprint(userId);
|
||||
await ensureOnboardingBaselineQscore(userId, opts.preferences);
|
||||
const ctx = await collectContext(userId, { userProfile: opts.userProfile, preferences: opts.preferences });
|
||||
const persisted = await readPersistedNotifications(userId);
|
||||
const newest = persisted[0]?.createdAt?.getTime() ?? 0;
|
||||
const hasLegacyMock = hasLegacyMockSeed(persisted);
|
||||
const hasDemo = persisted.some((row) => row.generatedBy === "demo");
|
||||
const fresh = newest > Date.now() - FRESH_MS;
|
||||
const hasModuleCoverage = MODULE_IDS.every((moduleId) => persisted.some((row) => row.moduleId === moduleId));
|
||||
|
||||
if (persisted.length && !hasLegacyMock && !opts.refresh && fresh) {
|
||||
const mode = hasAnyRealActivity(ctx) ? "dynamic" : "day1";
|
||||
if (persisted.length && hasModuleCoverage && (hasDemo || (!opts.refresh && fresh))) {
|
||||
const mode = hasDemo ? "demo" : hasAnyRealActivity(ctx) ? "dynamic" : "day1";
|
||||
return {
|
||||
generatedAt: new Date().toISOString(),
|
||||
mode,
|
||||
@@ -624,7 +580,40 @@ export async function getHomeFeed(userId: string, opts: { refresh?: boolean; use
|
||||
|
||||
const dayOneSeeds = buildDayOneSeeds();
|
||||
const deterministic = hasAnyRealActivity(ctx) ? buildDynamicSeeds(ctx) : dayOneSeeds;
|
||||
await replaceGeneratedNotifications(userId, ensureCoverage(deterministic, dayOneSeeds), "deterministic");
|
||||
const agentNotifications = await refineHomeNotificationsWithAgent({
|
||||
userId,
|
||||
context: {
|
||||
qscore: ctx.qscore,
|
||||
qscoreSignals: ctx.qscoreSignals,
|
||||
activeMissions: ctx.activeMissions,
|
||||
sessions: ctx.sessions,
|
||||
artifacts: ctx.artifacts,
|
||||
recentEvents: ctx.events,
|
||||
serviceStates: ctx.serviceStates,
|
||||
missionSuggestions: ctx.missionSuggestions,
|
||||
userProfile: ctx.userProfile,
|
||||
preferences: ctx.preferences,
|
||||
routeRules: SERVICE_HREFS,
|
||||
},
|
||||
seeds: deterministic,
|
||||
});
|
||||
|
||||
const generatedBy = agentNotifications.length ? "agent" : "deterministic";
|
||||
const generatedSeeds: SeedNotification[] = agentNotifications.length
|
||||
? agentNotifications.map((n, index) => ({
|
||||
moduleId: n.moduleId,
|
||||
title: n.title,
|
||||
subtitle: n.subtitle,
|
||||
tag: n.tag,
|
||||
urgency: n.urgency,
|
||||
href: n.href,
|
||||
source: n.source,
|
||||
reason: n.reason,
|
||||
priority: 100 - index,
|
||||
}))
|
||||
: deterministic;
|
||||
|
||||
await replaceGeneratedNotifications(userId, ensureCoverage(generatedSeeds, dayOneSeeds), generatedBy);
|
||||
const rows = await readPersistedNotifications(userId);
|
||||
const mode = hasAnyRealActivity(ctx) ? "dynamic" : "day1";
|
||||
|
||||
|
||||
165
src/home/seed-demo-home.ts
Normal file
165
src/home/seed-demo-home.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import "dotenv/config";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { growActiveMissions, growHomeNotifications, growQscoreLatest, growQscoreProjectionState, missionArtifacts, missionServiceSessions, users, type NewGrowHomeNotification } from "../db/schema.js";
|
||||
import type { HomeModuleId, HomeSource, HomeUrgency } from "./types.js";
|
||||
|
||||
type DemoNotification = {
|
||||
moduleId: HomeModuleId;
|
||||
title: string;
|
||||
subtitle: string;
|
||||
tag: string;
|
||||
urgency: HomeUrgency;
|
||||
href: string;
|
||||
source: HomeSource;
|
||||
priority: number;
|
||||
reason?: string;
|
||||
};
|
||||
|
||||
const demoNotifications: DemoNotification[] = [
|
||||
{ moduleId: "suggestions", title: "Approve resume v6", subtitle: "ATS 78 → 86 · two PM bullets are ready to ship.", tag: "Urgent", urgency: "now", href: "/agents/resume", source: "resume", priority: 120 },
|
||||
{ moduleId: "suggestions", title: "Mock follow-up at 6:30 PM", subtitle: "Behavioral drill based on yesterday's review gaps.", tag: "Today", urgency: "today", href: "/agents/interview", source: "interview", priority: 116 },
|
||||
{ moduleId: "suggestions", title: "Practice recruiter pushback", subtitle: "Roleplay can tighten your compensation and notice-period answer.", tag: "Soon", urgency: "soon", href: "/agents/roleplay", source: "roleplay", priority: 110 },
|
||||
|
||||
{ moduleId: "missions", title: "Interview-to-Offer — 72%", subtitle: "Resume fit done · mock review ready · final report locked.", tag: "Active", urgency: "today", href: "/missions", source: "mission", priority: 115 },
|
||||
{ moduleId: "missions", title: "2 approvals pending", subtitle: "Resume v6 and Mock #4 feedback need your confirmation.", tag: "Action", urgency: "now", href: "/missions", source: "mission", priority: 113 },
|
||||
{ moduleId: "missions", title: "Final readiness unlock", subtitle: "Complete one roleplay recovery drill to generate the final checklist.", tag: "Next", urgency: "soon", href: "/missions", source: "mission", priority: 106 },
|
||||
|
||||
{ moduleId: "social", title: "LinkedIn headline v3 ready", subtitle: "Clearer target: Product Intern · FinTech · Growth Systems.", tag: "Ready", urgency: "today", href: "/social", source: "social", priority: 104 },
|
||||
{ moduleId: "social", title: "Featured section has 3 proof pins", subtitle: "Use resume scan, mock review, and Q Score delta as credibility blocks.", tag: "Proof", urgency: "soon", href: "/social", source: "social", priority: 100 },
|
||||
{ moduleId: "social", title: "Banner options queued", subtitle: "Three calm orange/blue layouts are waiting for review.", tag: "Brand", urgency: "soon", href: "/social", source: "social", priority: 96 },
|
||||
|
||||
{ moduleId: "pathways", title: "Pathways stay locked for demo", subtitle: "Resume + interview data is enough; pathway service is not enabled yet.", tag: "Soon", urgency: "calm", href: "/pathways", source: "pathways", priority: 70 },
|
||||
{ moduleId: "pathways", title: "PM SaaS route predicted", subtitle: "Directional only until the pathways service is connected.", tag: "Preview", urgency: "calm", href: "/pathways", source: "pathways", priority: 68 },
|
||||
|
||||
{ moduleId: "productivity", title: "Resume ATS 86", subtitle: "Keyword relevance +9 after JD tailoring. Open Resume Builder.", tag: "Resume", urgency: "today", href: "/agents/resume", source: "resume", priority: 118 },
|
||||
{ moduleId: "productivity", title: "Interview review is ready", subtitle: "Overall 82 · storytelling and concise examples need one more drill.", tag: "Mock", urgency: "now", href: "/agents/interview", source: "interview", priority: 117 },
|
||||
{ moduleId: "productivity", title: "Roleplay recruiter screen", subtitle: "Scenario ready · handle salary range and start-date objections.", tag: "Roleplay", urgency: "soon", href: "/agents/roleplay", source: "roleplay", priority: 105 },
|
||||
{ moduleId: "productivity", title: "Q Score now 86", subtitle: "Signals: resume ATS, mock review, and roleplay completion.", tag: "Q Score", urgency: "today", href: "/agents/qscore", source: "qscore", priority: 102 },
|
||||
|
||||
{ moduleId: "rewards", title: "Mentor pass preview", subtitle: "Demo reward only · would unlock after 3 completed readiness actions.", tag: "Demo", urgency: "calm", href: "/rewards", source: "rewards", priority: 60 },
|
||||
{ moduleId: "rewards", title: "Resume Pro credit queued", subtitle: "Tabled for v1; shown as a consistent notification tile.", tag: "Soon", urgency: "calm", href: "/rewards", source: "rewards", priority: 58 },
|
||||
];
|
||||
|
||||
export async function seedDemoHome(userId: string) {
|
||||
await db.insert(users).values({ id: userId, email: `${userId}@demo.local`, displayName: "Demo User" }).onConflictDoNothing();
|
||||
|
||||
await db
|
||||
.delete(growHomeNotifications)
|
||||
.where(and(eq(growHomeNotifications.userId, userId), inArray(growHomeNotifications.generatedBy, ["demo", "agent", "deterministic"])));
|
||||
|
||||
const expiresAt = new Date(Date.now() + 14 * 24 * 60 * 60 * 1000);
|
||||
const rows: NewGrowHomeNotification[] = demoNotifications.map((n) => ({
|
||||
userId,
|
||||
moduleId: n.moduleId,
|
||||
title: n.title,
|
||||
subtitle: n.subtitle,
|
||||
tag: n.tag,
|
||||
urgency: n.urgency,
|
||||
href: n.href,
|
||||
source: n.source,
|
||||
priority: n.priority,
|
||||
generatedBy: "demo",
|
||||
reason: n.reason ?? "Seeded for multi-day dashboard demo.",
|
||||
status: "active",
|
||||
expiresAt,
|
||||
updatedAt: new Date(),
|
||||
}));
|
||||
await db.insert(growHomeNotifications).values(rows);
|
||||
|
||||
await db.insert(growQscoreProjectionState).values({
|
||||
userId,
|
||||
score: 86,
|
||||
signalCount: 14,
|
||||
dimensions: { strengths: ["resume.ats_compatibility", "interview.overall_score"], growth: ["interview.response_clarity", "roleplay.problem_resolution"] },
|
||||
summary: "Demo readiness score from resume, interview, and roleplay signals.",
|
||||
updatedAt: new Date(),
|
||||
}).onConflictDoUpdate({
|
||||
target: growQscoreProjectionState.userId,
|
||||
set: {
|
||||
score: 86,
|
||||
signalCount: 14,
|
||||
dimensions: { strengths: ["resume.ats_compatibility", "interview.overall_score"], growth: ["interview.response_clarity", "roleplay.problem_resolution"] },
|
||||
summary: "Demo readiness score from resume, interview, and roleplay signals.",
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
const signalRows = [
|
||||
["resume.uploaded", 100, "resume-service"],
|
||||
["resume.ats_compatibility", 86, "resume-service"],
|
||||
["resume.keyword_relevance", 84, "resume-service"],
|
||||
["interview.completed", 100, "interview-service"],
|
||||
["interview.overall_score", 82, "interview-service"],
|
||||
["interview.response_clarity", 74, "interview-service"],
|
||||
["roleplay.completed", 100, "roleplay-service"],
|
||||
["roleplay.communication_effectiveness", 79, "roleplay-service"],
|
||||
] as const;
|
||||
|
||||
for (const [signalId, score, source] of signalRows) {
|
||||
await db.insert(growQscoreLatest).values({
|
||||
userId,
|
||||
signalId,
|
||||
score,
|
||||
present: true,
|
||||
source,
|
||||
raw: { demo: true },
|
||||
occurredAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}).onConflictDoUpdate({
|
||||
target: [growQscoreLatest.userId, growQscoreLatest.signalId],
|
||||
set: { score, present: true, source, raw: { demo: true }, occurredAt: new Date(), updatedAt: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
await db.insert(growActiveMissions).values({
|
||||
instanceId: `demo-interview-to-offer-${userId}`,
|
||||
userId,
|
||||
missionId: "interview-to-offer",
|
||||
workflowId: "interview-to-offer",
|
||||
actorType: "interviewToOfferMissionActor",
|
||||
title: "Interview-to-Offer Accelerator",
|
||||
shortTitle: "Interview-to-Offer",
|
||||
status: "active",
|
||||
progressPercent: 72,
|
||||
currentStageId: "mock-interview-review",
|
||||
goal: "Land a PM internship offer",
|
||||
mission: { demo: true, missionId: "interview-to-offer" },
|
||||
snapshot: { demo: true, progressPercent: 72 },
|
||||
updatedAt: new Date(),
|
||||
}).onConflictDoUpdate({
|
||||
target: growActiveMissions.instanceId,
|
||||
set: { status: "active", progressPercent: 72, currentStageId: "mock-interview-review", snapshot: { demo: true, progressPercent: 72 }, updatedAt: new Date() },
|
||||
});
|
||||
|
||||
await db.insert(missionServiceSessions).values([
|
||||
{ userId, missionInstanceId: `demo-interview-to-offer-${userId}`, missionId: "interview-to-offer", stageId: "resume-fit-scan", serviceId: "resume-service", externalId: `demo-resume-${userId}`, status: "completed", metadata: { demo: true, ats: 86 }, updatedAt: new Date() },
|
||||
{ userId, missionInstanceId: `demo-interview-to-offer-${userId}`, missionId: "interview-to-offer", stageId: "mock-interview", serviceId: "interview-service", externalId: `demo-interview-${userId}`, status: "review_ready", metadata: { demo: true, overall: 82 }, updatedAt: new Date() },
|
||||
{ userId, missionInstanceId: `demo-interview-to-offer-${userId}`, missionId: "interview-to-offer", stageId: "roleplay-recovery", serviceId: "roleplay-service", externalId: `demo-roleplay-${userId}`, status: "configured", metadata: { demo: true }, updatedAt: new Date() },
|
||||
]).onConflictDoNothing();
|
||||
|
||||
await db.insert(missionArtifacts).values([
|
||||
{ userId, missionInstanceId: `demo-interview-to-offer-${userId}`, missionId: "interview-to-offer", stageId: "resume-fit-scan", serviceId: "resume-service", externalId: `demo-resume-${userId}`, type: "resume_fit_scan", title: "Resume Fit Scan", status: "ready", summary: "ATS 86 with stronger PM keywords.", metadata: { demo: true }, updatedAt: new Date() },
|
||||
{ userId, missionInstanceId: `demo-interview-to-offer-${userId}`, missionId: "interview-to-offer", stageId: "mock-interview", serviceId: "interview-service", externalId: `demo-interview-${userId}`, type: "mock_interview_review", title: "Mock Interview Review", status: "ready", summary: "Overall 82; improve story brevity and tradeoff framing.", metadata: { demo: true }, updatedAt: new Date() },
|
||||
]).onConflictDoNothing();
|
||||
|
||||
return { inserted: rows.length, userId };
|
||||
}
|
||||
|
||||
const entry = process.argv[1] ?? "";
|
||||
if (entry.endsWith("seed-demo-home.ts") || entry.endsWith("seed-demo-home.js")) {
|
||||
const userId = process.env.DEMO_USER_ID;
|
||||
if (!userId) {
|
||||
console.error("Set DEMO_USER_ID to seed demo home notifications.");
|
||||
process.exit(1);
|
||||
}
|
||||
seedDemoHome(userId)
|
||||
.then((result) => {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
@@ -34,7 +34,7 @@ export type HomeIdentity = {
|
||||
|
||||
export type HomeFeedResponse = {
|
||||
generatedAt: string;
|
||||
mode: "day1" | "dynamic";
|
||||
mode: "day1" | "dynamic" | "demo";
|
||||
identity: HomeIdentity;
|
||||
modules: HomeModule[];
|
||||
};
|
||||
@@ -42,55 +42,43 @@ export type HomeFeedResponse = {
|
||||
export const MODULE_META: Record<HomeModuleId, Omit<HomeModule, "count" | "notifications">> = {
|
||||
suggestions: { id: "suggestions", label: "Today's Queue", href: "/suggestions", accent: "orange" },
|
||||
missions: { id: "missions", label: "Missions", href: "/missions", accent: "orange" },
|
||||
social: { id: "social", label: "Social Branding", href: "/agents/social-branding", accent: "blue" },
|
||||
pathways: { id: "pathways", label: "Pathways", href: "/agents/matchmaking", accent: "teal" },
|
||||
productivity: { id: "productivity", label: "Interview · Roleplay · Resume", href: "/agents", accent: "orange" },
|
||||
social: { id: "social", label: "Social Branding", href: "/social", accent: "blue" },
|
||||
pathways: { id: "pathways", label: "Pathways", href: "/pathways", accent: "teal" },
|
||||
productivity: { id: "productivity", label: "Productivity", href: "/agents", accent: "orange" },
|
||||
rewards: { id: "rewards", label: "Rewards", href: "/rewards", accent: "amber" },
|
||||
};
|
||||
|
||||
export const MODULE_IDS: HomeModuleId[] = [
|
||||
"suggestions",
|
||||
"missions",
|
||||
"social",
|
||||
"pathways",
|
||||
"productivity",
|
||||
"rewards",
|
||||
];
|
||||
export const MODULE_IDS: HomeModuleId[] = ["suggestions", "missions", "pathways", "productivity", "social", "rewards"];
|
||||
|
||||
export const ALLOWED_NOTIFICATION_HREFS = new Set([
|
||||
"/suggestions",
|
||||
"/missions",
|
||||
"/missions/active",
|
||||
"/missions/available",
|
||||
"/agents/social-branding",
|
||||
"/agents/matchmaking",
|
||||
"/agents",
|
||||
"/social",
|
||||
"/pathways",
|
||||
"/productivity",
|
||||
"/rewards",
|
||||
"/agents/resume",
|
||||
"/agents/interview",
|
||||
"/agents/interview",
|
||||
"/agents/roleplay",
|
||||
"/agents/interview/setup",
|
||||
"/agents/roleplay",
|
||||
"/agents/roleplay/setup",
|
||||
"/agents/qscore",
|
||||
]);
|
||||
|
||||
export const ALLOWED_NOTIFICATION_HREF_PREFIXES = [
|
||||
"/missions/",
|
||||
"/missions/active",
|
||||
"/missions/available",
|
||||
"/agents/resume",
|
||||
"/agents/interview",
|
||||
"/agents/interview",
|
||||
"/agents/roleplay",
|
||||
"/agents/interview/setup",
|
||||
"/agents/roleplay",
|
||||
"/agents/roleplay/setup",
|
||||
"/agents/qscore",
|
||||
] as const;
|
||||
|
||||
export function isAllowedNotificationHref(href: string) {
|
||||
if (ALLOWED_NOTIFICATION_HREFS.has(href)) return true;
|
||||
return ALLOWED_NOTIFICATION_HREF_PREFIXES.some((prefix) =>
|
||||
prefix.endsWith("/")
|
||||
? href.startsWith(prefix)
|
||||
: href === prefix || href.startsWith(`${prefix}?`),
|
||||
);
|
||||
return ALLOWED_NOTIFICATION_HREF_PREFIXES.some((prefix) => href === prefix || href.startsWith(`${prefix}?`));
|
||||
}
|
||||
|
||||
12
src/index.ts
12
src/index.ts
@@ -18,21 +18,14 @@ import { growRoutes } from "./routes/grow.js";
|
||||
import { missionRoutes } from "./routes/missions.js";
|
||||
import { eventRoutes } from "./routes/events.js";
|
||||
import { homeRoutes } from "./routes/home.js";
|
||||
import { dailyMissionRoutes } from "./routes/daily-mission.js";
|
||||
import { analyticsRoutes } from "./routes/analytics.js";
|
||||
import { logRoutes } from "./routes/logs.js";
|
||||
import { v1Routes } from "./v1/index.js";
|
||||
import { startGrowEventsRedisConsumer } from "./events/redis-consumer.js";
|
||||
import { startPassiveMissionReviewLoop } from "./missions/passive-runner.js";
|
||||
import { db } from "./db/client.js";
|
||||
import { ensureRuntimeSchema } from "./db/ensure-runtime-schema.js";
|
||||
import { hydratePortAllocator, reconcileOnBoot, ensureCentralGiteaReady } from "./docker/manager.js";
|
||||
import { initCatalog } from "./agents/catalog.js";
|
||||
|
||||
async function main() {
|
||||
// Boot-time DB sanity + reconcile + central Gitea readiness.
|
||||
await db.execute("select 1");
|
||||
await ensureRuntimeSchema();
|
||||
await hydratePortAllocator();
|
||||
|
||||
// Ensure central Gitea is reachable before accepting traffic (changes.md §2A).
|
||||
@@ -48,7 +41,6 @@ async function main() {
|
||||
|
||||
await reconcileOnBoot();
|
||||
startGrowEventsRedisConsumer().catch((err) => log.error({ err }, "failed to start grow events redis consumer"));
|
||||
startPassiveMissionReviewLoop();
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
@@ -94,11 +86,7 @@ async function main() {
|
||||
app.route("/grow", growRoutes());
|
||||
app.route("/missions", missionRoutes());
|
||||
app.route("/events", eventRoutes());
|
||||
app.route("/analytics", analyticsRoutes());
|
||||
app.route("/v1", v1Routes());
|
||||
app.route("/logs", logRoutes());
|
||||
app.route("/home", homeRoutes());
|
||||
app.route("/daily-mission", dailyMissionRoutes());
|
||||
app.route("/conversations", conversationRoutes());
|
||||
app.route("/opencode", opencodeRoutes());
|
||||
app.route("/git", gitRoutes());
|
||||
|
||||
@@ -164,7 +164,7 @@ export async function loadPromptsFromDisk(): Promise<void> {
|
||||
} catch (err) {
|
||||
log.error({ err, path: SYSTEM_PROMPT_FILE }, "failed to load system prompt — using fallback");
|
||||
// Fallback: assemble from modules without a template file.
|
||||
const fallback = `You are Grow — a unified AI career assistant for the GrowQR platform.\n\n## Specialist Capabilities\n\n${modules.map((m) => `- **${m.name}**: ${m.description}`).join("\n")}`;
|
||||
const fallback = `You are the Grow Agent — a unified AI orchestrator for the GrowQR platform.\n\n## Sub-Agent Capabilities\n\n${modules.map((m) => `- **${m.name}**: ${m.description}`).join("\n")}`;
|
||||
cachedSystemPrompt = fallback;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import { missionActions, missionSuggestions } from "../db/schema.js";
|
||||
import type { GrowActiveMission } from "../actors/missions/types.js";
|
||||
import type { MissionActionPatch } from "./reducer-types.js";
|
||||
import { defaultMissionActionStatus, type MissionActionDto, type MissionActionRow, type MissionActionStatus, type NewMissionActionInput } from "./action-types.js";
|
||||
import { missionDetailHref } from "./reducer-helpers.js";
|
||||
import { buildServiceLink, getService, getServiceActionLabel } from "../services/service-registry.js";
|
||||
|
||||
const OPEN_STATUSES: MissionActionStatus[] = ["queued", "running", "waiting_approval", "waiting_user_input", "failed"];
|
||||
const DONE_STATUSES: MissionActionStatus[] = ["done", "dismissed", "snoozed"];
|
||||
@@ -49,30 +47,26 @@ export function actionToDto(row: MissionActionRow): MissionActionDto {
|
||||
function ctaForAction(action: MissionActionRow | NewMissionActionInput) {
|
||||
const payload = action.payload && typeof action.payload === "object" && !Array.isArray(action.payload) ? action.payload as Record<string, unknown> : {};
|
||||
const hrefFromPayload = typeof payload.href === "string" ? payload.href : undefined;
|
||||
const missionHref = missionDetailHref(action.missionInstanceId);
|
||||
const service = getService(action.serviceId);
|
||||
const href = hrefFromPayload ?? (
|
||||
service
|
||||
? buildServiceLink(service.id, service.curator.defaultPage, {
|
||||
source: "mission",
|
||||
missionInstanceId: action.missionInstanceId,
|
||||
missionId: action.missionId,
|
||||
stageId: action.stageId ?? undefined,
|
||||
}) ?? missionHref
|
||||
: missionHref
|
||||
);
|
||||
const serviceId = action.serviceId ?? "";
|
||||
const missionHref = `/missions/active?missionInstanceId=${encodeURIComponent(action.missionInstanceId)}`;
|
||||
const href = hrefFromPayload ??
|
||||
(serviceId.includes("interview") ? `/agents/interview/setup?source=mission&missionInstanceId=${encodeURIComponent(action.missionInstanceId)}&missionId=${encodeURIComponent(action.missionId)}${action.stageId ? `&stageId=${encodeURIComponent(action.stageId)}` : ""}` :
|
||||
serviceId.includes("roleplay") ? `/agents/roleplay/setup?source=mission&missionInstanceId=${encodeURIComponent(action.missionInstanceId)}&missionId=${encodeURIComponent(action.missionId)}${action.stageId ? `&stageId=${encodeURIComponent(action.stageId)}` : ""}` :
|
||||
serviceId.includes("resume") ? `/agents/resume?source=mission&missionInstanceId=${encodeURIComponent(action.missionInstanceId)}&missionId=${encodeURIComponent(action.missionId)}${action.stageId ? `&stageId=${encodeURIComponent(action.stageId)}` : ""}` : missionHref);
|
||||
|
||||
if (action.mode === "approval_required") return { ctaLabel: "Review", ctaHref: missionHref };
|
||||
if (action.mode === "user_input_required") return { ctaLabel: "Answer", ctaHref: missionHref };
|
||||
return { ctaLabel: service ? getServiceActionLabel(service.id, "start") : "Open", ctaHref: href };
|
||||
if (serviceId.includes("interview")) return { ctaLabel: "Start mock", ctaHref: href };
|
||||
if (serviceId.includes("roleplay")) return { ctaLabel: "Run drill", ctaHref: href };
|
||||
if (serviceId.includes("resume")) return { ctaLabel: "Open resume", ctaHref: href };
|
||||
return { ctaLabel: "Open", ctaHref: href };
|
||||
}
|
||||
|
||||
function suggestionTypeForAction(action: MissionActionRow | NewMissionActionInput) {
|
||||
if (action.mode === "user_input_required") return "blocked" as const;
|
||||
if (action.mode === "approval_required") return "review" as const;
|
||||
const category = getService(action.serviceId)?.category;
|
||||
if (category === "practice") return "practice" as const;
|
||||
if (category === "document") return "artifact" as const;
|
||||
if ((action.serviceId ?? "").includes("interview") || (action.serviceId ?? "").includes("roleplay")) return "practice" as const;
|
||||
if ((action.serviceId ?? "").includes("resume")) return "artifact" as const;
|
||||
return "action" as const;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
import type { MissionReducer, MissionReduction, MissionStagePatch } from "../reducer-types.js";
|
||||
import {
|
||||
actionForAgent,
|
||||
extractResumeSignals,
|
||||
extractWeakAreas,
|
||||
isFeedbackEvent,
|
||||
isInterviewEvent,
|
||||
isRelevantServiceEvent,
|
||||
isResumeEvent,
|
||||
isRoleplayEvent,
|
||||
missionExplicitlyMatches,
|
||||
passiveInterviewFeedbackResumeUpgrade,
|
||||
passiveResumeAnalysisInterviewPractice,
|
||||
passiveRoleplayFeedbackStoryBank,
|
||||
serviceHref,
|
||||
} from "../reducer-helpers.js";
|
||||
import { actionForAgent, extractResumeSignals, extractWeakAreas, isInterviewEvent, isRelevantServiceEvent, isResumeEvent, isRoleplayEvent, missionExplicitlyMatches, serviceHref } from "../reducer-helpers.js";
|
||||
|
||||
export const careerTransitionReducer: MissionReducer = {
|
||||
missionId: "career-transition",
|
||||
@@ -62,14 +48,6 @@ export const careerTransitionReducer: MissionReducer = {
|
||||
priority: 95,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passiveResumeAnalysisInterviewPractice({
|
||||
missionId: "career-transition",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "interview",
|
||||
priority: 98,
|
||||
}));
|
||||
eventMessage = "Transferable skills map created; repositioned resume action is ready.";
|
||||
}
|
||||
|
||||
@@ -78,7 +56,7 @@ export const careerTransitionReducer: MissionReducer = {
|
||||
eventMessage = "Adjacent-role interview practice started.";
|
||||
}
|
||||
|
||||
if (isInterviewEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isInterviewEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
stagePatches.push({ stageId: "interview", status: "done", progressPercent: 100, outputSummary: "Adjacent-role credibility checked." });
|
||||
stagePatches.push({ stageId: "roleplay", status: "ready", progressPercent: 0, outputSummary: "Practice the 'why I am switching' narrative next." });
|
||||
@@ -96,30 +74,12 @@ export const careerTransitionReducer: MissionReducer = {
|
||||
priority: 92,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passiveInterviewFeedbackResumeUpgrade({
|
||||
missionId: "career-transition",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "resume",
|
||||
priority: 104,
|
||||
}));
|
||||
eventMessage = "Career transition interview feedback produced the next pitch-practice action.";
|
||||
}
|
||||
|
||||
if (isRoleplayEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
const passive = passiveRoleplayFeedbackStoryBank({
|
||||
missionId: "career-transition",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "roleplay",
|
||||
priority: 94,
|
||||
});
|
||||
if (isRoleplayEvent(event.source, type) && type.includes("review_completed")) {
|
||||
stagePatches.push({ stageId: "roleplay", status: "done", progressPercent: 100, outputSummary: "Transition pitch practice reviewed." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 70, outputSummary: "Transition confidence signals updated." });
|
||||
artifacts.push(passive.artifact);
|
||||
actions.push(passive.action);
|
||||
eventMessage = "Transition narrative practice completed.";
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,8 @@ import { getString } from "../../events/envelope.js";
|
||||
import type { MissionReducer, MissionReduction, MissionStagePatch } from "../reducer-types.js";
|
||||
import {
|
||||
actionForAgent,
|
||||
extractMissingProof,
|
||||
extractOverallScore,
|
||||
extractResumeProofPoints,
|
||||
extractResumeSignals,
|
||||
extractStoryIssues,
|
||||
extractWeakAreas,
|
||||
isInterviewEvent,
|
||||
isQscoreEvent,
|
||||
@@ -30,10 +27,6 @@ function reviewSummary(payload: Record<string, unknown>) {
|
||||
return summary ?? "Mock interview review completed.";
|
||||
}
|
||||
|
||||
function isFeedbackEvent(type: string) {
|
||||
return type.includes("review_completed") || type.includes("review.completed") || type.includes("feedback.generated");
|
||||
}
|
||||
|
||||
export const interviewToOfferReducer: MissionReducer = {
|
||||
missionId: "interview-to-offer",
|
||||
accepts: acceptsMission,
|
||||
@@ -54,7 +47,6 @@ export const interviewToOfferReducer: MissionReducer = {
|
||||
|
||||
if (isResumeEvent(event.source, type) && (type.includes("analysis_completed") || type.includes("analysis.complete") || type.includes("analyzed"))) {
|
||||
const signals = extractResumeSignals(payload);
|
||||
const proofPoints = extractResumeProofPoints(payload);
|
||||
stagePatches.push({ stageId: "resume", status: "done", progressPercent: 100, outputSummary: "Resume talking points and fit scan are ready." });
|
||||
stagePatches.push({ stageId: "interview", status: "ready", progressPercent: 0 });
|
||||
artifacts.push({
|
||||
@@ -77,29 +69,6 @@ export const interviewToOfferReducer: MissionReducer = {
|
||||
priority: 92,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(actionForAgent("interview-to-offer", "interview", {
|
||||
stageId: "interview",
|
||||
serviceId: "interview-service",
|
||||
toolName: "interview.configure_practice",
|
||||
mode: "suggestion",
|
||||
title: "Practice explaining your strongest resume proof",
|
||||
body: proofPoints.strengths.length
|
||||
? `Run a mock focused on ${proofPoints.strengths.slice(0, 2).join(" and ")} so your resume turns into interview-ready stories.`
|
||||
: "Run a resume-led mock interview so your strongest proof turns into interview-ready stories.",
|
||||
payload: {
|
||||
passiveAction: "resume_analysis_to_interview_practice",
|
||||
resumeSignals: signals,
|
||||
proofPoints,
|
||||
prompt: proofPoints.strengths[0]
|
||||
? `Practice explaining ${proofPoints.strengths[0]} with clear ownership, impact, and tradeoffs.`
|
||||
: "Practice explaining your strongest resume project with clear ownership, impact, and tradeoffs.",
|
||||
href: serviceHref("interview", activeMission.instanceId, activeMission.missionId, "interview"),
|
||||
},
|
||||
sourceEventId: event.id,
|
||||
idempotencyKey: `${activeMission.instanceId}:resume-analysis:proof-interview:${event.id}`,
|
||||
priority: 98,
|
||||
urgency: "today",
|
||||
}));
|
||||
eventMessage = "Resume fit scan completed; mock interview is ready to run.";
|
||||
}
|
||||
|
||||
@@ -113,10 +82,8 @@ export const interviewToOfferReducer: MissionReducer = {
|
||||
eventMessage = "Mock interview completed; waiting for review.";
|
||||
}
|
||||
|
||||
if (isInterviewEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isInterviewEvent(event.source, type) && (type.includes("review_completed") || type.includes("review.completed"))) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
const missingProof = extractMissingProof(payload);
|
||||
const storyIssues = extractStoryIssues(payload);
|
||||
stagePatches.push({ stageId: "interview", status: "done", progressPercent: 100, outputSummary: reviewSummary(payload) });
|
||||
stagePatches.push({ stageId: "roleplay", status: "ready", progressPercent: 0, outputSummary: "Practice the communication gaps surfaced by interview feedback." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 60, outputSummary: "Readiness signals updated from interview review." });
|
||||
@@ -132,27 +99,14 @@ export const interviewToOfferReducer: MissionReducer = {
|
||||
serviceId: "resume-service",
|
||||
toolName: "resume.create_version_prompt_draft",
|
||||
mode: "approval_required",
|
||||
title: "Draft stronger resume bullets from interview feedback?",
|
||||
body: [...weakAreas, ...missingProof, ...storyIssues].length
|
||||
? `Approve a Resume Agent draft that fixes ${[...weakAreas, ...missingProof, ...storyIssues].slice(0, 3).join(", ")} with stronger bullets and proof.`
|
||||
: "Approve a Resume Agent draft that turns the interview feedback into stronger bullets and talking points.",
|
||||
prompt: "Create a resume upgrade draft from this interview feedback. Focus on measurable impact, ownership, missing proof, and reusable talking points.",
|
||||
payload: {
|
||||
passiveAction: "interview_feedback_to_resume_upgrade",
|
||||
weakAreas,
|
||||
missingProof,
|
||||
storyIssues,
|
||||
sourceReviewEventId: event.id,
|
||||
draftInstructions: [
|
||||
"Rewrite weak bullets with action, scope, metric, and result.",
|
||||
"Add proof for any interview gaps that lacked evidence.",
|
||||
"Create talking points that match the feedback themes.",
|
||||
],
|
||||
href: serviceHref("resume", activeMission.instanceId, activeMission.missionId, "resume"),
|
||||
},
|
||||
title: "Create a tailored resume version from this interview feedback?",
|
||||
body: weakAreas.length
|
||||
? `The interview exposed ${weakAreas.slice(0, 3).join(", ")}. Approve a Resume Agent draft that turns this feedback into targeted bullets and talking points.`
|
||||
: "Approve a Resume Agent draft that turns the interview feedback into targeted bullets and talking points.",
|
||||
payload: { weakAreas, sourceReviewEventId: event.id, href: serviceHref("resume", activeMission.instanceId, activeMission.missionId, "resume") },
|
||||
sourceEventId: event.id,
|
||||
idempotencyKey: `${activeMission.instanceId}:interview-review:tailor-resume:${event.id}`,
|
||||
priority: 108,
|
||||
priority: 100,
|
||||
urgency: "now",
|
||||
}));
|
||||
if (weakAreas.some((area) => /communication|story|clarity|confidence|concise/i.test(area))) {
|
||||
@@ -173,45 +127,9 @@ export const interviewToOfferReducer: MissionReducer = {
|
||||
eventMessage = "Interview review completed; resume and roleplay next actions were created.";
|
||||
}
|
||||
|
||||
if (isRoleplayEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
const storyIssues = extractStoryIssues(payload);
|
||||
if (isRoleplayEvent(event.source, type) && type.includes("review_completed")) {
|
||||
stagePatches.push({ stageId: "roleplay", status: "done", progressPercent: 100, outputSummary: "Communication drill reviewed." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 75, outputSummary: "Communication readiness updated." });
|
||||
artifacts.push({
|
||||
type: "story_bank_update",
|
||||
title: "Story bank updates from roleplay feedback",
|
||||
stageId: "roleplay",
|
||||
summary: [...weakAreas, ...storyIssues].length
|
||||
? `Turn these into reusable STAR stories: ${[...weakAreas, ...storyIssues].slice(0, 5).join(", ")}`
|
||||
: "Roleplay feedback captured story bank improvements for future interviews.",
|
||||
metadata: { sourceEventId: event.id, weakAreas, storyIssues, payload },
|
||||
});
|
||||
actions.push(actionForAgent("interview-to-offer", "interview", {
|
||||
stageId: "interview",
|
||||
serviceId: "interview-service",
|
||||
toolName: "interview.configure_practice",
|
||||
mode: "suggestion",
|
||||
title: "Run a story-bank recovery mock",
|
||||
body: [...weakAreas, ...storyIssues].length
|
||||
? `Practice the communication gaps from roleplay: ${[...weakAreas, ...storyIssues].slice(0, 3).join(", ")}.`
|
||||
: "Run a targeted mock interview that converts roleplay feedback into reusable STAR stories.",
|
||||
payload: {
|
||||
passiveAction: "roleplay_feedback_to_communication_drill",
|
||||
weakAreas,
|
||||
storyIssues,
|
||||
storyBankInstructions: [
|
||||
"Convert each weak communication moment into a STAR story prompt.",
|
||||
"Practice the answer in an interview setting.",
|
||||
"Save the strongest version as reusable story-bank material.",
|
||||
],
|
||||
href: serviceHref("interview", activeMission.instanceId, activeMission.missionId, "interview"),
|
||||
},
|
||||
sourceEventId: event.id,
|
||||
idempotencyKey: `${activeMission.instanceId}:roleplay-review:story-bank-interview:${event.id}`,
|
||||
priority: 96,
|
||||
urgency: "today",
|
||||
}));
|
||||
eventMessage = "Roleplay review improved interview communication readiness.";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,354 +0,0 @@
|
||||
import crypto from "node:crypto";
|
||||
import { createClient, type Client } from "rivetkit/client";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { config } from "../config.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { growEvents, users } from "../db/schema.js";
|
||||
import {
|
||||
completeMissionCoachRunPg,
|
||||
createMissionCoachRunPg,
|
||||
getActiveMissionPg,
|
||||
listActiveMissionsForPassiveReviewPg,
|
||||
listActiveMissionsPg,
|
||||
replaceMissionSuggestionsPg,
|
||||
upsertActiveMissionPg,
|
||||
} from "../grow/persistence.js";
|
||||
import { recordGrowEvent, markGrowEventFailed, markGrowEventProcessed, markGrowEventProcessing } from "../events/record-grow-event.js";
|
||||
import { routeGrowEventToUserActor } from "../events/route-to-user-actor.js";
|
||||
import { getPersistedMissionDefinition } from "./postgres-registry.js";
|
||||
import { buildDeterministicMissionSuggestions } from "./suggestions.js";
|
||||
import type { Registry } from "../actors/registry.js";
|
||||
import type { GrowActiveMission, MissionActorType, MissionId, MissionSnapshot } from "../actors/missions/types.js";
|
||||
import { log } from "../log.js";
|
||||
|
||||
const ONBOARDING_MISSION_LIMIT = 2;
|
||||
const PASSIVE_REVIEW_SOURCE = "growqr-backend:mission-passive";
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
function getClient(): Client<Registry> {
|
||||
return (_client ??= createClient<Registry>(config.rivetClientEndpoint));
|
||||
}
|
||||
|
||||
function missionActorFor(userId: string, instanceId: string, actorType: MissionActorType) {
|
||||
const client = getClient();
|
||||
switch (actorType) {
|
||||
case "interviewToOfferMissionActor":
|
||||
return client.interviewToOfferMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "careerTransitionMissionActor":
|
||||
return client.careerTransitionMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "salaryNegotiationWarRoomMissionActor":
|
||||
return client.salaryNegotiationWarRoomMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "promotionReadinessMissionActor":
|
||||
return client.promotionReadinessMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "personalBrandOpportunityEngineMissionActor":
|
||||
return client.personalBrandOpportunityEngineMissionActor.getOrCreate([userId, instanceId]);
|
||||
}
|
||||
}
|
||||
|
||||
function activeMissionFromSnapshot(snapshot: MissionSnapshot): GrowActiveMission {
|
||||
return {
|
||||
instanceId: snapshot.instanceId,
|
||||
missionId: snapshot.missionId,
|
||||
workflowId: snapshot.workflowId,
|
||||
title: snapshot.title,
|
||||
shortTitle: snapshot.shortTitle,
|
||||
status: snapshot.status,
|
||||
progressPercent: snapshot.progressPercent,
|
||||
currentStageId: snapshot.currentStageId,
|
||||
goal: snapshot.goal,
|
||||
actorType: actorTypeFor(snapshot.missionId),
|
||||
createdAt: new Date(snapshot.createdAt).getTime(),
|
||||
updatedAt: new Date(snapshot.updatedAt).getTime(),
|
||||
};
|
||||
}
|
||||
|
||||
function actorTypeFor(missionId: MissionId): MissionActorType | undefined {
|
||||
if (missionId === "interview-to-offer") return "interviewToOfferMissionActor";
|
||||
if (missionId === "career-transition") return "careerTransitionMissionActor";
|
||||
if (missionId === "salary-negotiation-war-room") return "salaryNegotiationWarRoomMissionActor";
|
||||
if (missionId === "promotion-readiness") return "promotionReadinessMissionActor";
|
||||
if (missionId === "personal-brand-opportunity-engine") return "personalBrandOpportunityEngineMissionActor";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function hashUser(userId: string) {
|
||||
return crypto.createHash("sha256").update(userId).digest("hex").slice(0, 12);
|
||||
}
|
||||
|
||||
export function onboardingMissionInstanceId(userId: string, missionId: MissionId) {
|
||||
return `mission-${missionId}-${hashUser(userId)}`;
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
function stringValues(value: unknown): string[] {
|
||||
if (Array.isArray(value)) return value.filter((item): item is string => typeof item === "string" && Boolean(item.trim())).map((item) => item.trim());
|
||||
if (typeof value === "string" && value.trim()) return [value.trim()];
|
||||
return [];
|
||||
}
|
||||
|
||||
function onboardingText(context?: Record<string, unknown>) {
|
||||
const source = asRecord(context);
|
||||
const preferences = asRecord(source.preferences);
|
||||
const onboarding = asRecord(source.onboarding ?? preferences.onboarding);
|
||||
const mission = asRecord(preferences.mission_preferences);
|
||||
const resume = asRecord(preferences.resume_preferences);
|
||||
const interview = asRecord(preferences.interview_preferences);
|
||||
const values = [
|
||||
...stringValues(onboarding.goal),
|
||||
...stringValues(onboarding.target_role ?? onboarding.targetRole ?? onboarding.role ?? onboarding.current_role),
|
||||
...stringValues(onboarding.timeline),
|
||||
...stringValues(mission.active_goal),
|
||||
...stringValues(resume.target_title),
|
||||
...stringValues(interview.job_description),
|
||||
...stringValues(preferences.target_roles),
|
||||
...stringValues(preferences.target_companies),
|
||||
];
|
||||
return values.join(" ").toLowerCase();
|
||||
}
|
||||
|
||||
export function selectOnboardingMissionIds(context?: Record<string, unknown>, limit = ONBOARDING_MISSION_LIMIT): MissionId[] {
|
||||
const text = onboardingText(context);
|
||||
const primary: MissionId =
|
||||
/salary|compensation|negotiat/.test(text) ? "salary-negotiation-war-room" :
|
||||
/promot|manager|leadership|level up|level-up/.test(text) ? "promotion-readiness" :
|
||||
/transition|switch|pivot|career change|new field/.test(text) ? "career-transition" :
|
||||
/brand|linkedin|network|visibility|opportunit/.test(text) ? "personal-brand-opportunity-engine" :
|
||||
"interview-to-offer";
|
||||
|
||||
const ordered: MissionId[] = [
|
||||
primary,
|
||||
"personal-brand-opportunity-engine",
|
||||
"interview-to-offer",
|
||||
"career-transition",
|
||||
"promotion-readiness",
|
||||
"salary-negotiation-war-room",
|
||||
];
|
||||
return Array.from(new Set(ordered)).slice(0, Math.max(1, limit));
|
||||
}
|
||||
|
||||
async function ensureUser(userId: string) {
|
||||
await db
|
||||
.insert(users)
|
||||
.values({ id: userId, email: `${userId}@service.local`, displayName: userId })
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
|
||||
export async function ensureOnboardingActiveMissions(input: {
|
||||
userId: string;
|
||||
context?: Record<string, unknown>;
|
||||
completedAt?: string | Date | null;
|
||||
sourceEventId?: string;
|
||||
source?: string;
|
||||
limit?: number;
|
||||
}) {
|
||||
const userId = input.userId.trim();
|
||||
if (!userId) return { status: "skipped" as const, reason: "missing_user_id" as const, started: [], existing: [] };
|
||||
|
||||
await ensureUser(userId);
|
||||
const missionIds = selectOnboardingMissionIds(input.context, input.limit ?? ONBOARDING_MISSION_LIMIT);
|
||||
const activeRows = await listActiveMissionsPg(userId);
|
||||
const started: GrowActiveMission[] = [];
|
||||
const existing: GrowActiveMission[] = [];
|
||||
|
||||
for (const missionId of missionIds) {
|
||||
const alreadyActive = activeRows.find((item) => item.mission.missionId === missionId && ["active", "paused"].includes(item.mission.status));
|
||||
if (alreadyActive) {
|
||||
existing.push(alreadyActive.mission);
|
||||
continue;
|
||||
}
|
||||
|
||||
const mission = await getPersistedMissionDefinition(missionId);
|
||||
if (!mission?.actorType) continue;
|
||||
|
||||
const instanceId = onboardingMissionInstanceId(userId, missionId);
|
||||
const existingInstance = await getActiveMissionPg(userId, instanceId);
|
||||
if (existingInstance) {
|
||||
existing.push(existingInstance.mission);
|
||||
continue;
|
||||
}
|
||||
|
||||
const actor = missionActorFor(userId, instanceId, mission.actorType);
|
||||
const completedAt = input.completedAt instanceof Date ? input.completedAt.toISOString() : input.completedAt ?? new Date().toISOString();
|
||||
const snapshot = await actor.init({
|
||||
userId,
|
||||
instanceId,
|
||||
missionId,
|
||||
goal: onboardingText(input.context) || undefined,
|
||||
input: {
|
||||
source: "onboarding",
|
||||
sourceEventId: input.sourceEventId,
|
||||
completedAt,
|
||||
context: input.context ?? {},
|
||||
},
|
||||
});
|
||||
const activeMission = activeMissionFromSnapshot(snapshot);
|
||||
await upsertActiveMissionPg(userId, activeMission, snapshot);
|
||||
started.push(activeMission);
|
||||
|
||||
const event = await recordGrowEvent({
|
||||
source: input.source ?? "onboarding",
|
||||
type: "mission.started",
|
||||
category: "mission",
|
||||
userId,
|
||||
occurredAt: completedAt,
|
||||
mission: { instanceId, missionId, stageId: snapshot.currentStageId },
|
||||
correlation: { sourceEventId: input.sourceEventId },
|
||||
payload: {
|
||||
trigger: "onboarding",
|
||||
title: snapshot.title,
|
||||
goal: snapshot.goal,
|
||||
selectedMissionIds: missionIds,
|
||||
},
|
||||
dedupeKey: `mission-onboarding-start:${userId}:${missionId}`,
|
||||
}, { userId, source: input.source ?? "onboarding" });
|
||||
await routeGrowEventToUserActor(event).catch((err) => log.warn({ err, userId, missionId }, "failed to route onboarding mission start event"));
|
||||
}
|
||||
|
||||
return {
|
||||
status: started.length ? "started" as const : "already_ready" as const,
|
||||
selectedMissionIds: missionIds,
|
||||
started,
|
||||
existing,
|
||||
};
|
||||
}
|
||||
|
||||
function passiveReviewDate(input?: string | Date) {
|
||||
const date = input instanceof Date ? input : input ? new Date(input) : new Date();
|
||||
return Number.isNaN(date.getTime()) ? new Date().toISOString().slice(0, 10) : date.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
async function passiveReviewAlreadyRan(instanceId: string, date: string) {
|
||||
const [existing] = await db
|
||||
.select({ id: growEvents.id, processingStatus: growEvents.processingStatus })
|
||||
.from(growEvents)
|
||||
.where(eq(growEvents.dedupeKey, `mission-passive-review:${instanceId}:${date}`))
|
||||
.limit(1);
|
||||
return existing ?? null;
|
||||
}
|
||||
|
||||
export async function runPassiveMissionReviewForMission(input: {
|
||||
userId: string;
|
||||
mission: GrowActiveMission;
|
||||
snapshot?: MissionSnapshot | null;
|
||||
date?: string | Date;
|
||||
force?: boolean;
|
||||
}) {
|
||||
const date = passiveReviewDate(input.date);
|
||||
const existing = input.force ? null : await passiveReviewAlreadyRan(input.mission.instanceId, date);
|
||||
if (existing) {
|
||||
return { status: "skipped" as const, reason: "already_ran" as const, eventId: existing.id, mission: input.mission };
|
||||
}
|
||||
if (!input.mission.actorType) {
|
||||
return { status: "skipped" as const, reason: "missing_actor" as const, mission: input.mission };
|
||||
}
|
||||
|
||||
const dedupeKey = input.force
|
||||
? `mission-passive-review:${input.mission.instanceId}:${date}:${Date.now()}`
|
||||
: `mission-passive-review:${input.mission.instanceId}:${date}`;
|
||||
const event = await recordGrowEvent({
|
||||
source: PASSIVE_REVIEW_SOURCE,
|
||||
type: "mission.passive_review.completed",
|
||||
category: "mission",
|
||||
userId: input.userId,
|
||||
occurredAt: new Date().toISOString(),
|
||||
mission: { instanceId: input.mission.instanceId, missionId: input.mission.missionId, stageId: input.mission.currentStageId },
|
||||
payload: {
|
||||
reviewDate: date,
|
||||
status: "started",
|
||||
},
|
||||
dedupeKey,
|
||||
}, { userId: input.userId, source: PASSIVE_REVIEW_SOURCE });
|
||||
await markGrowEventProcessing(event.id);
|
||||
|
||||
try {
|
||||
const actor = missionActorFor(input.userId, input.mission.instanceId, input.mission.actorType);
|
||||
const scrum = await actor.runDailyScrum({ trigger: "nightly" });
|
||||
const snapshot = scrum.snapshot ?? input.snapshot;
|
||||
if (!snapshot) {
|
||||
await markGrowEventFailed(event.id, new Error("mission_passive_review_missing_snapshot"));
|
||||
return { status: "skipped" as const, reason: "missing_snapshot" as const, eventId: event.id, mission: input.mission };
|
||||
}
|
||||
|
||||
const activeMission = activeMissionFromSnapshot(snapshot);
|
||||
await upsertActiveMissionPg(input.userId, activeMission, snapshot);
|
||||
const windowEnd = new Date(`${date}T23:59:59.999Z`);
|
||||
const windowStart = new Date(`${date}T00:00:00.000Z`);
|
||||
const run = await createMissionCoachRunPg({
|
||||
userId: input.userId,
|
||||
missionInstanceId: activeMission.instanceId,
|
||||
missionId: activeMission.missionId,
|
||||
windowStart,
|
||||
windowEnd,
|
||||
skillVersion: snapshot.skillVersion,
|
||||
inputDigest: {
|
||||
passive: true,
|
||||
reviewDate: date,
|
||||
trigger: "nightly",
|
||||
stageCount: snapshot.stages.length,
|
||||
currentStageId: snapshot.currentStageId,
|
||||
progressPercent: snapshot.progressPercent,
|
||||
artifactCount: snapshot.artifacts.length,
|
||||
eventCount: snapshot.events.length,
|
||||
},
|
||||
});
|
||||
|
||||
const snapshotContext = asRecord(snapshot.input?.context);
|
||||
const suggestions = await replaceMissionSuggestionsPg({
|
||||
userId: input.userId,
|
||||
missionInstanceId: activeMission.instanceId,
|
||||
missionId: activeMission.missionId,
|
||||
coachRunId: run.id,
|
||||
suggestions: buildDeterministicMissionSuggestions(snapshot, { preferences: asRecord(snapshotContext.preferences) }),
|
||||
});
|
||||
const summary = suggestions[0]
|
||||
? `Passive mission review refreshed ${suggestions.length} suggestion${suggestions.length === 1 ? "" : "s"}. Top action: ${suggestions[0].title}`
|
||||
: "Passive mission review found no open action.";
|
||||
await completeMissionCoachRunPg({ id: run.id, summary, output: { suggestions, passive: true, reviewDate: date } });
|
||||
|
||||
await db.update(growEvents).set({
|
||||
mission: { instanceId: activeMission.instanceId, missionId: activeMission.missionId, stageId: activeMission.currentStageId },
|
||||
payload: {
|
||||
reviewDate: date,
|
||||
status: "completed",
|
||||
coachRunId: run.id,
|
||||
suggestionIds: suggestions.map((item) => item.id),
|
||||
summary,
|
||||
},
|
||||
}).where(eq(growEvents.id, event.id));
|
||||
await markGrowEventProcessed(event.id);
|
||||
|
||||
return { status: "reviewed" as const, eventId: event.id, coachRunId: run.id, mission: activeMission, suggestionCount: suggestions.length, summary };
|
||||
} catch (err) {
|
||||
await markGrowEventFailed(event.id, err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function runPassiveMissionReviews(input: { userId?: string; date?: string | Date; force?: boolean; limit?: number } = {}) {
|
||||
const rows = await listActiveMissionsForPassiveReviewPg({ userId: input.userId, limit: input.limit });
|
||||
const results = [];
|
||||
for (const row of rows) {
|
||||
try {
|
||||
results.push(await runPassiveMissionReviewForMission({
|
||||
userId: row.userId,
|
||||
mission: row.mission,
|
||||
snapshot: row.snapshot,
|
||||
date: input.date,
|
||||
force: input.force,
|
||||
}));
|
||||
} catch (err) {
|
||||
log.error({ err, userId: row.userId, missionInstanceId: row.mission.instanceId }, "passive mission review failed");
|
||||
results.push({ status: "failed" as const, mission: row.mission, error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}
|
||||
return {
|
||||
date: passiveReviewDate(input.date),
|
||||
reviewed: results.filter((item) => item.status === "reviewed").length,
|
||||
skipped: results.filter((item) => item.status === "skipped").length,
|
||||
failed: results.filter((item) => item.status === "failed").length,
|
||||
results,
|
||||
};
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { config } from "../config.js";
|
||||
import { log } from "../log.js";
|
||||
import { runPassiveMissionReviews } from "./lifecycle.js";
|
||||
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
let running = false;
|
||||
|
||||
async function runOnce() {
|
||||
if (running) return;
|
||||
running = true;
|
||||
try {
|
||||
const result = await runPassiveMissionReviews({ limit: config.missionPassiveLoopBatchSize });
|
||||
if (result.reviewed || result.failed) {
|
||||
log.info({
|
||||
reviewed: result.reviewed,
|
||||
skipped: result.skipped,
|
||||
failed: result.failed,
|
||||
date: result.date,
|
||||
}, "passive mission review loop completed");
|
||||
}
|
||||
} catch (err) {
|
||||
log.error({ err }, "passive mission review loop failed");
|
||||
} finally {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function startPassiveMissionReviewLoop() {
|
||||
if (!config.missionPassiveLoopEnabled) {
|
||||
log.info("passive mission review loop disabled");
|
||||
return;
|
||||
}
|
||||
if (timer) return;
|
||||
|
||||
const intervalMs = Math.max(5 * 60 * 1000, config.missionPassiveLoopIntervalMs);
|
||||
const firstDelayMs = Math.min(60_000, intervalMs);
|
||||
const first = setTimeout(() => void runOnce(), firstDelayMs);
|
||||
first.unref?.();
|
||||
timer = setInterval(() => void runOnce(), intervalMs);
|
||||
timer.unref?.();
|
||||
log.info({ intervalMs, batchSize: config.missionPassiveLoopBatchSize }, "passive mission review loop scheduled");
|
||||
}
|
||||
@@ -1,20 +1,5 @@
|
||||
import type { MissionReducer, MissionReduction, MissionStagePatch } from "../reducer-types.js";
|
||||
import {
|
||||
actionForAgent,
|
||||
extractResumeSignals,
|
||||
extractWeakAreas,
|
||||
isFeedbackEvent,
|
||||
isInterviewEvent,
|
||||
isRelevantServiceEvent,
|
||||
isResumeEvent,
|
||||
isRoleplayEvent,
|
||||
missionDetailHref,
|
||||
missionExplicitlyMatches,
|
||||
passiveInterviewFeedbackResumeUpgrade,
|
||||
passiveResumeAnalysisInterviewPractice,
|
||||
passiveRoleplayFeedbackStoryBank,
|
||||
serviceHref,
|
||||
} from "../reducer-helpers.js";
|
||||
import { actionForAgent, extractResumeSignals, extractWeakAreas, isInterviewEvent, isRelevantServiceEvent, isResumeEvent, isRoleplayEvent, missionExplicitlyMatches, serviceHref } from "../reducer-helpers.js";
|
||||
|
||||
export const personalBrandOpportunityReducer: MissionReducer = {
|
||||
missionId: "personal-brand-opportunity-engine",
|
||||
@@ -63,58 +48,32 @@ export const personalBrandOpportunityReducer: MissionReducer = {
|
||||
priority: 92,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passiveResumeAnalysisInterviewPractice({
|
||||
missionId: "personal-brand-opportunity-engine",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "interview",
|
||||
priority: 90,
|
||||
}));
|
||||
eventMessage = "Resume proof points created a profile positioning action.";
|
||||
}
|
||||
|
||||
if (isRoleplayEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isRoleplayEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
const passive = passiveRoleplayFeedbackStoryBank({
|
||||
missionId: "personal-brand-opportunity-engine",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "roleplay",
|
||||
priority: 92,
|
||||
});
|
||||
stagePatches.push({ stageId: "roleplay", status: "done", progressPercent: 100, outputSummary: "Networking pitch reviewed." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 70, outputSummary: "Brand voice/readiness signals updated." });
|
||||
artifacts.push({ type: "networking_scripts", title: "Networking script improvements", stageId: "roleplay", summary: weakAreas.length ? `Improve: ${weakAreas.join(", ")}` : "Networking pitch practice completed.", metadata: { sourceEventId: event.id, weakAreas } });
|
||||
artifacts.push(passive.artifact);
|
||||
actions.push(actionForAgent("personal-brand-opportunity-engine", "planner", {
|
||||
stageId: "positioning",
|
||||
mode: "suggestion",
|
||||
title: "Turn this pitch into weekly content pillars",
|
||||
body: "Use the networking practice feedback to draft 3 credibility themes for weekly posts.",
|
||||
payload: { weakAreas, href: missionDetailHref(activeMission.instanceId) },
|
||||
payload: { weakAreas, href: `/missions/active?missionInstanceId=${encodeURIComponent(activeMission.instanceId)}` },
|
||||
sourceEventId: event.id,
|
||||
idempotencyKey: `${activeMission.instanceId}:content-pillars:${event.id}`,
|
||||
priority: 82,
|
||||
urgency: "soon",
|
||||
}));
|
||||
actions.push(passive.action);
|
||||
eventMessage = "Networking pitch review created brand content next steps.";
|
||||
}
|
||||
|
||||
if (isInterviewEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isInterviewEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
stagePatches.push({ stageId: "interview", status: "done", progressPercent: 100, outputSummary: "Credibility signals mined from interview review." });
|
||||
artifacts.push({ type: "credibility_signal_map", title: "Credibility signal map", stageId: "interview", summary: weakAreas.length ? `Recurring gaps/themes: ${weakAreas.join(", ")}` : "Interview review mined for positioning signals.", metadata: { sourceEventId: event.id, weakAreas } });
|
||||
actions.push(passiveInterviewFeedbackResumeUpgrade({
|
||||
missionId: "personal-brand-opportunity-engine",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "resume",
|
||||
priority: 98,
|
||||
}));
|
||||
eventMessage = "Interview feedback was mined for brand positioning signals.";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
import type { MissionReducer, MissionReduction, MissionStagePatch } from "../reducer-types.js";
|
||||
import {
|
||||
actionForAgent,
|
||||
extractResumeSignals,
|
||||
extractWeakAreas,
|
||||
isFeedbackEvent,
|
||||
isInterviewEvent,
|
||||
isRelevantServiceEvent,
|
||||
isResumeEvent,
|
||||
isRoleplayEvent,
|
||||
missionExplicitlyMatches,
|
||||
passiveInterviewFeedbackResumeUpgrade,
|
||||
passiveResumeAnalysisInterviewPractice,
|
||||
passiveRoleplayFeedbackStoryBank,
|
||||
serviceHref,
|
||||
} from "../reducer-helpers.js";
|
||||
import { actionForAgent, extractResumeSignals, extractWeakAreas, isInterviewEvent, isRelevantServiceEvent, isResumeEvent, isRoleplayEvent, missionExplicitlyMatches, serviceHref } from "../reducer-helpers.js";
|
||||
|
||||
export const promotionReadinessReducer: MissionReducer = {
|
||||
missionId: "promotion-readiness",
|
||||
@@ -62,31 +48,14 @@ export const promotionReadinessReducer: MissionReducer = {
|
||||
priority: 94,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passiveResumeAnalysisInterviewPractice({
|
||||
missionId: "promotion-readiness",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "interview",
|
||||
priority: 91,
|
||||
}));
|
||||
eventMessage = "Promotion evidence packet is ready; manager conversation practice is next.";
|
||||
}
|
||||
|
||||
if (isRoleplayEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isRoleplayEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
const passive = passiveRoleplayFeedbackStoryBank({
|
||||
missionId: "promotion-readiness",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "roleplay",
|
||||
priority: 95,
|
||||
});
|
||||
stagePatches.push({ stageId: "roleplay", status: "done", progressPercent: 100, outputSummary: "Manager conversation drill reviewed." });
|
||||
stagePatches.push({ stageId: "interview", status: "ready", progressPercent: 0, outputSummary: "Practice leadership narratives next if gaps remain." });
|
||||
artifacts.push({ type: "manager_conversation_script", title: "Manager conversation script", stageId: "roleplay", summary: weakAreas.length ? `Follow-up focus: ${weakAreas.join(", ")}` : "Manager conversation review completed.", metadata: { sourceEventId: event.id, weakAreas } });
|
||||
artifacts.push(passive.artifact);
|
||||
actions.push(actionForAgent("promotion-readiness", "interview", {
|
||||
stageId: "interview",
|
||||
serviceId: "interview-service",
|
||||
@@ -100,23 +69,14 @@ export const promotionReadinessReducer: MissionReducer = {
|
||||
priority: 86,
|
||||
urgency: "soon",
|
||||
}));
|
||||
actions.push(passive.action);
|
||||
eventMessage = "Manager conversation review updated promotion readiness.";
|
||||
}
|
||||
|
||||
if (isInterviewEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isInterviewEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
stagePatches.push({ stageId: "interview", status: "done", progressPercent: 100, outputSummary: "Leadership communication gap check completed." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 75, outputSummary: "Leadership readiness signals updated." });
|
||||
artifacts.push({ type: "leadership_gap_map", title: "Leadership gap map", stageId: "interview", summary: weakAreas.length ? weakAreas.join(", ") : "Leadership practice review completed.", metadata: { sourceEventId: event.id, weakAreas } });
|
||||
actions.push(passiveInterviewFeedbackResumeUpgrade({
|
||||
missionId: "promotion-readiness",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "resume",
|
||||
priority: 102,
|
||||
}));
|
||||
eventMessage = "Leadership practice review updated the promotion gap map.";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { asRecord, getNumber, getString } from "../events/envelope.js";
|
||||
import { buildServiceLink } from "../services/service-registry.js";
|
||||
import type { GrowActiveMission } from "../actors/missions/types.js";
|
||||
import type { MissionActionPatch, MissionArtifactPatch } from "./reducer-types.js";
|
||||
import type { MissionActionPatch } from "./reducer-types.js";
|
||||
|
||||
export function isResumeEvent(source: string, type: string) {
|
||||
const value = source.toLowerCase();
|
||||
@@ -23,10 +21,6 @@ export function isQscoreEvent(source: string, type: string) {
|
||||
return value.includes("qscore") || type.startsWith("qscore.");
|
||||
}
|
||||
|
||||
export function isFeedbackEvent(type: string) {
|
||||
return type.includes("review_completed") || type.includes("review.completed") || type.includes("feedback.generated");
|
||||
}
|
||||
|
||||
export function reviewRecord(payload: Record<string, unknown>) {
|
||||
return asRecord(payload.review ?? payload.result ?? payload.data ?? payload);
|
||||
}
|
||||
@@ -71,64 +65,6 @@ export function extractWeakAreas(payload: Record<string, unknown>): string[] {
|
||||
return Array.from(new Set(areas)).slice(0, 5);
|
||||
}
|
||||
|
||||
function extractStringListFromKeys(record: Record<string, unknown>, keys: string[]) {
|
||||
const values: string[] = [];
|
||||
for (const key of keys) {
|
||||
const value = record[key];
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
if (typeof item === "string" && item.trim()) values.push(item.trim());
|
||||
else if (item && typeof item === "object" && !Array.isArray(item)) {
|
||||
const row = item as Record<string, unknown>;
|
||||
const text = getString(row.title ?? row.name ?? row.label ?? row.summary ?? row.description ?? row.text);
|
||||
if (text) values.push(text);
|
||||
}
|
||||
}
|
||||
} else if (typeof value === "string" && value.trim()) {
|
||||
values.push(...value.split(/[;\n]/).map((part) => part.trim()).filter(Boolean));
|
||||
}
|
||||
}
|
||||
return Array.from(new Set(values)).slice(0, 8);
|
||||
}
|
||||
|
||||
export function extractMissingProof(payload: Record<string, unknown>): string[] {
|
||||
const review = reviewRecord(payload);
|
||||
return extractStringListFromKeys(review, [
|
||||
"missing_proof",
|
||||
"missingProof",
|
||||
"proof_gaps",
|
||||
"proofGaps",
|
||||
"evidence_gaps",
|
||||
"evidenceGaps",
|
||||
"missing_evidence",
|
||||
"missingEvidence",
|
||||
"gaps",
|
||||
]);
|
||||
}
|
||||
|
||||
export function extractStoryIssues(payload: Record<string, unknown>): string[] {
|
||||
const review = reviewRecord(payload);
|
||||
const values = extractStringListFromKeys(review, [
|
||||
"story_issues",
|
||||
"storyIssues",
|
||||
"story_gaps",
|
||||
"storyGaps",
|
||||
"star_gaps",
|
||||
"starGaps",
|
||||
"communication_gaps",
|
||||
"communicationGaps",
|
||||
"recommendations",
|
||||
]);
|
||||
const summary = getString(review.summary ?? review.feedback_summary ?? review.overall_feedback);
|
||||
if (summary) {
|
||||
const lower = summary.toLowerCase();
|
||||
if (lower.includes("star") || lower.includes("story")) values.push("tighten STAR story structure");
|
||||
if (lower.includes("metric") || lower.includes("impact") || lower.includes("measurable")) values.push("add measurable impact proof");
|
||||
if (lower.includes("ownership")) values.push("clarify ownership and scope");
|
||||
}
|
||||
return Array.from(new Set(values)).slice(0, 8);
|
||||
}
|
||||
|
||||
export function extractResumeSignals(payload: Record<string, unknown>): string[] {
|
||||
const analysis = asRecord(payload.analysis ?? payload.result ?? payload.data ?? payload);
|
||||
const signals: string[] = [];
|
||||
@@ -143,14 +79,6 @@ export function extractResumeSignals(payload: Record<string, unknown>): string[]
|
||||
return signals.slice(0, 8);
|
||||
}
|
||||
|
||||
export function extractResumeProofPoints(payload: Record<string, unknown>) {
|
||||
const analysis = asRecord(payload.analysis ?? payload.result ?? payload.data ?? payload);
|
||||
const strengths = extractStringListFromKeys(analysis, ["strengths", "top_strengths", "strong_projects", "projects", "achievements"]);
|
||||
const gaps = extractStringListFromKeys(analysis, ["gaps", "recommendations", "missing_keywords", "keyword_gaps", "weak_bullets"]);
|
||||
const keywords = extractStringListFromKeys(analysis, ["keywords", "matched_keywords", "missing_keywords", "keyword_gaps"]);
|
||||
return { strengths, gaps, keywords };
|
||||
}
|
||||
|
||||
export function missionExplicitlyMatches(eventMission: unknown, missionId: string) {
|
||||
const mission = asRecord(eventMission);
|
||||
const explicit = getString(mission.missionId ?? mission.mission_id);
|
||||
@@ -206,136 +134,10 @@ export function actionForAgent(missionId: string, agent: "planner" | "resume" |
|
||||
}
|
||||
|
||||
export function serviceHref(service: "resume" | "interview" | "roleplay" | "qscore", missionInstanceId: string, missionId: string, stageId?: string) {
|
||||
const serviceId = service === "qscore" ? "qscore-service" : `${service}-service`;
|
||||
const pageId = service === "resume" ? "workspace" : service === "qscore" ? "dashboard" : "setup";
|
||||
return buildServiceLink(serviceId, pageId, { source: "mission", missionInstanceId, missionId, stageId })
|
||||
?? missionDetailHref(missionInstanceId);
|
||||
}
|
||||
|
||||
export function missionDetailHref(missionInstanceId: string) {
|
||||
return `/missions/${encodeURIComponent(missionInstanceId)}`;
|
||||
}
|
||||
|
||||
export function passiveResumeAnalysisInterviewPractice(input: {
|
||||
missionId: string;
|
||||
activeMission: GrowActiveMission;
|
||||
eventId: string;
|
||||
payload: Record<string, unknown>;
|
||||
stageId?: string;
|
||||
priority?: number;
|
||||
}): MissionActionPatch {
|
||||
const signals = extractResumeSignals(input.payload);
|
||||
const proofPoints = extractResumeProofPoints(input.payload);
|
||||
return actionForAgent(input.missionId, "interview", {
|
||||
stageId: input.stageId ?? "interview",
|
||||
serviceId: "interview-service",
|
||||
toolName: "interview.configure_practice",
|
||||
mode: "suggestion",
|
||||
title: "Practice explaining your strongest resume proof",
|
||||
body: proofPoints.strengths.length
|
||||
? `Run a mock focused on ${proofPoints.strengths.slice(0, 2).join(" and ")} so your resume turns into interview-ready stories.`
|
||||
: "Run a resume-led mock interview so your strongest proof turns into interview-ready stories.",
|
||||
payload: {
|
||||
passiveAction: "resume_analysis_to_interview_practice",
|
||||
resumeSignals: signals,
|
||||
proofPoints,
|
||||
prompt: proofPoints.strengths[0]
|
||||
? `Practice explaining ${proofPoints.strengths[0]} with clear ownership, impact, and tradeoffs.`
|
||||
: "Practice explaining your strongest resume project with clear ownership, impact, and tradeoffs.",
|
||||
href: serviceHref("interview", input.activeMission.instanceId, input.activeMission.missionId, input.stageId ?? "interview"),
|
||||
},
|
||||
sourceEventId: input.eventId,
|
||||
idempotencyKey: `${input.activeMission.instanceId}:resume-analysis:proof-interview:${input.eventId}`,
|
||||
priority: input.priority ?? 98,
|
||||
urgency: "today",
|
||||
});
|
||||
}
|
||||
|
||||
export function passiveInterviewFeedbackResumeUpgrade(input: {
|
||||
missionId: string;
|
||||
activeMission: GrowActiveMission;
|
||||
eventId: string;
|
||||
payload: Record<string, unknown>;
|
||||
stageId?: string;
|
||||
priority?: number;
|
||||
}): MissionActionPatch {
|
||||
const weakAreas = extractWeakAreas(input.payload);
|
||||
const missingProof = extractMissingProof(input.payload);
|
||||
const storyIssues = extractStoryIssues(input.payload);
|
||||
return actionForAgent(input.missionId, "resume", {
|
||||
stageId: input.stageId ?? "resume",
|
||||
serviceId: "resume-service",
|
||||
toolName: "resume.create_version_prompt_draft",
|
||||
mode: "approval_required",
|
||||
title: "Draft stronger resume bullets from interview feedback?",
|
||||
body: [...weakAreas, ...missingProof, ...storyIssues].length
|
||||
? `Approve a Resume Agent draft that fixes ${[...weakAreas, ...missingProof, ...storyIssues].slice(0, 3).join(", ")} with stronger bullets and proof.`
|
||||
: "Approve a Resume Agent draft that turns the interview feedback into stronger bullets and talking points.",
|
||||
prompt: "Create a resume upgrade draft from this interview feedback. Focus on measurable impact, ownership, missing proof, and reusable talking points.",
|
||||
payload: {
|
||||
passiveAction: "interview_feedback_to_resume_upgrade",
|
||||
weakAreas,
|
||||
missingProof,
|
||||
storyIssues,
|
||||
sourceReviewEventId: input.eventId,
|
||||
draftInstructions: [
|
||||
"Rewrite weak bullets with action, scope, metric, and result.",
|
||||
"Add proof for any interview gaps that lacked evidence.",
|
||||
"Create talking points that match the feedback themes.",
|
||||
],
|
||||
href: serviceHref("resume", input.activeMission.instanceId, input.activeMission.missionId, input.stageId ?? "resume"),
|
||||
},
|
||||
sourceEventId: input.eventId,
|
||||
idempotencyKey: `${input.activeMission.instanceId}:interview-review:tailor-resume:${input.eventId}`,
|
||||
priority: input.priority ?? 108,
|
||||
urgency: "now",
|
||||
});
|
||||
}
|
||||
|
||||
export function passiveRoleplayFeedbackStoryBank(input: {
|
||||
missionId: string;
|
||||
activeMission: GrowActiveMission;
|
||||
eventId: string;
|
||||
payload: Record<string, unknown>;
|
||||
stageId?: string;
|
||||
priority?: number;
|
||||
}): { artifact: MissionArtifactPatch; action: MissionActionPatch } {
|
||||
const weakAreas = extractWeakAreas(input.payload);
|
||||
const storyIssues = extractStoryIssues(input.payload);
|
||||
return {
|
||||
artifact: {
|
||||
type: "story_bank_update",
|
||||
title: "Story bank updates from roleplay feedback",
|
||||
stageId: input.stageId ?? "roleplay",
|
||||
summary: [...weakAreas, ...storyIssues].length
|
||||
? `Turn these into reusable STAR stories: ${[...weakAreas, ...storyIssues].slice(0, 5).join(", ")}`
|
||||
: "Roleplay feedback captured story bank improvements for future interviews.",
|
||||
metadata: { sourceEventId: input.eventId, weakAreas, storyIssues, payload: input.payload },
|
||||
},
|
||||
action: actionForAgent(input.missionId, "interview", {
|
||||
stageId: "interview",
|
||||
serviceId: "interview-service",
|
||||
toolName: "interview.configure_practice",
|
||||
mode: "suggestion",
|
||||
title: "Run a story-bank recovery mock",
|
||||
body: [...weakAreas, ...storyIssues].length
|
||||
? `Practice the communication gaps from roleplay: ${[...weakAreas, ...storyIssues].slice(0, 3).join(", ")}.`
|
||||
: "Run a targeted mock interview that converts roleplay feedback into reusable STAR stories.",
|
||||
payload: {
|
||||
passiveAction: "roleplay_feedback_to_communication_drill",
|
||||
weakAreas,
|
||||
storyIssues,
|
||||
storyBankInstructions: [
|
||||
"Convert each weak communication moment into a STAR story prompt.",
|
||||
"Practice the answer in an interview setting.",
|
||||
"Save the strongest version as reusable story-bank material.",
|
||||
],
|
||||
href: serviceHref("interview", input.activeMission.instanceId, input.activeMission.missionId, "interview"),
|
||||
},
|
||||
sourceEventId: input.eventId,
|
||||
idempotencyKey: `${input.activeMission.instanceId}:roleplay-review:story-bank-interview:${input.eventId}`,
|
||||
priority: input.priority ?? 96,
|
||||
urgency: "today",
|
||||
}),
|
||||
};
|
||||
const params = new URLSearchParams({ source: "mission", missionInstanceId, missionId });
|
||||
if (stageId) params.set("stageId", stageId);
|
||||
if (service === "interview") return `/agents/interview/setup?${params.toString()}`;
|
||||
if (service === "roleplay") return `/agents/roleplay/setup?${params.toString()}`;
|
||||
if (service === "resume") return `/agents/resume?${params.toString()}`;
|
||||
return `/agents/qscore?${params.toString()}`;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
import type { MissionReducer, MissionReduction, MissionStagePatch } from "../reducer-types.js";
|
||||
import {
|
||||
actionForAgent,
|
||||
extractResumeSignals,
|
||||
extractWeakAreas,
|
||||
isFeedbackEvent,
|
||||
isInterviewEvent,
|
||||
isRelevantServiceEvent,
|
||||
isResumeEvent,
|
||||
isRoleplayEvent,
|
||||
missionExplicitlyMatches,
|
||||
passiveInterviewFeedbackResumeUpgrade,
|
||||
passiveResumeAnalysisInterviewPractice,
|
||||
passiveRoleplayFeedbackStoryBank,
|
||||
serviceHref,
|
||||
} from "../reducer-helpers.js";
|
||||
import { actionForAgent, extractResumeSignals, extractWeakAreas, isInterviewEvent, isRelevantServiceEvent, isResumeEvent, isRoleplayEvent, missionExplicitlyMatches, serviceHref } from "../reducer-helpers.js";
|
||||
|
||||
export const salaryNegotiationReducer: MissionReducer = {
|
||||
missionId: "salary-negotiation-war-room",
|
||||
@@ -62,14 +48,6 @@ export const salaryNegotiationReducer: MissionReducer = {
|
||||
priority: 96,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passiveResumeAnalysisInterviewPractice({
|
||||
missionId: "salary-negotiation-war-room",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "interview",
|
||||
priority: 88,
|
||||
}));
|
||||
eventMessage = "Value evidence is ready for negotiation practice.";
|
||||
}
|
||||
|
||||
@@ -78,20 +56,11 @@ export const salaryNegotiationReducer: MissionReducer = {
|
||||
eventMessage = "Negotiation drill started.";
|
||||
}
|
||||
|
||||
if (isRoleplayEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isRoleplayEvent(event.source, type) && type.includes("review_completed")) {
|
||||
const weakAreas = extractWeakAreas(payload);
|
||||
const passive = passiveRoleplayFeedbackStoryBank({
|
||||
missionId: "salary-negotiation-war-room",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "roleplay",
|
||||
priority: 93,
|
||||
});
|
||||
stagePatches.push({ stageId: "roleplay", status: "done", progressPercent: 100, outputSummary: "Negotiation drill reviewed." });
|
||||
stagePatches.push({ stageId: "qscore", status: "in_progress", progressPercent: 70, outputSummary: "Confidence signals updated." });
|
||||
artifacts.push({ type: "negotiation_objection_map", title: "Objection handling map", stageId: "roleplay", summary: weakAreas.length ? `Practice objections: ${weakAreas.join(", ")}` : "Negotiation practice review completed.", metadata: { sourceEventId: event.id, weakAreas } });
|
||||
artifacts.push(passive.artifact);
|
||||
actions.push(actionForAgent("salary-negotiation-war-room", "roleplay", {
|
||||
stageId: "roleplay",
|
||||
serviceId: "roleplay-service",
|
||||
@@ -105,20 +74,11 @@ export const salaryNegotiationReducer: MissionReducer = {
|
||||
priority: 94,
|
||||
urgency: "today",
|
||||
}));
|
||||
actions.push(passive.action);
|
||||
eventMessage = "Negotiation drill review created the next objection-handling action.";
|
||||
}
|
||||
|
||||
if (isInterviewEvent(event.source, type) && isFeedbackEvent(type)) {
|
||||
if (isInterviewEvent(event.source, type) && type.includes("review_completed")) {
|
||||
stagePatches.push({ stageId: "interview", status: "done", progressPercent: 100, outputSummary: "Communication confidence signal captured from interview review." });
|
||||
actions.push(passiveInterviewFeedbackResumeUpgrade({
|
||||
missionId: "salary-negotiation-war-room",
|
||||
activeMission,
|
||||
eventId: event.id,
|
||||
payload,
|
||||
stageId: "resume",
|
||||
priority: 99,
|
||||
}));
|
||||
eventMessage = "Interview feedback updated negotiation confidence signals.";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { MissionSnapshot, MissionStage } from "../actors/missions/types.js";
|
||||
import { missionDetailHref } from "./reducer-helpers.js";
|
||||
|
||||
export type MissionSuggestionType = "action" | "practice" | "review" | "artifact" | "blocked" | "insight";
|
||||
export type MissionSuggestionUrgency = "now" | "today" | "soon" | "calm";
|
||||
@@ -93,18 +92,18 @@ function ctaFor(stage: MissionStage, snapshot: MissionSnapshot, context?: Missio
|
||||
if (role === "Interview") {
|
||||
params.set("mode", "mission");
|
||||
params.set("prompt", `${stage.title} for ${profile.targetRole}`);
|
||||
return { label: stage.status === "in_progress" ? "Continue mock" : "Start mock", href: `/agents/interview?${params.toString()}` };
|
||||
return { label: stage.status === "in_progress" ? "Continue mock" : "Start mock", href: `/agents/interview/setup?${params.toString()}` };
|
||||
}
|
||||
if (role === "Roleplay") {
|
||||
params.set("scenario", `${stage.title} for ${profile.targetRole}`);
|
||||
return { label: stage.status === "in_progress" ? "Continue roleplay" : "Start roleplay", href: `/agents/roleplay?${params.toString()}` };
|
||||
return { label: stage.status === "in_progress" ? "Continue roleplay" : "Start roleplay", href: `/agents/roleplay/setup?${params.toString()}` };
|
||||
}
|
||||
if (role === "Resume") {
|
||||
params.set("focus", `${stage.title}: ${profile.targetRole}`);
|
||||
return { label: "Open resume", href: `/agents/resume?${params.toString()}` };
|
||||
}
|
||||
if (role === "Q Score") return { label: "View Q Score", href: `/agents/qscore?${params.toString()}` };
|
||||
return { label: "Continue", href: `${missionDetailHref(snapshot.instanceId)}?${params.toString()}` };
|
||||
return { label: "Continue", href: `/missions/active?${params.toString()}` };
|
||||
}
|
||||
|
||||
function suggestionId(snapshot: MissionSnapshot, stage: MissionStage, suffix: string) {
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import {
|
||||
systemNotificationPreferences,
|
||||
systemNotifications,
|
||||
type SystemNotificationPreferenceRow,
|
||||
type SystemNotificationRow,
|
||||
} from "../db/schema.js";
|
||||
|
||||
export const SYSTEM_NOTIFICATION_KINDS = ["session", "billing", "security", "feature", "account"] as const;
|
||||
|
||||
export type SystemNotificationKind = typeof SYSTEM_NOTIFICATION_KINDS[number];
|
||||
export type SystemNotificationPreference = { inApp: boolean; email: boolean };
|
||||
export type SystemNotificationPreferences = Record<SystemNotificationKind, SystemNotificationPreference>;
|
||||
|
||||
export type PublicSystemNotification = {
|
||||
id: string;
|
||||
kind: SystemNotificationKind;
|
||||
title: string;
|
||||
sub: string;
|
||||
when: string;
|
||||
read: boolean;
|
||||
href?: string;
|
||||
occurredAt: string;
|
||||
};
|
||||
|
||||
const DEFAULT_PREF: SystemNotificationPreference = { inApp: true, email: true };
|
||||
|
||||
export const DEFAULT_SYSTEM_NOTIFICATION_PREFERENCES = Object.fromEntries(
|
||||
SYSTEM_NOTIFICATION_KINDS.map((kind) => [kind, { ...DEFAULT_PREF }]),
|
||||
) as SystemNotificationPreferences;
|
||||
|
||||
type SeedSystemNotification = {
|
||||
id: string;
|
||||
kind: SystemNotificationKind;
|
||||
title: string;
|
||||
sub: string;
|
||||
whenLabel: string;
|
||||
href?: string;
|
||||
source: string;
|
||||
sourceRef?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function seedRows(userId: string): SeedSystemNotification[] {
|
||||
return [
|
||||
{
|
||||
id: `system:${userId}:session-current-device`,
|
||||
kind: "session",
|
||||
title: "Session active on this device",
|
||||
sub: "You are signed in securely on the current browser.",
|
||||
whenLabel: "Now",
|
||||
href: "/settings",
|
||||
source: "auth",
|
||||
},
|
||||
{
|
||||
id: `system:${userId}:security-profile-review`,
|
||||
kind: "security",
|
||||
title: "Security review available",
|
||||
sub: "Review connected sources and account access from settings.",
|
||||
whenLabel: "Today",
|
||||
href: "/settings",
|
||||
source: "security",
|
||||
},
|
||||
{
|
||||
id: `system:${userId}:feature-dashboard-refresh`,
|
||||
kind: "feature",
|
||||
title: "Dashboard experience updated",
|
||||
sub: "The home dashboard now separates agent inbox items from account notifications.",
|
||||
whenLabel: "Today",
|
||||
href: "/",
|
||||
source: "release",
|
||||
},
|
||||
{
|
||||
id: `system:${userId}:account-preferences-ready`,
|
||||
kind: "account",
|
||||
title: "Notification preferences are ready",
|
||||
sub: "Control in-app and email alerts by category in Settings.",
|
||||
whenLabel: "Today",
|
||||
href: "/settings",
|
||||
source: "account",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function isSystemNotificationKind(value: string): value is SystemNotificationKind {
|
||||
return (SYSTEM_NOTIFICATION_KINDS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
function normalizePreferences(rows: SystemNotificationPreferenceRow[]): SystemNotificationPreferences {
|
||||
const preferences = { ...DEFAULT_SYSTEM_NOTIFICATION_PREFERENCES };
|
||||
for (const row of rows) {
|
||||
if (!isSystemNotificationKind(row.kind)) continue;
|
||||
preferences[row.kind] = { inApp: row.inApp, email: row.email };
|
||||
}
|
||||
return preferences;
|
||||
}
|
||||
|
||||
function publicNotification(row: SystemNotificationRow): PublicSystemNotification {
|
||||
return {
|
||||
id: row.id,
|
||||
kind: row.kind as SystemNotificationKind,
|
||||
title: row.title,
|
||||
sub: row.sub,
|
||||
when: row.whenLabel,
|
||||
read: Boolean(row.readAt),
|
||||
href: row.href ?? undefined,
|
||||
occurredAt: row.occurredAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
export async function ensureDefaultSystemNotifications(userId: string) {
|
||||
const now = new Date();
|
||||
const rows = seedRows(userId).map((item) => ({
|
||||
...item,
|
||||
userId,
|
||||
sourceRef: item.sourceRef ?? {},
|
||||
updatedAt: now,
|
||||
}));
|
||||
|
||||
await db
|
||||
.insert(systemNotifications)
|
||||
.values(rows)
|
||||
.onConflictDoUpdate({
|
||||
target: systemNotifications.id,
|
||||
set: {
|
||||
title: sql`excluded.title`,
|
||||
sub: sql`excluded.sub`,
|
||||
whenLabel: sql`excluded.when_label`,
|
||||
href: sql`excluded.href`,
|
||||
source: sql`excluded.source`,
|
||||
sourceRef: sql`excluded.source_ref`,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function ensureSystemNotificationPreferences(userId: string) {
|
||||
const now = new Date();
|
||||
await db
|
||||
.insert(systemNotificationPreferences)
|
||||
.values(SYSTEM_NOTIFICATION_KINDS.map((kind) => ({ userId, kind, ...DEFAULT_PREF, updatedAt: now })))
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
|
||||
export async function getSystemNotificationPreferences(userId: string) {
|
||||
await ensureSystemNotificationPreferences(userId);
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(systemNotificationPreferences)
|
||||
.where(eq(systemNotificationPreferences.userId, userId));
|
||||
return normalizePreferences(rows);
|
||||
}
|
||||
|
||||
export async function updateSystemNotificationPreferences(userId: string, preferences: Partial<Record<SystemNotificationKind, Partial<SystemNotificationPreference>>>) {
|
||||
await ensureSystemNotificationPreferences(userId);
|
||||
const now = new Date();
|
||||
const current = await getSystemNotificationPreferences(userId);
|
||||
|
||||
for (const [kind, value] of Object.entries(preferences)) {
|
||||
if (!isSystemNotificationKind(kind)) continue;
|
||||
const next = {
|
||||
inApp: value?.inApp ?? current[kind].inApp,
|
||||
email: value?.email ?? current[kind].email,
|
||||
};
|
||||
await db
|
||||
.insert(systemNotificationPreferences)
|
||||
.values({
|
||||
userId,
|
||||
kind,
|
||||
inApp: next.inApp,
|
||||
email: next.email,
|
||||
updatedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [systemNotificationPreferences.userId, systemNotificationPreferences.kind],
|
||||
set: {
|
||||
inApp: next.inApp,
|
||||
email: next.email,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return getSystemNotificationPreferences(userId);
|
||||
}
|
||||
|
||||
export async function listSystemNotifications(userId: string) {
|
||||
await ensureDefaultSystemNotifications(userId);
|
||||
const preferences = await getSystemNotificationPreferences(userId);
|
||||
const enabledKinds = SYSTEM_NOTIFICATION_KINDS.filter((kind) => preferences[kind].inApp);
|
||||
|
||||
if (!enabledKinds.length) {
|
||||
return { notifications: [], preferences };
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(systemNotifications)
|
||||
.where(and(eq(systemNotifications.userId, userId), inArray(systemNotifications.kind, enabledKinds)))
|
||||
.orderBy(desc(systemNotifications.occurredAt), desc(systemNotifications.createdAt))
|
||||
.limit(50);
|
||||
|
||||
return {
|
||||
notifications: rows.map(publicNotification),
|
||||
preferences,
|
||||
};
|
||||
}
|
||||
|
||||
export async function markSystemNotificationRead(userId: string, notificationId: string) {
|
||||
const [row] = await db
|
||||
.update(systemNotifications)
|
||||
.set({ readAt: new Date(), updatedAt: new Date() })
|
||||
.where(and(eq(systemNotifications.userId, userId), eq(systemNotifications.id, notificationId)))
|
||||
.returning();
|
||||
return row ? publicNotification(row) : null;
|
||||
}
|
||||
|
||||
export async function markAllSystemNotificationsRead(userId: string) {
|
||||
await db
|
||||
.update(systemNotifications)
|
||||
.set({ readAt: new Date(), updatedAt: new Date() })
|
||||
.where(and(eq(systemNotifications.userId, userId), sql`${systemNotifications.readAt} is null`));
|
||||
return listSystemNotifications(userId);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import { Hono } from "hono";
|
||||
import { createClient, type Client } from "rivetkit/client";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { config } from "../config.js";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import type { Registry } from "../actors/registry.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { growEvents } from "../db/schema.js";
|
||||
import { listActiveMissionsPg } from "../grow/persistence.js";
|
||||
import { listMissionActions } from "../missions/actions.js";
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
function getClient(): Client<Registry> {
|
||||
return (_client ??= createClient<Registry>(config.rivetClientEndpoint));
|
||||
}
|
||||
|
||||
export function analyticsRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
|
||||
app.get("/platform", async (c) => {
|
||||
return c.json(await getClient().analyticsActor.getOrCreate(["platform"]).getPlatform());
|
||||
});
|
||||
|
||||
app.get("/user/qscore", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
return c.json(await getClient().analyticsActor.getOrCreate(["user", userId]).getUserQscore({ userId }));
|
||||
});
|
||||
|
||||
app.get("/user/activity", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const events = await db
|
||||
.select()
|
||||
.from(growEvents)
|
||||
.where(eq(growEvents.userId, userId))
|
||||
.orderBy(desc(growEvents.occurredAt))
|
||||
.limit(100);
|
||||
const activeMissions = await listActiveMissionsPg(userId).catch(() => []);
|
||||
const actions = await listMissionActions(userId, { openOnly: false }).catch(() => []);
|
||||
|
||||
return c.json({
|
||||
kind: "user-activity",
|
||||
userId,
|
||||
generatedAt: new Date().toISOString(),
|
||||
events,
|
||||
activeMissions: activeMissions.map((item) => item.mission),
|
||||
actions,
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ function buildTools() {
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "start_interview_session",
|
||||
description: "Create a real mock interview session via the interview-service microservice. Call this when the user asks to start or launch interview practice.",
|
||||
description: "Create a real interview practice session via the Interview Agent / interview-service microservice. Call this when the user asks to start or launch an interview.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -66,7 +66,7 @@ function buildTools() {
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "start_roleplay_session",
|
||||
description: "Create a real mock roleplay session via roleplay-service. Call when the user asks for roleplay or negotiation practice.",
|
||||
description: "Create a real roleplay session via Roleplay Agent / roleplay-service. Call when user asks for roleplay or negotiation practice.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -80,7 +80,7 @@ function buildTools() {
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "analyze_resume",
|
||||
description: "Analyze the user's resume using Resume Building. Returns completeness, skills, and gaps.",
|
||||
description: "Analyze user's resume using the Resume Agent. Returns completeness, skills, and gaps.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -94,7 +94,7 @@ function buildTools() {
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: "compute_qscore",
|
||||
description: "Compute the user's readiness Q Score via qscore-service.",
|
||||
description: "Compute user's readiness Q-Score via Q Score Agent / qscore-service.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {},
|
||||
@@ -174,14 +174,14 @@ export function chatRoutes() {
|
||||
switch (toolCall.name) {
|
||||
case "start_interview_session": {
|
||||
toolResult = await runServiceAgentProbe(
|
||||
{ id: "interview", name: "Mock Interview", role: "Interview practice", kind: "microservice", description: "Interview practice", service: "interview-service" },
|
||||
{ id: "interview", name: "Interview Agent", role: "Interview Agent", kind: "microservice", description: "Interview practice", service: "interview-service" },
|
||||
{ userId, goal: String(toolCall.arguments.target_role ?? "general preparation") },
|
||||
);
|
||||
if (toolResult.status === "ok" && toolResult.detail) {
|
||||
const detail = toolResult.detail as Record<string, unknown>;
|
||||
sessions.push({
|
||||
moduleId: "interview",
|
||||
moduleName: "Mock Interview",
|
||||
moduleName: "Interview Agent",
|
||||
status: "done",
|
||||
sessionId: detail.session_id as string,
|
||||
sessionUrl: typeof detail.ui_session_url === "string"
|
||||
@@ -194,14 +194,14 @@ export function chatRoutes() {
|
||||
}
|
||||
case "start_roleplay_session": {
|
||||
toolResult = await runServiceAgentProbe(
|
||||
{ id: "roleplay", name: "Mock Roleplay", role: "Roleplay practice", kind: "microservice", description: "Roleplay practice", service: "roleplay-service" },
|
||||
{ id: "roleplay", name: "Roleplay Agent", role: "Roleplay Agent", kind: "microservice", description: "Roleplay practice", service: "roleplay-service" },
|
||||
{ userId, goal: String(toolCall.arguments.goal ?? "general practice") },
|
||||
);
|
||||
if (toolResult.status === "ok" && toolResult.detail) {
|
||||
const detail = toolResult.detail as Record<string, unknown>;
|
||||
sessions.push({
|
||||
moduleId: "roleplay",
|
||||
moduleName: "Mock Roleplay",
|
||||
moduleName: "Roleplay Agent",
|
||||
status: "done",
|
||||
sessionId: detail.session_id as string,
|
||||
sessionUrl: typeof detail.ui_session_url === "string"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Hono } from "hono";
|
||||
import { z } from "zod";
|
||||
import { createClient, type Client } from "rivetkit/client";
|
||||
import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, generateText, stepCountIs, streamText, tool, type UIMessage } from "ai";
|
||||
import { convertToModelMessages, generateText, stepCountIs, streamText, tool, type UIMessage } from "ai";
|
||||
import { config } from "../config.js";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import type { Registry } from "../actors/registry.js";
|
||||
@@ -9,7 +9,7 @@ import { getConversationModel } from "../actors/conversation/agent.js";
|
||||
import { getMissionDefinition, isActorBackedMission, listMissionDefinitions } from "../missions/registry.js";
|
||||
import type { GrowActiveMission, MissionActorType, MissionSnapshot } from "../actors/missions/types.js";
|
||||
import { getSubAgentModules } from "../lib/prompt-loader.js";
|
||||
import { addMessagePg, createConversationPg, ensureConversation, ensureMissionConversationPg, getActiveMissionPg, getConversationMetadataPg, getConversationPg, listActiveMissionsPg, listConversationsPg, listMessagesPg, resetConversationPg, touchConversationPg, upsertActiveMissionPg } from "../grow/persistence.js";
|
||||
import { addMessagePg, createConversationPg, ensureConversation, getConversationPg, listActiveMissionsPg, listConversationsPg, listMessagesPg, resetConversationPg, touchConversationPg, upsertActiveMissionPg } from "../grow/persistence.js";
|
||||
import { getMissionAction, listMissionActions, updateMissionActionStatus } from "../missions/actions.js";
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
@@ -102,16 +102,9 @@ function missionActorFor(userId: string, instanceId: string, actorType: MissionA
|
||||
}
|
||||
|
||||
const createConversationSchema = z.object({ title: z.string().optional() });
|
||||
const createMissionConversationSchema = z.object({
|
||||
missionInstanceId: z.string().min(1),
|
||||
stageId: z.string().optional(),
|
||||
title: z.string().optional(),
|
||||
source: z.string().optional(),
|
||||
});
|
||||
const streamSchema = z.object({
|
||||
messages: z.array(z.custom<UIMessage>()),
|
||||
conversationId: z.string().optional(),
|
||||
growContext: z.any().optional(),
|
||||
});
|
||||
|
||||
const createMissionInstanceId = (missionId: string) =>
|
||||
@@ -155,25 +148,15 @@ function textFromMessage(message: UIMessage | undefined) {
|
||||
.trim();
|
||||
}
|
||||
|
||||
function forcedToolForPrompt(text: string, hasGrowContext = false) {
|
||||
function forcedToolForPrompt(text: string) {
|
||||
const lower = text.toLowerCase();
|
||||
const asksForTodayTasks =
|
||||
/\b(show|list|give|get|what|which|today|daily|streak|task|tasks|mission|missions|report|progress)\b/.test(lower) &&
|
||||
/\b(today|daily|streak|task|tasks|mission|missions|day|report|progress)\b/.test(lower);
|
||||
|
||||
if (hasGrowContext && /\b(report|result|outcome|kpi|summary)\b/.test(lower)) {
|
||||
return "showCareerSprintReport" as const;
|
||||
}
|
||||
|
||||
if (hasGrowContext && asksForTodayTasks) {
|
||||
return "showCareerSprintTasks" as const;
|
||||
}
|
||||
|
||||
// The legacy workflow catalog should only show when the user explicitly asks
|
||||
// for broader programs/workflows, not when they ask for streak missions.
|
||||
// Keep high-value dashboard UI predictable: when the user explicitly asks to
|
||||
// see/discover/find workflows, always produce workflow cards instead of a
|
||||
// text-only answer.
|
||||
if (
|
||||
/\b(discover|find|show|list|recommend|suggest|compare)\b/.test(lower) &&
|
||||
/\b(workflow|workflows|program|programs)\b/.test(lower)
|
||||
/\b(workflow|workflows|mission|missions|plan|plans|program|programs)\b/.test(lower)
|
||||
) {
|
||||
return "discoverWorkflows" as const;
|
||||
}
|
||||
@@ -181,48 +164,7 @@ function forcedToolForPrompt(text: string, hasGrowContext = false) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function buildGrowContextPrompt(growContext: any) {
|
||||
if (!growContext || typeof growContext !== "object") return "";
|
||||
const activeDay = growContext.activeDay;
|
||||
const tasks = Array.isArray(activeDay?.tasks) ? activeDay.tasks : [];
|
||||
const taskLines = tasks
|
||||
.map((task: any, index: number) => ` ${index + 1}. ${task.title ?? "Task"} — ${task.serviceName ?? task.serviceId ?? "service"} — status: ${task.status ?? "pending"} — route: ${task.route ?? "none"}`)
|
||||
.join("\n");
|
||||
return `\n\nCurrent CareerSprint context from the dashboard/streak UI:
|
||||
- localDate: ${growContext.localDate ?? "unknown"}
|
||||
- activeDayIndex: ${growContext.activeDayIndex ?? "unknown"}
|
||||
- streak: ${growContext.streak ?? 0} day(s)
|
||||
- progress: ${growContext.completedCount ?? 0}/${growContext.totalCount ?? 0} tasks (${growContext.overallProgressPercent ?? 0}%)
|
||||
- reportEligible: ${growContext.reportEligible ? "yes" : "no"}
|
||||
- activeDayTitle: ${activeDay?.title ?? "today"}
|
||||
- activeDayCompleted: ${activeDay?.completedCount ?? 0}/${activeDay?.totalCount ?? tasks.length}
|
||||
- activeDayTasks:
|
||||
${taskLines || " none"}
|
||||
|
||||
When the user asks for missions, tasks, today, streak status, missed work, or report readiness, use this CareerSprint context instead of the old workflow catalog. If reportEligible is true and the user asks about a report, ask whether they want to open the report. If activeDayIndex is 8 or higher, explain recovery/missed tasks only when there are incomplete tasks.`;
|
||||
}
|
||||
|
||||
async function buildMissionContextPrompt(userId: string, conversationId: string) {
|
||||
const metadata = await getConversationMetadataPg(userId, conversationId);
|
||||
const missionInstanceId = typeof metadata.missionInstanceId === "string" ? metadata.missionInstanceId : undefined;
|
||||
if (!missionInstanceId) return "";
|
||||
const active = await getActiveMissionPg(userId, missionInstanceId);
|
||||
if (!active) return "";
|
||||
const actions = await listMissionActions(userId, { missionInstanceId });
|
||||
return `\n\nCurrent mission context:
|
||||
- missionInstanceId: ${active.mission.instanceId}
|
||||
- missionId: ${active.mission.missionId}
|
||||
- title: ${active.mission.title}
|
||||
- status: ${active.mission.status}
|
||||
- progress: ${active.mission.progressPercent}%
|
||||
- currentStageId: ${active.mission.currentStageId ?? "none"}
|
||||
- goal: ${active.mission.goal ?? "none"}
|
||||
- openActions: ${actions.length}
|
||||
|
||||
Use this mission context when answering. If a service is needed, prepare a handoff/action instead of completing the service directly.`;
|
||||
}
|
||||
|
||||
function buildSystemPrompt(missionContext = "") {
|
||||
function buildSystemPrompt() {
|
||||
return `You are Grow — a friendly, normal career buddy inside GrowQR. Talk like a real person, not a coach or a robot.
|
||||
|
||||
Personality & Tone:
|
||||
@@ -239,10 +181,9 @@ How to help:
|
||||
- Don't call tools for general chitchat, emotional support, simple explanations, or when a normal text answer would do.
|
||||
- If you don't know something, say so. If a tool fails, just say it failed and move on.
|
||||
- When the user asks about interview prep, roleplay, resume, or Q Score — just answer normally. Only route to a specialist tool if they explicitly say something like "connect me to the interview specialist" or "let me talk to the roleplay agent".
|
||||
- When the user asks to see today’s missions, streak tasks, daily tasks, missed tasks, task progress, or report readiness, call showCareerSprintTasks if CareerSprint context is available.
|
||||
- Use discoverWorkflows only for broad workflow/program discovery. When they ask about memory, use the memory tools. Otherwise, just chat.
|
||||
- When the user asks to see missions or plans, call discoverWorkflows or showMissions. When they ask about memory, use the memory tools. Otherwise, just chat.
|
||||
- Only start a mission if the user clearly says yes to starting one. Don't push.
|
||||
- When you write memory, a quick "Saved." is enough. No need to over-confirm.${missionContext}`
|
||||
- When you write memory, a quick "Saved." is enough. No need to over-confirm.`
|
||||
}
|
||||
|
||||
function safeAgentRegistry() {
|
||||
@@ -297,107 +238,8 @@ When helping:
|
||||
Return compact, bullet-heavy markdown.`,
|
||||
};
|
||||
|
||||
function careerSprintTasksPayload(growContext: any, dayIndex?: number) {
|
||||
const days = Array.isArray(growContext?.days) ? growContext.days : [];
|
||||
const requestedDay = typeof dayIndex === "number" ? dayIndex : Number(growContext?.activeDayIndex ?? 1);
|
||||
const clampedDay = Math.max(1, Math.min(7, requestedDay || 1));
|
||||
const activeDay = days.find((day: any) => Number(day?.dayIndex) === clampedDay) ?? growContext?.activeDay ?? days[0] ?? null;
|
||||
const tasks = Array.isArray(activeDay?.tasks) ? activeDay.tasks : [];
|
||||
const missedTasks = days.flatMap((day: any) => (Array.isArray(day?.tasks) ? day.tasks : []).filter((task: any) => task?.status !== "completed"));
|
||||
function buildConversationTools(userId: string) {
|
||||
return {
|
||||
kind: "career-sprint-tasks",
|
||||
localDate: growContext?.localDate,
|
||||
activeDayIndex: growContext?.activeDayIndex,
|
||||
requestedDayIndex: clampedDay,
|
||||
streak: growContext?.streak ?? 0,
|
||||
completedCount: growContext?.completedCount ?? 0,
|
||||
totalCount: growContext?.totalCount ?? 0,
|
||||
overallProgressPercent: growContext?.overallProgressPercent ?? 0,
|
||||
reportEligible: Boolean(growContext?.reportEligible),
|
||||
isRecoveryDay: Number(growContext?.activeDayIndex ?? 1) > 7,
|
||||
missedCount: missedTasks.length,
|
||||
missedTasks,
|
||||
day: activeDay,
|
||||
tasks,
|
||||
};
|
||||
}
|
||||
|
||||
function careerSprintReportPayload(growContext: any) {
|
||||
const days = Array.isArray(growContext?.days) ? growContext.days : [];
|
||||
const completedCount = Number(growContext?.completedCount ?? 0);
|
||||
const totalCount = Number(growContext?.totalCount ?? 0);
|
||||
const reportEligible = Boolean(growContext?.reportEligible);
|
||||
const missedTasks = days.flatMap((day: any) => (Array.isArray(day?.tasks) ? day.tasks : []).filter((task: any) => task?.status !== "completed"));
|
||||
const params = new URLSearchParams();
|
||||
if (typeof growContext?.demoIcpId === "string" && growContext.demoIcpId) params.set("icpId", growContext.demoIcpId);
|
||||
if (Number.isFinite(Number(growContext?.activeDayIndex))) params.set("dayIndex", String(growContext.activeDayIndex));
|
||||
const href = `/agents/careersprint-report${params.size ? `?${params.toString()}` : ""}`;
|
||||
return {
|
||||
kind: "career-sprint-report-status",
|
||||
localDate: growContext?.localDate,
|
||||
activeDayIndex: growContext?.activeDayIndex,
|
||||
streak: growContext?.streak ?? 0,
|
||||
completedCount,
|
||||
totalCount,
|
||||
overallProgressPercent: growContext?.overallProgressPercent ?? 0,
|
||||
reportEligible,
|
||||
href,
|
||||
missedCount: missedTasks.length,
|
||||
missedTasks: missedTasks.slice(0, 8),
|
||||
};
|
||||
}
|
||||
|
||||
function deterministicCareerSprintResponse(options: {
|
||||
originalMessages: UIMessage[];
|
||||
toolName: "showCareerSprintTasks" | "showCareerSprintReport";
|
||||
input: Record<string, unknown>;
|
||||
output: unknown;
|
||||
onFinish: (responseMessage: UIMessage) => Promise<void>;
|
||||
}) {
|
||||
const messageId = `assistant-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||||
const toolCallId = `call-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||||
const stream = createUIMessageStream<UIMessage>({
|
||||
originalMessages: options.originalMessages,
|
||||
execute: ({ writer }) => {
|
||||
writer.write({ type: "start", messageId } as any);
|
||||
writer.write({ type: "start-step" } as any);
|
||||
writer.write({
|
||||
type: "tool-input-available",
|
||||
toolCallId,
|
||||
toolName: options.toolName,
|
||||
input: options.input,
|
||||
} as any);
|
||||
writer.write({
|
||||
type: "tool-output-available",
|
||||
toolCallId,
|
||||
output: options.output,
|
||||
} as any);
|
||||
writer.write({ type: "finish-step" } as any);
|
||||
writer.write({ type: "finish", finishReason: "stop" } as any);
|
||||
},
|
||||
onFinish: async ({ responseMessage }) => {
|
||||
await options.onFinish(responseMessage as UIMessage);
|
||||
},
|
||||
});
|
||||
return createUIMessageStreamResponse({ stream });
|
||||
}
|
||||
|
||||
function buildConversationTools(userId: string, growContext?: any) {
|
||||
return {
|
||||
showCareerSprintTasks: tool({
|
||||
description: "Return the current CareerSprint streak tasks from the dashboard context. Use for today missions, daily tasks, streak status, missed tasks, completed tasks, Day 8/recovery, and report readiness.",
|
||||
inputSchema: z.object({
|
||||
dayIndex: z.number().optional().describe("Optional requested CareerSprint day number. Defaults to active day."),
|
||||
}),
|
||||
execute: async ({ dayIndex }) => careerSprintTasksPayload(growContext, dayIndex),
|
||||
}),
|
||||
|
||||
showCareerSprintReport: tool({
|
||||
description: "Return CareerSprint report readiness, KPI completion status, and the report link. Use when the user asks for report, result, outcome, KPI, or 7-day summary.",
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => careerSprintReportPayload(growContext),
|
||||
}),
|
||||
|
||||
discoverWorkflows: tool({
|
||||
description: "Return sellable GrowQR missions as rich UI-ready discovery cards. Use whenever the user asks to see, find, discover, compare, choose, recommend, or understand missions/workflows.",
|
||||
inputSchema: z.object({
|
||||
@@ -496,36 +338,33 @@ function buildConversationTools(userId: string, growContext?: any) {
|
||||
inputSchema: z.object({
|
||||
focus: z.string().optional(),
|
||||
}),
|
||||
execute: async ({ focus }) => {
|
||||
if (growContext && typeof growContext === "object") return careerSprintTasksPayload(growContext);
|
||||
return {
|
||||
kind: "missions",
|
||||
focus,
|
||||
missions: [
|
||||
{
|
||||
id: "mission-share-goal",
|
||||
title: "Tell Grow what you are aiming for",
|
||||
description: "Share target role, company, deadline, and biggest blocker so the agent can set up your first mission.",
|
||||
status: "suggested",
|
||||
rewardLabel: "+10 readiness",
|
||||
},
|
||||
{
|
||||
id: "mission-pick-workflow",
|
||||
title: "Pick a mission to activate",
|
||||
description: "Start Interview-to-Offer if you have an interview coming up, or Career Transition if you are exploring a pivot.",
|
||||
status: "suggested",
|
||||
rewardLabel: "Unlock plan",
|
||||
},
|
||||
{
|
||||
id: "mission-save-memory",
|
||||
title: "Save one durable career fact",
|
||||
description: "Let Grow remember a role target, resume link, deadline, or interview date.",
|
||||
status: "suggested",
|
||||
rewardLabel: "Better recommendations",
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
execute: async ({ focus }) => ({
|
||||
kind: "missions",
|
||||
focus,
|
||||
missions: [
|
||||
{
|
||||
id: "mission-share-goal",
|
||||
title: "Tell Grow what you are aiming for",
|
||||
description: "Share target role, company, deadline, and biggest blocker so the agent can set up your first mission.",
|
||||
status: "suggested",
|
||||
rewardLabel: "+10 readiness",
|
||||
},
|
||||
{
|
||||
id: "mission-pick-workflow",
|
||||
title: "Pick a mission to activate",
|
||||
description: "Start Interview-to-Offer if you have an interview coming up, or Career Transition if you are exploring a pivot.",
|
||||
status: "suggested",
|
||||
rewardLabel: "Unlock plan",
|
||||
},
|
||||
{
|
||||
id: "mission-save-memory",
|
||||
title: "Save one durable career fact",
|
||||
description: "Let Grow remember a role target, resume link, deadline, or interview date.",
|
||||
status: "suggested",
|
||||
rewardLabel: "Better recommendations",
|
||||
},
|
||||
],
|
||||
}),
|
||||
}),
|
||||
|
||||
startWorkflow: tool({
|
||||
@@ -731,28 +570,6 @@ export function conversationRoutes() {
|
||||
return c.json({ conversation }, 201);
|
||||
});
|
||||
|
||||
app.post("/mission", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = createMissionConversationSchema.parse(await c.req.json().catch(() => ({})));
|
||||
const active = await getActiveMissionPg(userId, body.missionInstanceId);
|
||||
if (!active) return c.json({ error: "mission_not_found" }, 404);
|
||||
const conversation = await ensureMissionConversationPg({
|
||||
userId,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId: body.stageId,
|
||||
title: body.title ?? active.mission.shortTitle,
|
||||
source: body.source ?? "mission",
|
||||
});
|
||||
setupGrow(userId).then((grow) => grow.touchConversation({ conversationId: conversation.id, title: conversation.title })).catch((err) => console.warn("growActor mission conversation mirror failed", err));
|
||||
return c.json({
|
||||
conversation,
|
||||
mission: active.mission,
|
||||
snapshot: active.snapshot,
|
||||
messages: await listMessagesPg(userId, conversation.id),
|
||||
}, 201);
|
||||
});
|
||||
|
||||
app.get("/:conversationId", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const conversationId = c.req.param("conversationId");
|
||||
@@ -792,37 +609,12 @@ export function conversationRoutes() {
|
||||
withActorRecovery("conversationActor", () => conversation.addMessage({ role: "user", content: latestUserText, sender: "User" })).catch((err) => console.warn("conversationActor user-message mirror failed", err));
|
||||
}
|
||||
|
||||
const visualTool = forcedToolForPrompt(latestUserText, Boolean(body.growContext));
|
||||
if (visualTool === "showCareerSprintTasks" || visualTool === "showCareerSprintReport") {
|
||||
const output = visualTool === "showCareerSprintTasks"
|
||||
? careerSprintTasksPayload(body.growContext)
|
||||
: careerSprintReportPayload(body.growContext);
|
||||
return deterministicCareerSprintResponse({
|
||||
originalMessages: body.messages,
|
||||
toolName: visualTool,
|
||||
input: {},
|
||||
output,
|
||||
onFinish: async (responseMessage) => {
|
||||
const assistantText = textFromMessage(responseMessage);
|
||||
await addMessagePg(userId, {
|
||||
id: responseMessage.id,
|
||||
conversationId,
|
||||
role: "assistant",
|
||||
content: assistantText || JSON.stringify(responseMessage.parts ?? []),
|
||||
sender: "GrowQR",
|
||||
});
|
||||
await touchConversationPg(userId, conversationId, latestUserText.slice(0, 64) || undefined);
|
||||
await growPromise;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const missionContext = `${await buildMissionContextPrompt(userId, conversationId)}${buildGrowContextPrompt(body.growContext)}`;
|
||||
const visualTool = forcedToolForPrompt(latestUserText);
|
||||
const result = streamText({
|
||||
model: getConversationModel(),
|
||||
system: buildSystemPrompt(missionContext),
|
||||
system: buildSystemPrompt(),
|
||||
messages: await convertToModelMessages(body.messages),
|
||||
tools: buildConversationTools(userId, body.growContext),
|
||||
tools: buildConversationTools(userId),
|
||||
toolChoice: visualTool ? { type: "tool", toolName: visualTool } : "auto",
|
||||
stopWhen: stepCountIs(5),
|
||||
});
|
||||
@@ -845,7 +637,8 @@ export function conversationRoutes() {
|
||||
sender: "GrowQR",
|
||||
})).catch((err) => console.warn("conversationActor assistant-message mirror failed", err));
|
||||
await touchConversationPg(userId, conversationId, latestUserText.slice(0, 64) || undefined);
|
||||
await growPromise;
|
||||
const grow = await growPromise;
|
||||
if (grow) await grow.touchConversation({ conversationId, title: latestUserText.slice(0, 64) || undefined }).catch((err) => console.warn("growActor touch mirror failed", err));
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,299 +0,0 @@
|
||||
import { Hono } from "hono";
|
||||
import { z } from "zod";
|
||||
import { createClient, type Client } from "rivetkit/client";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import { config } from "../config.js";
|
||||
import { log } from "../log.js";
|
||||
import {
|
||||
dailyMissionMessageSchema,
|
||||
dailyMissionTaskSchema,
|
||||
type DailyMissionResult,
|
||||
runDailyMissionAgent,
|
||||
streamDailyMissionAgent,
|
||||
} from "../agents/daily-mission.js";
|
||||
import type { Registry } from "../actors/registry.js";
|
||||
import type { MissionActorType, MissionSnapshot } from "../actors/missions/types.js";
|
||||
import { addMessagePg, ensureMissionConversationPg, getActiveMissionPg, listMessagesPg, upsertActiveMissionPg } from "../grow/persistence.js";
|
||||
|
||||
const chatSchema = z.object({
|
||||
task: dailyMissionTaskSchema,
|
||||
messages: z.array(dailyMissionMessageSchema).min(1).max(24),
|
||||
missionInstanceId: z.string().optional(),
|
||||
missionId: z.string().optional(),
|
||||
stageId: z.string().optional(),
|
||||
conversationId: z.string().optional(),
|
||||
});
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
function getClient(): Client<Registry> {
|
||||
return (_client ??= createClient<Registry>(config.rivetClientEndpoint));
|
||||
}
|
||||
|
||||
function missionActorFor(userId: string, instanceId: string, actorType: MissionActorType) {
|
||||
const client = getClient();
|
||||
switch (actorType) {
|
||||
case "interviewToOfferMissionActor": return client.interviewToOfferMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "careerTransitionMissionActor": return client.careerTransitionMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "salaryNegotiationWarRoomMissionActor": return client.salaryNegotiationWarRoomMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "promotionReadinessMissionActor": return client.promotionReadinessMissionActor.getOrCreate([userId, instanceId]);
|
||||
case "personalBrandOpportunityEngineMissionActor": return client.personalBrandOpportunityEngineMissionActor.getOrCreate([userId, instanceId]);
|
||||
}
|
||||
}
|
||||
|
||||
function buildId(prefix: string) {
|
||||
return `${prefix}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||||
}
|
||||
|
||||
function latestUserText(messages: z.infer<typeof dailyMissionMessageSchema>[]) {
|
||||
return [...messages].reverse().find((message) => message.role === "user")?.content.trim();
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
function sse(event: string, payload: Record<string, unknown>) {
|
||||
return encoder.encode(`event: ${event}\ndata: ${JSON.stringify(payload)}\n\n`);
|
||||
}
|
||||
|
||||
function sanitizeAssistantText(text: string) {
|
||||
return text
|
||||
.replace(/[\u2013\u2014]/g, ". ")
|
||||
.replace(/[\u2018\u2019]/g, "'")
|
||||
.replace(/[\u201C\u201D]/g, '"')
|
||||
.replace(/\u2026/g, "...")
|
||||
.replace(/^\s*(Perfect|Great|Absolutely|Sure)[.!,:;-]*\s*/i, "")
|
||||
.replace(/\s+\./g, ".")
|
||||
.replace(/\.{2,}/g, ".")
|
||||
.replace(/\s{2,}/g, " ");
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function visibleTextChunks(text: string) {
|
||||
const words = text.match(/\S+\s*/g) ?? [text];
|
||||
const chunks: string[] = [];
|
||||
let current = "";
|
||||
|
||||
for (const word of words) {
|
||||
current += word;
|
||||
if (current.length >= 12 || /[.!?]\s*$/.test(current)) {
|
||||
chunks.push(current);
|
||||
current = "";
|
||||
}
|
||||
}
|
||||
|
||||
if (current) chunks.push(current);
|
||||
return chunks;
|
||||
}
|
||||
|
||||
async function enqueueVisibleText(controller: ReadableStreamDefaultController<Uint8Array>, text: string) {
|
||||
for (const chunk of visibleTextChunks(sanitizeAssistantText(text))) {
|
||||
controller.enqueue(sse("delta", { text: chunk }));
|
||||
await sleep(28);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyDailyMissionResult(input: {
|
||||
userId: string;
|
||||
body: z.infer<typeof chatSchema>;
|
||||
result: DailyMissionResult;
|
||||
}) {
|
||||
const { userId, body, result } = input;
|
||||
let conversationId = body.conversationId;
|
||||
let missionInstanceId = body.missionInstanceId;
|
||||
let responseStageId = body.stageId ?? body.task.questId;
|
||||
let snapshot: MissionSnapshot | null | undefined;
|
||||
|
||||
if (missionInstanceId) {
|
||||
const active = await getActiveMissionPg(userId, missionInstanceId);
|
||||
if (active) {
|
||||
const requestedStageId = body.stageId ?? body.task.questId;
|
||||
const stageId = active.snapshot?.stages.some((stage) => stage.id === requestedStageId)
|
||||
? requestedStageId
|
||||
: active.mission.currentStageId;
|
||||
responseStageId = stageId ?? responseStageId;
|
||||
|
||||
const conversation = await ensureMissionConversationPg({
|
||||
userId,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId,
|
||||
title: body.task.questTitle,
|
||||
source: "daily-mission",
|
||||
});
|
||||
log.info({
|
||||
userId,
|
||||
agent: "conversation-actor",
|
||||
conversationId: conversation.id,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId,
|
||||
service: body.task.service,
|
||||
}, "conversation actor linked to daily mission");
|
||||
conversationId = conversation.id;
|
||||
missionInstanceId = active.mission.instanceId;
|
||||
const conversationActor = getClient().conversationActor.getOrCreate([userId, conversation.id]);
|
||||
const latestUser = latestUserText(body.messages);
|
||||
if (latestUser) {
|
||||
const userMessage = {
|
||||
id: buildId("user"),
|
||||
conversationId: conversation.id,
|
||||
role: "user" as const,
|
||||
sender: "User",
|
||||
content: latestUser,
|
||||
};
|
||||
await addMessagePg(userId, userMessage);
|
||||
conversationActor.addMessage(userMessage).catch(() => undefined);
|
||||
}
|
||||
const assistantMessage = {
|
||||
id: buildId("assistant"),
|
||||
conversationId: conversation.id,
|
||||
role: "assistant" as const,
|
||||
sender: "Daily Mission",
|
||||
content: result.reply,
|
||||
};
|
||||
await addMessagePg(userId, assistantMessage);
|
||||
conversationActor.addMessage(assistantMessage).catch(() => undefined);
|
||||
|
||||
if (result.completed && active.mission.actorType && stageId) {
|
||||
snapshot = await missionActorFor(userId, active.mission.instanceId, active.mission.actorType).updateStage({
|
||||
stageId,
|
||||
status: "done",
|
||||
progressPercent: 100,
|
||||
outputSummary: result.updateSummary,
|
||||
}).catch(() => active.snapshot ?? null);
|
||||
log.info({
|
||||
userId,
|
||||
agent: active.mission.actorType,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
missionId: active.mission.missionId,
|
||||
stageId,
|
||||
completed: result.completed,
|
||||
progressPercent: snapshot?.progressPercent,
|
||||
currentStageId: snapshot?.currentStageId,
|
||||
}, "mission actor stage update requested from daily mission");
|
||||
if (snapshot) {
|
||||
await upsertActiveMissionPg(userId, {
|
||||
instanceId: snapshot.instanceId,
|
||||
missionId: snapshot.missionId,
|
||||
workflowId: snapshot.workflowId,
|
||||
title: snapshot.title,
|
||||
shortTitle: snapshot.shortTitle,
|
||||
status: snapshot.status,
|
||||
progressPercent: snapshot.progressPercent,
|
||||
currentStageId: snapshot.currentStageId,
|
||||
goal: snapshot.goal,
|
||||
actorType: active.mission.actorType,
|
||||
createdAt: new Date(snapshot.createdAt).getTime(),
|
||||
updatedAt: new Date(snapshot.updatedAt).getTime(),
|
||||
}, snapshot);
|
||||
}
|
||||
} else {
|
||||
snapshot = active.snapshot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
agent: "daily-mission",
|
||||
message: result.reply,
|
||||
completed: result.completed,
|
||||
updateSummary: result.updateSummary,
|
||||
actionLabel: result.actionLabel,
|
||||
actionRoute: result.actionRoute,
|
||||
conversationId,
|
||||
missionInstanceId,
|
||||
stageId: responseStageId,
|
||||
snapshot,
|
||||
messages: conversationId ? await listMessagesPg(userId, conversationId) : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function dailyMissionRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
|
||||
app.post("/chat", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = chatSchema.parse(await c.req.json());
|
||||
log.info({
|
||||
userId,
|
||||
agent: "daily-mission",
|
||||
missionInstanceId: body.missionInstanceId,
|
||||
missionId: body.missionId,
|
||||
stageId: body.stageId,
|
||||
service: body.task.service,
|
||||
route: body.task.route,
|
||||
questTitle: body.task.questTitle,
|
||||
subtask: body.task.subtask,
|
||||
}, "daily mission actor request");
|
||||
const result = await runDailyMissionAgent({ userId, ...body });
|
||||
return c.json(await applyDailyMissionResult({ userId, body, result }));
|
||||
});
|
||||
|
||||
app.post("/chat/stream", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = chatSchema.parse(await c.req.json());
|
||||
log.info({
|
||||
userId,
|
||||
agent: "daily-mission",
|
||||
missionInstanceId: body.missionInstanceId,
|
||||
missionId: body.missionId,
|
||||
stageId: body.stageId,
|
||||
service: body.task.service,
|
||||
route: body.task.route,
|
||||
questTitle: body.task.questTitle,
|
||||
subtask: body.task.subtask,
|
||||
streaming: true,
|
||||
}, "daily mission actor stream request");
|
||||
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
try {
|
||||
const streamed = await streamDailyMissionAgent({ userId, ...body });
|
||||
let reply = "";
|
||||
if (streamed.kind === "static") {
|
||||
reply = streamed.result.reply;
|
||||
await enqueueVisibleText(controller, reply);
|
||||
const finalPayload = await applyDailyMissionResult({ userId, body, result: streamed.result });
|
||||
controller.enqueue(sse("final", finalPayload));
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
for await (const delta of streamed.textStream) {
|
||||
const cleanDelta = sanitizeAssistantText(delta);
|
||||
reply += cleanDelta;
|
||||
controller.enqueue(sse("delta", { text: cleanDelta }));
|
||||
}
|
||||
let result = streamed.finalize(reply);
|
||||
result = {
|
||||
...result,
|
||||
reply: sanitizeAssistantText(result.reply),
|
||||
updateSummary: result.updateSummary ? sanitizeAssistantText(result.updateSummary) : result.updateSummary,
|
||||
};
|
||||
if (!result.reply.trim()) {
|
||||
throw new Error("daily_mission_empty_model_reply");
|
||||
}
|
||||
const finalPayload = await applyDailyMissionResult({ userId, body, result });
|
||||
controller.enqueue(sse("final", finalPayload));
|
||||
controller.close();
|
||||
} catch (error) {
|
||||
controller.enqueue(sse("error", { error: error instanceof Error ? error.message : String(error) }));
|
||||
controller.close();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"content-type": "text/event-stream; charset=utf-8",
|
||||
"cache-control": "no-cache, no-transform",
|
||||
connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -4,17 +4,8 @@ import { config } from "../config.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { growEvents } from "../db/schema.js";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import {
|
||||
markGrowEventFailed,
|
||||
markGrowEventProcessed,
|
||||
markGrowEventProcessing,
|
||||
recordGrowEventWithResult,
|
||||
shouldRouteGrowEvent,
|
||||
} from "../events/record-grow-event.js";
|
||||
import { recordGrowEvent } from "../events/record-grow-event.js";
|
||||
import { routeGrowEventToUserActor } from "../events/route-to-user-actor.js";
|
||||
import { applyQscoreProjection } from "../events/projectors/qscore-projector.js";
|
||||
import { applyServiceSessionProjection } from "../events/projectors/service-session-projector.js";
|
||||
import { ensureOnboardingSideEffectsForEvent } from "../events/onboarding-ledger.js";
|
||||
|
||||
function serviceAuthorized(auth: string | undefined) {
|
||||
const token = (auth ?? "").replace(/^Bearer\s+/i, "").trim();
|
||||
@@ -24,61 +15,12 @@ function serviceAuthorized(auth: string | undefined) {
|
||||
}
|
||||
|
||||
async function ingest(body: unknown, userId?: string, source?: string) {
|
||||
const { event, inserted } = await recordGrowEventWithResult(body, { userId, source });
|
||||
|
||||
// Route only newly inserted, user-resolved, pending events to the actor.
|
||||
// Dedupe hits (inserted=false) never re-enqueue, preventing actor saturation.
|
||||
const routed = shouldRouteGrowEvent(event, inserted);
|
||||
const route = routed
|
||||
? await routeGrowEventToUserActor(event).catch((err) => ({
|
||||
routed: false as const,
|
||||
reason: err instanceof Error ? err.message : String(err),
|
||||
}))
|
||||
: { routed: false as const, reason: "dedupe_or_unresolved" as const };
|
||||
|
||||
// Dedupe hit: event was already processed (or is unresolved). Return early
|
||||
// with idempotent semantics — no in-process re-projection.
|
||||
if (!inserted) {
|
||||
return {
|
||||
event,
|
||||
processingStatus: event.processingStatus,
|
||||
route,
|
||||
qscore: { signals: [], score: undefined, idempotent: true },
|
||||
onboarding: { qscoreBaselineSeeded: false, curatorOnboarding: { status: "already_processed" } },
|
||||
};
|
||||
}
|
||||
if (!event.userId) {
|
||||
return {
|
||||
event,
|
||||
processingStatus: event.processingStatus,
|
||||
route,
|
||||
qscore: { signals: [], score: undefined, skipped: "missing_user_id" },
|
||||
onboarding: { qscoreBaselineSeeded: false, curatorOnboarding: { status: "skipped", reason: "missing_user_id" } },
|
||||
};
|
||||
}
|
||||
|
||||
await markGrowEventProcessing(event.id);
|
||||
try {
|
||||
await applyServiceSessionProjection(event);
|
||||
const qscore = await applyQscoreProjection(event);
|
||||
const onboarding = await ensureOnboardingSideEffectsForEvent(event);
|
||||
if (
|
||||
onboarding.curatorOnboarding.status === "skipped" &&
|
||||
onboarding.curatorOnboarding.reason === "loop_failed"
|
||||
) {
|
||||
throw new Error("curator_onboarding_loop_failed");
|
||||
}
|
||||
// Always mark "processed" after successful synchronous projections, regardless
|
||||
// of route outcome. The actor will still run mission reducers on "processed"
|
||||
// events (isProcessableStatus allows "processed") — projections are idempotent
|
||||
// and the in-memory processedEventIds guard prevents double-processing.
|
||||
// This avoids orphaning the event if the route call failed.
|
||||
await markGrowEventProcessed(event.id);
|
||||
return { event, processingStatus: "processed" as const, route, qscore, onboarding };
|
||||
} catch (err) {
|
||||
await markGrowEventFailed(event.id, err);
|
||||
throw err;
|
||||
}
|
||||
const event = await recordGrowEvent(body, { userId, source });
|
||||
const route = await routeGrowEventToUserActor(event).catch((err) => ({
|
||||
routed: false as const,
|
||||
reason: err instanceof Error ? err.message : String(err),
|
||||
}));
|
||||
return { event, route };
|
||||
}
|
||||
|
||||
export function eventRoutes() {
|
||||
@@ -88,8 +30,8 @@ export function eventRoutes() {
|
||||
app.post("/ingest", requireUser, async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json().catch(() => ({}));
|
||||
const { event, processingStatus, route, qscore, onboarding } = await ingest(body, userId);
|
||||
return c.json({ eventId: event.id, processingStatus, route, qscore, onboarding }, 202);
|
||||
const { event, route } = await ingest(body, userId);
|
||||
return c.json({ eventId: event.id, processingStatus: event.processingStatus, route }, 202);
|
||||
});
|
||||
|
||||
// Service-to-service ingress. Services may include userId directly, or we resolve it from session correlation.
|
||||
@@ -99,8 +41,8 @@ export function eventRoutes() {
|
||||
}
|
||||
const body = await c.req.json().catch(() => ({}));
|
||||
const source = c.req.header("x-growqr-source") ?? undefined;
|
||||
const { event, processingStatus, route, qscore, onboarding } = await ingest(body, undefined, source);
|
||||
return c.json({ eventId: event.id, processingStatus, route, qscore, onboarding }, 202);
|
||||
const { event, route } = await ingest(body, undefined, source);
|
||||
return c.json({ eventId: event.id, processingStatus: event.processingStatus, route }, 202);
|
||||
});
|
||||
|
||||
app.get("/", requireUser, async (c) => {
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
import { Hono } from "hono";
|
||||
import { config } from "../config.js";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import { dismissHomeNotification, getHomeFeed, getHomeFeedDebugCounts } from "../home/home-feed.js";
|
||||
import { getRequestUserProfile } from "../services/user-context.js";
|
||||
import { log } from "../log.js";
|
||||
import {
|
||||
getSystemNotificationPreferences,
|
||||
listSystemNotifications,
|
||||
markAllSystemNotificationsRead,
|
||||
markSystemNotificationRead,
|
||||
updateSystemNotificationPreferences,
|
||||
type SystemNotificationKind,
|
||||
type SystemNotificationPreference,
|
||||
} from "../notifications/system-notifications.js";
|
||||
import { seedDemoHome } from "../home/seed-demo-home.js";
|
||||
|
||||
function canSeedDemo(userId: string) {
|
||||
return config.nodeEnv !== "production" || config.adminUserIds.includes(userId);
|
||||
}
|
||||
|
||||
async function getUserServiceProfile(req: Request): Promise<{ userProfile?: Record<string, unknown>; preferences?: Record<string, unknown> }> {
|
||||
const target = new URL("/api/v1/users/me", config.userServiceUrl.replace(/\/$/, ""));
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
try {
|
||||
const res = await fetch(target, { method: "GET", headers });
|
||||
if (!res.ok) return {};
|
||||
const userProfile = await res.json().catch(() => null) as Record<string, unknown> | null;
|
||||
const preferences = userProfile?.preferences;
|
||||
return {
|
||||
userProfile: userProfile ?? undefined,
|
||||
preferences: preferences && typeof preferences === "object" && !Array.isArray(preferences) ? preferences as Record<string, unknown> : {},
|
||||
};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function homeRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
@@ -19,12 +33,8 @@ export function homeRoutes() {
|
||||
|
||||
app.get("/feed", async (c) => {
|
||||
const refresh = c.req.query("refresh") === "1" || c.req.query("refresh") === "true";
|
||||
const userId = c.get("userId");
|
||||
const profile = await getRequestUserProfile(c.req.raw, userId).catch((err) => {
|
||||
log.warn({ err, userId }, "home feed continuing without user-service profile");
|
||||
return {};
|
||||
});
|
||||
return c.json(await getHomeFeed(userId, { refresh, ...profile }));
|
||||
const profile = await getUserServiceProfile(c.req.raw);
|
||||
return c.json(await getHomeFeed(c.get("userId"), { refresh, ...profile }));
|
||||
});
|
||||
|
||||
app.post("/notifications/:id/dismiss", async (c) => {
|
||||
@@ -32,33 +42,13 @@ export function homeRoutes() {
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
|
||||
app.get("/system-notifications", async (c) => {
|
||||
return c.json(await listSystemNotifications(c.get("userId")));
|
||||
});
|
||||
|
||||
app.post("/system-notifications/:id/read", async (c) => {
|
||||
const notification = await markSystemNotificationRead(c.get("userId"), c.req.param("id"));
|
||||
if (!notification) return c.json({ error: "notification_not_found" }, 404);
|
||||
return c.json({ notification });
|
||||
});
|
||||
|
||||
app.post("/system-notifications/read-all", async (c) => {
|
||||
return c.json(await markAllSystemNotificationsRead(c.get("userId")));
|
||||
});
|
||||
|
||||
app.get("/system-notifications/preferences", async (c) => {
|
||||
return c.json({ preferences: await getSystemNotificationPreferences(c.get("userId")) });
|
||||
});
|
||||
|
||||
app.put("/system-notifications/preferences", async (c) => {
|
||||
const body = await c.req.json().catch(() => ({}));
|
||||
const preferences = typeof body === "object" && body !== null && "preferences" in body
|
||||
? (body as { preferences?: Partial<Record<SystemNotificationKind, Partial<SystemNotificationPreference>>> }).preferences ?? {}
|
||||
: {};
|
||||
return c.json({ preferences: await updateSystemNotificationPreferences(c.get("userId"), preferences) });
|
||||
});
|
||||
|
||||
app.get("/debug/counts", async (c) => c.json(await getHomeFeedDebugCounts(c.get("userId"))));
|
||||
|
||||
app.post("/demo/seed", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
if (!canSeedDemo(userId)) return c.json({ error: "forbidden" }, 403);
|
||||
return c.json(await seedDemoHome(userId));
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
import Docker from "dockerode";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { Hono } from "hono";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
|
||||
const LOG_CONTAINERS = {
|
||||
backend: "growqr-backend",
|
||||
dashboard: "growqr-dashboard",
|
||||
actors: "growqr-rivet",
|
||||
interview: "interview-service-api-1",
|
||||
roleplay: "roleplay-service-api-1",
|
||||
social: "growqr_social_api",
|
||||
courses: "courses_service-api-1",
|
||||
assessment: "assessment-service-api-1",
|
||||
matchmaking: "matchmaking-service-api-1",
|
||||
} as const;
|
||||
|
||||
type LogService = keyof typeof LOG_CONTAINERS;
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
function parseServices(value: string | undefined): LogService[] {
|
||||
const requested = (value ?? "backend,dashboard,actors,interview,roleplay")
|
||||
.split(",")
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean);
|
||||
const services = requested.filter((item): item is LogService => item in LOG_CONTAINERS);
|
||||
return services.length ? services : ["backend", "dashboard", "actors", "interview", "roleplay"];
|
||||
}
|
||||
|
||||
function sse(event: string, payload: Record<string, unknown>) {
|
||||
return encoder.encode(`event: ${event}\ndata: ${JSON.stringify(payload)}\n\n`);
|
||||
}
|
||||
|
||||
function splitLines(chunk: Buffer | string) {
|
||||
return chunk
|
||||
.toString("utf8")
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trimEnd())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
export function logRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
|
||||
app.get("/stream", (c) => {
|
||||
const services = parseServices(c.req.query("services"));
|
||||
const tail = Math.max(10, Math.min(500, Number(c.req.query("tail") ?? 120)));
|
||||
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
|
||||
const openStreams: Array<{ destroy: () => void }> = [];
|
||||
let closed = false;
|
||||
let heartbeat: NodeJS.Timeout | undefined;
|
||||
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
const send = (event: string, payload: Record<string, unknown>) => {
|
||||
if (!closed) controller.enqueue(sse(event, payload));
|
||||
};
|
||||
|
||||
send("ready", { services, tail, at: new Date().toISOString() });
|
||||
|
||||
for (const service of services) {
|
||||
const containerName = LOG_CONTAINERS[service];
|
||||
try {
|
||||
const stream = await docker.getContainer(containerName).logs({
|
||||
follow: true,
|
||||
stdout: true,
|
||||
stderr: true,
|
||||
timestamps: true,
|
||||
tail,
|
||||
});
|
||||
const stdout = new PassThrough();
|
||||
const stderr = new PassThrough();
|
||||
docker.modem.demuxStream(stream, stdout, stderr);
|
||||
openStreams.push(stream as unknown as { destroy: () => void }, stdout, stderr);
|
||||
|
||||
stdout.on("data", (chunk) => {
|
||||
for (const line of splitLines(chunk)) send("log", { service, stream: "stdout", line });
|
||||
});
|
||||
stderr.on("data", (chunk) => {
|
||||
for (const line of splitLines(chunk)) send("log", { service, stream: "stderr", line });
|
||||
});
|
||||
stream.on("end", () => send("status", { service, status: "ended" }));
|
||||
stream.on("error", (error) => send("error", { service, error: error instanceof Error ? error.message : String(error) }));
|
||||
} catch (error) {
|
||||
send("error", { service, container: containerName, error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
}
|
||||
|
||||
heartbeat = setInterval(() => send("ping", { at: new Date().toISOString() }), 20_000);
|
||||
c.req.raw.signal.addEventListener("abort", () => {
|
||||
closed = true;
|
||||
if (heartbeat) clearInterval(heartbeat);
|
||||
for (const stream of openStreams) stream.destroy();
|
||||
controller.close();
|
||||
});
|
||||
},
|
||||
cancel() {
|
||||
closed = true;
|
||||
if (heartbeat) clearInterval(heartbeat);
|
||||
for (const stream of openStreams) stream.destroy();
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(body, {
|
||||
headers: {
|
||||
"content-type": "text/event-stream; charset=utf-8",
|
||||
"cache-control": "no-cache, no-transform",
|
||||
connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -12,9 +12,6 @@ import { buildDeterministicMissionSuggestions } from "../missions/suggestions.js
|
||||
import { createMissionAction, getMissionAction, listMissionActions, updateMissionActionStatus } from "../missions/actions.js";
|
||||
import { recordGrowEvent } from "../events/record-grow-event.js";
|
||||
import { routeGrowEventToUserActor } from "../events/route-to-user-actor.js";
|
||||
import { getRequestUserPreferences } from "../services/user-context.js";
|
||||
import { missionDetailHref } from "../missions/reducer-helpers.js";
|
||||
import { runPassiveMissionReviews } from "../missions/lifecycle.js";
|
||||
|
||||
let _client: Client<Registry> | null = null;
|
||||
function getClient(): Client<Registry> {
|
||||
@@ -70,12 +67,6 @@ const snoozeActionSchema = z.object({
|
||||
until: z.string().datetime().optional(),
|
||||
});
|
||||
|
||||
const passiveReviewSchema = z.object({
|
||||
date: z.string().optional(),
|
||||
force: z.boolean().optional(),
|
||||
limit: z.number().int().min(1).max(50).optional(),
|
||||
});
|
||||
|
||||
const createInstanceId = (missionId: string) =>
|
||||
`${missionId}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||||
|
||||
@@ -113,6 +104,18 @@ async function getMissionSnapshot(userId: string, active: GrowActiveMission): Pr
|
||||
return missionActorFor(userId, active.instanceId, active.actorType).getState();
|
||||
}
|
||||
|
||||
async function getUserPreferences(req: Request): Promise<Record<string, unknown>> {
|
||||
const target = new URL("/api/v1/users/me", config.userServiceUrl.replace(/\/$/, ""));
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
const res = await fetch(target, { method: "GET", headers });
|
||||
if (!res.ok) return {};
|
||||
const user = await res.json().catch(() => null) as Record<string, unknown> | null;
|
||||
const preferences = user?.preferences;
|
||||
return preferences && typeof preferences === "object" && !Array.isArray(preferences) ? preferences as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
export function missionRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
@@ -179,7 +182,7 @@ export function missionRoutes() {
|
||||
|
||||
const windowEnd = new Date();
|
||||
const windowStart = new Date(windowEnd.getTime() - 24 * 60 * 60 * 1000);
|
||||
const preferences = await getRequestUserPreferences(c.req.raw, userId) ?? {};
|
||||
const preferences = await getUserPreferences(c.req.raw);
|
||||
const run = await createMissionCoachRunPg({
|
||||
userId,
|
||||
missionInstanceId: active.mission.instanceId,
|
||||
@@ -252,7 +255,7 @@ export function missionRoutes() {
|
||||
if (active?.mission.actorType) {
|
||||
await missionActorFor(userId, active.mission.instanceId, active.mission.actorType).runAction({ actionId: existing.id }).catch(() => undefined);
|
||||
}
|
||||
const href = typeof existing.payload?.href === "string" ? existing.payload.href : missionDetailHref(existing.missionInstanceId);
|
||||
const href = typeof existing.payload?.href === "string" ? existing.payload.href : `/missions/active?missionInstanceId=${encodeURIComponent(existing.missionInstanceId)}`;
|
||||
const action = await updateMissionActionStatus(userId, existing.id, {
|
||||
status: "done",
|
||||
result: {
|
||||
@@ -292,17 +295,6 @@ export function missionRoutes() {
|
||||
return c.json({ action });
|
||||
});
|
||||
|
||||
app.post("/passive/run", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = passiveReviewSchema.parse(await c.req.json().catch(() => ({})));
|
||||
return c.json(await runPassiveMissionReviews({
|
||||
userId,
|
||||
date: body.date,
|
||||
force: body.force,
|
||||
limit: body.limit,
|
||||
}));
|
||||
});
|
||||
|
||||
app.post("/:missionId/start", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const missionId = c.req.param("missionId");
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
import { Hono } from "hono";
|
||||
import { HTTPException } from "hono/http-exception";
|
||||
import { createHash } from "node:crypto";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import { config } from "../config.js";
|
||||
import { listServiceCapabilities } from "../workflows/service-capabilities.js";
|
||||
import { interviewService, ProductServiceError, resumeService, roleplayService, type JsonObject } from "../services/product-service-clients.js";
|
||||
import { interviewService, resumeService, roleplayService, type JsonObject } from "../services/product-service-clients.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { events, growEvents, missionServiceSessions } from "../db/schema.js";
|
||||
import { events, growQscoreLatest, growQscoreProjectionState } from "../db/schema.js";
|
||||
import { recordGrowEvent } from "../events/record-grow-event.js";
|
||||
import { routeGrowEventToUserActor } from "../events/route-to-user-actor.js";
|
||||
import { ensureOnboardingBaselineQscoreFromLedger } from "../events/onboarding-ledger.js";
|
||||
import { getRequestUserPreferences, getRequestUserProfile } from "../services/user-context.js";
|
||||
import { buildMatchmakingGatewayEvent } from "../services/matchmaking-events.js";
|
||||
import { ensureOnboardingBaselineQscore } from "../events/onboarding-qscore.js";
|
||||
import { log } from "../log.js";
|
||||
import { DEFAULT_QSCORE_ORG_ID, getQscoreFromService } from "../services/qscore-proxy.js";
|
||||
|
||||
const LANDING_AGENTS = [
|
||||
{ id: "resume", title: "Resume", agent: "Resume Strategist", description: "Resume proof, versions, parsing, analysis, and mission artifacts.", route: "/agents/resume" },
|
||||
@@ -48,200 +43,6 @@ function missionFromBody(body: JsonObject): Record<string, unknown> | undefined
|
||||
return mission && typeof mission === "object" && !Array.isArray(mission) ? (mission as Record<string, unknown>) : undefined;
|
||||
}
|
||||
|
||||
function missionFromRequest(req: Request, body?: JsonObject): Record<string, unknown> | undefined {
|
||||
const fromBody = body ? missionFromBody(body) : undefined;
|
||||
if (fromBody) return fromBody;
|
||||
|
||||
const url = new URL(req.url);
|
||||
const instanceId = getString(url.searchParams.get("missionInstanceId"));
|
||||
const missionId = getString(url.searchParams.get("missionId"));
|
||||
const stageId = getString(url.searchParams.get("stageId"));
|
||||
const source = getString(url.searchParams.get("source"));
|
||||
|
||||
if (!instanceId && !missionId && !stageId) return undefined;
|
||||
return {
|
||||
instanceId,
|
||||
missionId,
|
||||
stageId,
|
||||
source: source ?? "mission",
|
||||
};
|
||||
}
|
||||
|
||||
function curatorTaskIdFromRequest(req: Request, body?: JsonObject) {
|
||||
const params = body && isRecord(body.params) ? body.params : undefined;
|
||||
const fromBody = body
|
||||
? getString((body as Record<string, unknown>).curatorTaskId ?? (body as Record<string, unknown>).taskId ?? params?.curatorTaskId ?? params?.taskId)
|
||||
: undefined;
|
||||
if (fromBody) return fromBody;
|
||||
const url = new URL(req.url);
|
||||
return getString(url.searchParams.get("curatorTaskId"));
|
||||
}
|
||||
|
||||
function orchestrationFromMission(mission?: Record<string, unknown>, taskId?: string) {
|
||||
return {
|
||||
...(taskId ? { taskId, curatorTaskId: taskId } : {}),
|
||||
...(getString(mission?.missionId ?? mission?.mission_id) ? { missionId: getString(mission?.missionId ?? mission?.mission_id) } : {}),
|
||||
...(getString(mission?.instanceId ?? mission?.instance_id ?? mission?.missionInstanceId ?? mission?.mission_instance_id)
|
||||
? { missionInstanceId: getString(mission?.instanceId ?? mission?.instance_id ?? mission?.missionInstanceId ?? mission?.mission_instance_id) }
|
||||
: {}),
|
||||
...(getString(mission?.stageId ?? mission?.stage_id) ? { stageId: getString(mission?.stageId ?? mission?.stage_id) } : {}),
|
||||
...(getString(mission?.source) ? { source: getString(mission?.source) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
type SessionMissionContext = {
|
||||
mission?: Record<string, unknown>;
|
||||
taskId?: string;
|
||||
};
|
||||
|
||||
async function sessionMissionContext(serviceId: "interview" | "roleplay", sessionId: string): Promise<SessionMissionContext> {
|
||||
const [row] = await db
|
||||
.select({
|
||||
missionInstanceId: missionServiceSessions.missionInstanceId,
|
||||
missionId: missionServiceSessions.missionId,
|
||||
stageId: missionServiceSessions.stageId,
|
||||
metadata: missionServiceSessions.metadata,
|
||||
})
|
||||
.from(missionServiceSessions)
|
||||
.where(and(eq(missionServiceSessions.serviceId, serviceId), eq(missionServiceSessions.externalId, sessionId)))
|
||||
.limit(1);
|
||||
const metadata = row && isRecord(row.metadata) ? row.metadata : {};
|
||||
const fromSession: SessionMissionContext = {
|
||||
mission: row && (row.missionInstanceId || row.missionId || row.stageId)
|
||||
? {
|
||||
instanceId: row.missionInstanceId ?? undefined,
|
||||
missionId: row.missionId ?? undefined,
|
||||
stageId: row.stageId ?? undefined,
|
||||
source: "curator-v1",
|
||||
}
|
||||
: undefined,
|
||||
taskId: getString(metadata.taskId ?? metadata.curatorTaskId ?? metadata.curator_task_id),
|
||||
};
|
||||
if (fromSession.taskId || fromSession.mission) return fromSession;
|
||||
|
||||
const eventRows = await db
|
||||
.select({
|
||||
mission: growEvents.mission,
|
||||
correlation: growEvents.correlation,
|
||||
payload: growEvents.payload,
|
||||
})
|
||||
.from(growEvents)
|
||||
.where(and(eq(growEvents.source, `${serviceId}-service`), eq(growEvents.type, serviceId === "roleplay" ? "roleplay.scenario.configured" : "interview.session.configured")))
|
||||
.orderBy(desc(growEvents.occurredAt))
|
||||
.limit(50);
|
||||
const eventRow = eventRows.find((item) => {
|
||||
const correlation = isRecord(item.correlation) ? item.correlation : {};
|
||||
return getString(correlation.sessionId ?? correlation.session_id ?? correlation.externalId) === sessionId;
|
||||
});
|
||||
|
||||
if (!eventRow) return fromSession;
|
||||
const correlation = isRecord(eventRow.correlation) ? eventRow.correlation : {};
|
||||
const payload = isRecord(eventRow.payload) ? eventRow.payload : {};
|
||||
const request = isRecord(payload.request) ? payload.request : {};
|
||||
return {
|
||||
mission: isRecord(eventRow.mission) ? eventRow.mission : fromSession.mission,
|
||||
taskId: getString(
|
||||
correlation.taskId ??
|
||||
correlation.curatorTaskId ??
|
||||
correlation.curator_task_id ??
|
||||
request.taskId ??
|
||||
request.curatorTaskId ??
|
||||
request.curator_task_id,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function stripMissionFromBody(body: JsonObject): JsonObject {
|
||||
if (!("mission" in body)) return body;
|
||||
const { mission: _mission, ...rest } = body;
|
||||
return rest;
|
||||
}
|
||||
|
||||
function canonicalSubjectServiceId(source: string) {
|
||||
if (source.includes("interview")) return "interview";
|
||||
if (source.includes("roleplay")) return "roleplay";
|
||||
if (source.includes("resume")) return "resume";
|
||||
if (source.includes("social")) return "social";
|
||||
if (source.includes("qscore")) return "qscore";
|
||||
if (source.includes("matchmaking")) return "matchmaking";
|
||||
if (source.includes("analytics")) return "analytics";
|
||||
return source;
|
||||
}
|
||||
|
||||
function externalIdFromPayload(payload: Record<string, unknown>, correlation?: Record<string, unknown>) {
|
||||
const result = isRecord(payload.result) ? payload.result : {};
|
||||
return getString(
|
||||
correlation?.externalId ??
|
||||
correlation?.sessionId ??
|
||||
correlation?.resumeId ??
|
||||
result.task_id ??
|
||||
result.taskId ??
|
||||
result.session_id ??
|
||||
result.sessionId ??
|
||||
result.scenario_id ??
|
||||
result.scenarioId ??
|
||||
result.id,
|
||||
);
|
||||
}
|
||||
|
||||
function dedupeSegment(value: unknown) {
|
||||
return getString(value)?.replace(/[^A-Za-z0-9_.:-]+/g, "-");
|
||||
}
|
||||
|
||||
function stableStringify(value: unknown): string {
|
||||
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
||||
if (Array.isArray(value)) return `[${value.map((item) => stableStringify(item)).join(",")}]`;
|
||||
const record = value as Record<string, unknown>;
|
||||
return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`).join(",")}}`;
|
||||
}
|
||||
|
||||
function stableDedupeFingerprint(input: {
|
||||
userId: string;
|
||||
source: string;
|
||||
type: string;
|
||||
payload: Record<string, unknown>;
|
||||
correlation?: Record<string, unknown>;
|
||||
subject?: Record<string, unknown>;
|
||||
}) {
|
||||
return createHash("sha256")
|
||||
.update(stableStringify({
|
||||
userId: input.userId,
|
||||
source: input.source,
|
||||
type: input.type,
|
||||
subject: input.subject,
|
||||
correlation: input.correlation,
|
||||
payload: input.payload,
|
||||
}))
|
||||
.digest("hex")
|
||||
.slice(0, 24);
|
||||
}
|
||||
|
||||
function gatewayDedupeKey(input: {
|
||||
userId: string;
|
||||
source: string;
|
||||
type: string;
|
||||
payload: Record<string, unknown>;
|
||||
correlation?: Record<string, unknown>;
|
||||
subject?: Record<string, unknown>;
|
||||
}) {
|
||||
const result = isRecord(input.payload.result) ? input.payload.result : {};
|
||||
const request = isRecord(input.payload.request) ? input.payload.request : {};
|
||||
const subject = input.subject ?? {};
|
||||
const externalId = externalIdFromPayload(input.payload, input.correlation);
|
||||
const anchor =
|
||||
dedupeSegment(request.request_id ?? request.requestId) ??
|
||||
dedupeSegment(input.correlation?.requestId) ??
|
||||
dedupeSegment(input.correlation?.taskId) ??
|
||||
dedupeSegment(input.correlation?.curatorTaskId) ??
|
||||
dedupeSegment(input.correlation?.sessionId) ??
|
||||
dedupeSegment(input.correlation?.resumeId) ??
|
||||
dedupeSegment(input.correlation?.externalId) ??
|
||||
dedupeSegment(subject.externalId) ??
|
||||
dedupeSegment(externalId) ??
|
||||
dedupeSegment(result.task_id ?? result.taskId ?? result.session_id ?? result.sessionId ?? result.id);
|
||||
return `${input.source}:${input.type}:${input.userId}:${anchor ?? stableDedupeFingerprint(input)}`;
|
||||
}
|
||||
|
||||
async function recordGatewayEvent(input: {
|
||||
userId: string;
|
||||
source: string;
|
||||
@@ -249,8 +50,6 @@ async function recordGatewayEvent(input: {
|
||||
payload: Record<string, unknown>;
|
||||
correlation?: Record<string, unknown>;
|
||||
mission?: Record<string, unknown>;
|
||||
subject?: Record<string, unknown>;
|
||||
dedupeKey?: string;
|
||||
}) {
|
||||
await db.insert(events).values({
|
||||
userId: input.userId,
|
||||
@@ -265,12 +64,7 @@ async function recordGatewayEvent(input: {
|
||||
category: "service",
|
||||
userId: input.userId,
|
||||
occurredAt: new Date().toISOString(),
|
||||
dedupeKey: input.dedupeKey ?? gatewayDedupeKey(input),
|
||||
mission: input.mission,
|
||||
subject: input.subject ?? {
|
||||
serviceId: canonicalSubjectServiceId(input.source),
|
||||
externalId: externalIdFromPayload(input.payload, input.correlation) ?? `${canonicalSubjectServiceId(input.source)}:${input.type}`,
|
||||
},
|
||||
correlation: input.correlation,
|
||||
payload: input.payload,
|
||||
});
|
||||
@@ -280,112 +74,21 @@ async function recordGatewayEvent(input: {
|
||||
|
||||
function eventTypeForReview(prefix: "interview" | "roleplay", result: Record<string, unknown>) {
|
||||
const status = getString(result.status);
|
||||
if (status === "completed") return prefix === "interview" ? "interview.feedback.generated" : "roleplay.feedback.generated";
|
||||
if (status === "failed") return prefix === "interview" ? "interview.feedback.failed" : "roleplay.feedback.failed";
|
||||
return prefix === "interview" ? "interview.feedback.processing" : "roleplay.feedback.processing";
|
||||
if (status === "completed") return `${prefix}.review_completed`;
|
||||
if (status === "failed") return `${prefix}.review_failed`;
|
||||
return `${prefix}.review_processing`;
|
||||
}
|
||||
|
||||
function resumeEventTypeForRest(method: string, rest: string, ok: boolean) {
|
||||
if (!ok) return "resume.request_failed";
|
||||
if (method === "POST" && /^resumes\/upload/.test(rest)) return "resume.uploaded";
|
||||
if (method === "POST" && /^parse\/resume\/[^/]+\/parse/.test(rest)) return "resume.parsed";
|
||||
if (method === "POST" && /^ai\/analyze\//.test(rest)) return "resume.analysis.completed";
|
||||
if (method === "POST" && /^ai\/analyze\//.test(rest)) return "resume.analysis_completed";
|
||||
if ((method === "POST" || method === "PUT" || method === "PATCH") && /versions?/.test(rest)) return "resume.version_created";
|
||||
if (method !== "GET") return "resume.updated";
|
||||
return "resume.loaded";
|
||||
}
|
||||
|
||||
function socialEventTypeForRest(method: string, rest: string, ok: boolean) {
|
||||
if (!ok) return "social.request_failed";
|
||||
const m = method.toUpperCase();
|
||||
const path = rest.replace(/^\/+/, "").split("?")[0] ?? rest;
|
||||
if (m === "POST" && /^accounts\/connect/.test(path)) return "social.account.connected";
|
||||
if (m === "POST" && /^accounts\/[^/]+\/profile\/polish/.test(path)) return "social.profile.polished";
|
||||
if (m === "POST" && /^accounts\/[^/]+\/profile\/refresh/.test(path)) return "social.profile.refreshed";
|
||||
if (m === "POST" && /^accounts\/[^/]+\/profile\/apply/.test(path)) return "social.profile.updated";
|
||||
if (m === "POST" && /^accounts\/[^/]+\/profile\/refine-experience/.test(path)) return "social.experience.refined";
|
||||
if (m === "POST" && /^profiles\/fetch-by-url/.test(path)) return "social.profile.fetched";
|
||||
if (m === "POST" && /^accounts\/[^/]+\/(content|posts)/.test(path)) return "social.content.generated";
|
||||
if (m !== "GET" && m !== "HEAD" && /^accounts\/[^/]+\/schedule/.test(path)) return "social.post.scheduled";
|
||||
if (m !== "GET" && m !== "HEAD" && /^accounts\/[^/]+\/publish/.test(path)) return "social.post.published";
|
||||
if (m === "POST" || m === "PUT" || m === "PATCH") return "social.updated";
|
||||
return "social.updated";
|
||||
}
|
||||
|
||||
function agentDataAction(result: Record<string, unknown>) {
|
||||
const messages = Array.isArray(result.messages) ? result.messages : [];
|
||||
for (const item of messages) {
|
||||
const message = isRecord(item) ? item : {};
|
||||
const action = getString(message.action);
|
||||
if (message.type === "agent_data" && action) return action;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resultHasAgentError(result: Record<string, unknown>) {
|
||||
if (getString(result.status) === "error") return true;
|
||||
const messages = Array.isArray(result.messages) ? result.messages : [];
|
||||
return messages.some((item) => isRecord(item) && item.type === "agent_error");
|
||||
}
|
||||
|
||||
function resumeEventTypeForA2a(action: string | undefined, result: Record<string, unknown>) {
|
||||
if (resultHasAgentError(result)) return "resume.request_failed";
|
||||
const effective = agentDataAction(result) ?? action ?? "";
|
||||
if (["ai_analyze", "analyze_resume", "bg_parse_analyze", "ai_analysis_complete"].includes(effective)) return "resume.analysis.completed";
|
||||
if (["parse_resume", "resume_parsed"].includes(effective)) return "resume.parsed";
|
||||
if (["export_pdf", "export_analysis_pdf", "resume_exported"].includes(effective)) return "resume.exported";
|
||||
if (["create_resume", "resume_created", "save_version", "update_resume_meta"].includes(effective)) return "resume.updated";
|
||||
return "resume.updated";
|
||||
}
|
||||
|
||||
function resumeIdFromA2a(body: JsonObject, result: Record<string, unknown>) {
|
||||
const params = isRecord(body.params) ? body.params : {};
|
||||
const messages = Array.isArray(result.messages) ? result.messages : [];
|
||||
for (const item of messages) {
|
||||
const message = isRecord(item) ? item : {};
|
||||
const data = isRecord(message.data) ? message.data : {};
|
||||
const resume = isRecord(data.resume) ? data.resume : {};
|
||||
const id = getString(data.resume_id ?? data.resumeId ?? resume.id);
|
||||
if (id) return id;
|
||||
}
|
||||
return getString(params.resume_id ?? params.resumeId ?? result.task_id ?? result.taskId);
|
||||
}
|
||||
|
||||
function serviceErrorResponse(err: unknown): never {
|
||||
if (err instanceof ProductServiceError) {
|
||||
let detail: unknown = err.body;
|
||||
try {
|
||||
detail = err.body ? JSON.parse(err.body) : {};
|
||||
} catch {
|
||||
detail = { detail: err.body };
|
||||
}
|
||||
throw new HTTPException(err.status as never, { message: JSON.stringify(detail) });
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
async function callMatchmakingA2a(body: Record<string, unknown>, userId: string) {
|
||||
const target = new URL("/a2a/tasks", config.matchmakingServiceUrl.replace(/\/$/, ""));
|
||||
const action = getString(body.action) ?? (body.session_start ? "session_start" : "");
|
||||
const payload = {
|
||||
...body,
|
||||
action: action === "get_feed" ? "get_scout_feed" : action,
|
||||
user_id: getString(body.user_id) ?? userId,
|
||||
};
|
||||
const res = await fetch(target, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
...(config.a2aAllowedKey ? { authorization: `Bearer ${config.a2aAllowedKey}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const text = await res.text();
|
||||
const result = text ? JSON.parse(text) as Record<string, unknown> : {};
|
||||
if (!res.ok) throw new HTTPException(res.status as never, { message: text || "matchmaking request failed" });
|
||||
return { action: String(payload.action || action), result };
|
||||
}
|
||||
|
||||
function parseJsonBody(body: ArrayBuffer | undefined, headers: Headers): JsonObject {
|
||||
if (!body || !headers.get("content-type")?.includes("application/json")) return {};
|
||||
try {
|
||||
@@ -396,17 +99,6 @@ function parseJsonBody(body: ArrayBuffer | undefined, headers: Headers): JsonObj
|
||||
}
|
||||
}
|
||||
|
||||
function decodedProxyHeaders(headers: Headers) {
|
||||
const forwarded = new Headers(headers);
|
||||
// Node fetch transparently decodes gzip/br bodies but retains the upstream
|
||||
// encoding and byte-length headers. Forwarding those stale headers makes the
|
||||
// next proxy/browser decode an already-decoded body and fail.
|
||||
forwarded.delete("content-encoding");
|
||||
forwarded.delete("content-length");
|
||||
forwarded.delete("transfer-encoding");
|
||||
return forwarded;
|
||||
}
|
||||
|
||||
async function proxyResumeRequest(req: Request, rest: string, userId: string) {
|
||||
const incoming = new URL(req.url);
|
||||
const normalizedRest = rest
|
||||
@@ -415,13 +107,8 @@ async function proxyResumeRequest(req: Request, rest: string, userId: string) {
|
||||
.replace(/^resumes\/([^/]+)\/analyze$/, "ai/analyze/$1")
|
||||
.replace(/^resumes\/([^/]+)\/suggestions$/, "ai/suggestions/$1")
|
||||
.replace(/^resumes\/([^/]+)\/preview$/, "export/resumes/$1/preview");
|
||||
const forwardedQuery = new URLSearchParams(incoming.searchParams);
|
||||
forwardedQuery.delete("missionInstanceId");
|
||||
forwardedQuery.delete("missionId");
|
||||
forwardedQuery.delete("stageId");
|
||||
forwardedQuery.delete("source");
|
||||
const target = new URL(
|
||||
`/api/v1/${normalizedRest}${forwardedQuery.toString() ? `?${forwardedQuery.toString()}` : ""}`,
|
||||
`/api/v1/${normalizedRest}${incoming.search}`,
|
||||
config.resumeServiceUrl.replace(/\/$/, ""),
|
||||
);
|
||||
|
||||
@@ -430,29 +117,18 @@ async function proxyResumeRequest(req: Request, rest: string, userId: string) {
|
||||
headers.delete("cookie");
|
||||
headers.set("authorization", `Bearer ${config.a2aAllowedKey}`);
|
||||
headers.set("x-growqr-user", userId);
|
||||
headers.set("accept-encoding", "identity");
|
||||
|
||||
const method = req.method.toUpperCase();
|
||||
const body = ["GET", "HEAD"].includes(method) ? undefined : await req.arrayBuffer();
|
||||
const requestJson = parseJsonBody(body, headers);
|
||||
const mission = missionFromRequest(req, requestJson);
|
||||
const forwardBody =
|
||||
body && headers.get("content-type")?.includes("application/json")
|
||||
? Buffer.from(JSON.stringify(stripMissionFromBody(requestJson)))
|
||||
: body;
|
||||
if (forwardBody !== body) headers.delete("content-length");
|
||||
const res = await fetch(target, {
|
||||
method,
|
||||
headers,
|
||||
body: forwardBody,
|
||||
body,
|
||||
});
|
||||
|
||||
if (method === "GET" || method === "HEAD") {
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: decodedProxyHeaders(res.headers),
|
||||
});
|
||||
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: res.headers });
|
||||
}
|
||||
|
||||
const responseBuffer = await res.arrayBuffer();
|
||||
@@ -469,15 +145,14 @@ async function proxyResumeRequest(req: Request, rest: string, userId: string) {
|
||||
correlation: {
|
||||
resumeId: getString(responseObj.resume_id ?? responseObj.resumeId ?? responseObj.id) ?? getString(requestJson.resume_id ?? requestJson.resumeId),
|
||||
externalId: getString(responseObj.resume_id ?? responseObj.resumeId ?? responseObj.id) ?? getString(requestJson.resume_id ?? requestJson.resumeId),
|
||||
taskId: curatorTaskIdFromRequest(req, requestJson),
|
||||
},
|
||||
mission,
|
||||
mission: missionFromBody(requestJson),
|
||||
}).catch((err) => log.warn({ err, path: normalizedRest }, "failed to record resume gateway event"));
|
||||
|
||||
return new Response(responseBuffer, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: decodedProxyHeaders(res.headers),
|
||||
headers: res.headers,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -489,6 +164,25 @@ function stringArray(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.map((item) => String(item).trim()).filter(Boolean) : [];
|
||||
}
|
||||
|
||||
async function getUserServiceProfile(req: Request): Promise<{ userProfile?: Record<string, unknown>; preferences?: Record<string, unknown> }> {
|
||||
const target = new URL("/api/v1/users/me", config.userServiceUrl.replace(/\/$/, ""));
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
const res = await fetch(target, { method: "GET", headers });
|
||||
if (!res.ok) return {};
|
||||
const userProfile = await res.json().catch(() => null) as Record<string, unknown> | null;
|
||||
const preferences = userProfile?.preferences;
|
||||
return {
|
||||
userProfile: userProfile ?? undefined,
|
||||
preferences: isRecord(preferences) ? preferences : {},
|
||||
};
|
||||
}
|
||||
|
||||
async function getUserServicePreferences(req: Request): Promise<Record<string, unknown> | undefined> {
|
||||
return (await getUserServiceProfile(req)).preferences;
|
||||
}
|
||||
|
||||
async function getServiceState(baseUrl: string, path: string): Promise<Record<string, unknown> | undefined> {
|
||||
const target = new URL(path, baseUrl.replace(/\/$/, ""));
|
||||
const res = await fetch(target, {
|
||||
@@ -515,7 +209,7 @@ function mergeUniqueSkills(existing: unknown, incoming: unknown): string[] {
|
||||
}
|
||||
|
||||
async function resolveGrowUserContext(req: Request, userId: string): Promise<Record<string, unknown>> {
|
||||
const { userProfile } = await getRequestUserProfile(req, userId);
|
||||
const { userProfile } = await getUserServiceProfile(req);
|
||||
const userContext: Record<string, unknown> = { ...(userProfile ?? {}) };
|
||||
userContext.clerk_id = String(userContext.clerk_id ?? userId);
|
||||
|
||||
@@ -535,9 +229,6 @@ async function resolveGrowUserContext(req: Request, userId: string): Promise<Rec
|
||||
if (resumeState.current_company && !userContext.current_company) userContext.current_company = resumeState.current_company;
|
||||
if (Array.isArray(resumeState.experience_history)) userContext.experience_history = resumeState.experience_history;
|
||||
if (Array.isArray(resumeState.education)) userContext.education = resumeState.education;
|
||||
userContext.resume_available = Number(resumeState.resume_count ?? 0) > 0;
|
||||
const resumeSummary = getString(resumeState.resume_summary ?? resumeState.resumeSummary);
|
||||
if (resumeSummary) userContext.resume_summary = resumeSummary;
|
||||
}
|
||||
|
||||
if (socialState) {
|
||||
@@ -553,9 +244,7 @@ async function resolveGrowUserContext(req: Request, userId: string): Promise<Rec
|
||||
|
||||
function hasResumeContext(userContext: Record<string, unknown>): boolean {
|
||||
return Boolean(
|
||||
userContext.resume_available ||
|
||||
getString(userContext.resume_summary) ||
|
||||
(Array.isArray(userContext.experience_history) && userContext.experience_history.length > 0) ||
|
||||
(Array.isArray(userContext.experience_history) && userContext.experience_history.length > 0) ||
|
||||
(Array.isArray(userContext.skills) && userContext.skills.length > 0) ||
|
||||
getString(userContext.current_role)
|
||||
);
|
||||
@@ -577,9 +266,6 @@ function composeCandidateProfile(userContext: Record<string, unknown>): string {
|
||||
if (currentRole) lines.push(`Current role: ${currentRole}`);
|
||||
if (headline && headline.toLowerCase() !== currentRole?.toLowerCase()) lines.push(`LinkedIn headline: ${headline}`);
|
||||
|
||||
const resumeSummary = getString(userContext.resume_summary);
|
||||
if (resumeSummary) lines.push(`Primary resume summary: ${resumeSummary.length > 600 ? `${resumeSummary.slice(0, 600).trimEnd()}…` : resumeSummary}`);
|
||||
|
||||
const skills = stringArray(userContext.skills);
|
||||
if (skills.length) lines.push(`Key skills: ${skills.slice(0, 12).join(", ")}`);
|
||||
|
||||
@@ -615,19 +301,18 @@ function composeCandidateProfile(userContext: Record<string, unknown>): string {
|
||||
if (education.length) lines.push(`Education: ${education.slice(0, 3).join("; ")}`);
|
||||
|
||||
const summary = getString(userContext.linkedin_summary);
|
||||
if (summary && summary !== resumeSummary) lines.push(`LinkedIn summary: ${summary.length > 300 ? `${summary.slice(0, 300).trimEnd()}…` : summary}`);
|
||||
if (summary) lines.push(`Profile summary: ${summary.length > 300 ? `${summary.slice(0, 300).trimEnd()}…` : summary}`);
|
||||
|
||||
const text = lines.join("\n");
|
||||
return text.length > 1200 ? `${text.slice(0, 1200).trimEnd()}…` : text;
|
||||
}
|
||||
|
||||
async function buildPersonalizedConfigurePayload(req: Request, body: JsonObject, userId: string): Promise<JsonObject> {
|
||||
const { mission: _mission, ...rest } = body;
|
||||
const userContext = await resolveGrowUserContext(req, userId).catch((err) => {
|
||||
log.warn({ err, userId }, "failed to resolve Grow user context for interview configure");
|
||||
return {} as Record<string, unknown>;
|
||||
});
|
||||
const incomingContext = isRecord(rest.context) ? rest.context : {};
|
||||
const incomingContext = isRecord(body.context) ? body.context : {};
|
||||
const context: Record<string, unknown> = {
|
||||
...incomingContext,
|
||||
candidate_name: getString(incomingContext.candidate_name) ?? getString(userContext.first_name) ?? "",
|
||||
@@ -637,27 +322,25 @@ async function buildPersonalizedConfigurePayload(req: Request, body: JsonObject,
|
||||
difficulty: getString(incomingContext.difficulty) ?? "medium",
|
||||
};
|
||||
|
||||
const curatorHandoff = getString(incomingContext.source) === "curator-v1" || Boolean(getString(incomingContext.curatorTaskId));
|
||||
if (incomingContext.personalize || curatorHandoff) {
|
||||
if (incomingContext.personalize) {
|
||||
const candidateProfile = composeCandidateProfile(userContext);
|
||||
if (candidateProfile) context.candidate_profile = candidateProfile;
|
||||
}
|
||||
|
||||
return {
|
||||
...rest,
|
||||
user_id: String(rest.user_id ?? userId),
|
||||
org_id: String(rest.org_id ?? "growqr"),
|
||||
...body,
|
||||
user_id: String(body.user_id ?? userId),
|
||||
org_id: String(body.org_id ?? "growqr"),
|
||||
context,
|
||||
};
|
||||
}
|
||||
|
||||
async function buildPersonalizedRoleplayConfigurePayload(req: Request, body: JsonObject, userId: string): Promise<JsonObject> {
|
||||
const { mission: _mission, ...rest } = body;
|
||||
const userContext = await resolveGrowUserContext(req, userId).catch((err) => {
|
||||
log.warn({ err, userId }, "failed to resolve Grow user context for roleplay configure");
|
||||
return {} as Record<string, unknown>;
|
||||
});
|
||||
const incomingMetadata = isRecord(rest.metadata) ? rest.metadata : {};
|
||||
const incomingMetadata = isRecord(body.metadata) ? body.metadata : {};
|
||||
const metadata: Record<string, unknown> = {
|
||||
...incomingMetadata,
|
||||
candidate_name: getString(incomingMetadata.candidate_name) ?? getString(userContext.first_name) ?? "",
|
||||
@@ -676,11 +359,11 @@ async function buildPersonalizedRoleplayConfigurePayload(req: Request, body: Jso
|
||||
}
|
||||
|
||||
return {
|
||||
...rest,
|
||||
user_id: String(rest.user_id ?? userId),
|
||||
org_id: String(rest.org_id ?? "growqr"),
|
||||
...body,
|
||||
user_id: String(body.user_id ?? userId),
|
||||
org_id: String(body.org_id ?? "growqr"),
|
||||
metadata,
|
||||
qscore: (rest.qscore as JsonObject | undefined) ?? (isRecord(userContext.qscore) ? userContext.qscore : DEFAULT_QSCORE),
|
||||
qscore: (body.qscore as JsonObject | undefined) ?? (isRecord(userContext.qscore) ? userContext.qscore : DEFAULT_QSCORE),
|
||||
user_context: userContext,
|
||||
};
|
||||
}
|
||||
@@ -688,13 +371,8 @@ async function buildPersonalizedRoleplayConfigurePayload(req: Request, body: Jso
|
||||
async function proxySocialRequest(req: Request, rest: string, userId: string) {
|
||||
const incoming = new URL(req.url);
|
||||
const normalizedRest = rest.replace(/^\/+/, "");
|
||||
const forwardedQuery = new URLSearchParams(incoming.searchParams);
|
||||
forwardedQuery.delete("missionInstanceId");
|
||||
forwardedQuery.delete("missionId");
|
||||
forwardedQuery.delete("stageId");
|
||||
forwardedQuery.delete("source");
|
||||
const target = new URL(
|
||||
`/${normalizedRest}${forwardedQuery.toString() ? `?${forwardedQuery.toString()}` : ""}`,
|
||||
`/api/v1/${normalizedRest}${incoming.search}`,
|
||||
config.socialBrandingServiceUrl.replace(/\/$/, ""),
|
||||
);
|
||||
|
||||
@@ -705,50 +383,9 @@ async function proxySocialRequest(req: Request, rest: string, userId: string) {
|
||||
|
||||
const method = req.method.toUpperCase();
|
||||
const body = ["GET", "HEAD"].includes(method) ? undefined : await req.arrayBuffer();
|
||||
const requestJson = parseJsonBody(body, headers);
|
||||
const mission = missionFromRequest(req, requestJson);
|
||||
// Strip mission fields from the forwarded body, mirroring the resume proxy,
|
||||
// so orchestration metadata is not passed downstream.
|
||||
const forwardBody =
|
||||
body && headers.get("content-type")?.includes("application/json")
|
||||
? Buffer.from(JSON.stringify(stripMissionFromBody(requestJson)))
|
||||
: body;
|
||||
if (forwardBody !== body) headers.delete("content-length");
|
||||
const res = await fetch(target, { method, headers, body: forwardBody });
|
||||
const res = await fetch(target, { method, headers, body });
|
||||
|
||||
if (method === "GET" || method === "HEAD") {
|
||||
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: res.headers });
|
||||
}
|
||||
|
||||
// Security note: the social-branding service handles LinkedIn sessions, tokens,
|
||||
// cookies, and credentials. The gateway event payload intentionally carries ONLY
|
||||
// canonical metadata (path/method/status/action) — never raw request/response bodies.
|
||||
const responseBuffer = await res.arrayBuffer();
|
||||
const responseText = Buffer.from(responseBuffer).toString("utf8");
|
||||
let responseJson: unknown;
|
||||
try { responseJson = responseText ? JSON.parse(responseText) : undefined; } catch { responseJson = undefined; }
|
||||
const responseObj = isRecord(responseJson) ? responseJson : {};
|
||||
|
||||
const eventType = socialEventTypeForRest(method, normalizedRest, res.ok);
|
||||
const derivedAction = eventType.split(".").slice(1).join(".");
|
||||
const accountMatch = normalizedRest.match(/^accounts\/(?!connect\/?$)[^/]+\/.+/);
|
||||
const externalId =
|
||||
(accountMatch ? accountMatch[0].split("/")[1] : undefined) ??
|
||||
getString(responseObj.account_id ?? responseObj.accountId ?? responseObj.id);
|
||||
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "agent-social-branding",
|
||||
type: eventType,
|
||||
payload: { path: normalizedRest, method, status: res.status, action: derivedAction },
|
||||
correlation: {
|
||||
externalId: externalId ?? `${canonicalSubjectServiceId("agent-social-branding")}:${eventType}`,
|
||||
taskId: curatorTaskIdFromRequest(req, requestJson),
|
||||
},
|
||||
mission,
|
||||
}).catch((err) => log.warn({ err, path: normalizedRest }, "failed to record social gateway event"));
|
||||
|
||||
return new Response(responseBuffer, {
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: res.headers,
|
||||
@@ -759,7 +396,7 @@ export function serviceRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
|
||||
app.get("/catalog", (c) => c.json({ services: listServiceCapabilities({ public: true }) }));
|
||||
app.get("/catalog", (c) => c.json({ services: listServiceCapabilities() }));
|
||||
|
||||
app.get("/agents", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
@@ -808,70 +445,67 @@ export function serviceRoutes() {
|
||||
app.get("/qscore/current", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
try {
|
||||
await ensureOnboardingBaselineQscoreFromLedger(userId);
|
||||
await ensureOnboardingBaselineQscore(userId, await getUserServicePreferences(c.req.raw));
|
||||
} catch (err) {
|
||||
log.warn({ err, userId }, "failed to seed onboarding Q Score baseline before current Q Score read");
|
||||
}
|
||||
|
||||
const qscoreResult = await getQscoreFromService(userId, DEFAULT_QSCORE_ORG_ID);
|
||||
const score = qscoreResult?.q_score ?? null;
|
||||
const breakdown = qscoreResult?.breakdown ?? {};
|
||||
const breakdownSignals = Array.isArray(breakdown.signals) ? breakdown.signals : [];
|
||||
const quotients = qscoreResult?.quotients ?? {};
|
||||
const [projection] = await db
|
||||
.select()
|
||||
.from(growQscoreProjectionState)
|
||||
.where(eq(growQscoreProjectionState.userId, userId))
|
||||
.limit(1);
|
||||
const signals = await db
|
||||
.select()
|
||||
.from(growQscoreLatest)
|
||||
.where(eq(growQscoreLatest.userId, userId))
|
||||
.orderBy(desc(growQscoreLatest.updatedAt));
|
||||
|
||||
const dimensionFromKey = (id: string, value: unknown): {
|
||||
id: string;
|
||||
label: string;
|
||||
score: number;
|
||||
signalCount: number;
|
||||
sources: string[];
|
||||
} | null => {
|
||||
const num = typeof value === "number" && Number.isFinite(value)
|
||||
? value
|
||||
: (isRecord(value) && typeof value.score === "number" ? value.score : null);
|
||||
if (num === null) return null;
|
||||
const label = id.replace(/-/g, " ").replace(/^./, (char) => char.toUpperCase());
|
||||
const signalCount = isRecord(value) && typeof value.signalCount === "number" ? value.signalCount : 0;
|
||||
const sources = isRecord(value) && Array.isArray(value.sources) ? value.sources.filter((s): s is string => typeof s === "string") : [];
|
||||
return { id, label, score: Math.round(num), signalCount, sources };
|
||||
};
|
||||
const dimensions = Object.entries(quotients)
|
||||
.map(([id, value]) => dimensionFromKey(id, value))
|
||||
.filter((d): d is { id: string; label: string; score: number; signalCount: number; sources: string[] } => d !== null);
|
||||
const grouped = new Map<string, { label: string; score: number; count: number; sources: Set<string> }>();
|
||||
for (const signal of signals) {
|
||||
const groupId = signal.signalId.split(".")[0] || "readiness";
|
||||
const current = grouped.get(groupId) ?? {
|
||||
label: groupId.replace(/-/g, " ").replace(/^./, (char) => char.toUpperCase()),
|
||||
score: 0,
|
||||
count: 0,
|
||||
sources: new Set<string>(),
|
||||
};
|
||||
current.score += signal.score;
|
||||
current.count += 1;
|
||||
if (signal.source) current.sources.add(signal.source);
|
||||
grouped.set(groupId, current);
|
||||
}
|
||||
|
||||
const response = {
|
||||
const dimensions = Array.from(grouped.entries()).map(([id, group]) => ({
|
||||
id,
|
||||
label: group.label,
|
||||
score: Math.round(group.score / Math.max(group.count, 1)),
|
||||
signalCount: group.count,
|
||||
sources: Array.from(group.sources),
|
||||
}));
|
||||
const score = projection?.score && projection.score > 0
|
||||
? projection.score
|
||||
: signals.length
|
||||
? Math.round(signals.reduce((sum, signal) => sum + signal.score, 0) / signals.length)
|
||||
: null;
|
||||
|
||||
return c.json({
|
||||
qscore: score === null ? null : {
|
||||
score,
|
||||
iq_score: qscoreResult?.iq_score ?? null,
|
||||
eq_score: qscoreResult?.eq_score ?? null,
|
||||
sq_score: qscoreResult?.sq_score ?? null,
|
||||
signalCount: breakdownSignals.length,
|
||||
summary: typeof breakdown.summary === "string" ? breakdown.summary : `Readiness score computed from ${breakdownSignals.length} current signal${breakdownSignals.length === 1 ? "" : "s"}.`,
|
||||
signalCount: projection?.signalCount ?? signals.length,
|
||||
summary: projection?.summary ?? `Readiness score computed from ${signals.length} current signal${signals.length === 1 ? "" : "s"}.`,
|
||||
dimensions,
|
||||
updatedAt: qscoreResult?.calculated_at ?? null,
|
||||
updatedAt: projection?.updatedAt?.toISOString() ?? signals[0]?.updatedAt?.toISOString() ?? null,
|
||||
},
|
||||
signals: breakdownSignals.map((signal) => {
|
||||
const s = isRecord(signal) ? signal : {};
|
||||
return {
|
||||
signalId: typeof s.signalId === "string" ? s.signalId : typeof s.signal_id === "string" ? s.signal_id : "",
|
||||
score: typeof s.score === "number" ? Math.round(s.score) : 0,
|
||||
present: typeof s.present === "boolean" ? s.present : true,
|
||||
source: typeof s.source === "string" ? s.source : "",
|
||||
occurredAt: typeof s.occurredAt === "string" ? s.occurredAt : qscoreResult?.calculated_at ?? new Date().toISOString(),
|
||||
updatedAt: typeof s.updatedAt === "string" ? s.updatedAt : qscoreResult?.calculated_at ?? new Date().toISOString(),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "qscore-service",
|
||||
type: "qscore.review.opened",
|
||||
payload: { score, signalCount: breakdownSignals.length, source: "services.qscore.current" },
|
||||
correlation: { taskId: curatorTaskIdFromRequest(c.req.raw) },
|
||||
}).catch((err) => log.warn({ err, userId }, "failed to record qscore review event"));
|
||||
|
||||
return c.json(response);
|
||||
signals: signals.map((signal) => ({
|
||||
signalId: signal.signalId,
|
||||
score: Math.round(signal.score),
|
||||
present: signal.present,
|
||||
source: signal.source,
|
||||
occurredAt: signal.occurredAt.toISOString(),
|
||||
updatedAt: signal.updatedAt.toISOString(),
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/interview/page-state", async (c) => {
|
||||
@@ -892,27 +526,20 @@ export function serviceRoutes() {
|
||||
app.post("/interview/configure", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json<JsonObject>();
|
||||
const mission = missionFromRequest(c.req.raw, body);
|
||||
const payload = await buildPersonalizedConfigurePayload(c.req.raw, body, userId);
|
||||
const result = await interviewService.configure(payload).catch(serviceErrorResponse);
|
||||
const result = await interviewService.configure(payload);
|
||||
const resultObj = result as Record<string, unknown>;
|
||||
const taskId = curatorTaskIdFromRequest(c.req.raw, body);
|
||||
const orchestration = orchestrationFromMission(mission, taskId);
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "interview-service",
|
||||
type: "interview.session.configured",
|
||||
payload: { ...orchestration, request: payload, result: resultObj },
|
||||
correlation: { sessionId: getSessionId(resultObj), requestId: getString(body.request_id), ...orchestration },
|
||||
mission,
|
||||
type: "interview.configured",
|
||||
payload: { request: payload, result: resultObj },
|
||||
correlation: { sessionId: getSessionId(resultObj), requestId: getString(body.request_id) },
|
||||
mission: missionFromBody(body),
|
||||
}).catch((err) => log.warn({ err }, "failed to record interview configured event"));
|
||||
return c.json(result);
|
||||
});
|
||||
app.post("/interview/preview", async (c) => {
|
||||
const body = await c.req.json<JsonObject>();
|
||||
const payload = await buildPersonalizedConfigurePayload(c.req.raw, body, c.get("userId"));
|
||||
return c.json(await interviewService.preview(payload));
|
||||
});
|
||||
app.post("/interview/preview", async (c) => c.json(await interviewService.preview(await c.req.json<JsonObject>())));
|
||||
app.post("/interview/questions", async (c) => c.json(await interviewService.editQuestions(await c.req.json())));
|
||||
app.post("/interview/approve", async (c) => {
|
||||
const body = await c.req.json<{ session_id: string }>();
|
||||
@@ -924,26 +551,22 @@ export function serviceRoutes() {
|
||||
app.post("/interview/results:bulk", async (c) => c.json(await interviewService.resultsBulk(await c.req.json<JsonObject>())));
|
||||
app.get("/interview/review/:sessionId", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const sessionId = c.req.param("sessionId");
|
||||
const result = await interviewService.review(sessionId, userId).catch(serviceErrorResponse);
|
||||
const sessionId = c.req.param("sessionId");
|
||||
const result = await interviewService.review(sessionId);
|
||||
const resultObj = result as Record<string, unknown>;
|
||||
const sessionContext = await sessionMissionContext("interview", sessionId).catch((): SessionMissionContext => ({}));
|
||||
const taskId = curatorTaskIdFromRequest(c.req.raw) ?? sessionContext.taskId;
|
||||
const orchestration = orchestrationFromMission(sessionContext.mission, taskId);
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "interview-service",
|
||||
type: eventTypeForReview("interview", resultObj),
|
||||
payload: { ...orchestration, ...resultObj },
|
||||
correlation: { sessionId, ...orchestration },
|
||||
mission: sessionContext.mission,
|
||||
payload: resultObj,
|
||||
correlation: { sessionId },
|
||||
}).catch((err) => log.warn({ err }, "failed to record interview review event"));
|
||||
return c.json(result);
|
||||
});
|
||||
app.get("/interview/leaderboard", async (c) => c.json(await interviewService.leaderboard()));
|
||||
app.get("/interview/artifacts/:sessionId/:artifactType", async (c) => c.json(await interviewService.artifact(c.req.param("sessionId"), c.req.param("artifactType"), c.get("userId"))));
|
||||
app.post("/interview/sessions/:sessionId/video/upload-url", async (c) => c.json(await interviewService.createVideoUploadUrl(c.req.param("sessionId"), c.get("userId"))));
|
||||
app.post("/interview/sessions/:sessionId/video/uploaded", async (c) => c.json(await interviewService.markVideoUploaded(c.req.param("sessionId"), c.get("userId"))));
|
||||
app.get("/interview/artifacts/:sessionId/:artifactType", async (c) => c.json(await interviewService.artifact(c.req.param("sessionId"), c.req.param("artifactType"))));
|
||||
app.post("/interview/sessions/:sessionId/video/upload-url", async (c) => c.json(await interviewService.createVideoUploadUrl(c.req.param("sessionId"))));
|
||||
app.post("/interview/sessions/:sessionId/video/uploaded", async (c) => c.json(await interviewService.markVideoUploaded(c.req.param("sessionId"))));
|
||||
|
||||
app.get("/roleplay/page-state", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
@@ -963,27 +586,20 @@ export function serviceRoutes() {
|
||||
app.post("/roleplay/configure", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json<JsonObject>();
|
||||
const mission = missionFromRequest(c.req.raw, body);
|
||||
const payload = await buildPersonalizedRoleplayConfigurePayload(c.req.raw, body, userId);
|
||||
const result = await roleplayService.configure(payload).catch(serviceErrorResponse);
|
||||
const result = await roleplayService.configure(payload);
|
||||
const resultObj = result as Record<string, unknown>;
|
||||
const taskId = curatorTaskIdFromRequest(c.req.raw, body);
|
||||
const orchestration = orchestrationFromMission(mission, taskId);
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "roleplay-service",
|
||||
type: "roleplay.scenario.configured",
|
||||
payload: { ...orchestration, request: payload, result: resultObj },
|
||||
correlation: { sessionId: getSessionId(resultObj), requestId: getString(body.request_id), ...orchestration },
|
||||
mission,
|
||||
type: "roleplay.configured",
|
||||
payload: { request: payload, result: resultObj },
|
||||
correlation: { sessionId: getSessionId(resultObj), requestId: getString(body.request_id) },
|
||||
mission: missionFromBody(body),
|
||||
}).catch((err) => log.warn({ err }, "failed to record roleplay configured event"));
|
||||
return c.json(result);
|
||||
});
|
||||
app.post("/roleplay/preview", async (c) => {
|
||||
const body = await c.req.json<JsonObject>();
|
||||
const payload = await buildPersonalizedRoleplayConfigurePayload(c.req.raw, body, c.get("userId"));
|
||||
return c.json(await roleplayService.preview(payload));
|
||||
});
|
||||
app.post("/roleplay/preview", async (c) => c.json(await roleplayService.preview(await c.req.json<JsonObject>())));
|
||||
app.post("/roleplay/questions", async (c) => c.json(await roleplayService.editQuestions(await c.req.json())));
|
||||
app.post("/roleplay/approve", async (c) => {
|
||||
const body = await c.req.json<{ session_id: string }>();
|
||||
@@ -996,45 +612,26 @@ export function serviceRoutes() {
|
||||
app.get("/roleplay/review/:sessionId", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const sessionId = c.req.param("sessionId");
|
||||
const result = await roleplayService.review(sessionId, userId).catch(serviceErrorResponse);
|
||||
const result = await roleplayService.review(sessionId);
|
||||
const resultObj = result as Record<string, unknown>;
|
||||
const sessionContext = await sessionMissionContext("roleplay", sessionId).catch((): SessionMissionContext => ({}));
|
||||
const taskId = curatorTaskIdFromRequest(c.req.raw) ?? sessionContext.taskId;
|
||||
const orchestration = orchestrationFromMission(sessionContext.mission, taskId);
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "roleplay-service",
|
||||
type: eventTypeForReview("roleplay", resultObj),
|
||||
payload: { ...orchestration, ...resultObj },
|
||||
correlation: { sessionId, ...orchestration },
|
||||
mission: sessionContext.mission,
|
||||
payload: resultObj,
|
||||
correlation: { sessionId },
|
||||
}).catch((err) => log.warn({ err }, "failed to record roleplay review event"));
|
||||
return c.json(result);
|
||||
});
|
||||
app.get("/roleplay/leaderboard", async (c) => c.json(await roleplayService.leaderboard()));
|
||||
app.get("/roleplay/artifacts/:sessionId/:artifactType", async (c) => c.json(await roleplayService.artifact(c.req.param("sessionId"), c.req.param("artifactType"), c.get("userId"))));
|
||||
app.post("/roleplay/sessions/:sessionId/video/upload-url", async (c) => c.json(await roleplayService.createVideoUploadUrl(c.req.param("sessionId"), c.get("userId"))));
|
||||
app.post("/roleplay/sessions/:sessionId/video/uploaded", async (c) => c.json(await roleplayService.markVideoUploaded(c.req.param("sessionId"), c.get("userId"))));
|
||||
app.get("/roleplay/artifacts/:sessionId/:artifactType", async (c) => c.json(await roleplayService.artifact(c.req.param("sessionId"), c.req.param("artifactType"))));
|
||||
app.post("/roleplay/sessions/:sessionId/video/upload-url", async (c) => c.json(await roleplayService.createVideoUploadUrl(c.req.param("sessionId"))));
|
||||
app.post("/roleplay/sessions/:sessionId/video/uploaded", async (c) => c.json(await roleplayService.markVideoUploaded(c.req.param("sessionId"))));
|
||||
|
||||
app.get("/resume/state/:clerkId", async (c) => c.json(await resumeService.state(c.req.param("clerkId"))));
|
||||
app.post("/resume/tasks", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json<JsonObject>();
|
||||
const result = await resumeService.task({ ...body, user_id: String(body.user_id ?? userId) });
|
||||
const resultObj = result as Record<string, unknown>;
|
||||
const resumeId = resumeIdFromA2a(body, resultObj);
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: "resume-builder",
|
||||
type: resumeEventTypeForA2a(getString(body.action), resultObj),
|
||||
payload: { request: body, result: resultObj },
|
||||
correlation: {
|
||||
taskId: curatorTaskIdFromRequest(c.req.raw, body),
|
||||
resumeId,
|
||||
externalId: resumeId,
|
||||
},
|
||||
}).catch((err) => log.warn({ err, userId, action: body.action }, "failed to record resume A2A workflow event"));
|
||||
return c.json(result);
|
||||
return c.json(await resumeService.task({ ...body, user_id: String(body.user_id ?? c.get("userId")) }));
|
||||
});
|
||||
|
||||
// Frontend Resume Builder routes should preserve the user's Clerk bearer token
|
||||
@@ -1054,22 +651,5 @@ export function serviceRoutes() {
|
||||
return proxySocialRequest(c.req.raw, rest, c.get("userId"));
|
||||
});
|
||||
|
||||
app.post("/matchmaking/a2a", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json<JsonObject>().catch(() => ({}));
|
||||
const { action, result } = await callMatchmakingA2a(body, userId);
|
||||
const event = buildMatchmakingGatewayEvent({ userId, action, body, result });
|
||||
await recordGatewayEvent({
|
||||
userId,
|
||||
source: event.source,
|
||||
type: event.type,
|
||||
payload: event.payload,
|
||||
correlation: event.correlation,
|
||||
mission: event.mission ?? missionFromRequest(c.req.raw, body),
|
||||
subject: event.subject,
|
||||
}).catch((err) => log.warn({ err, userId, action }, "failed to record matchmaking workflow event"));
|
||||
return c.json(result);
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -1,77 +1,12 @@
|
||||
import { Hono } from "hono";
|
||||
import { requireUser, type AuthContext } from "../auth/clerk.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { onboarding, users, userStacks, type UserStack } from "../db/schema.js";
|
||||
import { users, userStacks, type UserStack } from "../db/schema.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { provisionUserStack } from "../docker/manager.js";
|
||||
import { log } from "../log.js";
|
||||
import { fetchUserProfile, patchPreferencesRequest, userServiceTarget } from "../services/user-profile.js";
|
||||
import {
|
||||
onboardingCompletedAtFromPreferences,
|
||||
} from "../v1/curator/curator-onboarding-loop.js";
|
||||
import {
|
||||
getLatestValidOnboardingLedgerEvent,
|
||||
recordAndProcessOnboardingCompletion,
|
||||
extractOnboardingData,
|
||||
deriveOnboardingPayload,
|
||||
assertOnboardingRevision,
|
||||
mergeOnboardingPatch,
|
||||
resetOnboardingLedger,
|
||||
resetOnboardingPreferences,
|
||||
defaultOnboardingData,
|
||||
OnboardingRevisionConflict,
|
||||
type OnboardingData,
|
||||
} from "../events/onboarding-ledger.js";
|
||||
import { z } from "zod";
|
||||
|
||||
const onboardingPatchSchema = z.object({
|
||||
expectedRevision: z.number().int().nonnegative().optional(),
|
||||
data: z.record(z.unknown()),
|
||||
});
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
function strings(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : [];
|
||||
}
|
||||
|
||||
const curatorIcpByOnboardingIcp: Record<string, string> = {
|
||||
student: "student_recent_grad",
|
||||
intern: "intern",
|
||||
fresher: "fresher_early_professional",
|
||||
experienced: "experienced_professional",
|
||||
freelancer: "freelancer",
|
||||
founder: "founder",
|
||||
};
|
||||
|
||||
function buildOnboardingPayload(data: Record<string, unknown>) {
|
||||
const profile = asRecord(data.profile);
|
||||
const responses = asRecord(data.responses);
|
||||
const onboardingIcp = typeof profile.icp === "string" ? profile.icp : "fresher";
|
||||
const targetField = typeof responses.target_field === "string"
|
||||
? responses.target_field
|
||||
: typeof responses.venture_industry === "string"
|
||||
? responses.venture_industry
|
||||
: null;
|
||||
|
||||
return {
|
||||
schema_version: 1,
|
||||
source_revision: typeof data.revision === "number" ? data.revision : 0,
|
||||
primary_intent: typeof profile.intent === "string" ? profile.intent : null,
|
||||
mode: typeof profile.mode === "string" ? profile.mode : null,
|
||||
question_branch: typeof profile.question_branch === "string" ? profile.question_branch : null,
|
||||
onboarding_icp: onboardingIcp,
|
||||
curator_registry_icp: curatorIcpByOnboardingIcp[onboardingIcp] ?? "fresher_early_professional",
|
||||
target_role: typeof responses.target_role === "string" ? responses.target_role : null,
|
||||
target_field: targetField,
|
||||
experience_context: typeof responses.experience_level === "string" ? responses.experience_level : null,
|
||||
goals: strings(responses.desired_outcomes),
|
||||
barriers: strings(responses.career_barriers),
|
||||
priority: typeof responses.target_milestone === "string" ? responses.target_milestone : null,
|
||||
weekly_time_commitment: typeof responses.weekly_time_commitment === "string" ? responses.weekly_time_commitment : null,
|
||||
};
|
||||
}
|
||||
import { config } from "../config.js";
|
||||
import { ensureOnboardingBaselineQscore } from "../events/onboarding-qscore.js";
|
||||
|
||||
function publicStack(stack: UserStack | null | undefined) {
|
||||
if (!stack) return stack;
|
||||
@@ -79,6 +14,9 @@ function publicStack(stack: UserStack | null | undefined) {
|
||||
return safe;
|
||||
}
|
||||
|
||||
function userServiceTarget(path: string, search = "") {
|
||||
return new URL(`/api/v1/users${path}${search}`, config.userServiceUrl.replace(/\/$/, ""));
|
||||
}
|
||||
|
||||
async function fetchUserService(req: Request, path: string) {
|
||||
const incoming = new URL(req.url);
|
||||
@@ -114,75 +52,6 @@ async function ensureUserServiceUser(req: Request) {
|
||||
return res.json() as Promise<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
|
||||
function recordPreferences(profile: Record<string, unknown>): Record<string, unknown> {
|
||||
return isPlainObject(profile.preferences) ? profile.preferences : {};
|
||||
}
|
||||
|
||||
/** Build the { id, data, payload, revision, updatedAt } response shape. */
|
||||
function onboardingStateResponse(userId: string, data: OnboardingData) {
|
||||
const updatedAt = data.progress.updated_at ?? data.completed_at;
|
||||
return {
|
||||
id: userId,
|
||||
data,
|
||||
payload: deriveOnboardingPayload(data),
|
||||
revision: data.revision,
|
||||
updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
type OnboardingResetResult =
|
||||
| { error: null; body: Record<string, unknown>; status: 200 }
|
||||
| { error: string; message: string; status: 502 };
|
||||
|
||||
/**
|
||||
* Reusable per-user onboarding reset. Resets preferences.onboarding to the
|
||||
* default v3 doc (preserving all unrelated preference keys, history, and
|
||||
* curator data) and deletes ONLY the user's completion-type ledger rows so the
|
||||
* onboarding gate reopens. Idempotent — a user with no onboarding state is a
|
||||
* no-op success.
|
||||
*
|
||||
* Exported so the staging bulk-reset CLI can reuse the exact same logic
|
||||
* (preferences reset + backend ledger deletion) end-to-end via the service
|
||||
* token path, instead of re-implementing it.
|
||||
*/
|
||||
export async function resetUserOnboarding(req: Request, userId: string): Promise<OnboardingResetResult> {
|
||||
const profile = await fetchUserProfile(req);
|
||||
const preferences = recordPreferences(profile);
|
||||
const nextPreferences = resetOnboardingPreferences(preferences);
|
||||
|
||||
const updateRes = await fetchUserService(patchPreferencesRequest(req, nextPreferences), "/me");
|
||||
if (!updateRes.ok) {
|
||||
const text = await updateRes.text().catch(() => "");
|
||||
log.error({ err: text, userId, status: updateRes.status }, "failed to persist reset onboarding preferences");
|
||||
return {
|
||||
error: "user_service_persist_failed",
|
||||
message: "Failed to reset onboarding preferences",
|
||||
status: 502,
|
||||
};
|
||||
}
|
||||
|
||||
const ledgerDeleted = await resetOnboardingLedger(userId);
|
||||
log.info({ userId, ledgerDeleted }, "onboarding reset complete");
|
||||
|
||||
const data = extractOnboardingData(nextPreferences.onboarding);
|
||||
return {
|
||||
error: null,
|
||||
body: {
|
||||
id: userId,
|
||||
reset: true,
|
||||
ledgerRowsDeleted: ledgerDeleted,
|
||||
data,
|
||||
},
|
||||
status: 200,
|
||||
};
|
||||
}
|
||||
|
||||
export function userRoutes() {
|
||||
const app = new Hono<AuthContext>();
|
||||
app.use("*", requireUser);
|
||||
@@ -203,10 +72,6 @@ export function userRoutes() {
|
||||
);
|
||||
}
|
||||
|
||||
const rawPlan = typeof userServiceUser.plan === "string" ? userServiceUser.plan.toLowerCase() : "free";
|
||||
const plan = rawPlan === "pro" || rawPlan === "enterprise" ? rawPlan : "free";
|
||||
await db.update(users).set({ plan, updatedAt: new Date() }).where(eq(users.id, userId));
|
||||
|
||||
const userRow = await db.query.users.findFirst({
|
||||
where: eq(users.id, userId),
|
||||
});
|
||||
@@ -214,6 +79,12 @@ export function userRoutes() {
|
||||
where: eq(userStacks.userId, userId),
|
||||
});
|
||||
|
||||
if (!stack || stack.status !== "running") {
|
||||
void provisionUserStack(userId).catch((err) =>
|
||||
log.error({ err, userId }, "background provision failed"),
|
||||
);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
user: userServiceUser,
|
||||
backendUser: userRow,
|
||||
@@ -222,190 +93,6 @@ export function userRoutes() {
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/onboarding-status", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const state = await db.query.onboarding.findFirst({ where: eq(onboarding.userId, userId) });
|
||||
const event = await getLatestValidOnboardingLedgerEvent(userId);
|
||||
if (event) {
|
||||
return c.json({
|
||||
userId,
|
||||
hasOnboardingEvent: true,
|
||||
onboardingEvent: {
|
||||
id: event.id,
|
||||
type: event.type,
|
||||
occurredAt: event.occurredAt.toISOString(),
|
||||
processingStatus: event.processingStatus,
|
||||
},
|
||||
needsOnboarding: false,
|
||||
});
|
||||
}
|
||||
|
||||
// Ledger missing but canonical preferences may still show completed (the
|
||||
// completion row was lost/never written). Self-heal: re-run the existing
|
||||
// idempotent completion recording, which re-creates the ledger row via the
|
||||
// `onboarding:completed:<userId>` dedupe key. If the canonical state is NOT
|
||||
// completed, the gate stays open honestly. A heal failure must surface as a
|
||||
// service error — never as a false "needs onboarding" for an already-done
|
||||
// user, which would force them through onboarding again.
|
||||
let healed: { ok: true } | { ok: false; status: 502; error: string; message: string };
|
||||
try {
|
||||
const profile = await fetchUserProfile(c.req.raw);
|
||||
const preferences = recordPreferences(profile);
|
||||
const data = extractOnboardingData(preferences.onboarding);
|
||||
if (data.status === "completed" && data.completed_at) {
|
||||
await recordAndProcessOnboardingCompletion({
|
||||
userId,
|
||||
completedAt: data.completed_at,
|
||||
source: "onboarding-status-heal",
|
||||
context: { preferences, onboarding: data },
|
||||
});
|
||||
healed = { ok: true };
|
||||
} else {
|
||||
return c.json({
|
||||
userId,
|
||||
hasOnboardingEvent: false,
|
||||
onboardingEvent: null,
|
||||
needsOnboarding: true,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
log.error({ err, userId }, "onboarding status heal failed");
|
||||
healed = {
|
||||
ok: false,
|
||||
status: 502,
|
||||
error: "onboarding_status_heal_failed",
|
||||
message: "Onboarding status check failed; please retry",
|
||||
};
|
||||
}
|
||||
|
||||
if (!healed.ok) {
|
||||
return c.json({ error: healed.error, message: healed.message }, healed.status);
|
||||
}
|
||||
|
||||
// The heal re-ran the idempotent completion recording, so a fresh read
|
||||
// should now find the ledger row. Re-fetch to return a consistent shape
|
||||
// (onboardingEvent populated alongside hasOnboardingEvent: true) rather than
|
||||
// a contradictory true+null that downstream consumers would misread.
|
||||
const fresh = await getLatestValidOnboardingLedgerEvent(userId);
|
||||
return c.json({
|
||||
userId,
|
||||
hasOnboardingEvent: true,
|
||||
onboardingEvent: fresh
|
||||
? {
|
||||
id: fresh.id,
|
||||
type: fresh.type,
|
||||
occurredAt: fresh.occurredAt.toISOString(),
|
||||
processingStatus: fresh.processingStatus,
|
||||
}
|
||||
: null,
|
||||
needsOnboarding: false,
|
||||
healed: true,
|
||||
});
|
||||
});
|
||||
|
||||
// ── Onboarding preferences (canonical store: user-service preferences.onboarding) ─
|
||||
// GET returns the v3 OnboardingData (or a default v3 doc when none exists yet)
|
||||
// plus its faithful projection payload and the current revision.
|
||||
// PATCH performs optimistic-concurrency merge (expectedRevision must match),
|
||||
// preserves all unrelated preference keys, persists access_choice, validates
|
||||
// status/completed_at consistency, and fires the existing completion ledger
|
||||
// side effects when status is "completed".
|
||||
app.get("/onboarding", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const profile = await fetchUserProfile(c.req.raw);
|
||||
const preferences = recordPreferences(profile);
|
||||
const data = extractOnboardingData(preferences.onboarding);
|
||||
return c.json(onboardingStateResponse(userId, data));
|
||||
});
|
||||
|
||||
app.patch("/onboarding", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const body = await c.req.json().catch(() => null) as
|
||||
| { data?: unknown; expectedRevision?: unknown }
|
||||
| null;
|
||||
|
||||
if (!body || typeof body !== "object") {
|
||||
return c.json({ error: "invalid_body", message: "Expected { data, expectedRevision }" }, 400);
|
||||
}
|
||||
|
||||
const incoming = extractOnboardingData(body.data);
|
||||
const expectedRevision = typeof body.expectedRevision === "number" ? body.expectedRevision : undefined;
|
||||
|
||||
// Read current profile to resolve the stored revision for the OCC check.
|
||||
const profile = await fetchUserProfile(c.req.raw);
|
||||
const preferences = recordPreferences(profile);
|
||||
const current = extractOnboardingData(preferences.onboarding);
|
||||
|
||||
try {
|
||||
assertOnboardingRevision(expectedRevision, current);
|
||||
} catch (err) {
|
||||
if (err instanceof OnboardingRevisionConflict) {
|
||||
return c.json(
|
||||
{
|
||||
error: "revision_conflict",
|
||||
message: "Onboarding was modified by another session. Reload and retry.",
|
||||
expected: err.expected,
|
||||
actual: err.actual,
|
||||
revision: err.actual,
|
||||
},
|
||||
409,
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Stamp the incoming revision with the authoritative stored revision so the
|
||||
// merge helper bumps from it (the client sends the revision it read).
|
||||
const incomingWithClientRev: OnboardingData = { ...incoming, revision: current.revision };
|
||||
const nextPreferences = mergeOnboardingPatch(preferences, incomingWithClientRev);
|
||||
const nextData = extractOnboardingData(nextPreferences.onboarding);
|
||||
|
||||
// Persist back to user-service via PATCH /me (only preferences is written).
|
||||
const updateRes = await fetchUserService(patchPreferencesRequest(c.req.raw, nextPreferences), "/me");
|
||||
if (!updateRes.ok) {
|
||||
const text = await updateRes.text().catch(() => "");
|
||||
log.error({ err: text, userId, status: updateRes.status }, "failed to persist onboarding preferences");
|
||||
return c.json(
|
||||
{ error: "user_service_persist_failed", message: "Failed to save onboarding preferences" },
|
||||
502,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Completion ledger side effects (idempotent via dedupeKey onboarding:completed:<userId>).
|
||||
if (nextData.status === "completed" && nextData.completed_at) {
|
||||
try {
|
||||
await recordAndProcessOnboardingCompletion({
|
||||
userId,
|
||||
completedAt: nextData.completed_at,
|
||||
source: "onboarding-api",
|
||||
context: {
|
||||
preferences: nextPreferences,
|
||||
onboarding: nextData,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
log.warn({ err, userId }, "failed to run onboarding completion side effects");
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(onboardingStateResponse(userId, nextData));
|
||||
});
|
||||
|
||||
// DELETE /onboarding — authenticated per-user reset. Reopens the onboarding
|
||||
// gate by deleting ONLY the user's completion-type ledger rows (those carrying
|
||||
// the onboarding:completed dedupe key) and resetting preferences.onboarding to
|
||||
// the default v3 doc. Preserves unrelated preferences, snapshot history,
|
||||
// missions, and curator data. Idempotent: deleting nothing is a success.
|
||||
app.delete("/onboarding", async (c) => {
|
||||
const userId = c.get("userId");
|
||||
const result = await resetUserOnboarding(c.req.raw, userId);
|
||||
if (result.error !== null) {
|
||||
return c.json({ error: result.error, message: result.message }, result.status);
|
||||
}
|
||||
return c.json(result.body, result.status);
|
||||
});
|
||||
|
||||
app.get("/me", async (c) => proxyUserService(c.req.raw, "/me"));
|
||||
app.patch("/me", async (c) => {
|
||||
const res = await fetchUserService(c.req.raw, "/me");
|
||||
@@ -415,23 +102,14 @@ export function userRoutes() {
|
||||
try {
|
||||
const userProfile = JSON.parse(text) as Record<string, unknown>;
|
||||
const preferences = userProfile.preferences;
|
||||
const normalizedPreferences = preferences && typeof preferences === "object" && !Array.isArray(preferences)
|
||||
? (preferences as Record<string, unknown>)
|
||||
: undefined;
|
||||
const completedAt = onboardingCompletedAtFromPreferences(normalizedPreferences);
|
||||
if (completedAt) {
|
||||
await recordAndProcessOnboardingCompletion({
|
||||
userId: c.get("userId"),
|
||||
completedAt,
|
||||
source: "user-service-profile",
|
||||
context: {
|
||||
preferences: normalizedPreferences,
|
||||
profile: userProfile,
|
||||
},
|
||||
});
|
||||
}
|
||||
await ensureOnboardingBaselineQscore(
|
||||
c.get("userId"),
|
||||
preferences && typeof preferences === "object" && !Array.isArray(preferences)
|
||||
? (preferences as Record<string, unknown>)
|
||||
: undefined,
|
||||
);
|
||||
} catch (err) {
|
||||
log.warn({ err, userId: c.get("userId") }, "failed to run onboarding side effects after user update");
|
||||
log.warn({ err, userId: c.get("userId") }, "failed to seed onboarding Q Score baseline after user update");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -249,7 +249,7 @@ async function runModulesUntilGate(input: {
|
||||
}
|
||||
|
||||
function extractQScore(output: Record<string, unknown>): number | undefined {
|
||||
const direct = output.q_score;
|
||||
const direct = output.q_score ?? output.estimated_q_score;
|
||||
if (typeof direct === "number") return Math.round(direct);
|
||||
const compute = output.compute as Record<string, unknown> | undefined;
|
||||
if (typeof compute?.q_score === "number") return Math.round(compute.q_score);
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
|
||||
function isRecord(value: unknown): value is JsonRecord {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function getString(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function getNumber(value: unknown): number | undefined {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const n = Number(value);
|
||||
if (Number.isFinite(n)) return n;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function paramsFromBody(body: JsonRecord): JsonRecord {
|
||||
return isRecord(body.params) ? body.params : {};
|
||||
}
|
||||
|
||||
function agentData(result: JsonRecord, action: string): JsonRecord | undefined {
|
||||
const messages = Array.isArray(result.messages) ? result.messages : [];
|
||||
for (const item of messages) {
|
||||
const message = isRecord(item) ? item : {};
|
||||
if (message.type !== "agent_data") continue;
|
||||
if (message.action !== action) continue;
|
||||
const data = isRecord(message.data) ? message.data : {};
|
||||
return data;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function firstAgentData(result: JsonRecord, actions: string[]): JsonRecord {
|
||||
for (const action of actions) {
|
||||
const data = agentData(result, action);
|
||||
if (data) return data;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
export function matchmakingEventType(action: string, response: JsonRecord) {
|
||||
if (response.status === "error") return "matchmaking.request.failed";
|
||||
if (action === "run_search" || action === "generate_matches") return "matchmaking.matches.generated";
|
||||
if (action === "get_scout_feed" || action === "get_feed" || action === "session_start") return "matchmaking.feed.viewed";
|
||||
if (action === "get_opportunity_detail" || action === "mark_viewed") return "matchmaking.match.viewed";
|
||||
if (action === "mark_saved") return "matchmaking.match.saved";
|
||||
if (action === "record_feedback") return "matchmaking.matches.reviewed";
|
||||
if (action === "dismiss_opportunity") return "matchmaking.match.dismissed";
|
||||
if (action === "tailor_resume") return "matchmaking.application.started";
|
||||
if (action === "mark_applied") return "matchmaking.match.applied";
|
||||
if (action === "submit_application") return "matchmaking.application.completed";
|
||||
return "matchmaking.workflow.completed";
|
||||
}
|
||||
|
||||
function opportunityIdFrom(input: { params: JsonRecord; body: JsonRecord; data: JsonRecord; result: JsonRecord }) {
|
||||
return getString(
|
||||
input.params.opportunity_id ??
|
||||
input.params.opportunityId ??
|
||||
input.params.matchId ??
|
||||
input.params.jobId ??
|
||||
input.body.opportunity_id ??
|
||||
input.body.opportunityId ??
|
||||
input.data.opportunity_id ??
|
||||
input.data.opportunityId ??
|
||||
input.data.id ??
|
||||
input.result.opportunity_id ??
|
||||
input.result.opportunityId,
|
||||
);
|
||||
}
|
||||
|
||||
function missionFromParams(params: JsonRecord) {
|
||||
const instanceId = getString(params.missionInstanceId ?? params.instanceId);
|
||||
const missionId = getString(params.missionId);
|
||||
const stageId = getString(params.stageId);
|
||||
const source = getString(params.source);
|
||||
if (!instanceId && !missionId && !stageId) return undefined;
|
||||
return {
|
||||
missionId,
|
||||
instanceId,
|
||||
stageId,
|
||||
source: source ?? "curator-v1",
|
||||
};
|
||||
}
|
||||
|
||||
function countFromData(data: JsonRecord) {
|
||||
const opportunities = Array.isArray(data.opportunities) ? data.opportunities : undefined;
|
||||
return getNumber(data.matchCount ?? data.matches ?? data.shortlisted) ?? opportunities?.length;
|
||||
}
|
||||
|
||||
export function buildMatchmakingGatewayEvent(input: {
|
||||
userId: string;
|
||||
action: string;
|
||||
body: JsonRecord;
|
||||
result: JsonRecord;
|
||||
}) {
|
||||
const params = paramsFromBody(input.body);
|
||||
const type = matchmakingEventType(input.action, input.result);
|
||||
const data = firstAgentData(input.result, ["search_complete", "feed_loaded", "opportunity_state_saved", "opportunity_detail"]);
|
||||
const opportunityId = opportunityIdFrom({ params, body: input.body, data, result: input.result });
|
||||
const taskId = getString(params.curatorTaskId ?? params.taskId ?? input.body.curatorTaskId ?? input.body.taskId);
|
||||
const resultTaskId = getString(input.result.task_id ?? input.result.taskId);
|
||||
const matchCount = countFromData(data);
|
||||
const scanned = getNumber(data.scanned);
|
||||
|
||||
const payload: JsonRecord = {
|
||||
request: input.body,
|
||||
result: input.result,
|
||||
action: input.action,
|
||||
status: getString(input.result.status),
|
||||
...(taskId ? { taskId, curatorTaskId: taskId } : {}),
|
||||
...(opportunityId ? { opportunityId, matchId: opportunityId, jobId: opportunityId } : {}),
|
||||
...(matchCount !== undefined ? { matchCount } : {}),
|
||||
...(scanned !== undefined ? { scanned } : {}),
|
||||
...(getString(params.source) ? { source: getString(params.source) } : {}),
|
||||
};
|
||||
|
||||
return {
|
||||
userId: input.userId,
|
||||
source: "matchmaking-v2",
|
||||
type,
|
||||
payload,
|
||||
correlation: {
|
||||
...(taskId ? { taskId, curatorTaskId: taskId } : {}),
|
||||
...(opportunityId ? { opportunityId, matchId: opportunityId, jobId: opportunityId } : {}),
|
||||
externalId: opportunityId ?? resultTaskId,
|
||||
...(resultTaskId ? { a2aTaskId: resultTaskId } : {}),
|
||||
},
|
||||
mission: missionFromParams(params),
|
||||
subject: {
|
||||
serviceId: "matchmaking",
|
||||
kind: opportunityId ? "opportunity" : "opportunity_feed",
|
||||
id: opportunityId,
|
||||
externalId: opportunityId ?? resultTaskId ?? `matchmaking:${type}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -9,32 +9,10 @@ export type ServiceCallOptions = {
|
||||
timeoutMs?: number;
|
||||
};
|
||||
|
||||
export class ProductServiceError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: number,
|
||||
readonly body: string,
|
||||
readonly path: string,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "ProductServiceError";
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_SERVICE_TIMEOUT_MS = Number(process.env.PRODUCT_SERVICE_TIMEOUT_MS ?? 3500);
|
||||
const INTERACTIVE_SERVICE_TIMEOUT_MS = Number(process.env.PRODUCT_INTERACTIVE_SERVICE_TIMEOUT_MS ?? 120000);
|
||||
|
||||
function userHeader(userId?: string): Record<string, string> | undefined {
|
||||
return userId ? { "x-growqr-user": userId } : undefined;
|
||||
}
|
||||
|
||||
function resolveUserPayload(userIdOrPayload?: string | JsonObject, payload?: JsonObject) {
|
||||
return typeof userIdOrPayload === "string"
|
||||
? { userId: userIdOrPayload, payload }
|
||||
: { userId: undefined, payload: userIdOrPayload };
|
||||
}
|
||||
|
||||
export async function serviceJson<T = JsonObject>(
|
||||
async function serviceJson<T = JsonObject>(
|
||||
baseUrl: string,
|
||||
path: string,
|
||||
opts: ServiceCallOptions = {},
|
||||
@@ -50,16 +28,13 @@ export async function serviceJson<T = JsonObject>(
|
||||
signal: AbortSignal.timeout(opts.timeoutMs ?? DEFAULT_SERVICE_TIMEOUT_MS),
|
||||
});
|
||||
const text = await res.text();
|
||||
if (!res.ok) throw new ProductServiceError(`${path} returned HTTP ${res.status}: ${text}`, res.status, text, path);
|
||||
if (!res.ok) throw new Error(`${path} returned HTTP ${res.status}: ${text}`);
|
||||
return (text ? JSON.parse(text) : {}) as T;
|
||||
}
|
||||
|
||||
export const interviewService = {
|
||||
health: () => serviceJson(config.interviewServiceUrl, "/health"),
|
||||
pageState: (userId: string) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/interviews/page-state?${new URLSearchParams({ user_id: userId })}`, {
|
||||
headers: userHeader(userId),
|
||||
}),
|
||||
pageState: (userId: string) => serviceJson(config.interviewServiceUrl, `/api/v1/interviews/page-state?${new URLSearchParams({ user_id: userId })}`),
|
||||
configure: (payload: JsonObject) => serviceJson(config.interviewServiceUrl, "/api/v1/configure", { body: payload, timeoutMs: INTERACTIVE_SERVICE_TIMEOUT_MS }),
|
||||
preview: (payload: JsonObject) => serviceJson(config.interviewServiceUrl, "/api/v1/configure/preview", { body: payload, timeoutMs: INTERACTIVE_SERVICE_TIMEOUT_MS }),
|
||||
editQuestions: (payload: { session_id: string; questions: Array<JsonObject | string> }) =>
|
||||
@@ -74,39 +49,25 @@ export const interviewService = {
|
||||
serviceJson(config.interviewServiceUrl, "/api/v1/interviews/assignments/unassign", { body: payload }),
|
||||
resultsBulk: (payload: JsonObject) =>
|
||||
serviceJson(config.interviewServiceUrl, "/api/v1/interviews/results:bulk", { body: payload }),
|
||||
review: (sessionId: string, userId?: string) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/review/${encodeURIComponent(sessionId)}`, {
|
||||
headers: userHeader(userId),
|
||||
}),
|
||||
review: (sessionId: string) => serviceJson(config.interviewServiceUrl, `/api/v1/review/${encodeURIComponent(sessionId)}`),
|
||||
leaderboard: () => serviceJson(config.interviewServiceUrl, "/api/v1/leaderboard"),
|
||||
artifact: (sessionId: string, artifactType: string, userId?: string) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/artifacts/${encodeURIComponent(sessionId)}/${encodeURIComponent(artifactType)}`, {
|
||||
headers: userHeader(userId),
|
||||
artifact: (sessionId: string, artifactType: string) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/artifacts/${encodeURIComponent(sessionId)}/${encodeURIComponent(artifactType)}`),
|
||||
createVideoUploadUrl: (sessionId: string, payload?: JsonObject) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/upload-url`, {
|
||||
method: "POST",
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
}),
|
||||
createVideoUploadUrl: (sessionId: string, userIdOrPayload?: string | JsonObject, payloadInput?: JsonObject) => {
|
||||
const { userId, payload } = resolveUserPayload(userIdOrPayload, payloadInput);
|
||||
return serviceJson(config.interviewServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/upload-url`, {
|
||||
markVideoUploaded: (sessionId: string, payload?: JsonObject) =>
|
||||
serviceJson(config.interviewServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/uploaded`, {
|
||||
method: "POST",
|
||||
headers: userHeader(userId),
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
});
|
||||
},
|
||||
markVideoUploaded: (sessionId: string, userIdOrPayload?: string | JsonObject, payloadInput?: JsonObject) => {
|
||||
const { userId, payload } = resolveUserPayload(userIdOrPayload, payloadInput);
|
||||
return serviceJson(config.interviewServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/uploaded`, {
|
||||
method: "POST",
|
||||
headers: userHeader(userId),
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
});
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
export const roleplayService = {
|
||||
health: () => serviceJson(config.roleplayServiceUrl, "/health"),
|
||||
pageState: (userId: string) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/roleplays/page-state?${new URLSearchParams({ user_id: userId })}`, {
|
||||
headers: userHeader(userId),
|
||||
}),
|
||||
pageState: (userId: string) => serviceJson(config.roleplayServiceUrl, `/api/v1/roleplays/page-state?${new URLSearchParams({ user_id: userId })}`),
|
||||
configure: (payload: JsonObject) => serviceJson(config.roleplayServiceUrl, "/api/v1/roleplays/configure", { body: payload, timeoutMs: INTERACTIVE_SERVICE_TIMEOUT_MS }),
|
||||
preview: (payload: JsonObject) => serviceJson(config.roleplayServiceUrl, "/api/v1/roleplays/configure/preview", { body: payload, timeoutMs: INTERACTIVE_SERVICE_TIMEOUT_MS }),
|
||||
editQuestions: (payload: { session_id: string; questions: Array<JsonObject | string> }) =>
|
||||
@@ -121,31 +82,20 @@ export const roleplayService = {
|
||||
serviceJson(config.roleplayServiceUrl, "/api/v1/roleplays/assignments/unassign", { body: payload }),
|
||||
resultsBulk: (payload: JsonObject) =>
|
||||
serviceJson(config.roleplayServiceUrl, "/api/v1/roleplays/results:bulk", { body: payload }),
|
||||
review: (sessionId: string, userId?: string) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/roleplays/review/${encodeURIComponent(sessionId)}`, {
|
||||
headers: userHeader(userId),
|
||||
}),
|
||||
review: (sessionId: string) => serviceJson(config.roleplayServiceUrl, `/api/v1/roleplays/review/${encodeURIComponent(sessionId)}`),
|
||||
leaderboard: () => serviceJson(config.roleplayServiceUrl, "/api/v1/roleplays/leaderboard"),
|
||||
artifact: (sessionId: string, artifactType: string, userId?: string) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/artifacts/${encodeURIComponent(sessionId)}/${encodeURIComponent(artifactType)}`, {
|
||||
headers: userHeader(userId),
|
||||
artifact: (sessionId: string, artifactType: string) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/artifacts/${encodeURIComponent(sessionId)}/${encodeURIComponent(artifactType)}`),
|
||||
createVideoUploadUrl: (sessionId: string, payload?: JsonObject) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/upload-url`, {
|
||||
method: "POST",
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
}),
|
||||
createVideoUploadUrl: (sessionId: string, userIdOrPayload?: string | JsonObject, payloadInput?: JsonObject) => {
|
||||
const { userId, payload } = resolveUserPayload(userIdOrPayload, payloadInput);
|
||||
return serviceJson(config.roleplayServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/upload-url`, {
|
||||
markVideoUploaded: (sessionId: string, payload?: JsonObject) =>
|
||||
serviceJson(config.roleplayServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/uploaded`, {
|
||||
method: "POST",
|
||||
headers: userHeader(userId),
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
});
|
||||
},
|
||||
markVideoUploaded: (sessionId: string, userIdOrPayload?: string | JsonObject, payloadInput?: JsonObject) => {
|
||||
const { userId, payload } = resolveUserPayload(userIdOrPayload, payloadInput);
|
||||
return serviceJson(config.roleplayServiceUrl, `/api/v1/sessions/${encodeURIComponent(sessionId)}/video/uploaded`, {
|
||||
method: "POST",
|
||||
headers: userHeader(userId),
|
||||
...(payload === undefined ? {} : { body: payload }),
|
||||
});
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
export const resumeService = {
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { config } from "../config.js";
|
||||
import { log } from "../log.js";
|
||||
|
||||
// Current backend auth and actor contexts are single-org; keep the constraint
|
||||
// explicit and shared until org membership is threaded through every caller.
|
||||
export const DEFAULT_QSCORE_ORG_ID = "growqr";
|
||||
|
||||
/**
|
||||
* Deterministic identity mapping from a raw growqr userId to the qscore_service
|
||||
* stable UUID key. Both the signal feeder (write path) and the score readers
|
||||
* (read path) MUST use this so producer and consumer agree on identity.
|
||||
*
|
||||
* This is the single source of truth for the mapping; the feeder imports it
|
||||
* from here rather than maintaining a parallel copy.
|
||||
*/
|
||||
export function toQscoreUserId(userId: string): string {
|
||||
const hex = createHash("sha256").update(userId).digest("hex").slice(0, 32);
|
||||
return [
|
||||
hex.slice(0, 8),
|
||||
hex.slice(8, 12),
|
||||
`4${hex.slice(13, 16)}`,
|
||||
`8${hex.slice(17, 20)}`,
|
||||
hex.slice(20, 32),
|
||||
].join("-");
|
||||
}
|
||||
|
||||
/**
|
||||
* Raw response shape returned by qscore_service GET /v1/qscore/{user_id}.
|
||||
* See qscore_service/app/api/schemas.py QScoreReadResponse.
|
||||
*/
|
||||
export type QscoreServiceResult = {
|
||||
org_id: string;
|
||||
user_id: string;
|
||||
q_score: number;
|
||||
iq_score: number | null;
|
||||
eq_score: number | null;
|
||||
sq_score: number | null;
|
||||
overall_rqscore: number | null;
|
||||
profession: string;
|
||||
formula_version: string;
|
||||
formula_id: string;
|
||||
calculated_at: string;
|
||||
quotients: Record<string, unknown>;
|
||||
breakdown: Record<string, unknown>;
|
||||
ledger_seq_from: number | null;
|
||||
ledger_seq_to: number | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Public result type for the proxy helper. The assignment's contract names this
|
||||
* `QscoreProxyResult`; it is the non-null service response (the function itself
|
||||
* returns `QscoreProxyResult | null` for graceful degradation).
|
||||
*/
|
||||
export type QscoreProxyResult = QscoreServiceResult;
|
||||
|
||||
const QSCORE_PROXY_TIMEOUT_MS = Number(
|
||||
process.env.QSCORE_PROXY_TIMEOUT_MS ?? 3500,
|
||||
);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function parseResult(body: unknown): QscoreServiceResult {
|
||||
if (!isRecord(body)) {
|
||||
throw new Error("qscore_service returned non-object response");
|
||||
}
|
||||
const q = body.q_score;
|
||||
if (typeof q !== "number" || !Number.isFinite(q)) {
|
||||
throw new Error("qscore_service response missing numeric q_score");
|
||||
}
|
||||
const numOrNull = (v: unknown): number | null =>
|
||||
typeof v === "number" && Number.isFinite(v) ? v : null;
|
||||
const str = (v: unknown): string => (typeof v === "string" ? v : "");
|
||||
return {
|
||||
org_id: str(body.org_id),
|
||||
user_id: str(body.user_id),
|
||||
q_score: q,
|
||||
iq_score: numOrNull(body.iq_score),
|
||||
eq_score: numOrNull(body.eq_score),
|
||||
sq_score: numOrNull(body.sq_score),
|
||||
overall_rqscore: numOrNull(body.overall_rqscore),
|
||||
profession: str(body.profession),
|
||||
formula_version: str(body.formula_version),
|
||||
formula_id: str(body.formula_id),
|
||||
calculated_at: str(body.calculated_at),
|
||||
quotients: isRecord(body.quotients) ? body.quotients : {},
|
||||
breakdown: isRecord(body.breakdown) ? body.breakdown : {},
|
||||
ledger_seq_from: numOrNull(body.ledger_seq_from),
|
||||
ledger_seq_to: numOrNull(body.ledger_seq_to),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the latest computed Q-Score for a user from qscore_service.
|
||||
*
|
||||
* Takes a RAW growqr userId (matching the local-DB readers) and applies
|
||||
* {@link toQscoreUserId} internally before calling the service, so all callers
|
||||
* share one identity contract.
|
||||
*
|
||||
* Returns null when the service is unreachable, returns a non-success status,
|
||||
* or reports no score has been computed yet (404). Never throws.
|
||||
*/
|
||||
export async function getQscoreFromService(
|
||||
userId: string,
|
||||
orgId: string = DEFAULT_QSCORE_ORG_ID,
|
||||
timeoutMs: number = QSCORE_PROXY_TIMEOUT_MS,
|
||||
): Promise<QscoreProxyResult | null> {
|
||||
const qscoreUserId = toQscoreUserId(userId);
|
||||
const path = `/v1/qscore/${encodeURIComponent(qscoreUserId)}?org_id=${encodeURIComponent(orgId)}`;
|
||||
const url = `${config.qscoreServiceUrl.replace(/\/$/, "")}${path}`;
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
...(config.a2aAllowedKey
|
||||
? { authorization: `Bearer ${config.a2aAllowedKey}` }
|
||||
: {}),
|
||||
},
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
// 404 = no run computed yet for this user → treat as null (graceful).
|
||||
if (res.status === 404) return null;
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.warn(
|
||||
{ status: res.status, body: text, userId, orgId },
|
||||
"qscore_service returned non-success status",
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const body = await res.json();
|
||||
return parseResult(body);
|
||||
} catch (err) {
|
||||
log.warn({ err, userId, orgId }, "qscore_service proxy call failed");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
import { config } from "../config.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { buildServiceSessionPath } from "./service-registry.js";
|
||||
import type { QscoreSignal } from "../events/envelope.js";
|
||||
import { toQscoreUserId } from "./qscore-proxy.js";
|
||||
import { serviceJson as productServiceJson } from "./product-service-clients.js";
|
||||
|
||||
// Lightweight agent reference (works with both old AgentProfile and new SubAgentModule).
|
||||
export type ServiceAgentRef = {
|
||||
@@ -32,17 +28,32 @@ export function buildServiceSessionUrl(
|
||||
detail: Record<string, unknown> | undefined,
|
||||
goal?: string,
|
||||
): string | undefined {
|
||||
if (
|
||||
service !== "interview-service" &&
|
||||
service !== "roleplay-service" &&
|
||||
service !== "resume-service"
|
||||
) {
|
||||
return undefined;
|
||||
const base = config.workflowsDashboardUrl.replace(/\/$/, "");
|
||||
const sessionId = detail?.session_id ?? detail?.sessionId;
|
||||
const params = new URLSearchParams();
|
||||
if (sessionId && typeof sessionId === "string") params.set("session_id", sessionId);
|
||||
if (goal) params.set("goal", goal);
|
||||
|
||||
if (service === "interview-service") {
|
||||
if (!sessionId || typeof sessionId !== "string") return undefined;
|
||||
params.set("role", String(detail?.target_role ?? goal ?? "Interview practice"));
|
||||
params.set("type", String(detail?.interview_type ?? "behavioral"));
|
||||
return `${base}/v2/service-sessions/interview?${params.toString()}`;
|
||||
}
|
||||
|
||||
const path = buildServiceSessionPath(service, detail, goal);
|
||||
if (!path) return undefined;
|
||||
return `${config.workflowsDashboardUrl.replace(/\/$/, "")}${path}`;
|
||||
if (service === "roleplay-service") {
|
||||
if (!sessionId || typeof sessionId !== "string") return undefined;
|
||||
params.set("role", String(detail?.target_role ?? goal ?? "Roleplay practice"));
|
||||
params.set("type", String(detail?.roleplay_type ?? "custom"));
|
||||
return `${base}/v2/service-sessions/roleplay?${params.toString()}`;
|
||||
}
|
||||
|
||||
if (service === "resume-service") {
|
||||
if (goal) params.set("role", goal);
|
||||
return `${base}/v2/service-sessions/resume${params.size ? `?${params.toString()}` : ""}`;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function stableUuid(input: string): string {
|
||||
@@ -118,7 +129,7 @@ async function runInterviewService(ctx: ServiceAgentContext): Promise<ServiceAge
|
||||
);
|
||||
return {
|
||||
status: "ok",
|
||||
summary: `Mock Interview created interview session ${detail.session_id ?? "(pending id)"} for ${ctx.goal}.`,
|
||||
summary: `Interview Agent created interview session ${detail.session_id ?? "(pending id)"} for ${ctx.goal}.`,
|
||||
detail: {
|
||||
...detail,
|
||||
target_role: payload.context.target_role,
|
||||
@@ -162,7 +173,7 @@ async function runRoleplayService(ctx: ServiceAgentContext): Promise<ServiceAgen
|
||||
);
|
||||
return {
|
||||
status: "ok",
|
||||
summary: `Mock Roleplay created roleplay session ${detail.session_id ?? "(pending id)"} for ${ctx.goal}.`,
|
||||
summary: `Roleplay Agent created roleplay session ${detail.session_id ?? "(pending id)"} for ${ctx.goal}.`,
|
||||
detail: {
|
||||
...detail,
|
||||
target_role: payload.metadata.target_role,
|
||||
@@ -239,12 +250,18 @@ async function runQScoreService(ctx: ServiceAgentContext): Promise<ServiceAgentR
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
// Graceful fallback: formula store unavailable → use static estimate
|
||||
const avgSignalScore = Math.round(
|
||||
signals.reduce((sum, s) => sum + s.score, 0) / signals.length,
|
||||
);
|
||||
return {
|
||||
status: "unavailable",
|
||||
summary: `Q Score compute failed; no score was generated: ${err instanceof Error ? err.message : String(err)}`,
|
||||
status: "ok",
|
||||
summary: `Q Score Agent estimated Q-Score ~${avgSignalScore} (service compute unavailable: formula store may not be seeded). Based on ${signals.length} signals.`,
|
||||
detail: {
|
||||
ingest,
|
||||
estimated_q_score: avgSignalScore,
|
||||
signal_scores: signals.map(s => ({ id: s.signal_id, score: s.score })),
|
||||
compute_fallback: true,
|
||||
compute_error: err instanceof Error ? err.message : String(err),
|
||||
},
|
||||
};
|
||||
@@ -252,51 +269,12 @@ async function runQScoreService(ctx: ServiceAgentContext): Promise<ServiceAgentR
|
||||
|
||||
return {
|
||||
status: "ok",
|
||||
summary: `Q Score computed Q Score ${compute.q_score ?? "(unknown)"} for ${ctx.goal}.`,
|
||||
summary: `Q Score Agent computed Q-Score ${compute.q_score ?? "(unknown)"} for ${ctx.goal}.`,
|
||||
detail: { ingest, compute, qscore_user_id: qscoreUserId },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward extracted Grow signals to qscore_service for scoring.
|
||||
*
|
||||
* qscore_service OWNS all scoring; the backend only extracts signals and feeds
|
||||
* them here. The userId is hashed to the qscore UUID via toQscoreUserId (from
|
||||
* qscore-proxy.ts, the single source of truth shared with readers). The ingest
|
||||
* endpoint persists signals and marks the user dirty for async score
|
||||
* computation — it does NOT return a score, so the caller treats the result as
|
||||
* fire-and-forget.
|
||||
*/
|
||||
export async function forwardSignalsToQscoreService(params: {
|
||||
orgId: string;
|
||||
userId: string;
|
||||
profession: string;
|
||||
source: string;
|
||||
signals: QscoreSignal[];
|
||||
}): Promise<void> {
|
||||
const qscoreUserId = toQscoreUserId(params.userId);
|
||||
await productServiceJson(
|
||||
config.qscoreServiceUrl,
|
||||
"/v1/signals/ingest",
|
||||
{
|
||||
method: "POST",
|
||||
body: {
|
||||
org_id: params.orgId,
|
||||
user_id: qscoreUserId,
|
||||
profession: params.profession,
|
||||
source: params.source,
|
||||
signals: params.signals.map((s) => ({
|
||||
signal_id: s.signalId,
|
||||
present: s.present,
|
||||
score: s.score,
|
||||
raw: s.raw ?? null,
|
||||
})),
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ── Resume Building (resume-builder service from growqr-app) ──
|
||||
// ── Resume Agent (resume-builder service from growqr-app) ──
|
||||
|
||||
async function runResumeAnalyze(ctx: ServiceAgentContext): Promise<ServiceAgentResult> {
|
||||
// Probe resume state for the user
|
||||
@@ -311,8 +289,8 @@ async function runResumeAnalyze(ctx: ServiceAgentContext): Promise<ServiceAgentR
|
||||
return {
|
||||
status: "ok",
|
||||
summary: hasResume
|
||||
? `Resume Building found ${detail.resume_count} resume(s) at ${completeness}% completeness. Current role: ${detail.current_role ?? "unknown"}.`
|
||||
: "No existing resume found. Resume Building is ready to build one from scratch.",
|
||||
? `Resume Agent found ${detail.resume_count} resume(s) at ${completeness}% completeness. Current role: ${detail.current_role ?? "unknown"}.`
|
||||
: "No existing resume found. Resume Agent is ready to build one from scratch.",
|
||||
detail: {
|
||||
resume_count: detail.resume_count,
|
||||
completeness,
|
||||
@@ -324,7 +302,7 @@ async function runResumeAnalyze(ctx: ServiceAgentContext): Promise<ServiceAgentR
|
||||
} catch (err) {
|
||||
return {
|
||||
status: "unavailable",
|
||||
summary: `Resume Building unavailable: ${err instanceof Error ? err.message : String(err)}`,
|
||||
summary: `Resume Agent unavailable: ${err instanceof Error ? err.message : String(err)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -339,7 +317,7 @@ async function runResumeTailor(ctx: ServiceAgentContext): Promise<ServiceAgentRe
|
||||
// Return summary with optimization guidance
|
||||
return {
|
||||
status: "ok",
|
||||
summary: `Resume Building analyzed your profile for the role "${ctx.goal}". Skills detected: ${(stateResult.detail as any)?.skills?.slice(0, 5).join(", ") ?? "none"}. Resume ready for optimization.`,
|
||||
summary: `Resume Agent analyzed your profile for the role "${ctx.goal}". Skills detected: ${(stateResult.detail as any)?.skills?.slice(0, 5).join(", ") ?? "none"}. Resume ready for optimization.`,
|
||||
detail: {
|
||||
...(stateResult.detail as Record<string, unknown> ?? {}),
|
||||
goal: ctx.goal,
|
||||
@@ -404,11 +382,11 @@ export async function runServiceAgentProbe(
|
||||
case "interview-service":
|
||||
return ctx
|
||||
? await runInterviewService(ctx)
|
||||
: healthCheck(config.interviewServiceUrl, "Mock Interview / interview-service");
|
||||
: healthCheck(config.interviewServiceUrl, "Interview Agent / interview-service");
|
||||
case "roleplay-service":
|
||||
return ctx
|
||||
? await runRoleplayService(ctx)
|
||||
: healthCheck(config.roleplayServiceUrl, "Mock Roleplay / roleplay-service");
|
||||
: healthCheck(config.roleplayServiceUrl, "Roleplay Agent / roleplay-service");
|
||||
case "qscore-service":
|
||||
return ctx
|
||||
? await runQScoreService(ctx)
|
||||
@@ -416,7 +394,7 @@ export async function runServiceAgentProbe(
|
||||
case "resume-service":
|
||||
return ctx
|
||||
? await runResumeTailor(ctx)
|
||||
: healthCheck(config.resumeServiceUrl, "Resume Building / resume-service");
|
||||
: healthCheck(config.resumeServiceUrl, "Resume Agent / resume-service");
|
||||
case "matchmaking-service":
|
||||
return ctx
|
||||
? await runMatchmaking(ctx)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,102 +0,0 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { config } from "../config.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { users } from "../db/schema.js";
|
||||
|
||||
export type UserProfileContext = {
|
||||
userProfile?: Record<string, unknown>;
|
||||
preferences?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function bearerToken(req: Request): string {
|
||||
return (req.headers.get("authorization") ?? "").replace(/^Bearer\s+/i, "").trim();
|
||||
}
|
||||
|
||||
function isTrustedServiceToken(token: string): boolean {
|
||||
return Boolean(token && (token === config.serviceToken || token === config.a2aAllowedKey));
|
||||
}
|
||||
|
||||
function splitDisplayName(displayName: string | null | undefined) {
|
||||
const parts = (displayName ?? "").trim().split(/\s+/).filter(Boolean);
|
||||
return {
|
||||
firstName: parts[0] || undefined,
|
||||
lastName: parts.length > 1 ? parts.slice(1).join(" ") : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function mergeProfile(
|
||||
base: UserProfileContext,
|
||||
incoming: Record<string, unknown> | null | undefined,
|
||||
userId: string,
|
||||
): UserProfileContext {
|
||||
const userProfile: Record<string, unknown> = { ...(base.userProfile ?? {}) };
|
||||
if (incoming) {
|
||||
for (const [key, value] of Object.entries(incoming)) {
|
||||
if (value !== null && value !== undefined) userProfile[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
userProfile.clerk_id = String(userProfile.clerk_id ?? userId);
|
||||
const preferences = isRecord(incoming?.preferences) ? incoming.preferences : base.preferences ?? {};
|
||||
return { userProfile, preferences };
|
||||
}
|
||||
|
||||
async function backendMirrorProfile(userId: string): Promise<UserProfileContext> {
|
||||
const row = await db.query.users.findFirst({ where: eq(users.id, userId) });
|
||||
const displayName = row?.displayName ?? userId;
|
||||
const { firstName, lastName } = splitDisplayName(displayName);
|
||||
return {
|
||||
userProfile: {
|
||||
clerk_id: row?.id ?? userId,
|
||||
email: row?.email ?? `${userId}@service.local`,
|
||||
display_name: displayName,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
preferences: {},
|
||||
metadata: { source: "backend_user_mirror" },
|
||||
},
|
||||
preferences: {},
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchUserServiceJson(path: string, headers: Headers): Promise<Record<string, unknown> | null> {
|
||||
const target = new URL(path, config.userServiceUrl.replace(/\/$/, ""));
|
||||
const res = await fetch(target, { method: "GET", headers });
|
||||
if (!res.ok) return null;
|
||||
const json = await res.json().catch(() => null);
|
||||
return isRecord(json) ? json : null;
|
||||
}
|
||||
|
||||
async function a2aUserState(userId: string): Promise<Record<string, unknown> | null> {
|
||||
const headers = new Headers();
|
||||
headers.set("authorization", `Bearer ${config.a2aAllowedKey}`);
|
||||
return fetchUserServiceJson(`/api/state/${encodeURIComponent(userId)}`, headers);
|
||||
}
|
||||
|
||||
async function clerkUserProfile(req: Request): Promise<Record<string, unknown> | null> {
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
return fetchUserServiceJson("/api/v1/users/me", headers);
|
||||
}
|
||||
|
||||
export async function getRequestUserProfile(req: Request, userId: string): Promise<UserProfileContext> {
|
||||
const base = await backendMirrorProfile(userId);
|
||||
const token = bearerToken(req);
|
||||
|
||||
if (token && !isTrustedServiceToken(token)) {
|
||||
const profile = await clerkUserProfile(req);
|
||||
if (profile) return mergeProfile(base, profile, userId);
|
||||
}
|
||||
|
||||
const state = await a2aUserState(userId);
|
||||
return mergeProfile(base, state, userId);
|
||||
}
|
||||
|
||||
export async function getRequestUserPreferences(req: Request, userId: string): Promise<Record<string, unknown> | undefined> {
|
||||
return (await getRequestUserProfile(req, userId)).preferences;
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
import { config } from "../config.js";
|
||||
|
||||
/**
|
||||
* Build the absolute user-service URL for a given sub-path.
|
||||
* Pure — no IO. Exported so callers (and tests) can resolve targets without
|
||||
* duplicating the base-URL/trim logic.
|
||||
*/
|
||||
export function userServiceTarget(path: string, search = ""): URL {
|
||||
return new URL(`/api/v1/users${path}${search}`, config.userServiceUrl.replace(/\/$/, ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the user-service profile via an explicit GET /me.
|
||||
*
|
||||
* This is a READ and must never replay the inbound request method/body. The
|
||||
* PATCH /onboarding handler parses its own JSON payload with `c.req.json()`,
|
||||
* which consumes `c.req.raw` (marks bodyUsed). Reusing that Request here would
|
||||
* (a) wrongly issue a PATCH /me write for a read, and (b) call
|
||||
* `arrayBuffer()` on an already-consumed body —
|
||||
* `TypeError: Body is unusable: Body has already been read` — which bubbles
|
||||
* out of the handler as an uncaught 500 `{"error":"internal"}`.
|
||||
*
|
||||
* Building a fresh GET sidesteps both: no body replay, and the inbound
|
||||
* Request's body is never touched.
|
||||
*/
|
||||
export async function fetchUserProfile(req: Request): Promise<Record<string, unknown>> {
|
||||
const target = userServiceTarget("/me", new URL(req.url).search);
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
headers.delete("content-type");
|
||||
headers.delete("content-length");
|
||||
headers.delete("transfer-encoding");
|
||||
const res = await fetch(target, { method: "GET", headers });
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`user-service /me fetch failed: ${res.status} ${text}`);
|
||||
}
|
||||
return res.json() as Promise<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a synthetic PATCH /me request carrying only `{ preferences }` so the
|
||||
* backend can persist the merged onboarding blob without disturbing other
|
||||
* profile fields. Strips host/cookie like the other proxy helpers.
|
||||
*
|
||||
* The body differs from the inbound request, so hop-by-hop and length headers
|
||||
* copied from the inbound request MUST be dropped: a stale `content-length`
|
||||
* (or `transfer-encoding`) describes the ORIGINAL body, not the synthetic one,
|
||||
* and undici closes the socket mid-write ("other side closed") when the
|
||||
* declared length disagrees with the bytes sent. The Request constructor does
|
||||
* NOT recompute content-length when an explicit body is provided alongside
|
||||
* forwarded headers, so these deletes are load-bearing.
|
||||
*/
|
||||
export function patchPreferencesRequest(req: Request, preferences: Record<string, unknown>): Request {
|
||||
const target = userServiceTarget("/me");
|
||||
const headers = new Headers(req.headers);
|
||||
headers.delete("host");
|
||||
headers.delete("cookie");
|
||||
headers.delete("content-length");
|
||||
headers.delete("transfer-encoding");
|
||||
headers.set("content-type", "application/json");
|
||||
const body = JSON.stringify({ preferences });
|
||||
return new Request(target, { method: "PATCH", headers, body });
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
# V1 Analytics
|
||||
|
||||
V1 Analytics reuses the existing Analytics Actor for platform, Q-score, and activity reads.
|
||||
|
||||
The added responsibility here is the nightly improvement loop:
|
||||
|
||||
1. Read Grow events, service events, Q-score signals, and conversation summaries.
|
||||
2. Generate validated improvement signal objects with the Vercel AI SDK.
|
||||
3. Apply those signals to the V1 Curator as events.
|
||||
4. The Curator uses them on the next day when shaping tasks and nudges.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user