subagent-test-store.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import type {
  2. CreateSessionInput,
  3. SessionId,
  4. SessionInfo,
  5. MessageInfo,
  6. MessagePart,
  7. ProjectId,
  8. PermissionRuleset,
  9. TodoItem,
  10. SessionStorePort,
  11. MessageWithParts,
  12. } from "@zcode/contracts";
  13. export function createRecordingSubagentSessionStore(): SessionStorePort {
  14. const sessions = new Map<SessionId, SessionInfo>();
  15. const entries = new Map<
  16. string,
  17. Parameters<NonNullable<SessionStorePort["saveSessionEntry"]>>[0]
  18. >();
  19. const messagesBySession = new Map<SessionId, MessageInfo[]>();
  20. const partsByMessage = new Map<string, MessagePart[]>();
  21. const permissionsByProject = new Map<ProjectId, PermissionRuleset>();
  22. const todosBySession = new Map<SessionId, TodoItem[]>();
  23. const store = {
  24. async saveSessionEntry(
  25. entry: Parameters<NonNullable<SessionStorePort["saveSessionEntry"]>>[0],
  26. ) {
  27. entries.set(entry.id, entry);
  28. },
  29. async sessionEntries({
  30. sessionID,
  31. type,
  32. }: Parameters<NonNullable<SessionStorePort["sessionEntries"]>>[0]) {
  33. return [...entries.values()].filter(
  34. (entry) => entry.sessionID === sessionID && (!type || entry.type === type),
  35. );
  36. },
  37. async createSession(input: CreateSessionInput): Promise<SessionInfo> {
  38. const now = Date.now();
  39. const session: SessionInfo = {
  40. id: input.id,
  41. projectID: input.projectID,
  42. workspaceID: input.workspaceID,
  43. parentID: input.parentID,
  44. traceID: input.traceID,
  45. taskType: input.taskType ?? "interactive",
  46. slug: input.slug,
  47. directory: input.directory,
  48. path: input.path,
  49. title: input.title,
  50. titleSource: input.titleSource,
  51. titleMessageID: input.titleMessageID,
  52. version: input.version,
  53. shareURL: input.shareURL,
  54. permission: input.permission,
  55. time: {
  56. created: input.time?.created ?? now,
  57. updated: input.time?.updated ?? input.time?.created ?? now,
  58. },
  59. };
  60. sessions.set(session.id, session);
  61. return session;
  62. },
  63. async updateSession(
  64. input: Parameters<SessionStorePort["updateSession"]>[0],
  65. ): Promise<SessionInfo> {
  66. const session = sessions.get(input.id);
  67. if (!session) {
  68. throw new Error(`Unknown session: ${input.id}`);
  69. }
  70. const updated: SessionInfo = {
  71. ...session,
  72. ...(input.title !== undefined ? { title: input.title } : {}),
  73. ...(input.titleSource !== undefined ? { titleSource: input.titleSource } : {}),
  74. ...(input.titleMessageID !== undefined && input.titleMessageID !== null
  75. ? { titleMessageID: input.titleMessageID }
  76. : {}),
  77. time: {
  78. ...session.time,
  79. updated: Date.now(),
  80. },
  81. };
  82. sessions.set(updated.id, updated);
  83. return updated;
  84. },
  85. async getSession(sessionID: SessionId): Promise<SessionInfo | null> {
  86. return sessions.get(sessionID) ?? null;
  87. },
  88. async listSessions(): Promise<SessionInfo[]> {
  89. return [...sessions.values()];
  90. },
  91. async saveMessage(input: MessageInfo): Promise<void> {
  92. const messages = messagesBySession.get(input.sessionID) ?? [];
  93. messages.push(input);
  94. messagesBySession.set(input.sessionID, messages);
  95. },
  96. async removeMessage(input: Parameters<SessionStorePort["removeMessage"]>[0]): Promise<void> {
  97. const messages = messagesBySession.get(input.sessionID) ?? [];
  98. messagesBySession.set(
  99. input.sessionID,
  100. messages.filter((message) => message.id !== input.messageID),
  101. );
  102. partsByMessage.delete(input.messageID);
  103. },
  104. async savePart(input: MessagePart): Promise<void> {
  105. const parts = partsByMessage.get(input.messageID) ?? [];
  106. parts.push(input);
  107. partsByMessage.set(input.messageID, parts);
  108. },
  109. async removePart(input: Parameters<SessionStorePort["removePart"]>[0]): Promise<void> {
  110. const parts = partsByMessage.get(input.messageID) ?? [];
  111. partsByMessage.set(
  112. input.messageID,
  113. parts.filter((part) => part.id !== input.partID),
  114. );
  115. },
  116. async messages(input): Promise<MessageWithParts[]> {
  117. return (messagesBySession.get(input.sessionID) ?? []).map((message) => ({
  118. info: message,
  119. parts: partsByMessage.get(message.id) ?? [],
  120. }));
  121. },
  122. async getProjectPermission(projectID: ProjectId): Promise<PermissionRuleset | null> {
  123. return permissionsByProject.get(projectID) ?? null;
  124. },
  125. async saveProjectPermission(input: {
  126. permission: PermissionRuleset;
  127. projectID: ProjectId;
  128. }): Promise<PermissionRuleset> {
  129. permissionsByProject.set(input.projectID, input.permission);
  130. return input.permission;
  131. },
  132. async readTodos(input: { sessionID: SessionId }) {
  133. return todosBySession.get(input.sessionID) ?? [];
  134. },
  135. async updateTodos(input: { sessionID: SessionId; todos: TodoItem[] }) {
  136. todosBySession.set(input.sessionID, input.todos);
  137. },
  138. };
  139. return store as unknown as SessionStorePort;
  140. }