query-application.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import type {
  2. SessionLookupInput as GetSessionReq,
  3. SessionLookupResult as GetSessionResp,
  4. SessionTreeInput as GetSessionTreeReq,
  5. SessionTreePage as GetSessionTreeResp,
  6. ListSessionsInput as ListSessionsReq,
  7. SessionPage as ListSessionsResp,
  8. SearchSessionsInput as SearchSessionsReq,
  9. SessionPage as SearchSessionsResp,
  10. } from "./query-contract.js";
  11. import {
  12. SessionQueryServiceError,
  13. type SessionQueryService,
  14. } from "../../service/session-system/index.js";
  15. import type { ApplicationContext } from "../context.js";
  16. import { AppError } from "../errors.js";
  17. import { toSessionInfoView, toSessionTreeChildView } from "./wire.js";
  18. export interface SessionQueryApplicationOptions {
  19. readonly service: Pick<
  20. SessionQueryService,
  21. "list" | "search" | "tree" | "get"
  22. >;
  23. }
  24. /** Local Session query; parameters come from the service contract and results are built by local view converters. */
  25. export class SessionQueryApplication {
  26. constructor(private readonly options: SessionQueryApplicationOptions) {}
  27. async listSessions(
  28. _context: ApplicationContext,
  29. req: ListSessionsReq,
  30. ): Promise<ListSessionsResp> {
  31. const page = await this.invoke(() =>
  32. this.options.service.list({
  33. agentName: req.name,
  34. includeArchived: req.includeArchived,
  35. onlyArchived: req.onlyArchived,
  36. onlyCompressed: req.onlyCompressed,
  37. includeHidden: req.includeHidden,
  38. includePurposePrefix: req.includePurposePrefix,
  39. excludePurposePrefix: req.excludePurposePrefix,
  40. limit: req.limit,
  41. offset: req.offset,
  42. cursor: req.cursor,
  43. }),
  44. );
  45. return {
  46. sessions: page.sessions.map(toSessionInfoView),
  47. hasMore: page.hasMore,
  48. ...(page.nextCursor ? { nextCursor: page.nextCursor } : {}),
  49. };
  50. }
  51. async searchSessions(
  52. _context: ApplicationContext,
  53. req: SearchSessionsReq,
  54. ): Promise<SearchSessionsResp> {
  55. const page = await this.invoke(() =>
  56. this.options.service.search({
  57. keyword: req.keyword,
  58. agentName: req.agentName,
  59. limit: req.limit,
  60. cursor: req.cursor,
  61. }),
  62. );
  63. return {
  64. sessions: page.sessions.map(toSessionInfoView),
  65. hasMore: page.hasMore,
  66. ...(page.nextCursor ? { nextCursor: page.nextCursor } : {}),
  67. };
  68. }
  69. async getSessionTree(
  70. _context: ApplicationContext,
  71. req: GetSessionTreeReq,
  72. ): Promise<GetSessionTreeResp> {
  73. const page = await this.invoke(() =>
  74. this.options.service.tree({
  75. agentName: req.name,
  76. includeArchived: req.includeArchived,
  77. onlyArchived: req.onlyArchived,
  78. onlyCompressed: req.onlyCompressed,
  79. includeHidden: req.includeHidden,
  80. includePurposePrefix: req.includePurposePrefix,
  81. excludePurposePrefix: req.excludePurposePrefix,
  82. limit: req.limit,
  83. cursor: req.cursor,
  84. }),
  85. );
  86. return {
  87. sessions: page.sessions.map(({ session, children }) => ({
  88. session: toSessionInfoView(session),
  89. childSessions: children.map(toSessionTreeChildView),
  90. })),
  91. hasMore: page.hasMore,
  92. ...(page.nextCursor ? { nextCursor: page.nextCursor } : {}),
  93. };
  94. }
  95. async getSession(
  96. _context: ApplicationContext,
  97. req: GetSessionReq,
  98. ): Promise<GetSessionResp> {
  99. const session = await this.invoke(() => this.options.service.get(req.id));
  100. return { session: toSessionInfoView(session) };
  101. }
  102. private async invoke<T>(operation: () => Promise<T>): Promise<T> {
  103. try {
  104. return await operation();
  105. } catch (error) {
  106. if (error instanceof SessionQueryServiceError) {
  107. const mapping = queryFailureMapping(error.reason);
  108. throw new AppError(mapping.status, mapping.key, error.message);
  109. }
  110. throw error;
  111. }
  112. }
  113. }
  114. function queryFailureMapping(reason: SessionQueryServiceError["reason"]): {
  115. readonly status: number;
  116. readonly key: string;
  117. } {
  118. switch (reason) {
  119. case "session-not-found":
  120. return { status: 404, key: "SESSION_NOT_FOUND" };
  121. }
  122. }