-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrchestrator.ts
More file actions
490 lines (471 loc) · 16.1 KB
/
Orchestrator.ts
File metadata and controls
490 lines (471 loc) · 16.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import type {
ChatMessage,
OllamaService,
RequestChat,
ResponseChat,
ResponseChatStream,
ToolCall
} from '@neabyte/ollama-native'
import type {
OrchestratorConfig,
OrchestratorOptions,
OrchestratorPermission,
OrchestratorPermissionResponse,
OrchestratorSession,
OrchestratorToolCall,
OrchestratorToolResult
} from '@interfaces/Orchestrator'
import { ToolExecutor } from '@core/index'
import { ChatManager, ContextSys } from '@integrator/index'
import { generateId, downloadRipgrep, resolveRipgrepPath } from '@utils/index'
import allToolSchemas from '@schemas/index'
/**
* Orchestrates chat interactions with tool execution and permission management.
* @description Manages chat sessions, handles tool calls, and coordinates between chat and tool execution.
*/
export class Orchestrator {
/** Active sessions map */
private readonly activeSessions: Map<string, boolean> = new Map()
/** Manages chat sessions and message storage */
private readonly chatManager: ChatManager
/** The Ollama service client for chat interactions */
private readonly client: OllamaService
/** System context generator */
private readonly contextSys: ContextSys
/** Session permission settings - tracks which sessions have "allow all" enabled */
private readonly sessionPermissions: Map<string, { allowAll: boolean }> = new Map()
/** Available tools for the orchestrator */
private readonly tools: ToolCall[]
/** Executes tool operations */
private readonly toolExecutor: ToolExecutor
/**
* Creates a new Orchestrator instance.
* @description Initializes the orchestrator with client and tools.
* @param config - Configuration object containing client and optional tools
*/
constructor(config: OrchestratorConfig) {
this.chatManager = new ChatManager()
this.client = config.client
this.contextSys = new ContextSys()
this.toolExecutor = new ToolExecutor()
this.tools = (config.tools ?? allToolSchemas) as ToolCall[]
}
/**
* Initializes Ripgrep binary if not already available.
* @description Checks if Ripgrep is available locally, downloads it if needed.
* @throws {Error} When Ripgrep initialization fails
*/
private async initializeRipgrep(): Promise<void> {
const ripgrepPath: string | null = resolveRipgrepPath()
if (ripgrepPath === null) {
try {
await downloadRipgrep()
} catch (error) {
const errorMessage: string = error instanceof Error ? error.message : 'Unknown error'
throw new Error(`Failed to download Ripgrep: ${errorMessage}`)
}
}
}
/**
* Aborts all active sessions.
* @description Marks all sessions as inactive and aborts the client.
*/
abort(): void {
this.activeSessions.clear()
this.client.abort()
}
/**
* Aborts a specific session.
* @description Marks a session as inactive and aborts the client.
* @param sessionId - The session ID to abort
*/
private abortSession(sessionId: string): void {
this.activeSessions.set(sessionId, false)
this.client.abort()
}
/**
* Processes a chat request with tool execution and permission support.
* @description Handles chat interactions, tool calls, and emits events.
* @param message - The user message to process
* @param options - Chat options including event callbacks and settings
* @returns Promise that resolves to a chat session
* @throws {Error} When message processing fails or permission is denied
*/
async chat(message: string, options: OrchestratorOptions): Promise<OrchestratorSession> {
await this.initializeRipgrep()
const sessionId: string = this.chatManager.createSession()
this.activeSessions.set(sessionId, true)
const session: OrchestratorSession = {
id: sessionId,
abort: () => {
this.abortSession(sessionId)
},
isActive: () => (this.activeSessions.get(sessionId) ?? false) === true
}
try {
await this.processChatLoop(sessionId, message, options)
return session
} catch (error) {
this.activeSessions.set(sessionId, false)
throw error
}
}
/**
* Emits tool call event.
* @description Emits the tool call event to notify listeners.
* @param toolName - Name of the tool
* @param toolArguments - Tool arguments
* @param sessionId - Session ID
* @param options - Chat options
*/
private emitToolCallEvent(
toolName: string,
toolArguments: Record<string, unknown>,
sessionId: string,
options: OrchestratorOptions
): void {
const toolCallEvent: OrchestratorToolCall = {
sessionId,
toolName,
arguments: toolArguments
}
options.onToolCall?.(toolCallEvent)
}
/**
* Executes a tool and handles the result.
* @description Executes the tool and adds the result to the session.
* @param sessionId - Session ID
* @param toolName - Name of the tool
* @param toolArguments - Tool arguments
* @param options - Chat options
*/
private async executeTool(
toolName: string,
toolArguments: Record<string, unknown>,
sessionId: string,
options: OrchestratorOptions
): Promise<void> {
const result: string = await this.toolExecutor.execute(toolName, toolArguments)
const toolResultEvent: OrchestratorToolResult = {
sessionId,
toolName,
result
}
options.onToolResult?.(toolResultEvent)
const toolMessage: ChatMessage = {
role: 'tool',
content: result,
tool_name: toolName
}
this.chatManager.addMessage(sessionId, toolMessage)
}
/**
* Extracts tool name and arguments from a tool call.
* @description Parses the tool call object to extract name and arguments.
* @param toolCall - The tool call object
* @returns Object containing tool name and arguments
*/
private extractToolCallData(toolCall: unknown): {
toolName: string
toolArguments: Record<string, unknown>
} {
const toolCallObj: { function?: { name?: string; arguments?: Record<string, unknown> } } =
toolCall as { function?: { name?: string; arguments?: Record<string, unknown> } }
return {
toolName: toolCallObj.function?.name ?? '',
toolArguments: toolCallObj.function?.arguments ?? {}
}
}
/**
* Finalizes a streaming message when done.
* @description Adds message to session and handles tool calls.
* @param sessionId - The session ID
* @param message - The message to finalize
* @param options - Chat options
*/
private async finalizeStreamingMessage(
sessionId: string,
message: ChatMessage,
options: OrchestratorOptions
): Promise<void> {
this.chatManager.addMessage(sessionId, message)
if (message.tool_calls !== undefined && message.tool_calls.length > 0) {
await this.handleToolCalls(sessionId, message.tool_calls, options)
}
}
/**
* Handles permission denied scenario.
* @description Aborts the session and emits abort event.
* @param sessionId - Session ID
* @param options - Chat options
*/
private handlePermissionDenied(sessionId: string, options: OrchestratorOptions): void {
options.onAbort?.({
sessionId,
reason: 'Permission denied by user'
})
this.activeSessions.set(sessionId, false)
}
/**
* Handles tool call execution with permission management.
* @description Executes tool calls and adds tool responses to the session.
* @param sessionId - The session ID to add tool responses to
* @param toolCalls - Array of tool calls to execute
* @param options - Chat options
*/
private async handleToolCalls(
sessionId: string,
toolCalls: unknown[],
options: OrchestratorOptions
): Promise<void> {
for (const toolCall of toolCalls) {
if ((this.activeSessions.get(sessionId) ?? false) !== true) {
break
}
const {
toolName,
toolArguments
}: { toolName: string; toolArguments: Record<string, unknown> } =
this.extractToolCallData(toolCall)
this.emitToolCallEvent(toolName, toolArguments, sessionId, options)
const approved: boolean = await this.requestPermission(
sessionId,
toolName,
toolArguments,
options
)
if (!approved) {
this.handlePermissionDenied(sessionId, options)
return
}
await this.executeTool(toolName, toolArguments, sessionId, options)
}
}
/**
* Processes the main chat loop with tool execution and permissions.
* @description Handles iterative chat processing until no more tool calls needed.
* @param sessionId - The session ID to process
* @param message - The user message
* @param options - Chat options
*/
private async processChatLoop(
sessionId: string,
message: string,
options: OrchestratorOptions
): Promise<void> {
this.chatManager.addMessage(sessionId, {
role: 'user',
content: message
})
const sessionMessages: ChatMessage[] = this.chatManager.getMessages(sessionId) ?? []
const hasSystemMessage: boolean = sessionMessages.some(
(msg: ChatMessage) => msg.role === 'system'
)
if (!hasSystemMessage) {
this.chatManager.addMessage(sessionId, {
role: 'system',
content: this.contextSys.getSystemPrompt()
})
}
let hasToolCalls: boolean = true
let iterationCount: number = 0
while (hasToolCalls && (this.activeSessions.get(sessionId) ?? false) === true) {
iterationCount++
console.log(`🔄 Processing iteration ${iterationCount}...`)
const sessionMessages: ChatMessage[] = this.chatManager.getMessages(sessionId) ?? []
const request: RequestChat = {
model: options.model,
messages: sessionMessages,
tools: this.tools,
stream: options.stream
}
const response: ResponseChat | AsyncIterable<ResponseChatStream> =
await this.client.chat(request)
if ((this.activeSessions.get(sessionId) ?? false) !== true) {
break
}
if (options.stream && Symbol.asyncIterator in response) {
const finalMessage: ChatMessage | null = await this.processStreamingResponse(
sessionId,
response,
options
)
hasToolCalls = finalMessage?.tool_calls !== undefined && finalMessage.tool_calls.length > 0
} else {
const nonStreamResponse: ResponseChat = response as ResponseChat
await this.processNonStreamingResponse(sessionId, nonStreamResponse, options)
hasToolCalls =
nonStreamResponse.message?.tool_calls !== undefined &&
nonStreamResponse.message.tool_calls.length > 0
}
}
}
/**
* Processes non-streaming chat response.
* @description Handles standard chat responses without streaming.
* @param sessionId - The session ID
* @param response - The chat response
* @param options - Chat options
*/
private async processNonStreamingResponse(
sessionId: string,
response: ResponseChat,
options: OrchestratorOptions
): Promise<void> {
if (response.message != null) {
this.chatManager.addMessage(sessionId, response.message)
options.onMessage?.(response.message)
if (response.message.tool_calls !== undefined && response.message.tool_calls.length > 0) {
await this.handleToolCalls(sessionId, response.message.tool_calls, options)
}
}
if (response.thinking !== undefined && response.thinking.length > 0) {
options.onThinking?.(response.thinking)
}
}
/**
* Processes a single streaming chunk.
* @description Handles thinking and content emission for a chunk.
* @param chunk - The streaming chunk to process
* @param currentMessage - The current message being built
* @param options - Chat options
*/
private processStreamingChunk(
chunk: ResponseChatStream,
currentMessage: ChatMessage | null,
options: OrchestratorOptions
): void {
if (chunk.thinking !== undefined && chunk.thinking.length > 0) {
options.onThinking?.(chunk.thinking)
}
if (chunk.message?.content != null && chunk.message.content.length > 0) {
options.onMessage?.({
content: chunk.message.content,
role: currentMessage?.role ?? 'assistant'
})
}
}
/**
* Processes streaming chat response.
* @description Handles streaming responses with real-time content updates.
* @param sessionId - The session ID
* @param stream - The streaming response iterator
* @param options - Chat options
* @returns The final message for tool call checking
*/
private async processStreamingResponse(
sessionId: string,
stream: AsyncIterable<ResponseChatStream>,
options: OrchestratorOptions
): Promise<ChatMessage | null> {
let currentMessage: ChatMessage | null = null
try {
for await (const chunk of stream) {
if ((this.activeSessions.get(sessionId) ?? false) !== true) {
break
}
this.processStreamingChunk(chunk, currentMessage, options)
currentMessage = this.updateCurrentMessage(chunk, currentMessage)
if (chunk.done && currentMessage !== null) {
await this.finalizeStreamingMessage(sessionId, currentMessage, options)
}
}
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
return currentMessage
}
throw error
}
return currentMessage
}
/**
* Processes permission response from user.
* @description Handles the three permission states: approve, deny, allow_all.
* @param response - Permission response from user
* @param toolName - Name of the tool
* @param sessionId - Session ID
* @returns True if approved, false if denied
*/
private processPermissionResponse(
sessionId: string,
response: OrchestratorPermissionResponse,
toolName: string
): boolean {
if (response.action === 'approve') {
console.log(`✅ Permission granted for ${toolName}`)
return true
}
if (response.action === 'allow_all') {
this.sessionPermissions.set(sessionId, { allowAll: true })
console.log(`✅ Permission granted for ${toolName} and enabled allow-all for session`)
return true
}
if (response.action === 'deny') {
console.log(`❌ Permission denied for ${toolName}`)
return false
}
return false
}
/**
* Requests permission for a tool call.
* @description Handles permission logic including session-level allow-all.
* @param toolName - Name of the tool
* @param toolArguments - Tool arguments
* @param sessionId - Session ID
* @param options - Chat options
* @returns Promise that resolves to true if approved, false if denied
*/
private async requestPermission(
sessionId: string,
toolName: string,
toolArguments: Record<string, unknown>,
options: OrchestratorOptions
): Promise<boolean> {
const sessionPermission: { allowAll: boolean } | undefined =
this.sessionPermissions.get(sessionId)
if (sessionPermission?.allowAll === true) {
return true
}
if (!options.onAskPermission) {
return true
}
const permissionRequest: OrchestratorPermission = {
sessionId,
toolName,
arguments: toolArguments,
requestId: generateId('permission')
}
try {
const permissionResponse: OrchestratorPermissionResponse =
await options.onAskPermission(permissionRequest)
return this.processPermissionResponse(sessionId, permissionResponse, toolName)
} catch {
return false
}
}
/**
* Updates the current message with chunk data.
* @description Merges chunk data into the current message.
* @param chunk - The streaming chunk
* @param currentMessage - The current message being built
* @returns Updated current message
*/
private updateCurrentMessage(
chunk: ResponseChatStream,
currentMessage: ChatMessage | null
): ChatMessage | null {
if (chunk.message === undefined) {
return currentMessage
}
if (currentMessage === null) {
return chunk.message
}
if (chunk.message.content != null) {
currentMessage.content = (currentMessage.content ?? '') + chunk.message.content
}
if (chunk.message.tool_calls !== undefined) {
currentMessage.tool_calls = chunk.message.tool_calls
}
return currentMessage
}
}