Просмотр исходного кода

WIP: refactor request type schemas

Yuki Takei 9 месяцев назад
Родитель
Сommit
1ccbd76a59

+ 17 - 0
apps/app/src/features/openai/interfaces/editor-assistant/sse-schemas.ts

@@ -6,6 +6,23 @@ import { LlmEditorAssistantDiffSchema } from './llm-response-schemas';
 // SSE Schemas for Streaming Editor Assistant
 // SSE Schemas for Streaming Editor Assistant
 // -----------------------------------------------------------------------------
 // -----------------------------------------------------------------------------
 
 
+// Request schemas
+export const EditRequestBodySchema = z.object({
+  userMessage: z.string(),
+  pageBody: z.string(),
+  isPageBodyPartial: z.boolean().optional()
+    .describe('Whether the page body is a partial content'),
+  partialPageBodyStartIndex: z.number().optional()
+    .describe('0-based index for the start of the partial page body'),
+  selectedText: z.string().optional(),
+  selectedPosition: z.number().optional(),
+  threadId: z.string().optional(),
+});
+
+// Type definitions
+export type EditRequestBody = z.infer<typeof EditRequestBodySchema>;
+
+
 export const SseMessageSchema = z.object({
 export const SseMessageSchema = z.object({
   appendedMessage: z.string()
   appendedMessage: z.string()
     .describe('The message that should be appended to the chat window'),
     .describe('The message that should be appended to the chat window'),

+ 5 - 13
apps/app/src/features/openai/server/routes/edit/index.ts

@@ -16,7 +16,9 @@ import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-respo
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { LlmEditorAssistantDiffSchema, LlmEditorAssistantMessageSchema } from '../../../interfaces/editor-assistant/llm-response-schemas';
 import { LlmEditorAssistantDiffSchema, LlmEditorAssistantMessageSchema } from '../../../interfaces/editor-assistant/llm-response-schemas';
-import type { SseDetectedDiff, SseFinalized, SseMessage } from '../../../interfaces/editor-assistant/sse-schemas';
+import type {
+  SseDetectedDiff, SseFinalized, SseMessage, EditRequestBody,
+} from '../../../interfaces/editor-assistant/sse-schemas';
 import { MessageErrorCode } from '../../../interfaces/message-error';
 import { MessageErrorCode } from '../../../interfaces/message-error';
 import ThreadRelationModel from '../../models/thread-relation';
 import ThreadRelationModel from '../../models/thread-relation';
 import { getOrCreateEditorAssistant } from '../../services/assistant';
 import { getOrCreateEditorAssistant } from '../../services/assistant';
@@ -40,17 +42,7 @@ const LlmEditorAssistantResponseSchema = z.object({
 }).describe('The response format for the editor assistant');
 }).describe('The response format for the editor assistant');
 
 
 
 
-type ReqBody = {
-  userMessage: string,
-  pageBody: string,
-  isPageBodyPartial?: boolean, // Whether the page body is a partial content
-  partialPageBodyStartIndex?: number, // 0-based index for the start of the partial page body
-  selectedText?: string,
-  selectedPosition?: number,
-  threadId?: string,
-}
-
-type Req = Request<undefined, Response, ReqBody> & {
+type Req = Request<undefined, Response, EditRequestBody> & {
   user: IUserHasId,
   user: IUserHasId,
 }
 }
 
 
@@ -117,7 +109,7 @@ ${withMarkdown ? withMarkdownCaution : ''}`;
 }
 }
 /* eslint-disable max-len */
 /* eslint-disable max-len */
 
 
-function instructionForContexts(args: Pick<ReqBody, 'pageBody' | 'isPageBodyPartial' | 'partialPageBodyStartIndex' | 'selectedText' | 'selectedPosition'>): string {
+function instructionForContexts(args: Pick<EditRequestBody, 'pageBody' | 'isPageBodyPartial' | 'partialPageBodyStartIndex' | 'selectedText' | 'selectedPosition'>): string {
   return `# Contexts:
   return `# Contexts:
 ## ${args.isPageBodyPartial ? 'pageBodyPartial' : 'pageBody'}:
 ## ${args.isPageBodyPartial ? 'pageBodyPartial' : 'pageBody'}: