model-invocation-context.test.ts 854 B

12345678910111213141516171819202122232425
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getCurrentModelInvocationContext,
  4. runWithModelInvocationContext,
  5. } from "../src/model/invocation-context.js";
  6. describe("ModelInvocationContext", () => {
  7. it("keeps invocation context while an async stream is consumed outside the creation callback", async () => {
  8. const stream = runWithModelInvocationContext(
  9. { metadata: { querySource: "test-stream" } },
  10. async function* () {
  11. await Promise.resolve();
  12. yield getCurrentModelInvocationContext()?.metadata?.querySource;
  13. },
  14. );
  15. expect(getCurrentModelInvocationContext()).toBeUndefined();
  16. const values: unknown[] = [];
  17. for await (const value of stream) values.push(value);
  18. expect(values).toEqual(["test-stream"]);
  19. expect(getCurrentModelInvocationContext()).toBeUndefined();
  20. });
  21. });