@unieai/rabi-tool-session-query 0.2.1-rc.3 → 0.2.2-rc.5
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.
- package/lib/index.js +3 -4
- package/lib/types/workspace-access.d.ts +1 -1
- package/locale/de.json +6 -0
- package/locale/en.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +16 -12
- package/src/index.ts +136 -0
- package/src/input.ts +307 -0
- package/src/operations.ts +280 -0
- package/src/presentation.ts +255 -0
- package/src/service-boundary.ts +179 -0
- package/src/workspace-access.ts +256 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool operation orchestration over session-query service capabilities.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query/operations
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Context } from '@unieai/cordis'
|
|
8
|
+
import { HarnessError } from '@unieai/rabi-llm'
|
|
9
|
+
import { SessionSeq } from '@unieai/rabi-session'
|
|
10
|
+
import type { SessionId } from '@unieai/rabi-session'
|
|
11
|
+
import {
|
|
12
|
+
SessionQueryError,
|
|
13
|
+
type SessionEventSearchPage,
|
|
14
|
+
type SessionEventSurface,
|
|
15
|
+
type SessionRecord,
|
|
16
|
+
type SessionSearchCursor,
|
|
17
|
+
} from '@unieai/rabi-session-query'
|
|
18
|
+
import type { ToolRunContext } from '@unieai/rabi-tools'
|
|
19
|
+
import { toolInput } from './input.ts'
|
|
20
|
+
import { presentation } from './presentation.ts'
|
|
21
|
+
import { serviceBoundary } from './service-boundary.ts'
|
|
22
|
+
import { workspaceAccess } from './workspace-access.ts'
|
|
23
|
+
|
|
24
|
+
type SessionSearchArgs = Parameters<typeof toolInput.buildSessionFilters>[0]
|
|
25
|
+
|
|
26
|
+
interface EventSearchArgs {
|
|
27
|
+
session_id?: string
|
|
28
|
+
query: string
|
|
29
|
+
seq_from?: number
|
|
30
|
+
seq_to?: number
|
|
31
|
+
time_from?: string
|
|
32
|
+
time_to?: string
|
|
33
|
+
event_types?: string[]
|
|
34
|
+
surfaces?: SessionEventSurface[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface SessionTargetArgs {
|
|
38
|
+
session_id?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface EventTargetArgs extends SessionTargetArgs {
|
|
42
|
+
seq: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface EventReadArgs extends EventTargetArgs {
|
|
46
|
+
before?: number
|
|
47
|
+
after?: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface SearchCollection<T> {
|
|
51
|
+
readonly items: T[]
|
|
52
|
+
readonly capped: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function executeSessionSearch(
|
|
56
|
+
ctx: Context,
|
|
57
|
+
args: SessionSearchArgs,
|
|
58
|
+
exec: ToolRunContext,
|
|
59
|
+
maxResults: number,
|
|
60
|
+
): Promise<string> {
|
|
61
|
+
const caller = workspaceAccess.callerOf(exec, ctx)
|
|
62
|
+
const cwd = caller.header.cwd
|
|
63
|
+
if (cwd === undefined) {
|
|
64
|
+
throw new HarnessError(
|
|
65
|
+
'cross-session search is unavailable because the caller session has no workspace',
|
|
66
|
+
'SESSION_QUERY_TOOL_UNAUTHORIZED',
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
const query = toolInput.normalizeQuery(args.query)
|
|
70
|
+
const sessionFilters = toolInput.buildSessionFilters(args)
|
|
71
|
+
const eventFilters = toolInput.buildEventFilters({
|
|
72
|
+
seqFrom: args.event_seq_from,
|
|
73
|
+
seqTo: args.event_seq_to,
|
|
74
|
+
timeFrom: args.event_time_from,
|
|
75
|
+
timeTo: args.event_time_to,
|
|
76
|
+
eventTypes: args.event_types,
|
|
77
|
+
surfaces: args.event_surfaces,
|
|
78
|
+
})
|
|
79
|
+
const requestedParentIds = toolInput.materializeParentSessionIds(args.parent_session_ids)
|
|
80
|
+
if (requestedParentIds !== undefined || args.include_root_sessions === true) {
|
|
81
|
+
const authorizedParentIds = requestedParentIds === undefined
|
|
82
|
+
? new Set<SessionId>()
|
|
83
|
+
: await workspaceAccess.authorizeSessionIds(ctx, caller, requestedParentIds, exec.signal)
|
|
84
|
+
const parentValues: Array<SessionId | null> = requestedParentIds
|
|
85
|
+
?.filter(id => authorizedParentIds.has(id)) ?? []
|
|
86
|
+
if (args.include_root_sessions === true) parentValues.push(null)
|
|
87
|
+
if (parentValues.length === 0) return presentation.formatEmptySessionSearch()
|
|
88
|
+
sessionFilters.push({ kind: 'parent', values: parentValues })
|
|
89
|
+
}
|
|
90
|
+
sessionFilters.push({ kind: 'cwd', values: [cwd] })
|
|
91
|
+
const collected = await collectPages(
|
|
92
|
+
maxResults,
|
|
93
|
+
exec.signal,
|
|
94
|
+
cursor => serviceBoundary.call(ctx, exec.signal, 'session search', () =>
|
|
95
|
+
ctx.sessionQuery.searchSessions({
|
|
96
|
+
query,
|
|
97
|
+
sessionFilters,
|
|
98
|
+
eventFilters,
|
|
99
|
+
...cursor === undefined ? {} : { cursor },
|
|
100
|
+
}, { signal: exec.signal })),
|
|
101
|
+
hit => hit.header.id !== caller.id && workspaceAccess.recordAuthorized(hit, caller),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
const parentIds = collected.items
|
|
105
|
+
.map(hit => hit.header.parentSession)
|
|
106
|
+
.filter((id): id is SessionId => id !== undefined)
|
|
107
|
+
const authorizedParents = await workspaceAccess.authorizeSessionIds(ctx, caller, parentIds, exec.signal)
|
|
108
|
+
const titles = await workspaceAccess.readTitles(
|
|
109
|
+
ctx,
|
|
110
|
+
caller,
|
|
111
|
+
collected.items.map(hit => hit.header.id),
|
|
112
|
+
exec.signal,
|
|
113
|
+
)
|
|
114
|
+
return presentation.formatSessionSearch(collected, titles, authorizedParents)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function executeEventSearch(
|
|
118
|
+
ctx: Context,
|
|
119
|
+
args: EventSearchArgs,
|
|
120
|
+
exec: ToolRunContext,
|
|
121
|
+
maxResults: number,
|
|
122
|
+
): Promise<string> {
|
|
123
|
+
const caller = workspaceAccess.callerOf(exec, ctx)
|
|
124
|
+
const sessionId = workspaceAccess.targetId(args, caller)
|
|
125
|
+
await workspaceAccess.authorizeTarget(ctx, caller, sessionId, exec.signal)
|
|
126
|
+
const query = toolInput.normalizeQuery(args.query)
|
|
127
|
+
const range = toolInput.sequenceRange(args.seq_from, args.seq_to)
|
|
128
|
+
if (sessionId === caller.id) {
|
|
129
|
+
const stepStartSeq = caller.boundary.lastStepStartSeq
|
|
130
|
+
// `null` (no step started yet) caps the range to a degenerate `to` the
|
|
131
|
+
// filter validation rejects — the loop never runs tools outside a step.
|
|
132
|
+
range.to = Math.min(range.to ?? Number.MAX_SAFE_INTEGER, (stepStartSeq ?? 0) - 1)
|
|
133
|
+
}
|
|
134
|
+
const title = await workspaceAccess.readTitle(ctx, caller, sessionId, exec.signal)
|
|
135
|
+
if (range.from !== undefined && range.to !== undefined && range.from > range.to) {
|
|
136
|
+
return presentation.formatEventSearch(sessionId, title, { items: [], capped: false })
|
|
137
|
+
}
|
|
138
|
+
const filters = toolInput.buildEventFilters({
|
|
139
|
+
seqFrom: range.from,
|
|
140
|
+
seqTo: range.to,
|
|
141
|
+
timeFrom: args.time_from,
|
|
142
|
+
timeTo: args.time_to,
|
|
143
|
+
eventTypes: args.event_types,
|
|
144
|
+
surfaces: args.surfaces,
|
|
145
|
+
})
|
|
146
|
+
const collected = await collectPages(
|
|
147
|
+
maxResults,
|
|
148
|
+
exec.signal,
|
|
149
|
+
async (cursor): Promise<SessionEventSearchPage> => {
|
|
150
|
+
const page = await serviceBoundary.call(ctx, exec.signal, 'event search', () =>
|
|
151
|
+
ctx.sessionQuery.searchEvents({
|
|
152
|
+
sessionId,
|
|
153
|
+
query,
|
|
154
|
+
filters,
|
|
155
|
+
...cursor === undefined ? {} : { cursor },
|
|
156
|
+
}, { signal: exec.signal }))
|
|
157
|
+
workspaceAccess.assertObservedTargetAuthorized(caller, sessionId, page.session)
|
|
158
|
+
return page
|
|
159
|
+
},
|
|
160
|
+
() => true,
|
|
161
|
+
)
|
|
162
|
+
return presentation.formatEventSearch(sessionId, title, collected)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function executeSessionTrace(
|
|
166
|
+
ctx: Context,
|
|
167
|
+
args: SessionTargetArgs,
|
|
168
|
+
exec: ToolRunContext,
|
|
169
|
+
): Promise<string> {
|
|
170
|
+
const caller = workspaceAccess.callerOf(exec, ctx)
|
|
171
|
+
const sessionId = workspaceAccess.targetId(args, caller)
|
|
172
|
+
await workspaceAccess.authorizeTarget(ctx, caller, sessionId, exec.signal)
|
|
173
|
+
const trace = await serviceBoundary.call(ctx, exec.signal, 'session lineage trace', () =>
|
|
174
|
+
ctx.sessionQuery.traceSession(sessionId, exec.signal))
|
|
175
|
+
workspaceAccess.assertObservedTargetAuthorized(caller, sessionId, trace.target.header)
|
|
176
|
+
|
|
177
|
+
const ancestors: SessionRecord[] = []
|
|
178
|
+
let ancestorBoundary = false
|
|
179
|
+
for (const ancestor of trace.ancestors) {
|
|
180
|
+
if (!workspaceAccess.recordAuthorized(ancestor, caller)) {
|
|
181
|
+
ancestorBoundary = true
|
|
182
|
+
break
|
|
183
|
+
}
|
|
184
|
+
ancestors.push(ancestor)
|
|
185
|
+
}
|
|
186
|
+
if (ancestors.length === trace.ancestors.length && !trace.complete) ancestorBoundary = true
|
|
187
|
+
const descendants = workspaceAccess.authorizeDescendants(trace.descendants, caller)
|
|
188
|
+
const visibleIds = [
|
|
189
|
+
trace.target.header.id,
|
|
190
|
+
...ancestors.map(record => record.header.id),
|
|
191
|
+
...workspaceAccess.descendantIds(descendants),
|
|
192
|
+
]
|
|
193
|
+
const titles = await workspaceAccess.readTitles(ctx, caller, visibleIds, exec.signal)
|
|
194
|
+
return presentation.formatSessionTrace(trace, ancestors, ancestorBoundary, descendants, titles)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function executeEventTrace(
|
|
198
|
+
ctx: Context,
|
|
199
|
+
args: EventTargetArgs,
|
|
200
|
+
exec: ToolRunContext,
|
|
201
|
+
): Promise<string> {
|
|
202
|
+
toolInput.assertNonNegativeSafeInteger('seq', args.seq)
|
|
203
|
+
const seq = SessionSeq(args.seq)
|
|
204
|
+
const caller = workspaceAccess.callerOf(exec, ctx)
|
|
205
|
+
const sessionId = workspaceAccess.targetId(args, caller)
|
|
206
|
+
await workspaceAccess.authorizeTarget(ctx, caller, sessionId, exec.signal)
|
|
207
|
+
const trace = await serviceBoundary.call(ctx, exec.signal, 'event trace', () =>
|
|
208
|
+
ctx.sessionQuery.traceEvent({ sessionId, seq }, exec.signal))
|
|
209
|
+
workspaceAccess.assertObservedTargetAuthorized(caller, sessionId, trace.session)
|
|
210
|
+
const title = await workspaceAccess.readTitle(ctx, caller, sessionId, exec.signal)
|
|
211
|
+
return presentation.formatEventTrace(sessionId, title, trace)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function executeEventRead(
|
|
215
|
+
ctx: Context,
|
|
216
|
+
args: EventReadArgs,
|
|
217
|
+
exec: ToolRunContext,
|
|
218
|
+
): Promise<string> {
|
|
219
|
+
toolInput.assertNonNegativeSafeInteger('seq', args.seq)
|
|
220
|
+
const seq = SessionSeq(args.seq)
|
|
221
|
+
if (args.before !== undefined) toolInput.assertNonNegativeSafeInteger('before', args.before)
|
|
222
|
+
if (args.after !== undefined) toolInput.assertNonNegativeSafeInteger('after', args.after)
|
|
223
|
+
const caller = workspaceAccess.callerOf(exec, ctx)
|
|
224
|
+
const sessionId = workspaceAccess.targetId(args, caller)
|
|
225
|
+
await workspaceAccess.authorizeTarget(ctx, caller, sessionId, exec.signal)
|
|
226
|
+
const window = await serviceBoundary.call(ctx, exec.signal, 'event read', () =>
|
|
227
|
+
ctx.sessionQuery.readEvent({
|
|
228
|
+
sessionId,
|
|
229
|
+
seq,
|
|
230
|
+
...args.before === undefined ? {} : { before: args.before },
|
|
231
|
+
...args.after === undefined ? {} : { after: args.after },
|
|
232
|
+
}, exec.signal))
|
|
233
|
+
workspaceAccess.assertObservedTargetAuthorized(caller, sessionId, window.session)
|
|
234
|
+
const title = await workspaceAccess.readTitle(ctx, caller, sessionId, exec.signal)
|
|
235
|
+
return presentation.formatEventRead(sessionId, title, window)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function collectPages<T>(
|
|
239
|
+
maxResults: number,
|
|
240
|
+
signal: AbortSignal,
|
|
241
|
+
request: (cursor?: SessionSearchCursor) => Promise<{
|
|
242
|
+
readonly items: readonly T[]
|
|
243
|
+
readonly nextCursor?: SessionSearchCursor
|
|
244
|
+
}>,
|
|
245
|
+
accept: (item: T) => boolean,
|
|
246
|
+
): Promise<SearchCollection<T>> {
|
|
247
|
+
const items: T[] = []
|
|
248
|
+
const seen = new Set<SessionSearchCursor>()
|
|
249
|
+
let cursor: SessionSearchCursor | undefined
|
|
250
|
+
while (true) {
|
|
251
|
+
signal.throwIfAborted()
|
|
252
|
+
const page = await request(cursor)
|
|
253
|
+
signal.throwIfAborted()
|
|
254
|
+
for (const item of page.items) {
|
|
255
|
+
if (!accept(item)) continue
|
|
256
|
+
if (items.length === maxResults) {
|
|
257
|
+
return { items, capped: true }
|
|
258
|
+
}
|
|
259
|
+
items.push(item)
|
|
260
|
+
}
|
|
261
|
+
if (page.nextCursor === undefined) return { items, capped: false }
|
|
262
|
+
if (seen.has(page.nextCursor)) {
|
|
263
|
+
throw new SessionQueryError(
|
|
264
|
+
'session-search provider repeated a continuation cursor',
|
|
265
|
+
'SESSION_QUERY_INVALID_CURSOR',
|
|
266
|
+
)
|
|
267
|
+
}
|
|
268
|
+
seen.add(page.nextCursor)
|
|
269
|
+
cursor = page.nextCursor
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Five model-facing session-query operation implementations. */
|
|
274
|
+
export const operations = {
|
|
275
|
+
executeSessionSearch,
|
|
276
|
+
executeEventSearch,
|
|
277
|
+
executeSessionTrace,
|
|
278
|
+
executeEventTrace,
|
|
279
|
+
executeEventRead,
|
|
280
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model text rendering and generic tool-call presentation.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query/presentation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
extractSessionEventText,
|
|
9
|
+
type SessionEventSearchHit,
|
|
10
|
+
type SessionEventTraceObservation,
|
|
11
|
+
type SessionEventWindow,
|
|
12
|
+
type SessionLineageTrace,
|
|
13
|
+
type SessionRecord,
|
|
14
|
+
type SessionSearchHit,
|
|
15
|
+
} from '@unieai/rabi-session-query'
|
|
16
|
+
import type {
|
|
17
|
+
SessionEvent,
|
|
18
|
+
SessionId,
|
|
19
|
+
} from '@unieai/rabi-session'
|
|
20
|
+
import type { GenericCallView } from '@unieai/rabi-tools'
|
|
21
|
+
import { workspaceAccess } from './workspace-access.ts'
|
|
22
|
+
|
|
23
|
+
type TitleView = Awaited<ReturnType<typeof workspaceAccess.readTitle>>
|
|
24
|
+
type CompleteTitleMap = Awaited<ReturnType<typeof workspaceAccess.readTitles>>
|
|
25
|
+
type AuthorizedDescendants = ReturnType<typeof workspaceAccess.authorizeDescendants>
|
|
26
|
+
|
|
27
|
+
interface SearchCollection<T> {
|
|
28
|
+
readonly items: T[]
|
|
29
|
+
readonly capped: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface SessionSearchCallArgs {
|
|
33
|
+
readonly query: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface EventSearchCallArgs {
|
|
37
|
+
readonly query: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface SessionTargetCallArgs {
|
|
41
|
+
readonly session_id?: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface EventTargetCallArgs extends SessionTargetCallArgs {
|
|
45
|
+
readonly seq: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function formatSessionSearch(
|
|
49
|
+
collected: SearchCollection<SessionSearchHit>,
|
|
50
|
+
titles: CompleteTitleMap,
|
|
51
|
+
authorizedParents: ReadonlySet<SessionId>,
|
|
52
|
+
): string {
|
|
53
|
+
if (collected.items.length === 0) return formatEmptySessionSearch()
|
|
54
|
+
const lines = [`Session search results (${collected.items.length}):`]
|
|
55
|
+
for (const [index, hit] of collected.items.entries()) {
|
|
56
|
+
const parent = hit.header.parentSession === undefined
|
|
57
|
+
? 'root'
|
|
58
|
+
: authorizedParents.has(hit.header.parentSession)
|
|
59
|
+
? hit.header.parentSession
|
|
60
|
+
: '[outside workspace]'
|
|
61
|
+
const availability = [
|
|
62
|
+
hit.live ? 'live' : undefined,
|
|
63
|
+
hit.persisted ? 'persisted' : undefined,
|
|
64
|
+
].filter((value): value is string => value !== undefined).join(', ') || 'unavailable'
|
|
65
|
+
lines.push(
|
|
66
|
+
'',
|
|
67
|
+
`${index + 1}. Session ${hit.header.id} — ${workspaceAccess.titleText(titles.get(hit.header.id))}`,
|
|
68
|
+
` Created: ${formatTime(hit.header.createdAt)}`,
|
|
69
|
+
` Parent: ${parent}`,
|
|
70
|
+
` Availability: ${availability}`,
|
|
71
|
+
` Best match: seq ${hit.bestMatch.seq} | ${hit.bestMatch.type} | ${hit.bestMatch.surface} | ${formatTime(hit.bestMatch.time)}`,
|
|
72
|
+
` Snippet: ${hit.bestMatch.snippet}`,
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
if (collected.capped) {
|
|
76
|
+
lines.push('', 'Result cap reached. Narrow the query or add filters to find additional matches.')
|
|
77
|
+
}
|
|
78
|
+
return lines.join('\n')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function formatEmptySessionSearch(): string {
|
|
82
|
+
return 'No prior session matches found.'
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function formatEventSearch(
|
|
86
|
+
sessionId: SessionId,
|
|
87
|
+
title: TitleView,
|
|
88
|
+
collected: SearchCollection<SessionEventSearchHit>,
|
|
89
|
+
): string {
|
|
90
|
+
const lines = [`Session ${sessionId} — ${workspaceAccess.titleText(title)}`]
|
|
91
|
+
if (collected.items.length === 0) {
|
|
92
|
+
lines.push('', 'No prior event matches found.')
|
|
93
|
+
return lines.join('\n')
|
|
94
|
+
}
|
|
95
|
+
lines.push('', `Event search results (${collected.items.length}):`)
|
|
96
|
+
for (const [index, hit] of collected.items.entries()) {
|
|
97
|
+
lines.push(
|
|
98
|
+
`${index + 1}. seq ${hit.seq} | ${hit.type} | ${hit.surface} | ${formatTime(hit.time)}`,
|
|
99
|
+
` Snippet: ${hit.snippet}`,
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
if (collected.capped) {
|
|
103
|
+
lines.push('', 'Result cap reached. Narrow the query or add filters to find additional matches.')
|
|
104
|
+
}
|
|
105
|
+
return lines.join('\n')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function formatSessionTrace(
|
|
109
|
+
trace: SessionLineageTrace,
|
|
110
|
+
ancestors: readonly SessionRecord[],
|
|
111
|
+
ancestorBoundary: boolean,
|
|
112
|
+
descendants: AuthorizedDescendants,
|
|
113
|
+
titles: CompleteTitleMap,
|
|
114
|
+
): string {
|
|
115
|
+
const lines = [
|
|
116
|
+
`Session ${trace.target.header.id} — ${workspaceAccess.titleText(titles.get(trace.target.header.id))}`,
|
|
117
|
+
`Created: ${formatTime(trace.target.header.createdAt)}`,
|
|
118
|
+
`Availability: ${availabilityText(trace.target)}`,
|
|
119
|
+
'',
|
|
120
|
+
'Ancestors (nearest first):',
|
|
121
|
+
]
|
|
122
|
+
if (ancestors.length === 0 && !ancestorBoundary) lines.push('- none (target is a root session)')
|
|
123
|
+
for (const record of ancestors) {
|
|
124
|
+
lines.push(`- ${record.header.id} — ${workspaceAccess.titleText(titles.get(record.header.id))} | ${formatTime(record.header.createdAt)} | ${availabilityText(record)}`)
|
|
125
|
+
}
|
|
126
|
+
if (ancestorBoundary) lines.push('- [outside workspace boundary]')
|
|
127
|
+
lines.push('', 'Descendants:')
|
|
128
|
+
if (descendants.length === 0) lines.push('- none')
|
|
129
|
+
else renderDescendants(lines, descendants, titles)
|
|
130
|
+
return lines.join('\n')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function renderDescendants(
|
|
134
|
+
lines: string[],
|
|
135
|
+
nodes: AuthorizedDescendants,
|
|
136
|
+
titles: CompleteTitleMap,
|
|
137
|
+
): void {
|
|
138
|
+
for (const { node, depth } of workspaceAccess.visitDescendants(nodes)) {
|
|
139
|
+
const indent = ' '.repeat(depth)
|
|
140
|
+
if (node === null) {
|
|
141
|
+
lines.push(`${indent}- [outside workspace subtree]`)
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
const id = node.record.header.id
|
|
145
|
+
lines.push(`${indent}- ${id} — ${workspaceAccess.titleText(titles.get(id))} | ${formatTime(node.record.header.createdAt)} | ${availabilityText(node.record)}`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function formatEventTrace(
|
|
150
|
+
sessionId: SessionId,
|
|
151
|
+
title: TitleView,
|
|
152
|
+
trace: SessionEventTraceObservation,
|
|
153
|
+
): string {
|
|
154
|
+
return [
|
|
155
|
+
`Session ${sessionId} — ${workspaceAccess.titleText(title)}`,
|
|
156
|
+
`Target: seq ${trace.target.seq} | ${trace.target.type} | ${trace.target.surface} | ${formatTime(trace.target.time)}`,
|
|
157
|
+
`Replaced by: ${trace.replacedBy ?? 'none'}`,
|
|
158
|
+
`Replacement chain: ${seqList(trace.replacementChain)}`,
|
|
159
|
+
`Events replaced by target: ${seqList(trace.replacedEventSeqs)}`,
|
|
160
|
+
`Events cited directly as sources: ${seqList(trace.sourceEventSeqs)}`,
|
|
161
|
+
`Direct derived events: ${seqList(trace.derivedEventSeqs)}`,
|
|
162
|
+
].join('\n')
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function formatEventRead(
|
|
166
|
+
sessionId: SessionId,
|
|
167
|
+
title: TitleView,
|
|
168
|
+
window: SessionEventWindow,
|
|
169
|
+
): string {
|
|
170
|
+
const before = window.events.filter(event => event.seq < window.target.seq)
|
|
171
|
+
const after = window.events.filter(event => event.seq > window.target.seq)
|
|
172
|
+
const lines = [
|
|
173
|
+
`Session ${sessionId} — ${workspaceAccess.titleText(title)}`,
|
|
174
|
+
`Target event seq ${window.target.seq}:`,
|
|
175
|
+
'```json',
|
|
176
|
+
JSON.stringify(window.target, null, 2),
|
|
177
|
+
'```',
|
|
178
|
+
]
|
|
179
|
+
if (before.length > 0) {
|
|
180
|
+
lines.push('', 'Before:')
|
|
181
|
+
for (const event of before) lines.push(formatNeighbor(event))
|
|
182
|
+
}
|
|
183
|
+
if (after.length > 0) {
|
|
184
|
+
lines.push('', 'After:')
|
|
185
|
+
for (const event of after) lines.push(formatNeighbor(event))
|
|
186
|
+
}
|
|
187
|
+
return lines.join('\n')
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function formatNeighbor(event: SessionEvent): string {
|
|
191
|
+
const text = extractSessionEventText(event)
|
|
192
|
+
return `- seq ${event.seq} | ${event.type} | ${formatTime(event.time)}`
|
|
193
|
+
+ (text.length === 0 ? ' | (no semantic text)' : `\n ${text.replaceAll('\n', '\n ')}`)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function availabilityText(record: SessionRecord): string {
|
|
197
|
+
return [
|
|
198
|
+
record.live ? 'live' : undefined,
|
|
199
|
+
record.persisted ? 'persisted' : undefined,
|
|
200
|
+
].filter((value): value is string => value !== undefined).join(', ') || 'unavailable'
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function seqList(values: readonly number[]): string {
|
|
204
|
+
return values.length === 0 ? 'none' : values.join(', ')
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function formatTime(value: number): string {
|
|
208
|
+
return new Date(value).toISOString()
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function presentSessionSearchCall(args: SessionSearchCallArgs): GenericCallView {
|
|
212
|
+
return { card: 'generic', kind: 'search', title: 'Search prior sessions', rawInput: args.query }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function presentEventSearchCall(args: EventSearchCallArgs): GenericCallView {
|
|
216
|
+
return { card: 'generic', kind: 'search', title: 'Search session events', rawInput: args.query }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function presentSessionTraceCall(args: SessionTargetCallArgs): GenericCallView {
|
|
220
|
+
return {
|
|
221
|
+
card: 'generic',
|
|
222
|
+
kind: 'read',
|
|
223
|
+
title: args.session_id === undefined ? 'Trace current session' : `Trace session ${args.session_id}`,
|
|
224
|
+
...args.session_id === undefined ? {} : { rawInput: args.session_id },
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function presentEventTargetCall(
|
|
229
|
+
action: string,
|
|
230
|
+
args: EventTargetCallArgs,
|
|
231
|
+
): GenericCallView {
|
|
232
|
+
return {
|
|
233
|
+
card: 'generic',
|
|
234
|
+
kind: 'read',
|
|
235
|
+
title: `${action} ${args.seq}`,
|
|
236
|
+
rawInput: {
|
|
237
|
+
...args.session_id === undefined ? {} : { session_id: args.session_id },
|
|
238
|
+
seq: args.seq,
|
|
239
|
+
},
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Text output and call-card presentation for every session-query tool. */
|
|
244
|
+
export const presentation = {
|
|
245
|
+
formatSessionSearch,
|
|
246
|
+
formatEmptySessionSearch,
|
|
247
|
+
formatEventSearch,
|
|
248
|
+
formatSessionTrace,
|
|
249
|
+
formatEventTrace,
|
|
250
|
+
formatEventRead,
|
|
251
|
+
presentSessionSearchCall,
|
|
252
|
+
presentEventSearchCall,
|
|
253
|
+
presentSessionTraceCall,
|
|
254
|
+
presentEventTargetCall,
|
|
255
|
+
}
|