|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + AbstractAgent, |
| 6 | + type BaseEvent, |
| 7 | + EventType, |
| 8 | + type RunAgentInput, |
| 9 | +} from "@ag-ui/client"; |
| 10 | +import { |
| 11 | + A2UIMiddleware, |
| 12 | + A2UI_SCHEMA_CONTEXT_DESCRIPTION, |
| 13 | +} from "@ag-ui/a2ui-middleware"; |
| 14 | +import { Observable, firstValueFrom, toArray } from "rxjs"; |
| 15 | + |
| 16 | +import { dynamicSchemaCatalog } from "./a2ui-catalog"; |
| 17 | +import { DOJO_A2UI_MIDDLEWARE_CONFIG } from "./a2ui-config"; |
| 18 | + |
| 19 | +class CaptureAgent extends AbstractAgent { |
| 20 | + readonly runCalls: RunAgentInput[] = []; |
| 21 | + |
| 22 | + run(input: RunAgentInput): Observable<BaseEvent> { |
| 23 | + this.runCalls.push(input); |
| 24 | + return new Observable((subscriber) => { |
| 25 | + subscriber.next({ |
| 26 | + type: EventType.RUN_STARTED, |
| 27 | + threadId: input.threadId, |
| 28 | + runId: input.runId, |
| 29 | + }); |
| 30 | + subscriber.next({ |
| 31 | + type: EventType.RUN_FINISHED, |
| 32 | + threadId: input.threadId, |
| 33 | + runId: input.runId, |
| 34 | + }); |
| 35 | + subscriber.complete(); |
| 36 | + }); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +test("Dojo A2UI middleware forwards the rendered dynamic catalog", async () => { |
| 41 | + const downstream = new CaptureAgent(); |
| 42 | + const middleware = new A2UIMiddleware(DOJO_A2UI_MIDDLEWARE_CONFIG); |
| 43 | + |
| 44 | + await firstValueFrom( |
| 45 | + middleware |
| 46 | + .run( |
| 47 | + { |
| 48 | + threadId: "test-thread", |
| 49 | + runId: "test-run", |
| 50 | + tools: [], |
| 51 | + context: [], |
| 52 | + forwardedProps: {}, |
| 53 | + state: {}, |
| 54 | + messages: [], |
| 55 | + }, |
| 56 | + downstream, |
| 57 | + ) |
| 58 | + .pipe(toArray()), |
| 59 | + ); |
| 60 | + |
| 61 | + assert.equal(downstream.runCalls.length, 1); |
| 62 | + const forwarded = downstream.runCalls[0]; |
| 63 | + const schemaContext = forwarded.context.find( |
| 64 | + ({ description }) => description === A2UI_SCHEMA_CONTEXT_DESCRIPTION, |
| 65 | + ); |
| 66 | + assert.ok( |
| 67 | + schemaContext && typeof schemaContext.value === "string", |
| 68 | + "dynamic catalog schema must reach downstream agent context", |
| 69 | + ); |
| 70 | + |
| 71 | + const schema = JSON.parse(schemaContext.value) as { |
| 72 | + catalogId: string; |
| 73 | + components: Record< |
| 74 | + string, |
| 75 | + { allOf?: Array<{ properties?: Record<string, unknown> }> } |
| 76 | + >; |
| 77 | + }; |
| 78 | + assert.equal(schema.catalogId, dynamicSchemaCatalog.id); |
| 79 | + assert.deepEqual(Object.keys(schema.components), [ |
| 80 | + ...dynamicSchemaCatalog.components.keys(), |
| 81 | + ]); |
| 82 | + |
| 83 | + const hotelProperties = schema.components.HotelCard.allOf?.find( |
| 84 | + ({ properties }) => properties, |
| 85 | + )?.properties; |
| 86 | + assert.ok(hotelProperties); |
| 87 | + assert.ok("pricePerNight" in hotelProperties); |
| 88 | + const rating = hotelProperties.rating as { |
| 89 | + anyOf?: Array<{ type?: string }>; |
| 90 | + }; |
| 91 | + assert.ok(rating.anyOf?.some(({ type }) => type === "number")); |
| 92 | +}); |
0 commit comments