@studious-lms/server 1.1.12 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routers/_app.d.ts +34 -2
- package/dist/routers/_app.d.ts.map +1 -1
- package/dist/routers/conversation.d.ts +4 -0
- package/dist/routers/conversation.d.ts.map +1 -1
- package/dist/routers/conversation.js +7 -0
- package/dist/routers/folder.d.ts +13 -0
- package/dist/routers/folder.d.ts.map +1 -1
- package/dist/routers/folder.js +20 -1
- package/dist/routers/labChat.d.ts +0 -1
- package/dist/routers/labChat.d.ts.map +1 -1
- package/dist/routers/labChat.js +251 -10
- package/dist/utils/aiUser.d.ts +13 -0
- package/dist/utils/aiUser.d.ts.map +1 -0
- package/dist/utils/aiUser.js +57 -0
- package/dist/utils/inference.d.ts +56 -0
- package/dist/utils/inference.d.ts.map +1 -0
- package/dist/utils/inference.js +135 -0
- package/package.json +2 -1
- package/src/routers/conversation.ts +7 -0
- package/src/routers/folder.ts +23 -0
- package/src/routers/labChat.ts +293 -10
- package/src/utils/aiUser.ts +65 -0
- package/src/utils/inference.ts +193 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import { logger } from './logger.js';
|
|
3
|
+
import { prisma } from '../lib/prisma.js';
|
|
4
|
+
import { pusher } from '../lib/pusher.js';
|
|
5
|
+
import { ensureAIUserExists, getAIUserId } from './aiUser.js';
|
|
6
|
+
|
|
7
|
+
// Initialize inference client (Cohere via OpenAI SDK)
|
|
8
|
+
|
|
9
|
+
logger.info('Inference API Key', { apiKey: process.env.INFERENCE_API_KEY });
|
|
10
|
+
logger.info('Inference API Base URL', { baseURL: process.env.INFERENCE_API_BASE_URL });
|
|
11
|
+
|
|
12
|
+
export const inferenceClient = new OpenAI({
|
|
13
|
+
apiKey: process.env.INFERENCE_API_KEY,
|
|
14
|
+
baseURL: process.env.INFERENCE_API_BASE_URL,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// Types for lab chat context
|
|
18
|
+
export interface LabChatContext {
|
|
19
|
+
subject: string;
|
|
20
|
+
topic: string;
|
|
21
|
+
difficulty: 'beginner' | 'intermediate' | 'advanced';
|
|
22
|
+
objectives: string[];
|
|
23
|
+
resources?: string[];
|
|
24
|
+
persona: string;
|
|
25
|
+
constraints: string[];
|
|
26
|
+
examples?: any[];
|
|
27
|
+
metadata?: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface InferenceResponse {
|
|
31
|
+
content: string;
|
|
32
|
+
model: string;
|
|
33
|
+
tokensUsed: number;
|
|
34
|
+
finishReason: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Centralized function to send AI messages to conversations
|
|
39
|
+
* Handles database storage and Pusher broadcasting
|
|
40
|
+
*/
|
|
41
|
+
export async function sendAIMessage(
|
|
42
|
+
content: string,
|
|
43
|
+
conversationId: string,
|
|
44
|
+
options: {
|
|
45
|
+
subject?: string;
|
|
46
|
+
customSender?: {
|
|
47
|
+
displayName: string;
|
|
48
|
+
profilePicture?: string | null;
|
|
49
|
+
};
|
|
50
|
+
} = {}
|
|
51
|
+
): Promise<{
|
|
52
|
+
id: string;
|
|
53
|
+
content: string;
|
|
54
|
+
senderId: string;
|
|
55
|
+
conversationId: string;
|
|
56
|
+
createdAt: Date;
|
|
57
|
+
}> {
|
|
58
|
+
// Ensure AI user exists
|
|
59
|
+
await ensureAIUserExists();
|
|
60
|
+
|
|
61
|
+
// Create message in database
|
|
62
|
+
const aiMessage = await prisma.message.create({
|
|
63
|
+
data: {
|
|
64
|
+
content,
|
|
65
|
+
senderId: getAIUserId(),
|
|
66
|
+
conversationId,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
logger.info('AI Message sent', {
|
|
71
|
+
messageId: aiMessage.id,
|
|
72
|
+
conversationId,
|
|
73
|
+
contentLength: content.length,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Prepare sender info
|
|
77
|
+
const senderInfo = {
|
|
78
|
+
id: getAIUserId(),
|
|
79
|
+
username: 'AI Assistant',
|
|
80
|
+
profile: {
|
|
81
|
+
displayName: options.customSender?.displayName || `${options.subject || 'AI'} Assistant`,
|
|
82
|
+
profilePicture: options.customSender?.profilePicture || null,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Broadcast via Pusher
|
|
87
|
+
try {
|
|
88
|
+
await pusher.trigger(`conversation-${conversationId}`, 'new-message', {
|
|
89
|
+
id: aiMessage.id,
|
|
90
|
+
content: aiMessage.content,
|
|
91
|
+
senderId: getAIUserId(),
|
|
92
|
+
conversationId: aiMessage.conversationId,
|
|
93
|
+
createdAt: aiMessage.createdAt,
|
|
94
|
+
sender: senderInfo,
|
|
95
|
+
mentionedUserIds: [],
|
|
96
|
+
});
|
|
97
|
+
} catch (error) {
|
|
98
|
+
logger.error('Failed to broadcast AI message:', { error, messageId: aiMessage.id });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
id: aiMessage.id,
|
|
103
|
+
content: aiMessage.content,
|
|
104
|
+
senderId: getAIUserId(),
|
|
105
|
+
conversationId: aiMessage.conversationId,
|
|
106
|
+
createdAt: aiMessage.createdAt,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Simple inference function for general use
|
|
112
|
+
*/
|
|
113
|
+
export async function generateInferenceResponse(
|
|
114
|
+
subject: string,
|
|
115
|
+
question: string,
|
|
116
|
+
options: {
|
|
117
|
+
model?: string;
|
|
118
|
+
maxTokens?: number;
|
|
119
|
+
} = {}
|
|
120
|
+
): Promise<InferenceResponse> {
|
|
121
|
+
const { model = 'command-r-plus', maxTokens = 500 } = options;
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
const completion = await inferenceClient.chat.completions.create({
|
|
125
|
+
model,
|
|
126
|
+
messages: [
|
|
127
|
+
{
|
|
128
|
+
role: 'system',
|
|
129
|
+
content: `You are a helpful educational assistant for ${subject}. Provide clear, concise, and accurate answers. Keep responses educational and appropriate for students.`,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
role: 'user',
|
|
133
|
+
content: question,
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
max_tokens: maxTokens,
|
|
137
|
+
temperature: 0.5,
|
|
138
|
+
// Remove OpenAI-specific parameters for Cohere compatibility
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const response = completion.choices[0]?.message?.content;
|
|
142
|
+
|
|
143
|
+
if (!response) {
|
|
144
|
+
throw new Error('No response generated from inference API');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
content: response,
|
|
149
|
+
model,
|
|
150
|
+
tokensUsed: completion.usage?.total_tokens || 0,
|
|
151
|
+
finishReason: completion.choices[0]?.finish_reason || 'unknown',
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
} catch (error) {
|
|
155
|
+
logger.error('Failed to generate inference response', { error, subject, question: question.substring(0, 50) + '...' });
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Validate inference configuration
|
|
162
|
+
*/
|
|
163
|
+
export function validateInferenceConfig(): boolean {
|
|
164
|
+
if (!process.env.INFERENCE_API_KEY) {
|
|
165
|
+
logger.error('Inference API key not configured for Cohere');
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Get available inference models (for admin/config purposes)
|
|
173
|
+
*/
|
|
174
|
+
export async function getAvailableModels(): Promise<string[]> {
|
|
175
|
+
try {
|
|
176
|
+
const models = await inferenceClient.models.list();
|
|
177
|
+
return models.data
|
|
178
|
+
.filter(model => model.id.includes('command'))
|
|
179
|
+
.map(model => model.id)
|
|
180
|
+
.sort();
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logger.error('Failed to fetch inference models', { error });
|
|
183
|
+
return ['command-r-plus', 'command-r', 'command-light']; // Fallback Cohere models
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Estimate token count for a message (rough approximation)
|
|
189
|
+
*/
|
|
190
|
+
export function estimateTokenCount(text: string): number {
|
|
191
|
+
// Rough approximation: 1 token ≈ 4 characters for English text
|
|
192
|
+
return Math.ceil(text.length / 4);
|
|
193
|
+
}
|