|
1 | 1 | import { |
| 2 | + CheckpointId, |
| 3 | + CheckpointRef, |
| 4 | + CheckpointScopeId, |
2 | 5 | EnvironmentId, |
3 | 6 | EventId, |
4 | 7 | MessageId, |
| 8 | + NodeId, |
5 | 9 | RunId, |
6 | 10 | TurnItemId, |
| 11 | + type OrchestrationV2Checkpoint, |
| 12 | + type OrchestrationV2ConversationMessage, |
| 13 | + type OrchestrationV2Run, |
7 | 14 | type OrchestrationV2ThreadProjection, |
8 | 15 | type OrchestrationV2TurnItem, |
9 | 16 | } from "@t3tools/contracts"; |
@@ -279,6 +286,139 @@ it("only compares replaced entries in a streamed reducer delta", () => { |
279 | 286 | expect(slotReads).toBeLessThanOrEqual(2); |
280 | 287 | }); |
281 | 288 |
|
| 289 | +describe("derived thread state through a streamed message", () => { |
| 290 | + const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId }; |
| 291 | + const runId = RunId.make("run-1"); |
| 292 | + const streamed: OrchestrationV2ConversationMessage = { |
| 293 | + id: MessageId.make("message-assistant"), |
| 294 | + threadId: v2ThreadId, |
| 295 | + runId, |
| 296 | + nodeId: null, |
| 297 | + role: "assistant", |
| 298 | + text: "Hel", |
| 299 | + attachments: [], |
| 300 | + streaming: true, |
| 301 | + createdBy: "agent", |
| 302 | + creationSource: "provider", |
| 303 | + createdAt: v2Now, |
| 304 | + updatedAt: v2Now, |
| 305 | + }; |
| 306 | + const queuedMessage: OrchestrationV2ConversationMessage = { |
| 307 | + ...streamed, |
| 308 | + id: MessageId.make("message-queued"), |
| 309 | + runId: RunId.make("run-2"), |
| 310 | + role: "user", |
| 311 | + text: "Then run the tests", |
| 312 | + streaming: false, |
| 313 | + createdBy: "user", |
| 314 | + creationSource: "mobile", |
| 315 | + }; |
| 316 | + const queuedRun: OrchestrationV2Run = { |
| 317 | + id: RunId.make("run-2"), |
| 318 | + threadId: v2ThreadId, |
| 319 | + ordinal: 2, |
| 320 | + providerInstanceId: v2Projection.thread.providerInstanceId, |
| 321 | + modelSelection: v2Projection.thread.modelSelection, |
| 322 | + providerThreadId: null, |
| 323 | + userMessageId: queuedMessage.id, |
| 324 | + rootNodeId: null, |
| 325 | + activeAttemptId: null, |
| 326 | + status: "queued", |
| 327 | + requestedAt: v2Now, |
| 328 | + startedAt: null, |
| 329 | + completedAt: null, |
| 330 | + checkpointId: null, |
| 331 | + contextHandoffId: null, |
| 332 | + }; |
| 333 | + const checkpoint = (ordinal: number): OrchestrationV2Checkpoint => ({ |
| 334 | + id: CheckpointId.make(`checkpoint-${ordinal}`), |
| 335 | + threadId: v2ThreadId, |
| 336 | + scopeId: CheckpointScopeId.make("scope"), |
| 337 | + runId, |
| 338 | + nodeId: NodeId.make("node"), |
| 339 | + parentCheckpointId: null, |
| 340 | + ordinalWithinScope: ordinal, |
| 341 | + appRunOrdinal: ordinal, |
| 342 | + ref: CheckpointRef.make(`refs/t3/checkpoint-${ordinal}`), |
| 343 | + status: "ready", |
| 344 | + files: [], |
| 345 | + capturedAt: v2Now, |
| 346 | + }); |
| 347 | + const initial: OrchestrationV2ThreadProjection = { |
| 348 | + ...v2Projection, |
| 349 | + runs: [queuedRun], |
| 350 | + messages: [streamed, queuedMessage], |
| 351 | + checkpoints: [checkpoint(1)], |
| 352 | + }; |
| 353 | + |
| 354 | + function setup() { |
| 355 | + const stateAtom = Atom.make( |
| 356 | + AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(initial) }), |
| 357 | + ); |
| 358 | + const details = createEnvironmentThreadDetailAtoms(() => stateAtom); |
| 359 | + const queue = details.queueWorkflowAtom(ref); |
| 360 | + const checkpoints = details.checkpointSummariesAtom(ref); |
| 361 | + const registry = AtomRegistry.make(); |
| 362 | + registry.mount(queue); |
| 363 | + registry.mount(checkpoints); |
| 364 | + let projection = initial; |
| 365 | + return { |
| 366 | + queue: () => registry.get(queue), |
| 367 | + checkpoints: () => registry.get(checkpoints), |
| 368 | + apply: (event: Parameters<typeof applyOrchestrationV2ProjectionEvent>[1]) => { |
| 369 | + projection = applyOrchestrationV2ProjectionEvent(projection, event)!; |
| 370 | + registry.set( |
| 371 | + stateAtom, |
| 372 | + AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }), |
| 373 | + ); |
| 374 | + }, |
| 375 | + [Symbol.dispose]: () => registry.dispose(), |
| 376 | + }; |
| 377 | + } |
| 378 | + |
| 379 | + const messageUpdated = (message: OrchestrationV2ConversationMessage) => ({ |
| 380 | + id: EventId.make(`event-${message.id}-${message.text.length}`), |
| 381 | + type: "message.updated" as const, |
| 382 | + threadId: v2ThreadId, |
| 383 | + occurredAt: later, |
| 384 | + payload: message, |
| 385 | + }); |
| 386 | + |
| 387 | + it("keeps the queue workflow through streamed text and follows a queued message edit", () => { |
| 388 | + using thread = setup(); |
| 389 | + const first = thread.queue(); |
| 390 | + thread.apply(messageUpdated({ ...streamed, text: "Hello", updatedAt: later })); |
| 391 | + thread.apply(messageUpdated({ ...streamed, text: "Hello there", updatedAt: later })); |
| 392 | + expect(thread.queue()).toBe(first); |
| 393 | + |
| 394 | + thread.apply(messageUpdated({ ...queuedMessage, text: "Then run lint", updatedAt: later })); |
| 395 | + expect(thread.queue()?.queuedRuns.map((queued) => queued.text)).toEqual(["Then run lint"]); |
| 396 | + }); |
| 397 | + |
| 398 | + it("keeps checkpoint summaries through streamed text and follows new assistant messages and checkpoints", () => { |
| 399 | + using thread = setup(); |
| 400 | + const first = thread.checkpoints(); |
| 401 | + thread.apply(messageUpdated({ ...streamed, text: "Hello", updatedAt: later })); |
| 402 | + thread.apply(messageUpdated({ ...streamed, text: "Hello there", updatedAt: later })); |
| 403 | + expect(thread.checkpoints()).toBe(first); |
| 404 | + |
| 405 | + const followUp = { ...streamed, id: MessageId.make("message-follow-up"), text: "Done" }; |
| 406 | + thread.apply(messageUpdated(followUp)); |
| 407 | + expect(thread.checkpoints().map((summary) => summary.assistantMessageId)).toEqual([ |
| 408 | + followUp.id, |
| 409 | + ]); |
| 410 | + |
| 411 | + thread.apply({ |
| 412 | + id: EventId.make("checkpoint-2"), |
| 413 | + type: "checkpoint.captured", |
| 414 | + threadId: v2ThreadId, |
| 415 | + occurredAt: later, |
| 416 | + payload: checkpoint(2), |
| 417 | + }); |
| 418 | + expect(thread.checkpoints().map((summary) => summary.checkpointTurnCount)).toEqual([1, 2]); |
| 419 | + }); |
| 420 | +}); |
| 421 | + |
282 | 422 | function replaceItem(projection: OrchestrationV2ThreadProjection, item: OrchestrationV2TurnItem) { |
283 | 423 | return applyOrchestrationV2ProjectionEvent(projection, { |
284 | 424 | id: EventId.make(`event-${item.id}`), |
|
0 commit comments