diff --git a/src/modules/aix/server/api/aix.router.ts b/src/modules/aix/server/api/aix.router.ts index b02e496da..d07d1647d 100644 --- a/src/modules/aix/server/api/aix.router.ts +++ b/src/modules/aix/server/api/aix.router.ts @@ -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; - -/** - * 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 --- diff --git a/src/modules/aix/server/api/aix.consts.ts b/src/modules/aix/server/api/aix.security.ts similarity index 100% rename from src/modules/aix/server/api/aix.consts.ts rename to src/modules/aix/server/api/aix.security.ts diff --git a/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.debug.ts b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.debug.ts new file mode 100644 index 000000000..f097ef3d9 --- /dev/null +++ b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.debug.ts @@ -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; + +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 + }; +} \ No newline at end of file diff --git a/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.executor.ts b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.executor.ts index d968d3d29..e748e9e26 100644 --- a/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.executor.ts +++ b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.executor.ts @@ -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'; diff --git a/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.retrier.ts b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.retrier.ts index 7b17bb16c..2cc9d5f3e 100644 --- a/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.retrier.ts +++ b/src/modules/aix/server/dispatch/chatGenerate/chatGenerate.retrier.ts @@ -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'; diff --git a/src/modules/aix/server/dispatch/stream.demuxer.fastsse.ts b/src/modules/aix/server/dispatch/stream.demuxer.fastsse.ts index bbff35cf8..226e6a407 100644 --- a/src/modules/aix/server/dispatch/stream.demuxer.fastsse.ts +++ b/src/modules/aix/server/dispatch/stream.demuxer.fastsse.ts @@ -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'; /**