@someonegg/codex-hub 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +101 -2
  3. package/dist/client/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  4. package/dist/client/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  5. package/dist/client/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  6. package/dist/client/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  7. package/dist/client/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  8. package/dist/client/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  9. package/dist/client/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  10. package/dist/client/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  11. package/dist/client/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  12. package/dist/client/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  13. package/dist/client/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  14. package/dist/client/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  15. package/dist/client/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  16. package/dist/client/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  17. package/dist/client/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  18. package/dist/client/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  19. package/dist/client/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  20. package/dist/client/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  21. package/dist/client/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  22. package/dist/client/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  23. package/dist/client/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  24. package/dist/client/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  25. package/dist/client/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  26. package/dist/client/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  27. package/dist/client/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  28. package/dist/client/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  29. package/dist/client/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  30. package/dist/client/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  31. package/dist/client/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  32. package/dist/client/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  33. package/dist/client/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  34. package/dist/client/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  35. package/dist/client/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  36. package/dist/client/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  37. package/dist/client/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  38. package/dist/client/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  39. package/dist/client/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  40. package/dist/client/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  41. package/dist/client/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  42. package/dist/client/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  43. package/dist/client/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  44. package/dist/client/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  45. package/dist/client/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  46. package/dist/client/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  47. package/dist/client/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  48. package/dist/client/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  49. package/dist/client/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  50. package/dist/client/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  51. package/dist/client/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  52. package/dist/client/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  53. package/dist/client/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  54. package/dist/client/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  55. package/dist/client/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  56. package/dist/client/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  57. package/dist/client/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  58. package/dist/client/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  59. package/dist/client/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  60. package/dist/client/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  61. package/dist/client/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  62. package/dist/client/assets/index-Dx1LBBmn.js +304 -0
  63. package/dist/client/assets/index-lmwuA3r2.css +1 -0
  64. package/dist/client/index.html +16 -0
  65. package/dist/server/server/adapters/codex/agent-identity.js +9 -0
  66. package/dist/server/server/adapters/codex/catalog-source.js +2 -0
  67. package/dist/server/server/adapters/codex/codex-session-source.js +300 -0
  68. package/dist/server/server/adapters/codex/identity-resolver.js +107 -0
  69. package/dist/server/server/adapters/codex/interaction-parser.js +90 -0
  70. package/dist/server/server/adapters/codex/internal-event-parser.js +56 -0
  71. package/dist/server/server/adapters/codex/io-errors.js +5 -0
  72. package/dist/server/server/adapters/codex/jsonl-catalog-source.js +58 -0
  73. package/dist/server/server/adapters/codex/limits.js +10 -0
  74. package/dist/server/server/adapters/codex/message-normalizer.js +121 -0
  75. package/dist/server/server/adapters/codex/path-policy.js +88 -0
  76. package/dist/server/server/adapters/codex/project-directory-resolver.js +79 -0
  77. package/dist/server/server/adapters/codex/response-item-parser.js +100 -0
  78. package/dist/server/server/adapters/codex/rollout-decoder.js +286 -0
  79. package/dist/server/server/adapters/codex/session-index-reader.js +101 -0
  80. package/dist/server/server/adapters/codex/session-manager.js +62 -0
  81. package/dist/server/server/adapters/codex/session-normalizer.js +257 -0
  82. package/dist/server/server/adapters/codex/tool-normalizer.js +58 -0
  83. package/dist/server/server/adapters/codex/user-input-normalizer.js +123 -0
  84. package/dist/server/server/api/session-api-mapper.js +194 -0
  85. package/dist/server/server/application/session-read-service.js +109 -0
  86. package/dist/server/server/application/session-reader.js +2 -0
  87. package/dist/server/server/catalog/config.js +4 -0
  88. package/dist/server/server/catalog/session-catalog-service.js +103 -0
  89. package/dist/server/server/config.js +145 -0
  90. package/dist/server/server/create-hub.js +64 -0
  91. package/dist/server/server/domain/project-directory.js +5 -0
  92. package/dist/server/server/domain/session-domain.js +2 -0
  93. package/dist/server/server/domain/session-text.js +45 -0
  94. package/dist/server/server/http/api-route-handler.js +2 -0
  95. package/dist/server/server/http/api-router.js +63 -0
  96. package/dist/server/server/http/create-server.js +148 -0
  97. package/dist/server/server/http/query-params.js +19 -0
  98. package/dist/server/server/http/request-body.js +48 -0
  99. package/dist/server/server/http/router.js +16 -0
  100. package/dist/server/server/http/security.js +19 -0
  101. package/dist/server/server/http/session-interaction-routes.js +101 -0
  102. package/dist/server/server/http/session-management-routes.js +90 -0
  103. package/dist/server/server/http/session-path.js +13 -0
  104. package/dist/server/server/http/session-read-routes.js +198 -0
  105. package/dist/server/server/http/session-updates-routes.js +84 -0
  106. package/dist/server/server/http/terminal-preview-websocket.js +179 -0
  107. package/dist/server/server/http/upload-routes.js +74 -0
  108. package/dist/server/server/interaction/interaction-service.js +247 -0
  109. package/dist/server/server/interaction/message.js +19 -0
  110. package/dist/server/server/interaction/pane-binding-manager.js +60 -0
  111. package/dist/server/server/interaction/reconcile-session-state.js +21 -0
  112. package/dist/server/server/interaction/session-runtime.js +86 -0
  113. package/dist/server/server/interaction/terminal-preview-diff.js +71 -0
  114. package/dist/server/server/interaction/terminal-preview-service.js +194 -0
  115. package/dist/server/server/interaction/tmux-launcher.js +75 -0
  116. package/dist/server/server/interaction/tmux-preview-listener.js +101 -0
  117. package/dist/server/server/interaction/tmux-service.js +269 -0
  118. package/dist/server/server/interaction/transport.js +9 -0
  119. package/dist/server/server/main.js +41 -0
  120. package/dist/server/server/management/operation-service.js +540 -0
  121. package/dist/server/server/management/session-exit-service.js +50 -0
  122. package/dist/server/server/management/session-manager.js +59 -0
  123. package/dist/server/server/repository/catalog-snapshot-store.js +304 -0
  124. package/dist/server/server/repository/diagnostic-logger.js +70 -0
  125. package/dist/server/server/repository/opaque-cursor.js +79 -0
  126. package/dist/server/server/repository/refresh-coordinator.js +14 -0
  127. package/dist/server/server/repository/session-queries.js +151 -0
  128. package/dist/server/server/repository/session-query-criteria.js +2 -0
  129. package/dist/server/server/repository/timeline-prefix-index.js +342 -0
  130. package/dist/server/server/repository/timeline-prefix-registry.js +55 -0
  131. package/dist/server/server/security/opaque-id.js +13 -0
  132. package/dist/server/server/source/session-source.js +2 -0
  133. package/dist/server/server/updates/session-updates-service.js +147 -0
  134. package/dist/server/server/updates/update-revision.js +25 -0
  135. package/dist/server/shared/api-contract.js +2 -0
  136. package/dist/server/shared/domain.js +2 -0
  137. package/dist/server/shared/interaction.js +12 -0
  138. package/dist/server/shared/uploads.js +2 -0
  139. package/package.json +79 -4
@@ -0,0 +1,257 @@
1
+ import { normalizeSessionTitle, sessionTitleFromMarkdown } from "../../domain/session-text.js";
2
+ import { codexInteraction } from "./interaction-parser.js";
3
+ import { internalDetail, internalItem, internalItemFromPayload } from "./internal-event-parser.js";
4
+ import { MAX_SESSION_DIAGNOSTICS } from "./limits.js";
5
+ import { eventMessage } from "./message-normalizer.js";
6
+ import { parseResponseItem } from "./response-item-parser.js";
7
+ import { isObject } from "./rollout-decoder.js";
8
+ import { normalizeToolCall, normalizeToolOutput, } from "./tool-normalizer.js";
9
+ import { normalizeUserInputRequest, normalizeUserInputResponse, } from "./user-input-normalizer.js";
10
+ export class DefaultSessionNormalizer {
11
+ create(descriptor) {
12
+ return {
13
+ descriptor,
14
+ timeline: [],
15
+ directiveDetails: new Map(),
16
+ internalDetails: new Map(),
17
+ toolDetails: new Map(),
18
+ pendingToolCalls: new Map(),
19
+ pendingUserInputRequests: new Map(),
20
+ decoderDiagnostics: [],
21
+ normalizerDiagnostics: [],
22
+ hasFirstUserMessage: false,
23
+ firstUserTitle: null,
24
+ messageCount: 0,
25
+ toolCount: 0,
26
+ };
27
+ }
28
+ append(state, records, decoderDiagnostics) {
29
+ const nextDecoderDiagnostics = decoderDiagnostics.slice(0, MAX_SESSION_DIAGNOSTICS);
30
+ const decoderDiagnosticsUnchanged = sameDiagnostics(state.decoderDiagnostics, nextDecoderDiagnostics);
31
+ if (records.length === 0 && decoderDiagnosticsUnchanged) {
32
+ return state;
33
+ }
34
+ const items = [];
35
+ const directiveDetails = new Map(state.directiveDetails);
36
+ const internalDetails = new Map(state.internalDetails);
37
+ const toolDetails = new Map(state.toolDetails);
38
+ const pendingToolCalls = new Map(state.pendingToolCalls);
39
+ const pendingUserInputRequests = new Map(state.pendingUserInputRequests);
40
+ const normalizerDiagnostics = [...state.normalizerDiagnostics];
41
+ for (const record of records) {
42
+ consumeRecord(record, items, directiveDetails, internalDetails, toolDetails, pendingToolCalls, pendingUserInputRequests, normalizerDiagnostics);
43
+ }
44
+ let hasFirstUserMessage = state.hasFirstUserMessage;
45
+ let firstTitle = state.firstUserTitle;
46
+ let messageCount = state.messageCount;
47
+ let toolCount = state.toolCount;
48
+ for (const item of items) {
49
+ if (item.kind === "message") {
50
+ messageCount += 1;
51
+ if (!hasFirstUserMessage && item.role === "user") {
52
+ hasFirstUserMessage = true;
53
+ firstTitle = sessionTitleFromMarkdown(item.markdown);
54
+ }
55
+ }
56
+ else if (item.kind === "tool" && item.stage === "call") {
57
+ toolCount += 1;
58
+ }
59
+ }
60
+ return {
61
+ ...state,
62
+ timeline: items.length === 0 ? state.timeline : [...state.timeline, ...items],
63
+ directiveDetails: sameMapEntries(state.directiveDetails, directiveDetails)
64
+ ? state.directiveDetails
65
+ : directiveDetails,
66
+ internalDetails: sameMapEntries(state.internalDetails, internalDetails)
67
+ ? state.internalDetails
68
+ : internalDetails,
69
+ toolDetails: sameMapEntries(state.toolDetails, toolDetails) ? state.toolDetails : toolDetails,
70
+ pendingToolCalls,
71
+ pendingUserInputRequests,
72
+ decoderDiagnostics: decoderDiagnosticsUnchanged
73
+ ? state.decoderDiagnostics
74
+ : nextDecoderDiagnostics,
75
+ normalizerDiagnostics,
76
+ hasFirstUserMessage,
77
+ firstUserTitle: firstTitle,
78
+ messageCount,
79
+ toolCount,
80
+ };
81
+ }
82
+ materialize(state, metadata, origin = DEFAULT_SESSION_ORIGIN) {
83
+ const diagnostics = combinedDiagnostics(state);
84
+ const warningCount = diagnostics.filter((diagnostic) => diagnostic.severity !== "info").length;
85
+ const title = normalizeSessionTitle(metadata.title) ?? state.firstUserTitle ?? "Untitled session";
86
+ const session = {
87
+ id: state.descriptor.id,
88
+ sourceId: metadata.threadId,
89
+ origin,
90
+ title,
91
+ workingDirectory: metadata.workingDirectory,
92
+ projectDirectory: metadata.projectDirectory,
93
+ createdAt: metadata.createdAt,
94
+ updatedAt: metadata.updatedAt,
95
+ archived: metadata.archived,
96
+ parentId: metadata.parentThreadId,
97
+ childIds: [],
98
+ agent: metadata.agent ?? null,
99
+ messageCount: state.messageCount,
100
+ toolCount: state.toolCount,
101
+ warningCount,
102
+ diagnostics,
103
+ itemCount: state.timeline.length,
104
+ };
105
+ return {
106
+ session,
107
+ timeline: state.timeline,
108
+ toolDetails: state.toolDetails,
109
+ directiveDetails: state.directiveDetails,
110
+ internalDetails: state.internalDetails,
111
+ interaction: codexInteraction(),
112
+ };
113
+ }
114
+ normalize(decoded, metadata, origin = DEFAULT_SESSION_ORIGIN) {
115
+ const state = this.append(this.create(decoded.descriptor), decoded.records, decoded.diagnostics);
116
+ return this.materialize(state, metadata, origin);
117
+ }
118
+ }
119
+ function combinedDiagnostics(state) {
120
+ const remaining = MAX_SESSION_DIAGNOSTICS - state.decoderDiagnostics.length;
121
+ return [...state.decoderDiagnostics, ...state.normalizerDiagnostics.slice(0, remaining)];
122
+ }
123
+ function sameDiagnostics(left, right) {
124
+ return (left.length === right.length &&
125
+ left.every((item, index) => {
126
+ const candidate = right[index];
127
+ return (candidate !== undefined &&
128
+ item.code === candidate.code &&
129
+ item.severity === candidate.severity &&
130
+ item.message === candidate.message &&
131
+ item.ordinal === candidate.ordinal);
132
+ }));
133
+ }
134
+ function sameMapEntries(left, right) {
135
+ if (left.size !== right.size)
136
+ return false;
137
+ for (const [key, value] of right) {
138
+ if (left.get(key) !== value)
139
+ return false;
140
+ }
141
+ return true;
142
+ }
143
+ const DEFAULT_SESSION_ORIGIN = {
144
+ sourceType: "test",
145
+ sourceInstanceId: "test",
146
+ agentName: "Test",
147
+ agentVersion: null,
148
+ formatVersion: null,
149
+ };
150
+ function consumeRecord(record, items, directiveDetails, internalDetails, toolDetails, pendingToolCalls, pendingUserInputRequests, diagnostics) {
151
+ const timestamp = string(record.value.timestamp);
152
+ const payload = record.value.payload;
153
+ if (record.value.type === "response_item" && isObject(payload)) {
154
+ consumeParsedResponse(parseResponseItem(record.ordinal, timestamp, payload), items, directiveDetails, toolDetails, pendingToolCalls, pendingUserInputRequests, diagnostics);
155
+ const item = items.at(-1);
156
+ if (item?.kind === "internal" && item.ordinal === record.ordinal) {
157
+ addInternalDetail(item, record, internalDetails);
158
+ }
159
+ return;
160
+ }
161
+ if (record.value.type === "event_msg" && isObject(payload)) {
162
+ const message = eventMessage(record.ordinal, timestamp, payload);
163
+ if (message !== null)
164
+ items.push(message);
165
+ else {
166
+ const item = internalItemFromPayload(record.ordinal, timestamp, payload);
167
+ items.push(item);
168
+ if (item.kind === "internal") {
169
+ addInternalDetail(item, record, internalDetails);
170
+ }
171
+ }
172
+ return;
173
+ }
174
+ if (record.value.type === "session_meta")
175
+ return;
176
+ if (record.value.type === "turn_context") {
177
+ const item = internalItem(record.ordinal, timestamp, "turn_context");
178
+ items.push(item);
179
+ addInternalDetail(item, record, internalDetails);
180
+ return;
181
+ }
182
+ const eventType = string(record.value.type);
183
+ if (eventType !== null) {
184
+ const item = internalItem(record.ordinal, timestamp, eventType);
185
+ items.push(item);
186
+ addInternalDetail(item, record, internalDetails);
187
+ return;
188
+ }
189
+ appendDiagnostic(diagnostics, {
190
+ code: "unknown_record",
191
+ severity: "info",
192
+ message: "A record without a recognized type was reduced to a safe diagnostic.",
193
+ ordinal: record.ordinal,
194
+ });
195
+ }
196
+ function addInternalDetail(item, record, details) {
197
+ const detail = internalDetail(record.value);
198
+ details.set(item.id, detail);
199
+ }
200
+ function appendDiagnostic(diagnostics, diagnostic) {
201
+ if (diagnostics.length < MAX_SESSION_DIAGNOSTICS) {
202
+ diagnostics.push(diagnostic);
203
+ }
204
+ }
205
+ function consumeParsedResponse(parsed, items, directiveDetails, toolDetails, pendingToolCalls, pendingUserInputRequests, diagnostics) {
206
+ switch (parsed.kind) {
207
+ case "directive":
208
+ items.push(parsed.value.item);
209
+ if (parsed.value.detail !== null) {
210
+ directiveDetails.set(parsed.value.item.id, parsed.value.detail);
211
+ }
212
+ break;
213
+ case "timeline":
214
+ items.push(parsed.value);
215
+ break;
216
+ case "tool_call":
217
+ pendingToolCalls.set(parsed.value.callId, parsed.value);
218
+ addTool(normalizeToolCall(parsed.value), items, toolDetails);
219
+ break;
220
+ case "tool_output": {
221
+ if (pendingUserInputRequests.delete(parsed.value.callId)) {
222
+ addUserInput(normalizeUserInputResponse(parsed.value), items, diagnostics);
223
+ }
224
+ else {
225
+ const call = pendingToolCalls.get(parsed.value.callId);
226
+ pendingToolCalls.delete(parsed.value.callId);
227
+ addTool(normalizeToolOutput(parsed.value, call), items, toolDetails);
228
+ }
229
+ break;
230
+ }
231
+ case "user_input_request":
232
+ pendingUserInputRequests.set(parsed.value.callId, parsed.value);
233
+ addUserInput(normalizeUserInputRequest(parsed.value), items, diagnostics);
234
+ break;
235
+ case "ignored":
236
+ break;
237
+ }
238
+ }
239
+ function addUserInput(accumulated, items, diagnostics) {
240
+ items.push(accumulated.item);
241
+ if (accumulated.malformed) {
242
+ appendDiagnostic(diagnostics, {
243
+ code: "invalid_user_input",
244
+ severity: "warning",
245
+ message: "A request_user_input call or output had an unsupported payload shape.",
246
+ ordinal: accumulated.item.ordinal,
247
+ });
248
+ }
249
+ }
250
+ function addTool(tool, items, details) {
251
+ items.push(tool.item);
252
+ details.set(tool.item.id, tool.detail);
253
+ }
254
+ function string(value) {
255
+ return typeof value === "string" && value.length > 0 ? value : null;
256
+ }
257
+ //# sourceMappingURL=session-normalizer.js.map
@@ -0,0 +1,58 @@
1
+ import { MAX_PREVIEW_CHARS, truncateText } from "../../domain/session-text.js";
2
+ import { MAX_TOOL_DETAIL_CHARS } from "./limits.js";
3
+ export function normalizeToolCall(call) {
4
+ const input = truncateNullable(call.input);
5
+ const preview = previewText(call.input);
6
+ const detailTruncated = input.truncated;
7
+ return {
8
+ item: {
9
+ kind: "tool",
10
+ stage: "call",
11
+ id: `tool-${call.ordinal}`,
12
+ ordinal: call.ordinal,
13
+ timestamp: call.timestamp,
14
+ callId: call.callId,
15
+ toolName: call.toolName,
16
+ preview: preview.text,
17
+ charCount: call.input?.length ?? 0,
18
+ hasDetail: call.input !== null,
19
+ },
20
+ detail: { input: input.text, output: null, truncated: detailTruncated },
21
+ };
22
+ }
23
+ export function normalizeToolOutput(output, call) {
24
+ const input = truncateNullable(call?.input ?? null);
25
+ const result = truncateNullable(output.output);
26
+ const preview = previewText(output.output !== null && output.output.length > 0 ? output.output : (call?.input ?? null));
27
+ const detailTruncated = input.truncated || result.truncated;
28
+ return {
29
+ item: {
30
+ kind: "tool",
31
+ stage: "output",
32
+ id: `tool-${output.ordinal}`,
33
+ ordinal: output.ordinal,
34
+ timestamp: output.timestamp,
35
+ callId: output.callId,
36
+ toolName: call?.toolName ?? "unknown tool",
37
+ status: output.failed ? "failed" : "completed",
38
+ preview: preview.text,
39
+ charCount: (call?.input?.length ?? 0) + (output.output?.length ?? 0),
40
+ hasDetail: (call?.input !== null && call?.input !== undefined) || output.output !== null,
41
+ },
42
+ detail: {
43
+ input: input.text,
44
+ output: result.text,
45
+ truncated: detailTruncated,
46
+ },
47
+ };
48
+ }
49
+ function previewText(value) {
50
+ return value === null ? { text: null, truncated: false } : truncateText(value, MAX_PREVIEW_CHARS);
51
+ }
52
+ function truncateNullable(value) {
53
+ if (value === null)
54
+ return { text: null, truncated: false };
55
+ const result = truncateText(value, MAX_TOOL_DETAIL_CHARS);
56
+ return { text: result.text, truncated: result.truncated };
57
+ }
58
+ //# sourceMappingURL=tool-normalizer.js.map
@@ -0,0 +1,123 @@
1
+ import { MAX_PREVIEW_CHARS, truncateText } from "../../domain/session-text.js";
2
+ import { isObject } from "./rollout-decoder.js";
3
+ export function normalizeUserInputRequest(request) {
4
+ return {
5
+ item: {
6
+ kind: "user_input",
7
+ stage: "request",
8
+ id: `user-input-${request.ordinal}`,
9
+ ordinal: request.ordinal,
10
+ timestamp: request.timestamp,
11
+ callId: request.callId,
12
+ questions: request.questions,
13
+ },
14
+ malformed: request.malformed,
15
+ };
16
+ }
17
+ export function normalizeUserInputResponse(output) {
18
+ const parsed = parseResponse(output.output);
19
+ const base = {
20
+ kind: "user_input",
21
+ stage: "response",
22
+ id: `user-input-${output.ordinal}`,
23
+ ordinal: output.ordinal,
24
+ timestamp: output.timestamp,
25
+ callId: output.callId,
26
+ };
27
+ if (parsed.kind === "answered") {
28
+ return {
29
+ item: { ...base, outcome: "answered", answers: parsed.answers },
30
+ malformed: false,
31
+ };
32
+ }
33
+ if (parsed.kind === "aborted") {
34
+ return {
35
+ item: { ...base, outcome: "aborted" },
36
+ malformed: false,
37
+ };
38
+ }
39
+ return {
40
+ item: {
41
+ ...base,
42
+ outcome: "unavailable",
43
+ summary: truncateText(output.output ?? "User input response was unavailable.", MAX_PREVIEW_CHARS).text,
44
+ },
45
+ malformed: true,
46
+ };
47
+ }
48
+ export function parseUserInputQuestions(value) {
49
+ const object = parseObject(value);
50
+ if (object === null || !Array.isArray(object.questions))
51
+ return null;
52
+ if (object.questions.length < 1 || object.questions.length > 3)
53
+ return null;
54
+ const questions = object.questions.map(parseQuestion);
55
+ if (!questions.every((question) => question !== null))
56
+ return null;
57
+ const parsed = questions;
58
+ return new Set(parsed.map((question) => question.id)).size === parsed.length ? parsed : null;
59
+ }
60
+ function parseQuestion(value) {
61
+ if (!isObject(value) || !Array.isArray(value.options))
62
+ return null;
63
+ if (value.options.length < 2 || value.options.length > 3)
64
+ return null;
65
+ const id = string(value.id);
66
+ const header = string(value.header);
67
+ const question = string(value.question);
68
+ const options = value.options.map((option) => {
69
+ if (!isObject(option))
70
+ return null;
71
+ const label = string(option.label);
72
+ const description = string(option.description);
73
+ return label === null || description === null ? null : { label, description };
74
+ });
75
+ if (id === null ||
76
+ header === null ||
77
+ question === null ||
78
+ !options.every((option) => option !== null))
79
+ return null;
80
+ const parsedOptions = options;
81
+ if (new Set(parsedOptions.map((option) => option.label)).size !== parsedOptions.length) {
82
+ return null;
83
+ }
84
+ return { id, header, question, options: parsedOptions };
85
+ }
86
+ function parseResponse(value) {
87
+ if (value === null)
88
+ return { kind: "unavailable" };
89
+ if (value.trim().toLocaleLowerCase("en-US").startsWith("aborted by user")) {
90
+ return { kind: "aborted" };
91
+ }
92
+ const object = parseObject(value);
93
+ if (object === null || !isObject(object.answers))
94
+ return { kind: "unavailable" };
95
+ const answers = [];
96
+ for (const [questionId, rawAnswer] of Object.entries(object.answers)) {
97
+ if (!isObject(rawAnswer) || !Array.isArray(rawAnswer.answers)) {
98
+ return { kind: "unavailable" };
99
+ }
100
+ const values = rawAnswer.answers.filter((answer) => typeof answer === "string");
101
+ if (values.length !== rawAnswer.answers.length)
102
+ return { kind: "unavailable" };
103
+ answers.push({ questionId, answers: values });
104
+ }
105
+ return { kind: "answered", answers };
106
+ }
107
+ function parseObject(value) {
108
+ if (isObject(value))
109
+ return value;
110
+ if (typeof value !== "string")
111
+ return null;
112
+ try {
113
+ const parsed = JSON.parse(value);
114
+ return isObject(parsed) ? parsed : null;
115
+ }
116
+ catch {
117
+ return null;
118
+ }
119
+ }
120
+ function string(value) {
121
+ return typeof value === "string" && value.length > 0 ? value : null;
122
+ }
123
+ //# sourceMappingURL=user-input-normalizer.js.map
@@ -0,0 +1,194 @@
1
+ import { createHash } from "node:crypto";
2
+ import { opaqueIdForParts } from "../security/opaque-id.js";
3
+ export class SessionApiMapper {
4
+ resolveNickname;
5
+ homes;
6
+ constructor(resolveNickname = () => null, homes = []) {
7
+ this.resolveNickname = resolveNickname;
8
+ this.homes = homes;
9
+ }
10
+ list(sessions) {
11
+ const projects = new Map();
12
+ const summaries = sessions
13
+ .map((session) => {
14
+ const directory = session.projectDirectory;
15
+ const projectId = directory === null ? null : opaqueIdForParts("project", directory);
16
+ const isChild = session.isChild ?? session.parentId !== null;
17
+ if (!isChild && directory !== null && projectId !== null)
18
+ projects.set(projectId, { id: projectId, directory });
19
+ return {
20
+ id: session.id,
21
+ sourceInstanceId: session.origin.sourceInstanceId,
22
+ title: this.resolveNickname(session) ?? session.title,
23
+ projectId,
24
+ status: session.archived ? "archived" : "normal",
25
+ parentId: session.parentId,
26
+ isChild,
27
+ updatedAt: session.updatedAt,
28
+ };
29
+ })
30
+ .sort((a, b) => compareIds(a.id, b.id));
31
+ const structure = {
32
+ homes: [...this.homes].sort((a, b) => compareIds(a.sourceInstanceId, b.sourceInstanceId)),
33
+ projects: [...projects.values()].sort((a, b) => compareIds(a.id, b.id)),
34
+ sessions: summaries.map(({ updatedAt, ...summary }) => summary),
35
+ };
36
+ return {
37
+ revision: createHash("sha256").update(JSON.stringify(structure)).digest("base64url"),
38
+ homes: structure.homes,
39
+ projects: structure.projects,
40
+ sessions: summaries,
41
+ };
42
+ }
43
+ detail(result) {
44
+ return { session: this.sessionDetail(result) };
45
+ }
46
+ itemPage(result) {
47
+ return {
48
+ session: this.sessionDetail(result.context.session),
49
+ cursor: result.context.cursor,
50
+ hasMore: result.context.hasMore,
51
+ items: result.items.map((item) => this.timelineItem(item)),
52
+ };
53
+ }
54
+ toolDetail(result) {
55
+ return {
56
+ input: result.detail.input,
57
+ output: result.detail.output,
58
+ truncated: result.detail.truncated,
59
+ };
60
+ }
61
+ directiveDetail(result) {
62
+ return {
63
+ text: result.detail.text,
64
+ truncated: result.detail.truncated,
65
+ };
66
+ }
67
+ internalDetail(result) {
68
+ return {
69
+ json: result.detail.json,
70
+ truncated: result.detail.truncated,
71
+ };
72
+ }
73
+ sessionDetail(session) {
74
+ return {
75
+ resume: { allowed: false, reason: "Runtime validation unavailable" },
76
+ id: session.id,
77
+ origin: { ...session.origin },
78
+ title: session.title,
79
+ nickname: this.resolveNickname(session),
80
+ workingDirectory: session.workingDirectory,
81
+ projectDirectory: session.projectDirectory,
82
+ createdAt: session.createdAt,
83
+ updatedAt: session.updatedAt,
84
+ status: session.archived ? "archived" : "normal",
85
+ parentId: session.parentId,
86
+ childIds: [...session.childIds],
87
+ agent: session.agent === null ? null : { ...session.agent },
88
+ messageCount: session.messageCount,
89
+ toolCount: session.toolCount,
90
+ sourceId: session.sourceId,
91
+ itemCount: session.itemCount,
92
+ };
93
+ }
94
+ timelineItem(item) {
95
+ const base = { id: item.id, ordinal: item.ordinal, timestamp: item.timestamp };
96
+ switch (item.kind) {
97
+ case "message":
98
+ return {
99
+ ...base,
100
+ kind: "message",
101
+ role: item.role,
102
+ phase: item.phase,
103
+ itemType: item.itemType,
104
+ markdown: item.markdown,
105
+ };
106
+ case "directive":
107
+ return item.hasDetail
108
+ ? {
109
+ ...base,
110
+ kind: "directive",
111
+ hasDetail: true,
112
+ summary: item.summary,
113
+ charCount: item.charCount,
114
+ }
115
+ : { ...base, kind: "directive", hasDetail: false, text: item.text };
116
+ case "tool": {
117
+ const tool = {
118
+ ...base,
119
+ kind: "tool",
120
+ callId: item.callId,
121
+ toolName: item.toolName,
122
+ preview: item.preview,
123
+ charCount: item.charCount,
124
+ hasDetail: item.hasDetail,
125
+ };
126
+ return item.stage === "call"
127
+ ? { ...tool, stage: "call" }
128
+ : { ...tool, stage: "output", status: item.status };
129
+ }
130
+ case "user_input":
131
+ if (item.stage === "request") {
132
+ return {
133
+ ...base,
134
+ kind: "user_input",
135
+ stage: "request",
136
+ callId: item.callId,
137
+ questions: item.questions.map((question) => ({
138
+ id: question.id,
139
+ header: question.header,
140
+ question: question.question,
141
+ options: question.options.map((option) => ({
142
+ label: option.label,
143
+ description: option.description,
144
+ })),
145
+ })),
146
+ };
147
+ }
148
+ if (item.outcome === "answered") {
149
+ return {
150
+ ...base,
151
+ kind: "user_input",
152
+ stage: "response",
153
+ callId: item.callId,
154
+ outcome: "answered",
155
+ answers: item.answers.map((answer) => ({
156
+ questionId: answer.questionId,
157
+ answers: [...answer.answers],
158
+ })),
159
+ };
160
+ }
161
+ return item.outcome === "unavailable"
162
+ ? {
163
+ ...base,
164
+ kind: "user_input",
165
+ stage: "response",
166
+ callId: item.callId,
167
+ outcome: "unavailable",
168
+ summary: item.summary,
169
+ }
170
+ : {
171
+ ...base,
172
+ kind: "user_input",
173
+ stage: "response",
174
+ callId: item.callId,
175
+ outcome: "aborted",
176
+ };
177
+ case "token":
178
+ return {
179
+ ...base,
180
+ kind: "token",
181
+ tokenUsage: {
182
+ total: item.tokenUsage.total === null ? null : { ...item.tokenUsage.total },
183
+ last: item.tokenUsage.last === null ? null : { ...item.tokenUsage.last },
184
+ },
185
+ };
186
+ case "internal":
187
+ return { ...base, kind: "internal", eventType: item.eventType };
188
+ }
189
+ }
190
+ }
191
+ function compareIds(a, b) {
192
+ return a < b ? -1 : a > b ? 1 : 0;
193
+ }
194
+ //# sourceMappingURL=session-api-mapper.js.map