learning_ai_common_plat/services/cowork-service/src/lib/request-context.ts
saravanakumardb1 19674c7ef7 feat(cowork-service): ecosystem alignment + IPC bridge to Rust runtime
ECOSYSTEM GAPS CLOSED — cowork-service now matches the pattern used by
all other product backends (FlowMonk, ActionTrail, NoteLett, etc.):

New lib files (6):
- lib/product-config.ts — canonical product identity (PRODUCT_ID, productConfig)
- lib/auth.ts — @bytelyst/fastify-auth createAuthMiddleware
- lib/request-context.ts — getUserId(), getRequestProductId()
- lib/telemetry.ts — @bytelyst/backend-telemetry buffer
- lib/feature-flags.ts — @bytelyst/backend-flags with 12 cowork flags
- lib/ipc-bridge.ts — IpcBridge class: spawn Rust child, JSON-RPC, 13 methods

Updated files:
- lib/config.ts — extends @bytelyst/backend-config baseBackendConfigSchema
- server.ts — JWT context, bootstrap endpoint, IPC startup, graceful shutdown
- modules/tasks/routes.ts — IPC bridge forwarding with in-memory fallback
- modules/health/routes.ts — productId from product-config, IPC status
- package.json — 7 new @bytelyst/* workspace deps

IPC bridge features:
- Spawns cowork-orchestrator --ipc-bridge as child process
- JSON-RPC 2.0 over stdin/stdout (line-delimited)
- 13 convenience methods matching Rust IpcHandler
- Timeout + pending request tracking
- Graceful shutdown with SIGTERM
- Singleton pattern with setIpcBridge() for testing

24 tests passing (was 8), typecheck clean.
2026-04-02 22:14:24 -07:00

24 lines
844 B
TypeScript

/**
* Request-level product context helpers — delegates to @bytelyst/fastify-auth.
*/
import { createRequestContext } from '@bytelyst/fastify-auth';
import type { FastifyRequest } from 'fastify';
import { BadRequestError } from '@bytelyst/errors';
import { PRODUCT_ID } from './product-config.js';
export type { JwtPayload } from '@bytelyst/fastify-auth';
const _ctx = createRequestContext({ productId: PRODUCT_ID });
export const getRequestProductId = _ctx.getRequestProductId;
/**
* Get authenticated user ID from JWT payload.
* Falls back to 'demo-user' in development when no JWT is present.
*/
export function getUserId(req: FastifyRequest): string {
if (req.jwtPayload?.sub) return req.jwtPayload.sub;
if (process.env.NODE_ENV !== 'production') return 'demo-user';
throw new BadRequestError('Authentication required');
}