case-usage-anchor-provider.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { chatCompletion, contextExceededError } from "./scripted-provider.mjs";
  2. export const FULL_DONE = "USAGE_FULL_COMPACT_DONE";
  3. export const REACTIVE_DONE = "USAGE_REACTIVE_COMPACT_DONE";
  4. export const INPUT_ONLY_DONE = "USAGE_INPUT_ONLY_DONE";
  5. export const HYDRATION_SETUP = "USAGE_HYDRATION_SETUP";
  6. export const HYDRATION_DONE = "USAGE_HYDRATION_DONE";
  7. export const REWIND_KEPT = "USAGE_REWIND_KEPT";
  8. export const REWIND_REMOVED = "USAGE_REWIND_REMOVED";
  9. const HIGH_INPUT_TOKENS = 10_000;
  10. const SUMMARY_USAGE_INPUT_TOKENS = 15_000;
  11. const REWIND_REMOVED_USAGE_INPUT_TOKENS = 19_000;
  12. export function createScenarioHandler(scenario) {
  13. let mainRequestCount = 0;
  14. let compactRequestCount = 0;
  15. let overflowSent = false;
  16. return async ({ body }) => {
  17. if (isCompactRequest(body)) {
  18. compactRequestCount += 1;
  19. return {
  20. body: chatCompletion({
  21. content: `Summary for ${scenario} compact ${compactRequestCount}.`,
  22. completionTokens: 10,
  23. model: body?.model,
  24. promptTokens:
  25. scenario === "full" || scenario === "reactive" ? SUMMARY_USAGE_INPUT_TOKENS : 50,
  26. }),
  27. };
  28. }
  29. mainRequestCount += 1;
  30. if (scenario === "reactive" && mainRequestCount === 3 && !overflowSent) {
  31. overflowSent = true;
  32. return { body: contextExceededError(), status: 400 };
  33. }
  34. if (scenario === "hydration" && mainRequestCount === 2) {
  35. return {
  36. body: chatCompletion({
  37. content: "",
  38. model: body?.model,
  39. promptTokens: SUMMARY_USAGE_INPUT_TOKENS,
  40. }),
  41. };
  42. }
  43. if (scenario === "input-only" && mainRequestCount === 1) {
  44. return {
  45. body: chatCompletion({
  46. content: "USAGE_INPUT_ONLY_SETUP",
  47. model: body?.model,
  48. usage: { prompt_tokens: HIGH_INPUT_TOKENS },
  49. }),
  50. };
  51. }
  52. if (scenario === "reactive" && mainRequestCount >= 4) {
  53. return {
  54. body: chatCompletion({
  55. content: REACTIVE_DONE,
  56. model: body?.model,
  57. promptTokens: HIGH_INPUT_TOKENS,
  58. usage: {},
  59. }),
  60. };
  61. }
  62. if (scenario === "rewind" && mainRequestCount >= 3) {
  63. return {
  64. body: chatCompletion({
  65. content: REWIND_KEPT,
  66. model: body?.model,
  67. usage: {},
  68. }),
  69. };
  70. }
  71. const content =
  72. scenario === "full"
  73. ? mainRequestCount >= 3
  74. ? FULL_DONE
  75. : `USAGE_FULL_SETUP_${mainRequestCount}`
  76. : scenario === "reactive"
  77. ? mainRequestCount >= 4
  78. ? REACTIVE_DONE
  79. : `USAGE_REACTIVE_SETUP_${mainRequestCount}`
  80. : scenario === "input-only"
  81. ? INPUT_ONLY_DONE
  82. : scenario === "hydration"
  83. ? mainRequestCount === 1
  84. ? HYDRATION_SETUP
  85. : HYDRATION_DONE
  86. : mainRequestCount === 1
  87. ? REWIND_KEPT
  88. : mainRequestCount === 2
  89. ? REWIND_REMOVED
  90. : REWIND_KEPT;
  91. const usageInputTokens =
  92. scenario === "rewind" && mainRequestCount === 2
  93. ? REWIND_REMOVED_USAGE_INPUT_TOKENS
  94. : HIGH_INPUT_TOKENS;
  95. return {
  96. body: chatCompletion({
  97. content,
  98. completionTokens: 200,
  99. model: body?.model,
  100. promptTokens: usageInputTokens,
  101. }),
  102. };
  103. };
  104. }
  105. export function promptsForScenario(scenario) {
  106. switch (scenario) {
  107. case "full":
  108. return [
  109. "Usage full compact setup A.",
  110. "Usage full compact setup B.",
  111. "/compact Keep only the full compact marker.",
  112. ];
  113. case "reactive":
  114. return [
  115. "Usage reactive setup A.",
  116. "Usage reactive setup B.",
  117. `Trigger reactive compact and finish with ${REACTIVE_DONE}.`,
  118. ];
  119. case "input-only":
  120. return ["Create an input-only usage anchor.", `Finish with ${INPUT_ONLY_DONE}.`];
  121. case "hydration":
  122. return [
  123. "Create a non-empty hydration usage anchor.",
  124. "Return an empty assistant response with usage.",
  125. ];
  126. case "rewind":
  127. return ["Create the kept rewind branch.", "Create the branch that will be rewound."];
  128. default:
  129. throw new Error(`Unknown usage anchor scenario: ${scenario}`);
  130. }
  131. }
  132. export function isCompactRequest(body) {
  133. const userContents = (body?.messages ?? [])
  134. .filter((message) => message?.role === "user" && typeof message?.content === "string")
  135. .map((message) => message.content);
  136. return (
  137. userContents.some((content) => content.includes("Your task is to create a detailed summary")) &&
  138. !userContents.some((content) =>
  139. content.includes("This session is being continued from a previous conversation"),
  140. )
  141. );
  142. }