AIX: dispatch: extract debugger / move security

This commit is contained in:
Enrico Ros
2025-11-22 23:12:15 -08:00
parent 2bf1eaaa0f
commit a4de84a842
6 changed files with 44 additions and 40 deletions
+2 -35
View File
@@ -1,45 +1,12 @@
import * as z from 'zod/v4';
import { createServerDebugWireEvents, serverCapitalizeFirstLetter } from '~/server/wire';
import { createTRPCRouter, edgeProcedure } from '~/server/trpc/trpc.server';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from './aix.consts';
import { AixAPI_Access, AixAPI_Context_ChatGenerate, AixWire_API, AixWire_API_ChatContentGenerate } from './aix.wiretypes';
import { PerformanceProfiler } from '../dispatch/PerformanceProfiler';
import { _createDebugConfig } from '../dispatch/chatGenerate/chatGenerate.debug';
import { createChatGenerateDispatch, createChatGenerateResumeDispatch } from '../dispatch/chatGenerate/chatGenerate.dispatch';
import { executeChatGenerateWithRetry } from '../dispatch/chatGenerate/chatGenerate.retrier';
// -- Utilities ---
export type AixDebugObject = ReturnType<typeof _createDebugConfig>;
/**
* Production-allowed contexts for AIX inspector.
* These are the only contexts that can be captured in production builds for security.
*/
const AIX_INSPECTOR_ALLOWED_CONTEXTS: (AixAPI_Context_ChatGenerate['name'] | string)[] = [
'beam-followup',
'beam-gather',
'beam-scatter',
'chat-react-turn',
'conversation',
'scratch-chat',
] as const;
function _createDebugConfig(access: AixAPI_Access, options: undefined | { debugDispatchRequest?: boolean, debugProfilePerformance?: boolean }, chatGenerateContextName: string) {
const echoRequest = !!options?.debugDispatchRequest && (AIX_SECURITY_ONLY_IN_DEV_BUILDS || AIX_INSPECTOR_ALLOWED_CONTEXTS.includes(chatGenerateContextName));
const consoleLogErrors =
(access.dialect === 'openai' && access.oaiHost) ? false as const // do not server-log OpenAI Custom hosts (often self-hosted and buggy) from server-side console error logging
: 'srv-warn' as const; // keeping the highest level of server-side logging for 'fetching' issues (usually however we see the messages of the TRPC retrier `createRetryablePromise` already)
return {
prettyDialect: serverCapitalizeFirstLetter(access.dialect), // string
echoRequest: echoRequest, // boolean
profiler: AIX_SECURITY_ONLY_IN_DEV_BUILDS && echoRequest && !!options?.debugProfilePerformance ? new PerformanceProfiler() : undefined, // PerformanceProfiler | undefined
wire: createServerDebugWireEvents() ?? undefined, // ServerDebugWireEvents | undefined
consoleLogErrors,
};
}
import { AixWire_API, AixWire_API_ChatContentGenerate } from './aix.wiretypes';
// --- AIX tRPC Router ---
@@ -0,0 +1,36 @@
import { createServerDebugWireEvents, serverCapitalizeFirstLetter } from '~/server/wire';
import type { AixAPI_Access, AixAPI_Context_ChatGenerate } from '../../api/aix.wiretypes';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from '../../api/aix.security';
import { PerformanceProfiler } from '../PerformanceProfiler';
/**
* Production-allowed contexts for AIX inspector.
* These are the only contexts that can be captured in production builds for security.
*/
const AIX_INSPECTOR_ALLOWED_CONTEXTS: (AixAPI_Context_ChatGenerate['name'] | string)[] = [
'beam-followup',
'beam-gather',
'beam-scatter',
'chat-react-turn',
'conversation',
'scratch-chat',
] as const;
export type AixDebugObject = ReturnType<typeof _createDebugConfig>;
export function _createDebugConfig(access: AixAPI_Access, options: undefined | { debugDispatchRequest?: boolean, debugProfilePerformance?: boolean }, chatGenerateContextName: string) {
const echoRequest = !!options?.debugDispatchRequest && (AIX_SECURITY_ONLY_IN_DEV_BUILDS || AIX_INSPECTOR_ALLOWED_CONTEXTS.includes(chatGenerateContextName));
const consoleLogErrors =
(access.dialect === 'openai' && access.oaiHost) ? false as const // do not server-log OpenAI Custom hosts (often self-hosted and buggy) from server-side console error logging
: 'srv-warn' as const; // keeping the highest level of server-side logging for 'fetching' issues (usually however we see the messages of the TRPC retrier `createRetryablePromise` already)
return {
prettyDialect: serverCapitalizeFirstLetter(access.dialect), // string
echoRequest: echoRequest, // boolean
consoleLogErrors,
profiler: AIX_SECURITY_ONLY_IN_DEV_BUILDS && echoRequest && !!options?.debugProfilePerformance ? new PerformanceProfiler() : undefined, // PerformanceProfiler | undefined
wire: createServerDebugWireEvents() ?? undefined, // ServerDebugWireEvents | undefined
};
}
@@ -2,10 +2,10 @@ import { createEmptyReadableStream, safeErrorString } from '~/server/wire';
import { createRetryablePromise, RetryAttempt } from '~/server/trpc/trpc.fetchers.retrier';
import { fetchResponseOrTRPCThrow } from '~/server/trpc/trpc.router.fetchers';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from '../../api/aix.consts';
import { AixDebugObject } from '../../api/aix.router';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from '../../api/aix.security';
import { AixWire_Particles } from '../../api/aix.wiretypes';
import { AixDebugObject } from './chatGenerate.debug';
import { AixDemuxers } from '../stream.demuxers';
import { ChatGenerateDispatch, ChatGenerateDispatchRequest, ChatGenerateParseFunction } from './chatGenerate.dispatch';
import { ChatGenerateTransmitter } from './ChatGenerateTransmitter';
@@ -1,8 +1,9 @@
import type { AixDebugObject } from '../../api/aix.router';
import { abortableDelay } from '~/server/wire';
import type { AixWire_Particles } from '../../api/aix.wiretypes';
import type { AixDebugObject } from './chatGenerate.debug';
import type { ChatGenerateDispatch } from './chatGenerate.dispatch';
import { abortableDelay } from '~/server/wire';
import { executeChatGenerate } from './chatGenerate.executor';
@@ -1,5 +1,5 @@
import type { AixDemuxers } from './stream.demuxers';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from '../api/aix.consts';
import { AIX_SECURITY_ONLY_IN_DEV_BUILDS } from '../api/aix.security';
/**