compaction-extensions-example.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Verify the documentation example from extensions.md compiles and works.
  3. */
  4. import { describe, expect, it } from "vitest";
  5. import type { ExtensionAPI, SessionBeforeCompactEvent, SessionCompactEvent } from "../src/core/extensions/index.ts";
  6. describe("Documentation example", () => {
  7. it("custom compaction example should type-check correctly", () => {
  8. // This is the example from extensions.md - verify it compiles
  9. const exampleExtension = (pi: ExtensionAPI) => {
  10. pi.on("session_before_compact", async (event: SessionBeforeCompactEvent, ctx) => {
  11. // All these should be accessible on the event
  12. const { preparation, branchEntries } = event;
  13. // sessionManager, modelRegistry, and model come from ctx
  14. const { sessionManager, modelRegistry } = ctx;
  15. const { messagesToSummarize, turnPrefixMessages, tokensBefore, firstKeptEntryId, isSplitTurn } =
  16. preparation;
  17. // Verify types
  18. expect(Array.isArray(messagesToSummarize)).toBe(true);
  19. expect(Array.isArray(turnPrefixMessages)).toBe(true);
  20. expect(typeof isSplitTurn).toBe("boolean");
  21. expect(typeof tokensBefore).toBe("number");
  22. expect(typeof sessionManager.getEntries).toBe("function");
  23. expect(typeof modelRegistry.getApiKeyAndHeaders).toBe("function");
  24. expect(typeof firstKeptEntryId).toBe("string");
  25. expect(Array.isArray(branchEntries)).toBe(true);
  26. const summary = messagesToSummarize
  27. .filter((m) => m.role === "user")
  28. .map((m) => `- ${typeof m.content === "string" ? m.content.slice(0, 100) : "[complex]"}`)
  29. .join("\n");
  30. // Extensions return compaction content - SessionManager adds id/parentId
  31. return {
  32. compaction: {
  33. summary: `User requests:\n${summary}`,
  34. firstKeptEntryId,
  35. tokensBefore,
  36. },
  37. };
  38. });
  39. };
  40. // Just verify the function exists and is callable
  41. expect(typeof exampleExtension).toBe("function");
  42. });
  43. it("compact event should have correct fields", () => {
  44. const checkCompactEvent = (pi: ExtensionAPI) => {
  45. pi.on("session_compact", async (event: SessionCompactEvent) => {
  46. // These should all be accessible
  47. const entry = event.compactionEntry;
  48. const fromExtension = event.fromExtension;
  49. expect(entry.type).toBe("compaction");
  50. expect(typeof entry.summary).toBe("string");
  51. expect(typeof entry.tokensBefore).toBe("number");
  52. expect(typeof fromExtension).toBe("boolean");
  53. });
  54. };
  55. expect(typeof checkCompactEvent).toBe("function");
  56. });
  57. });