feat(*): Add websockets via partykit for live notifications of tool usage

This commit is contained in:
Josh Creek
2025-04-21 15:20:32 +01:00
parent 839e1183a6
commit 76fcdac563
19 changed files with 2205 additions and 8 deletions
+20 -5
View File
@@ -1,9 +1,6 @@
import { client, agent } from '../agent/services/agentService';
import type { MessageTextContentOutput, ToolOutput } from '@azure/ai-projects';
const OPENAI_API_KEY = process.env['AZURE_OPENAI_API_KEY'];
const OPENAI_ENDPOINT = process.env['AZURE_OPENAI_ENDPOINT'];
const OPENAI_DEPLOYMENT = process.env['AZURE_OPENAI_DEPLOYMENT'] || 'gpt-4';
import { PARTYKIT_BASE_URL } from '$env/static/private';
// Prompt template for grading and feedback
export function buildGradingPrompt(submission: string, task: string) {
@@ -66,7 +63,7 @@ function fallbackGrade(submission: string, task: string): OpenAIResponse {
};
}
export async function gradeSubmissionWithAgent(submission: string, task: string): Promise<OpenAIResponse> {
export async function gradeSubmissionWithAgent(submission: string, task: string, roomId?: string): Promise<OpenAIResponse> {
try {
if (!client || !agent) throw new Error('Agent not available');
@@ -111,6 +108,24 @@ export async function gradeSubmissionWithAgent(submission: string, task: string)
let output = '';
const args = JSON.parse(call.function.arguments);
if (roomId) {
try {
const ws = new (typeof WebSocket !== 'undefined' ? WebSocket : (await import('ws')).default)(`${PARTYKIT_BASE_URL}/party/tool-usage-server-${roomId}`);
// Wait for connection to open
await new Promise<void>((resolve, reject) => {
ws.onopen = () => resolve();
ws.onerror = (err) => reject(err);
});
ws.send(JSON.stringify({ tool: call.function.name, time: new Date().toISOString() }));
ws.close();
} catch (err) {
console.error('Failed to send tool usage event to PartyKit:', err);
}
}
if (
call.function.name === 'matchRubric' ||
call.function.name === 'analyzeEssay'