runtime-services-composition.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import type { GlobalEventInput } from "@mavis/shared/global-events";
  2. import type {
  3. V1ServiceCompatibility,
  4. V1ServiceFactory,
  5. } from "../../compat/v1/runtime.js";
  6. import type { LocalAgentService } from "../../service/agent/index.js";
  7. import type { PluginServiceLogger } from "../../service/plugin-system/index.js";
  8. import {
  9. initializeWorkspaceSystem,
  10. type WorkspaceSystem,
  11. } from "../../service/workspace/index.js";
  12. import type { ProductionSessionComposition } from "./runtime-session-composition.js";
  13. export function resolveServicesCompatibility(
  14. compatibility: V1ServiceCompatibility | V1ServiceFactory,
  15. ): V1ServiceCompatibility {
  16. return "create" in compatibility ? compatibility.create() : compatibility;
  17. }
  18. export function createRuntimeAgentReferenceProjection(
  19. compatibility: V1ServiceCompatibility,
  20. agentService: LocalAgentService,
  21. logger: PluginServiceLogger,
  22. ): NonNullable<
  23. ProductionSessionComposition["product"]["inputPreparation"]["agentReferenceProjection"]
  24. > {
  25. return {
  26. resolveAgentReference: async (requestRef) => {
  27. const resolution =
  28. await compatibility.agentReferences.resolveDelegatable(requestRef);
  29. if (resolution !== "authorized") return resolution;
  30. try {
  31. const trustedDisplayName = (
  32. await agentService.get(requestRef)
  33. ).displayName.trim();
  34. return trustedDisplayName
  35. ? { status: "authorized" as const, trustedDisplayName }
  36. : { status: "authorized" as const };
  37. } catch {
  38. return { status: "authorized" as const };
  39. }
  40. },
  41. emitDiagnostic: (event) =>
  42. logger.info(
  43. {
  44. stage: event.stage,
  45. outcome: event.outcome,
  46. count: event.count,
  47. surface: event.surface,
  48. runtime: event.runtime,
  49. ...(event.errorCode ? { errorCode: event.errorCode } : {}),
  50. },
  51. "agent_reference",
  52. ),
  53. };
  54. }
  55. export function createServiceWorkspace(
  56. writeGlobalEvent: (event: GlobalEventInput) => void,
  57. ): WorkspaceSystem {
  58. return initializeWorkspaceSystem({
  59. git: {
  60. publishChanged: (payload) =>
  61. writeGlobalEvent({ type: "workspace.git.changed", payload }),
  62. },
  63. });
  64. }