Files
platform/src/servers/channels/send-and-await.ts
T
brunorezioandClaude Opus 5 044aacf4d5 remove the dead multi-user surface
Officer is single-user: the server owner is the only account, created once by
/auth/bootstrap. Everything that existed to serve additional users was
unreachable, so it is gone rather than left looking like it does something.

Accounts: drop the invite / resend-invite / delete / list-users routes and the
Users settings screen, the inert /auth/signup handler, and the account
verification chain it fed (verify, resend-verification, VerifyScreen, the
UserInvite + VerifyAdmin + VerifyRegistration templates). /auth/verify-token
survives for password resets only, and now requires a reset-password token
rather than accepting any signed JWT.

Roles: drop the users.role column and the four-value USER_ROLES enum. The
permissions table granted every role identical methods, and every
role === 'Super Admin' check was permanently true. The JWT no longer carries a
role claim.

Sandbox: remove sidecar/sandbox.ts and its five call sites. bwrap was selected
only for non-Super-Admin users, so it never ran. It was also not a usable agent
jail as written — --share-net, the project root (with .env) bound read-only,
and runuser dropping to the server's own uid. Rebuilding it for agent
containment would be a different construction, and git history keeps this one.

getHomeDir keeps its DATA_PATH meaning; the new getOwnerHomeDir resolves the
owner's real login home, which is what terminals, chats and task runs use.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-25 23:30:20 +01:00

91 lines
3.0 KiB
TypeScript

import type { MessageCost } from '@@/api/chat/types';
import { getUserSettings } from 'officerdb';
import { logger } from '@@/api/chat/logger';
import { sendClaudeCode, clearClaudeCodeSession } from './send-claude-code';
const DEFAULT_MODEL = 'claude-code';
type SendAndAwaitParams = {
userId: number;
email: string;
username: string;
prompt: string;
context: string;
contextId: string;
model?: string;
};
type SendAndAwaitResult = {
text: string;
sessionId: string;
model: string;
cost: MessageCost;
};
// Per-session mutex to serialize concurrent prompts
const sessionLocks = new Map<string, Promise<void>>();
// Channel model overrides — survive session eviction/recreation
const channelModelOverrides = new Map<string, string>();
function buildSessionId(context: string, userId: number, contextId: string): string {
return `channel-${context}-${userId}-${contextId}`;
}
async function getUserDefaultModel(userId: number): Promise<string | null> {
try {
const settings = await getUserSettings(userId);
const chat = settings?.chat as Record<string, unknown> | undefined;
return (chat?.defaultModel as string) || null;
} catch {
return null;
}
}
export function getSessionModel(context: string, userId: number, contextId: string): string | null {
return channelModelOverrides.get(buildSessionId(context, userId, contextId)) ?? null;
}
export function setSessionModel(context: string, userId: number, contextId: string, model: string): void {
const sessionId = buildSessionId(context, userId, contextId);
channelModelOverrides.set(sessionId, model);
// Reset the Claude session so the next prompt starts fresh under the new model.
clearClaudeCodeSession(sessionId);
logger.info('Channel model override stored', { sessionId, model });
}
export async function sendAndAwait(params: SendAndAwaitParams): Promise<SendAndAwaitResult> {
const { userId, context, contextId } = params;
const sessionId = buildSessionId(context, userId, contextId);
// Serialize per session — if two messages arrive at once, the second waits for the first.
const existing = sessionLocks.get(sessionId) ?? Promise.resolve();
let releaseLock: () => void;
const lockPromise = new Promise<void>((resolve) => {
releaseLock = resolve;
});
const chained = existing.then(() => lockPromise);
sessionLocks.set(sessionId, chained);
await existing;
try {
const override = channelModelOverrides.get(sessionId);
let model = params.model ?? override ?? (await getUserDefaultModel(userId)) ?? DEFAULT_MODEL;
// Claude-only: coerce any legacy non-Claude model preference to the Claude default.
if (!model.startsWith('claude-code')) model = DEFAULT_MODEL;
return await sendClaudeCode({
userId: params.userId,
email: params.email,
username: params.username,
prompt: params.prompt,
sessionKey: sessionId,
model,
});
} finally {
releaseLock!();
if (sessionLocks.get(sessionId) === chained) sessionLocks.delete(sessionId);
}
}