-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathanswer-question.ts
More file actions
184 lines (164 loc) · 5.69 KB
/
answer-question.ts
File metadata and controls
184 lines (164 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { syncOrganizationEmbeddings } from '@/vector-store/lib';
import { logger, metadata, tags, task } from '@trigger.dev/sdk';
import { generateAnswerWithRAG } from './answer-question-helpers';
export interface AnswerQuestionPayload {
question: string;
organizationId: string;
questionIndex: number;
totalQuestions: number;
}
export interface AnswerQuestionResult {
success: boolean;
questionIndex: number;
question: string;
answer: string | null;
sources: Array<{
sourceType: string;
sourceName?: string;
score: number;
}>;
error?: string;
}
export interface AnswerQuestionOptions {
/**
* Whether to push updates to Trigger.dev metadata.
* Disable when running outside of a Trigger task (e.g. server actions).
*/
useMetadata?: boolean;
/**
* Whether to skip syncing organization embeddings.
* Set to true when sync has already been performed (e.g., in batch operations).
*/
skipSync?: boolean;
}
/**
* Core function to answer a question - can be called directly or wrapped in a task
*/
export async function answerQuestion(
payload: AnswerQuestionPayload,
options: AnswerQuestionOptions = {},
): Promise<AnswerQuestionResult> {
const { useMetadata = true, skipSync = false } = options;
const withMetadata = (fn: () => void) => {
if (!useMetadata) {
return;
}
try {
fn();
} catch (error) {
logger.warn('Metadata operation failed – continuing without metadata', {
questionIndex: payload.questionIndex,
error: error instanceof Error ? error.message : 'Unknown error',
});
}
};
logger.info('🚀 Starting to process question', {
questionIndex: payload.questionIndex,
totalQuestions: payload.totalQuestions,
question: payload.question.substring(0, 100),
organizationId: payload.organizationId,
});
// Update metadata to mark this question as processing
// This allows frontend to show spinner for this specific question when it starts
withMetadata(() => {
metadata.set(`question_${payload.questionIndex}_status`, 'processing');
});
const buildMetadataAnswerPayload = (answerValue: string | null) => ({
questionIndex: payload.questionIndex,
question: payload.question,
answer: answerValue,
// Sources are NOT included in metadata to avoid blocking incremental updates
// Sources will be available in the final output and updated separately
sources: [],
});
try {
// Sync organization embeddings before generating answer
// Uses incremental sync: only updates what changed (much faster than full sync)
// Lock mechanism prevents concurrent syncs for the same organization
// Skip sync if already performed (e.g., in batch operations)
if (!skipSync) {
try {
await syncOrganizationEmbeddings(payload.organizationId);
logger.info('Organization embeddings synced successfully', {
organizationId: payload.organizationId,
});
} catch (error) {
logger.warn('Failed to sync organization embeddings', {
organizationId: payload.organizationId,
error: error instanceof Error ? error.message : 'Unknown error',
});
// Continue with existing embeddings if sync fails
}
} else {
logger.info('Skipping sync (already performed)', {
organizationId: payload.organizationId,
});
}
logger.info('🔍 Calling generateAnswerWithRAG', {
questionIndex: payload.questionIndex,
});
const result = await generateAnswerWithRAG(
payload.question,
payload.organizationId,
);
// Update metadata with this answer immediately
// This allows frontend to show answers as they complete individually
// Sources are NOT included in metadata to avoid blocking incremental updates
// Sources will be available in the final output
const metadataAnswer = buildMetadataAnswerPayload(result.answer);
withMetadata(() => {
metadata.set(`answer_${payload.questionIndex}`, metadataAnswer);
metadata.set(`question_${payload.questionIndex}_status`, 'completed');
metadata.increment('questionsCompleted', 1);
metadata.increment('questionsRemaining', -1);
});
return {
success: true,
questionIndex: payload.questionIndex,
question: payload.question,
answer: result.answer,
sources: result.sources,
};
} catch (error) {
logger.error('❌ Failed to answer question', {
questionIndex: payload.questionIndex,
error: error instanceof Error ? error.message : 'Unknown error',
errorStack: error instanceof Error ? error.stack : undefined,
});
const failedAnswerData = buildMetadataAnswerPayload(null);
// Update metadata even on failure
withMetadata(() => {
metadata.set(`answer_${payload.questionIndex}`, failedAnswerData);
metadata.set(`question_${payload.questionIndex}_status`, 'completed');
metadata.increment('questionsCompleted', 1);
metadata.increment('questionsRemaining', -1);
});
return {
success: false,
questionIndex: payload.questionIndex,
question: payload.question,
answer: null,
sources: [],
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}
/**
* Trigger.dev task wrapper for frontend use (single question answers)
* This wraps the answerQuestion function so it can be triggered from the frontend
*/
export const answerQuestionTask = task({
id: 'answer-question',
retry: {
maxAttempts: 3,
},
run: async (payload: {
question: string;
organizationId: string;
questionIndex: number;
totalQuestions: number;
}) => {
await tags.add([`org:${payload.organizationId}`]);
return await answerQuestion(payload);
},
});