zcode-protocol-fixture.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { zcodeProtocolMethods, type ZCodeProtocolMessage } from "@zcode/shared";
  2. import { type ExecutionShellSelection } from "@zcode/contracts";
  3. import { ZCodeProtocolAgentServer } from "../src/zcode-protocol/server.js";
  4. import type { ZCodeApp, ZCodeAppOptions } from "../src/app/types.js";
  5. import { answerReplyLanguageRequest } from "./helpers/fake-host-requests.js";
  6. import { createFakeApp } from "./helpers/fake-zcode-app.js";
  7. export const workspace = {
  8. workspacePath: "/workspace/app",
  9. workspaceKey: "/workspace/app",
  10. };
  11. export async function requestResult(
  12. server: ZCodeProtocolAgentServer,
  13. message: ZCodeProtocolMessage,
  14. ) {
  15. const response = await server.handleMessage(message);
  16. if (!response || !("result" in response)) {
  17. throw new Error(`Expected protocol success response: ${JSON.stringify(response)}`);
  18. }
  19. return response.result;
  20. }
  21. export function attachSessionRuntimePreferencesResponder(
  22. server: ZCodeProtocolAgentServer,
  23. resolvePreferences: (scope: "runtime-materialization" | "user-execution") => {
  24. nativeSearchEnhancementsEnabled: boolean;
  25. toolSearchMode?: "auto" | "local" | "off";
  26. totalTokensReminderEnabled?: boolean;
  27. agentAlwaysBackgroundEnabled?: boolean;
  28. memoryEnabled?: boolean;
  29. subagentRuntimeConfigEnabled?: boolean;
  30. modelContextBudgetStrategy?: "legacy" | "preflight-v1";
  31. integratedTerminalShell?:
  32. | {
  33. mode: "auto";
  34. }
  35. | {
  36. mode: "shell";
  37. dialect: "git-bash" | "cmd";
  38. id: string;
  39. label: string;
  40. path: string;
  41. };
  42. },
  43. ): ZCodeProtocolMessage[] {
  44. const requests: ZCodeProtocolMessage[] = [];
  45. server.setNotificationSink((message) => {
  46. if (answerReplyLanguageRequest(server, message)) return;
  47. if (
  48. "id" in message &&
  49. message.method === zcodeProtocolMethods.sessionRequestRuntimePreferences
  50. ) {
  51. requests.push(message);
  52. const scope = (message.params as { scope: "runtime-materialization" | "user-execution" })
  53. .scope;
  54. void server.handleMessage({
  55. id: message.id,
  56. result: resolvePreferences(scope),
  57. });
  58. }
  59. });
  60. return requests;
  61. }
  62. export type ShellInitializationCandidate = Parameters<
  63. ZCodeApp["runtime"]["initializeSessionShellEnvironmentIfNeeded"]
  64. >[0];
  65. export function resolveShellInitializationCandidate(
  66. candidate: ShellInitializationCandidate,
  67. ): ExecutionShellSelection {
  68. return typeof candidate === "function" ? candidate() : candidate;
  69. }
  70. export function createShellRecordingFakeApp(
  71. options: ZCodeAppOptions,
  72. shellInitializations: unknown[],
  73. createdAppOptions?: ZCodeAppOptions[],
  74. ): ZCodeApp {
  75. createdAppOptions?.push(options);
  76. const app = createFakeApp(options);
  77. let effectiveShellSelection = options.runtimeConfig?.bashShellSelection;
  78. let shellInitialized = effectiveShellSelection !== undefined;
  79. let shellInitializationPromise: Promise<void> | undefined;
  80. const recordShellInitialization = (selection: ShellInitializationCandidate) => {
  81. if (shellInitialized) {
  82. return false;
  83. }
  84. const resolvedSelection = resolveShellInitializationCandidate(selection);
  85. effectiveShellSelection = resolvedSelection;
  86. shellInitialized = true;
  87. shellInitializations.push(resolvedSelection);
  88. return true;
  89. };
  90. const initializeFromStartupPreferences = async () => {
  91. if (shellInitialized) {
  92. return;
  93. }
  94. shellInitializationPromise ??= (async () => {
  95. const selection = await options.resolveInitialBashShellSelection?.();
  96. if (selection) {
  97. recordShellInitialization(selection);
  98. }
  99. })();
  100. await shellInitializationPromise;
  101. };
  102. return {
  103. ...app,
  104. resume: async (resumeOptions) => {
  105. await initializeFromStartupPreferences();
  106. return app.resume(resumeOptions);
  107. },
  108. runtime: {
  109. ...app.runtime,
  110. getSessionShellSelection: () => effectiveShellSelection,
  111. initializeSessionShellEnvironmentIfNeeded: recordShellInitialization,
  112. } as never,
  113. sendInput: async (input, sendOptions) => {
  114. await initializeFromStartupPreferences();
  115. return app.sendInput(input, sendOptions);
  116. },
  117. };
  118. }