mirror of https://github.com/buster-so/buster.git
added in the server shared types for post-processing and moved the type into shared from trigger.
This commit is contained in:
parent
7fa5e496ff
commit
f21690acc0
|
@ -19,6 +19,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@buster/access-controls": "workspace:*",
|
||||
"@buster/server-shared": "workspace:*",
|
||||
"@buster/ai": "workspace:*",
|
||||
"@buster/database": "workspace:*",
|
||||
"@buster/test-utils": "workspace:*",
|
||||
|
|
|
@ -302,7 +302,7 @@ describe('data-transformers', () => {
|
|||
it('should handle null userName', () => {
|
||||
const messageContextWithNullUser = {
|
||||
...baseMessageContext,
|
||||
userName: null,
|
||||
userName: 'Unknown User',
|
||||
};
|
||||
|
||||
const result = buildWorkflowInput(
|
||||
|
|
|
@ -53,7 +53,7 @@ export async function fetchMessageWithContext(messageId: string): Promise<Messag
|
|||
chatId: messageData.chatId,
|
||||
createdBy: messageData.createdBy,
|
||||
createdAt: new Date(messageData.createdAt),
|
||||
userName: messageData.userName,
|
||||
userName: messageData.userName ?? 'Unknown',
|
||||
organizationId: messageData.organizationId,
|
||||
};
|
||||
} catch (error) {
|
||||
|
|
|
@ -2,6 +2,13 @@ import postProcessingWorkflow, {
|
|||
type PostProcessingWorkflowOutput,
|
||||
} from '@buster/ai/workflows/post-processing-workflow';
|
||||
import { eq, getDb, messages } from '@buster/database';
|
||||
import type {
|
||||
Assumption,
|
||||
AssumptionClassification,
|
||||
AssumptionLabel,
|
||||
ConfidenceScore,
|
||||
PostProcessingMessage,
|
||||
} from '@buster/server-shared/message';
|
||||
import { logger, schemaTask } from '@trigger.dev/sdk/v3';
|
||||
import { initLogger, wrapTraced } from 'braintrust';
|
||||
import { z } from 'zod/v4';
|
||||
|
@ -16,57 +23,13 @@ import {
|
|||
import { DataFetchError, MessageNotFoundError, TaskInputSchema } from './types';
|
||||
import type { TaskInput, TaskOutput } from './types';
|
||||
|
||||
// Schema for the subset of fields we want to save to the database
|
||||
const PostProcessingDbDataSchema = z.object({
|
||||
confidence_score: z.enum(['low', 'high']),
|
||||
summary_message: z.string(),
|
||||
summary_title: z.string(),
|
||||
assumptions: z
|
||||
.array(
|
||||
z.object({
|
||||
descriptive_title: z.string(),
|
||||
classification: z.enum([
|
||||
'fieldMapping',
|
||||
'tableRelationship',
|
||||
'dataQuality',
|
||||
'dataFormat',
|
||||
'dataAvailability',
|
||||
'timePeriodInterpretation',
|
||||
'timePeriodGranularity',
|
||||
'metricInterpretation',
|
||||
'segmentInterpretation',
|
||||
'quantityInterpretation',
|
||||
'requestScope',
|
||||
'metricDefinition',
|
||||
'segmentDefinition',
|
||||
'businessLogic',
|
||||
'policyInterpretation',
|
||||
'optimization',
|
||||
'aggregation',
|
||||
'filtering',
|
||||
'sorting',
|
||||
'grouping',
|
||||
'calculationMethod',
|
||||
'dataRelevance',
|
||||
]),
|
||||
explanation: z.string(),
|
||||
label: z.enum(['timeRelated', 'vagueRequest', 'major', 'minor']),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
tool_called: z.string(),
|
||||
user_name: z.string().nullable().optional(),
|
||||
});
|
||||
|
||||
type PostProcessingDbData = z.infer<typeof PostProcessingDbDataSchema>;
|
||||
|
||||
/**
|
||||
* Extract only the specific fields we want to save to the database
|
||||
*/
|
||||
function extractDbFields(
|
||||
workflowOutput: PostProcessingWorkflowOutput,
|
||||
userName: string | null
|
||||
): PostProcessingDbData {
|
||||
userName: string
|
||||
): PostProcessingMessage {
|
||||
logger.log('Extracting database fields from workflow output', {
|
||||
workflowOutput,
|
||||
});
|
||||
|
@ -79,7 +42,7 @@ function extractDbFields(
|
|||
// - Low if toolCalled is 'flagChat'
|
||||
// - Low if there are any major assumptions
|
||||
// - High otherwise
|
||||
let confidence_score: 'low' | 'high' = 'high';
|
||||
let confidence_score: ConfidenceScore = 'high';
|
||||
if (workflowOutput.toolCalled === 'flagChat' || hasMajorAssumptions) {
|
||||
confidence_score = 'low';
|
||||
}
|
||||
|
@ -98,15 +61,15 @@ function extractDbFields(
|
|||
summaryTitle = workflowOutput.summaryTitle || 'Summary';
|
||||
}
|
||||
|
||||
const extracted: PostProcessingDbData = {
|
||||
const extracted: PostProcessingMessage = {
|
||||
summary_message: summaryMessage,
|
||||
summary_title: summaryTitle,
|
||||
confidence_score,
|
||||
assumptions: workflowOutput.assumptions?.map((assumption) => ({
|
||||
descriptive_title: assumption.descriptiveTitle,
|
||||
classification: assumption.classification,
|
||||
classification: assumption.classification as AssumptionClassification,
|
||||
explanation: assumption.explanation,
|
||||
label: assumption.label,
|
||||
label: assumption.label as AssumptionLabel,
|
||||
})),
|
||||
tool_called: workflowOutput.toolCalled || 'unknown', // Provide default if missing
|
||||
user_name: userName,
|
||||
|
|
|
@ -43,7 +43,7 @@ export const MessageContextSchema = z.object({
|
|||
chatId: z.string(),
|
||||
createdBy: z.string(),
|
||||
createdAt: z.date(),
|
||||
userName: z.string().nullable(),
|
||||
userName: z.string(),
|
||||
organizationId: z.string(),
|
||||
});
|
||||
|
||||
|
|
|
@ -45,6 +45,10 @@
|
|||
"types": "./dist/user/index.d.ts",
|
||||
"default": "./dist/user/index.js"
|
||||
},
|
||||
"./message": {
|
||||
"types": "./dist/message/index.d.ts",
|
||||
"default": "./dist/message/index.js"
|
||||
},
|
||||
"./organization": {
|
||||
"types": "./dist/organization/index.d.ts",
|
||||
"default": "./dist/organization/index.js"
|
||||
|
|
|
@ -1,11 +1,52 @@
|
|||
import { z } from 'zod/v4';
|
||||
|
||||
export const AssumptionClassificationSchema = z.enum([
|
||||
'fieldMapping',
|
||||
'tableRelationship',
|
||||
'dataQuality',
|
||||
'dataFormat',
|
||||
'dataAvailability',
|
||||
'timePeriodInterpretation',
|
||||
'timePeriodGranularity',
|
||||
'metricInterpretation',
|
||||
'segmentInterpretation',
|
||||
'quantityInterpretation',
|
||||
'requestScope',
|
||||
'metricDefinition',
|
||||
'segmentDefinition',
|
||||
'businessLogic',
|
||||
'policyInterpretation',
|
||||
'optimization',
|
||||
'aggregation',
|
||||
'filtering',
|
||||
'sorting',
|
||||
'grouping',
|
||||
'calculationMethod',
|
||||
'dataRelevance',
|
||||
]);
|
||||
|
||||
export const AssumptionLabelSchema = z.enum(['timeRelated', 'vagueRequest', 'major', 'minor']);
|
||||
|
||||
export const ConfidenceScoreSchema = z.enum(['low', 'high']);
|
||||
|
||||
export const AssumptionSchema = z.object({
|
||||
label: z.enum(['minor', 'vagueRequest']),
|
||||
descriptive_title: z.string(),
|
||||
classification: AssumptionClassificationSchema,
|
||||
explanation: z.string(),
|
||||
label: AssumptionLabelSchema,
|
||||
});
|
||||
|
||||
export const PostProcessingMessageSchema = z.object({
|
||||
assumptions: z.array(AssumptionSchema),
|
||||
confidence_score: ConfidenceScoreSchema,
|
||||
summary_message: z.string(),
|
||||
summary_title: z.string(),
|
||||
assumptions: z.array(AssumptionSchema).optional(),
|
||||
tool_called: z.string(),
|
||||
user_name: z.string(),
|
||||
});
|
||||
|
||||
export type AssumptionClassification = z.infer<typeof AssumptionClassificationSchema>;
|
||||
export type AssumptionLabel = z.infer<typeof AssumptionLabelSchema>;
|
||||
export type ConfidenceScore = z.infer<typeof ConfidenceScoreSchema>;
|
||||
export type Assumption = z.infer<typeof AssumptionSchema>;
|
||||
export type PostProcessingMessage = z.infer<typeof PostProcessingMessageSchema>;
|
||||
|
|
|
@ -160,6 +160,9 @@ importers:
|
|||
'@buster/database':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/database
|
||||
'@buster/server-shared':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/server-shared
|
||||
'@buster/test-utils':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/test-utils
|
||||
|
|
Loading…
Reference in New Issue