Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { IOllamaClient } from '../contracts/IOllamaClient';
import { ChatService, MessageMetadata } from './chatService';
import { FramingService } from './framingService';
import { ModelService } from './modelService';
export interface ChatOrchestrationResult {
fullResponse: string;
}
export class ChatOrchestrator {
constructor(
private chatService: ChatService,
private modelService: ModelService,
private framingService: FramingService,
private ollamaClient: IOllamaClient,
) {}
/**
* Orchestrates a single chat turn:
* 1. Prepares messages for Ollama (including framing prefixes/suffixes).
* 2. Calls the Ollama API.
* 3. Streams tokens to the provided callback.
* 4. Saves the final assistant message to history.
*/
async generateResponse(
chatId: string,
onToken: (token: string) => void,
onStart?: () => void,
): Promise<ChatOrchestrationResult> {
const chat = this.chatService.getChat(chatId);
if (!chat) {
throw new Error(`Chat ${chatId} not found`);
}
const instance = this.modelService.getSettings(chat.modelName);
const apiMessages: { role: string; content: string }[] = [];
for (const m of chat.messages) {
if (m.role === 'system') {
apiMessages.push({ role: 'system', content: m.content });
} else if (m.role === 'user') {
if (m.systemTurnPrefix) {
apiMessages.push({ role: 'system', content: m.systemTurnPrefix });
}
let userContent = m.content;
if (m.userPrefix) {
userContent = `${m.userPrefix}${userContent}`;
}
if (m.userSuffix) {
userContent = `${userContent}${m.userSuffix}`;
}
apiMessages.push({ role: 'user', content: userContent });
if (m.systemTurnSuffix) {
apiMessages.push({ role: 'system', content: m.systemTurnSuffix });
}
} else {
apiMessages.push({ role: m.role, content: m.content });
}
}
let fullResponse = '';
let hasStarted = false;
const ollamaName = instance.ollamaModelName || instance.modelName;
const options = instance.config;
await this.ollamaClient.chat(
ollamaName,
apiMessages,
(token) => {
if (!hasStarted) {
hasStarted = true;
if (onStart) {
onStart();
}
}
fullResponse += token;
onToken(token);
},
options,
);
const activeFramingId = chat.activeFramingId;
const activeFraming = activeFramingId ? this.framingService.getFraming(activeFramingId) : undefined;
await this.chatService.addMessage(chat.id, 'assistant', fullResponse, {
modelName: instance.modelName,
instanceName: instance.name,
instanceId: instance.id,
framingId: activeFraming?.id,
framingName: activeFraming?.name,
});
return { fullResponse };
}
/**
* Prepares and adds a user message to the chat, handling system prompts and metadata.
*/
async handleUserMessage(
chatId: string,
text: string,
options?: {
activeFramingId?: string;
},
): Promise<void> {
const chat = this.chatService.getChat(chatId);
if (!chat) {
throw new Error(`Chat ${chatId} not found`);
}
const instance = this.modelService.getSettings(chat.modelName);
let settings = { ...instance };
let activeFraming = undefined;
const framingId = options?.activeFramingId || chat.activeFramingId;
if (framingId) {
activeFraming = this.framingService.getFraming(framingId);
if (activeFraming) {
settings = {
...settings,
systemMessage: activeFraming.systemMessage,
};
}
}
const lastSystemPrompt = chat.messages.filter((m) => m.role === 'system').pop()?.content;
if (chat.messages.length === 0 || (settings.systemMessage && settings.systemMessage !== lastSystemPrompt)) {
await this.chatService.addMessage(chat.id, 'system', settings.systemMessage || '', {
modelName: instance.modelName,
instanceName: instance.name,
instanceId: instance.id,
framingId: activeFraming?.id,
framingName: activeFraming?.name,
});
}
const metadata: MessageMetadata = {
systemTurnPrefix: activeFraming?.systemTurnPrefix,
userPrefix: activeFraming?.userMessagePrefix,
userSuffix: activeFraming?.userMessageSuffix,
systemTurnSuffix: activeFraming?.systemTurnSuffix,
framingId: activeFraming?.id,
framingName: activeFraming?.name,
modelName: instance.modelName,
instanceName: instance.name,
instanceId: instance.id,
};
await this.chatService.addMessage(chat.id, 'user', text.trim(), metadata);
}
}
|