| ... | ... | @@ -10,7 +10,7 @@ import { |
| 10 | 10 | type ProviderRuntimeEvent, |
| 11 | 11 | type ProviderSessionId, |
| 12 | 12 | } from "@t3tools/contracts"; |
| 13 | | import { Cache, Cause, Duration, Effect, Layer, Option, Queue, Stream } from "effect"; |
| 13 | import { Cache, Cause, Duration, Effect, Layer, Option, Queue, Ref, Stream } from "effect"; |
| 14 | 14 | |
| 15 | 15 | import { ProviderService } from "../../provider/Services/ProviderService.ts"; |
| 16 | 16 | import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts"; |
| ... | ... | @@ -22,15 +22,10 @@ import { |
| 22 | 22 | const providerTurnKey = (sessionId: ProviderSessionId, turnId: TurnId) => `${sessionId}:${turnId}`; |
| 23 | 23 | const providerCommandId = (event: ProviderRuntimeEvent, tag: string): CommandId => |
| 24 | 24 | CommandId.makeUnsafe(`provider:${event.eventId}:${tag}:${crypto.randomUUID()}`); |
| 25 | |
| 25 | 26 | const DEFAULT_ASSISTANT_DELIVERY_MODE: AssistantDeliveryMode = "buffered"; |
| 26 | 27 | const TURN_MESSAGE_IDS_BY_TURN_CACHE_CAPACITY = 10_000; |
| 27 | 28 | const TURN_MESSAGE_IDS_BY_TURN_TTL = Duration.minutes(120); |
| 28 | | const TURN_DELIVERY_MODE_BY_TURN_CACHE_CAPACITY = 10_000; |
| 29 | | const TURN_DELIVERY_MODE_BY_TURN_TTL = Duration.minutes(120); |
| 30 | | const PENDING_DELIVERY_MODE_BY_THREAD_CACHE_CAPACITY = 10_000; |
| 31 | | const PENDING_DELIVERY_MODE_BY_THREAD_TTL = Duration.minutes(30); |
| 32 | | const MESSAGE_DELIVERY_MODE_BY_MESSAGE_ID_CACHE_CAPACITY = 20_000; |
| 33 | | const MESSAGE_DELIVERY_MODE_BY_MESSAGE_ID_TTL = Duration.minutes(120); |
| 34 | 29 | const BUFFERED_MESSAGE_TEXT_BY_MESSAGE_ID_CACHE_CAPACITY = 20_000; |
| 35 | 30 | const BUFFERED_MESSAGE_TEXT_BY_MESSAGE_ID_TTL = Duration.minutes(120); |
| 36 | 31 | |
| ... | ... | @@ -115,6 +110,7 @@ function runtimeEventToActivities( |
| 115 | 110 | }, |
| 116 | 111 | ]; |
| 117 | 112 | } |
| 113 | |
| 118 | 114 | case "tool.completed": { |
| 119 | 115 | return [ |
| 120 | 116 | { |
| ... | ... | @@ -131,6 +127,7 @@ function runtimeEventToActivities( |
| 131 | 127 | }, |
| 132 | 128 | ]; |
| 133 | 129 | } |
| 130 | |
| 134 | 131 | case "tool.started": { |
| 135 | 132 | return [ |
| 136 | 133 | { |
| ... | ... | @@ -157,26 +154,16 @@ const make = Effect.gen(function* () { |
| 157 | 154 | const orchestrationEngine = yield* OrchestrationEngineService; |
| 158 | 155 | const providerService = yield* ProviderService; |
| 159 | 156 | |
| 157 | const assistantDeliveryModeRef = yield* Ref.make<AssistantDeliveryMode>( |
| 158 | DEFAULT_ASSISTANT_DELIVERY_MODE, |
| 159 | ); |
| 160 | |
| 160 | 161 | const turnMessageIdsByTurnKey = yield* Cache.make<string, Set<MessageId>>({ |
| 161 | 162 | capacity: TURN_MESSAGE_IDS_BY_TURN_CACHE_CAPACITY, |
| 162 | 163 | timeToLive: TURN_MESSAGE_IDS_BY_TURN_TTL, |
| 163 | 164 | lookup: () => Effect.succeed(new Set<MessageId>()), |
| 164 | 165 | }); |
| 165 | | const assistantDeliveryModeByTurnKey = yield* Cache.make<string, AssistantDeliveryMode>({ |
| 166 | | capacity: TURN_DELIVERY_MODE_BY_TURN_CACHE_CAPACITY, |
| 167 | | timeToLive: TURN_DELIVERY_MODE_BY_TURN_TTL, |
| 168 | | lookup: () => Effect.succeed(DEFAULT_ASSISTANT_DELIVERY_MODE), |
| 169 | | }); |
| 170 | | const pendingAssistantDeliveryModeByThreadId = yield* Cache.make<ThreadId, AssistantDeliveryMode>({ |
| 171 | | capacity: PENDING_DELIVERY_MODE_BY_THREAD_CACHE_CAPACITY, |
| 172 | | timeToLive: PENDING_DELIVERY_MODE_BY_THREAD_TTL, |
| 173 | | lookup: () => Effect.succeed(DEFAULT_ASSISTANT_DELIVERY_MODE), |
| 174 | | }); |
| 175 | | const assistantDeliveryModeByMessageId = yield* Cache.make<MessageId, AssistantDeliveryMode>({ |
| 176 | | capacity: MESSAGE_DELIVERY_MODE_BY_MESSAGE_ID_CACHE_CAPACITY, |
| 177 | | timeToLive: MESSAGE_DELIVERY_MODE_BY_MESSAGE_ID_TTL, |
| 178 | | lookup: () => Effect.succeed(DEFAULT_ASSISTANT_DELIVERY_MODE), |
| 179 | | }); |
| 166 | |
| 180 | 167 | const bufferedAssistantTextByMessageId = yield* Cache.make<MessageId, string>({ |
| 181 | 168 | capacity: BUFFERED_MESSAGE_TEXT_BY_MESSAGE_ID_CACHE_CAPACITY, |
| 182 | 169 | timeToLive: BUFFERED_MESSAGE_TEXT_BY_MESSAGE_ID_TTL, |
| ... | ... | @@ -236,45 +223,6 @@ const make = Effect.gen(function* () { |
| 236 | 223 | const clearAssistantMessageIdsForTurn = (sessionId: ProviderSessionId, turnId: TurnId) => |
| 237 | 224 | Cache.invalidate(turnMessageIdsByTurnKey, providerTurnKey(sessionId, turnId)); |
| 238 | 225 | |
| 239 | | const rememberAssistantDeliveryModeForTurn = ( |
| 240 | | sessionId: ProviderSessionId, |
| 241 | | turnId: TurnId, |
| 242 | | assistantDeliveryMode: AssistantDeliveryMode, |
| 243 | | ) => |
| 244 | | Cache.set( |
| 245 | | assistantDeliveryModeByTurnKey, |
| 246 | | providerTurnKey(sessionId, turnId), |
| 247 | | assistantDeliveryMode, |
| 248 | | ); |
| 249 | | |
| 250 | | const clearAssistantDeliveryModeForTurn = (sessionId: ProviderSessionId, turnId: TurnId) => |
| 251 | | Cache.invalidate(assistantDeliveryModeByTurnKey, providerTurnKey(sessionId, turnId)); |
| 252 | | |
| 253 | | const rememberPendingAssistantDeliveryModeForThread = ( |
| 254 | | threadId: ThreadId, |
| 255 | | assistantDeliveryMode: AssistantDeliveryMode, |
| 256 | | ) => Cache.set(pendingAssistantDeliveryModeByThreadId, threadId, assistantDeliveryMode); |
| 257 | | |
| 258 | | const takePendingAssistantDeliveryModeForThread = (threadId: ThreadId) => |
| 259 | | Cache.getOption(pendingAssistantDeliveryModeByThreadId, threadId).pipe( |
| 260 | | Effect.flatMap((assistantDeliveryMode) => |
| 261 | | Cache.invalidate(pendingAssistantDeliveryModeByThreadId, threadId).pipe( |
| 262 | | Effect.as(assistantDeliveryMode), |
| 263 | | ), |
| 264 | | ), |
| 265 | | ); |
| 266 | | |
| 267 | | const clearPendingAssistantDeliveryModeForThread = (threadId: ThreadId) => |
| 268 | | Cache.invalidate(pendingAssistantDeliveryModeByThreadId, threadId); |
| 269 | | |
| 270 | | const rememberAssistantDeliveryModeForMessage = ( |
| 271 | | messageId: MessageId, |
| 272 | | assistantDeliveryMode: AssistantDeliveryMode, |
| 273 | | ) => Cache.set(assistantDeliveryModeByMessageId, messageId, assistantDeliveryMode); |
| 274 | | |
| 275 | | const clearAssistantDeliveryModeForMessage = (messageId: MessageId) => |
| 276 | | Cache.invalidate(assistantDeliveryModeByMessageId, messageId); |
| 277 | | |
| 278 | 226 | const appendBufferedAssistantText = (messageId: MessageId, delta: string) => |
| 279 | 227 | Cache.getOption(bufferedAssistantTextByMessageId, messageId).pipe( |
| 280 | 228 | Effect.flatMap((existingText) => |
| ... | ... | @@ -301,66 +249,18 @@ const make = Effect.gen(function* () { |
| 301 | 249 | const clearBufferedAssistantText = (messageId: MessageId) => |
| 302 | 250 | Cache.invalidate(bufferedAssistantTextByMessageId, messageId); |
| 303 | 251 | |
| 304 | | const clearAssistantMessageState = (messageId: MessageId) => |
| 305 | | Effect.all( |
| 306 | | [ |
| 307 | | clearAssistantDeliveryModeForMessage(messageId), |
| 308 | | clearBufferedAssistantText(messageId), |
| 309 | | ], |
| 310 | | { concurrency: 1 }, |
| 311 | | ).pipe(Effect.asVoid); |
| 312 | | |
| 313 | | const resolveAssistantDeliveryModeForMessage = (input: { |
| 314 | | threadId: ThreadId; |
| 315 | | sessionId: ProviderSessionId; |
| 316 | | turnId?: TurnId; |
| 317 | | messageId: MessageId; |
| 318 | | }) => |
| 319 | | Effect.gen(function* () { |
| 320 | | const messageMode = yield* Cache.getOption(assistantDeliveryModeByMessageId, input.messageId); |
| 321 | | if (Option.isSome(messageMode)) { |
| 322 | | return messageMode.value; |
| 323 | | } |
| 324 | | |
| 325 | | if (input.turnId) { |
| 326 | | const turnMode = yield* Cache.getOption( |
| 327 | | assistantDeliveryModeByTurnKey, |
| 328 | | providerTurnKey(input.sessionId, input.turnId), |
| 329 | | ); |
| 330 | | if (Option.isSome(turnMode)) { |
| 331 | | return turnMode.value; |
| 332 | | } |
| 333 | | } |
| 334 | | |
| 335 | | const pendingMode = yield* Cache.getOption( |
| 336 | | pendingAssistantDeliveryModeByThreadId, |
| 337 | | input.threadId, |
| 338 | | ); |
| 339 | | if (Option.isSome(pendingMode)) { |
| 340 | | if (input.turnId) { |
| 341 | | yield* rememberAssistantDeliveryModeForTurn(input.sessionId, input.turnId, pendingMode.value); |
| 342 | | yield* clearPendingAssistantDeliveryModeForThread(input.threadId); |
| 343 | | } |
| 344 | | return pendingMode.value; |
| 345 | | } |
| 346 | | |
| 347 | | return DEFAULT_ASSISTANT_DELIVERY_MODE; |
| 348 | | }); |
| 252 | const clearAssistantMessageState = (messageId: MessageId) => clearBufferedAssistantText(messageId); |
| 349 | 253 | |
| 350 | 254 | const dispatchAssistantCompletion = (input: { |
| 351 | 255 | event: ProviderRuntimeEvent; |
| 352 | 256 | threadId: ThreadId; |
| 353 | 257 | messageId: MessageId; |
| 354 | 258 | turnId?: TurnId; |
| 355 | | assistantDeliveryMode: AssistantDeliveryMode; |
| 356 | 259 | createdAt: string; |
| 357 | 260 | commandTag: string; |
| 358 | 261 | }) => |
| 359 | 262 | Effect.gen(function* () { |
| 360 | | const text = |
| 361 | | input.assistantDeliveryMode === "buffered" |
| 362 | | ? yield* takeBufferedAssistantText(input.messageId) |
| 363 | | : undefined; |
| 263 | const text = yield* takeBufferedAssistantText(input.messageId); |
| 364 | 264 | |
| 365 | 265 | yield* orchestrationEngine.dispatch({ |
| 366 | 266 | type: "thread.message.assistant.complete", |
| ... | ... | @@ -368,7 +268,7 @@ const make = Effect.gen(function* () { |
| 368 | 268 | threadId: input.threadId, |
| 369 | 269 | messageId: input.messageId, |
| 370 | 270 | ...(input.turnId ? { turnId: input.turnId } : {}), |
| 371 | | ...(text !== undefined ? { text } : {}), |
| 271 | text, |
| 372 | 272 | createdAt: input.createdAt, |
| 373 | 273 | }); |
| 374 | 274 | yield* clearAssistantMessageState(input.messageId); |
| ... | ... | @@ -397,16 +297,6 @@ const make = Effect.gen(function* () { |
| 397 | 297 | }), |
| 398 | 298 | { concurrency: 1 }, |
| 399 | 299 | ).pipe(Effect.asVoid); |
| 400 | | |
| 401 | | const deliveryModeKeys = Array.from(yield* Cache.keys(assistantDeliveryModeByTurnKey)); |
| 402 | | yield* Effect.forEach( |
| 403 | | deliveryModeKeys, |
| 404 | | (key) => |
| 405 | | key.startsWith(prefix) |
| 406 | | ? Cache.invalidate(assistantDeliveryModeByTurnKey, key) |
| 407 | | : Effect.void, |
| 408 | | { concurrency: 1 }, |
| 409 | | ).pipe(Effect.asVoid); |
| 410 | 300 | }); |
| 411 | 301 | |
| 412 | 302 | const processRuntimeEvent = (event: ProviderRuntimeEvent) => |
| ... | ... | @@ -427,18 +317,6 @@ const make = Effect.gen(function* () { |
| 427 | 317 | event.type === "turn.completed" |
| 428 | 318 | ) { |
| 429 | 319 | const activeTurnId = event.type === "turn.started" ? (toTurnId(event.turnId) ?? null) : null; |
| 430 | | if (event.type === "turn.started" && activeTurnId) { |
| 431 | | const pendingAssistantDeliveryMode = yield* takePendingAssistantDeliveryModeForThread( |
| 432 | | thread.id, |
| 433 | | ); |
| 434 | | if (Option.isSome(pendingAssistantDeliveryMode)) { |
| 435 | | yield* rememberAssistantDeliveryModeForTurn( |
| 436 | | event.sessionId, |
| 437 | | activeTurnId, |
| 438 | | pendingAssistantDeliveryMode.value, |
| 439 | | ); |
| 440 | | } |
| 441 | | } |
| 442 | 320 | const providerThreadIdFromEvent = |
| 443 | 321 | event.type === "thread.started" |
| 444 | 322 | ? ProviderThreadId.makeUnsafe(event.threadId) |
| ... | ... | @@ -488,14 +366,8 @@ const make = Effect.gen(function* () { |
| 488 | 366 | if (turnId) { |
| 489 | 367 | yield* rememberAssistantMessageId(event.sessionId, turnId, assistantMessageId); |
| 490 | 368 | } |
| 491 | | const assistantDeliveryMode = yield* resolveAssistantDeliveryModeForMessage({ |
| 492 | | threadId: thread.id, |
| 493 | | sessionId: event.sessionId, |
| 494 | | ...(turnId ? { turnId } : {}), |
| 495 | | messageId: assistantMessageId, |
| 496 | | }); |
| 497 | | yield* rememberAssistantDeliveryModeForMessage(assistantMessageId, assistantDeliveryMode); |
| 498 | 369 | |
| 370 | const assistantDeliveryMode = yield* Ref.get(assistantDeliveryModeRef); |
| 499 | 371 | if (assistantDeliveryMode === "buffered") { |
| 500 | 372 | yield* appendBufferedAssistantText(assistantMessageId, event.delta); |
| 501 | 373 | } else { |
| ... | ... | @@ -517,20 +389,12 @@ const make = Effect.gen(function* () { |
| 517 | 389 | if (turnId) { |
| 518 | 390 | yield* rememberAssistantMessageId(event.sessionId, turnId, assistantMessageId); |
| 519 | 391 | } |
| 520 | | const assistantDeliveryMode = yield* resolveAssistantDeliveryModeForMessage({ |
| 521 | | threadId: thread.id, |
| 522 | | sessionId: event.sessionId, |
| 523 | | ...(turnId ? { turnId } : {}), |
| 524 | | messageId: assistantMessageId, |
| 525 | | }); |
| 526 | | yield* rememberAssistantDeliveryModeForMessage(assistantMessageId, assistantDeliveryMode); |
| 527 | 392 | |
| 528 | 393 | yield* dispatchAssistantCompletion({ |
| 529 | 394 | event, |
| 530 | 395 | threadId: thread.id, |
| 531 | 396 | messageId: assistantMessageId, |
| 532 | 397 | ...(turnId ? { turnId } : {}), |
| 533 | | assistantDeliveryMode, |
| 534 | 398 | createdAt: now, |
| 535 | 399 | commandTag: "assistant-complete", |
| 536 | 400 | }); |
| ... | ... | @@ -548,19 +412,11 @@ const make = Effect.gen(function* () { |
| 548 | 412 | assistantMessageIds, |
| 549 | 413 | (assistantMessageId) => |
| 550 | 414 | Effect.gen(function* () { |
| 551 | | const assistantDeliveryMode = |
| 552 | | yield* resolveAssistantDeliveryModeForMessage({ |
| 553 | | threadId: thread.id, |
| 554 | | sessionId: event.sessionId, |
| 555 | | turnId, |
| 556 | | messageId: assistantMessageId, |
| 557 | | }); |
| 558 | 415 | yield* dispatchAssistantCompletion({ |
| 559 | 416 | event, |
| 560 | 417 | threadId: thread.id, |
| 561 | 418 | messageId: assistantMessageId, |
| 562 | 419 | turnId, |
| 563 | | assistantDeliveryMode, |
| 564 | 420 | createdAt: now, |
| 565 | 421 | commandTag: "assistant-complete-finalize", |
| 566 | 422 | }); |
| ... | ... | @@ -568,13 +424,11 @@ const make = Effect.gen(function* () { |
| 568 | 424 | { concurrency: 1 }, |
| 569 | 425 | ).pipe(Effect.asVoid); |
| 570 | 426 | yield* clearAssistantMessageIdsForTurn(event.sessionId, turnId); |
| 571 | | yield* clearAssistantDeliveryModeForTurn(event.sessionId, turnId); |
| 572 | 427 | } |
| 573 | 428 | } |
| 574 | 429 | |
| 575 | 430 | if (event.type === "session.exited") { |
| 576 | 431 | yield* clearTurnStateForSession(event.sessionId); |
| 577 | | yield* clearPendingAssistantDeliveryModeForThread(thread.id); |
| 578 | 432 | } |
| 579 | 433 | |
| 580 | 434 | if (event.type === "runtime.error") { |
| ... | ... | @@ -614,8 +468,8 @@ const make = Effect.gen(function* () { |
| 614 | 468 | }); |
| 615 | 469 | |
| 616 | 470 | const processDomainEvent = (event: TurnStartRequestedDomainEvent) => |
| 617 | | rememberPendingAssistantDeliveryModeForThread( |
| 618 | | event.payload.threadId, |
| 471 | Ref.set( |
| 472 | assistantDeliveryModeRef, |
| 619 | 473 | event.payload.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE, |
| 620 | 474 | ); |
| 621 | 475 | |