@unieai/rabi-tool-session-query 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +1033 -0
- package/lib/types/index.d.ts +27 -0
- package/lib/types/input.d.ts +170 -0
- package/lib/types/operations.d.ts +45 -0
- package/lib/types/presentation.d.ts +53 -0
- package/lib/types/service-boundary.d.ts +18 -0
- package/lib/types/workspace-access.d.ts +63 -0
- 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 +42 -4
- 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
package/src/input.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model argument schemas, normalization, and filter construction.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query/input
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
type SessionEventType,
|
|
9
|
+
type SessionId as SessionIdValue,
|
|
10
|
+
} from '@unieai/rabi-session'
|
|
11
|
+
import { brandString } from '@unieai/rabi-brand'
|
|
12
|
+
import {
|
|
13
|
+
SessionQueryError,
|
|
14
|
+
type SessionAvailability,
|
|
15
|
+
type SessionEventMetadataFilter,
|
|
16
|
+
type SessionEventSurface,
|
|
17
|
+
type SessionResultFilter,
|
|
18
|
+
} from '@unieai/rabi-session-query'
|
|
19
|
+
|
|
20
|
+
interface SessionSearchArgs {
|
|
21
|
+
query: string
|
|
22
|
+
session_ids?: string[]
|
|
23
|
+
created_at_from?: string
|
|
24
|
+
created_at_to?: string
|
|
25
|
+
parent_session_ids?: string[]
|
|
26
|
+
include_root_sessions?: boolean
|
|
27
|
+
availability?: SessionAvailability[]
|
|
28
|
+
event_seq_from?: number
|
|
29
|
+
event_seq_to?: number
|
|
30
|
+
event_time_from?: string
|
|
31
|
+
event_time_to?: string
|
|
32
|
+
event_types?: string[]
|
|
33
|
+
event_surfaces?: SessionEventSurface[]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface EventFilterInput {
|
|
37
|
+
readonly seqFrom?: number | undefined
|
|
38
|
+
readonly seqTo?: number | undefined
|
|
39
|
+
readonly timeFrom?: string | undefined
|
|
40
|
+
readonly timeTo?: string | undefined
|
|
41
|
+
readonly eventTypes?: string[] | undefined
|
|
42
|
+
readonly surfaces?: SessionEventSurface[] | undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const sessionSearchParameters = {
|
|
46
|
+
query: { type: 'string', required: true, description: 'Literal full-text query over prior session history.' },
|
|
47
|
+
session_ids: { type: 'array', items: { type: 'string' }, description: 'Optional session ids to include.' },
|
|
48
|
+
created_at_from: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 creation-time lower bound.' },
|
|
49
|
+
created_at_to: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 creation-time upper bound.' },
|
|
50
|
+
parent_session_ids: { type: 'array', items: { type: 'string' }, description: 'Optional direct parent session ids.' },
|
|
51
|
+
include_root_sessions: { type: 'boolean', description: 'Include sessions with no parent in the parent filter.' },
|
|
52
|
+
availability: {
|
|
53
|
+
type: 'array',
|
|
54
|
+
items: { type: 'string', enum: ['live', 'persisted'] },
|
|
55
|
+
description: 'Require at least one selected source availability.',
|
|
56
|
+
},
|
|
57
|
+
event_seq_from: { type: 'integer', description: 'Inclusive event sequence lower bound.' },
|
|
58
|
+
event_seq_to: { type: 'integer', description: 'Inclusive event sequence upper bound.' },
|
|
59
|
+
event_time_from: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 event-time lower bound.' },
|
|
60
|
+
event_time_to: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 event-time upper bound.' },
|
|
61
|
+
event_types: { type: 'array', items: { type: 'string' }, description: 'Event types to include.' },
|
|
62
|
+
event_surfaces: {
|
|
63
|
+
type: 'array',
|
|
64
|
+
items: { type: 'string', enum: ['current', 'shadowed', 'log-only'] },
|
|
65
|
+
description: 'Event surfaces to include.',
|
|
66
|
+
},
|
|
67
|
+
} as const
|
|
68
|
+
|
|
69
|
+
const eventSearchParameters = {
|
|
70
|
+
session_id: { type: 'string', description: 'Target session id. Omit for the current session.' },
|
|
71
|
+
query: { type: 'string', required: true, description: 'Literal full-text query over the target session.' },
|
|
72
|
+
seq_from: { type: 'integer', description: 'Inclusive event sequence lower bound.' },
|
|
73
|
+
seq_to: { type: 'integer', description: 'Inclusive event sequence upper bound.' },
|
|
74
|
+
time_from: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 event-time lower bound.' },
|
|
75
|
+
time_to: { type: 'string', description: 'Inclusive timezone-qualified ISO 8601 event-time upper bound.' },
|
|
76
|
+
event_types: { type: 'array', items: { type: 'string' }, description: 'Event types to include.' },
|
|
77
|
+
surfaces: {
|
|
78
|
+
type: 'array',
|
|
79
|
+
items: { type: 'string', enum: ['current', 'shadowed', 'log-only'] },
|
|
80
|
+
description: 'Event surfaces to include.',
|
|
81
|
+
},
|
|
82
|
+
} as const
|
|
83
|
+
|
|
84
|
+
const targetSessionParameter = {
|
|
85
|
+
session_id: { type: 'string', description: 'Target session id. Omit for the current session.' },
|
|
86
|
+
} as const
|
|
87
|
+
|
|
88
|
+
function buildSessionFilters(args: SessionSearchArgs): SessionResultFilter[] {
|
|
89
|
+
const filters: SessionResultFilter[] = []
|
|
90
|
+
if (args.session_ids !== undefined) {
|
|
91
|
+
assertNonEmptyArray('session_ids', args.session_ids)
|
|
92
|
+
filters.push({ kind: 'id', values: args.session_ids.map(value => brandString<SessionIdValue>(value)) })
|
|
93
|
+
}
|
|
94
|
+
const created = timestampRange('created_at', args.created_at_from, args.created_at_to)
|
|
95
|
+
if (created !== undefined) filters.push({ kind: 'created-at', ...created })
|
|
96
|
+
if (args.availability !== undefined) {
|
|
97
|
+
assertNonEmptyArray('availability', args.availability)
|
|
98
|
+
filters.push({ kind: 'availability', values: args.availability })
|
|
99
|
+
}
|
|
100
|
+
return filters
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function materializeParentSessionIds(values: readonly string[] | undefined): SessionIdValue[] | undefined {
|
|
104
|
+
if (values === undefined) return undefined
|
|
105
|
+
assertNonEmptyArray('parent_session_ids', values)
|
|
106
|
+
return [...new Set(values.map(value => brandString<SessionIdValue>(value)))]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function buildEventFilters(input: EventFilterInput): SessionEventMetadataFilter[] {
|
|
110
|
+
const filters: SessionEventMetadataFilter[] = []
|
|
111
|
+
const seq = sequenceRange(input.seqFrom, input.seqTo)
|
|
112
|
+
if (seq.from !== undefined || seq.to !== undefined) filters.push({ kind: 'seq', ...seq })
|
|
113
|
+
const time = timestampRange('time', input.timeFrom, input.timeTo)
|
|
114
|
+
if (time !== undefined) filters.push({ kind: 'time', ...time })
|
|
115
|
+
if (input.eventTypes !== undefined) {
|
|
116
|
+
assertNonEmptyArray('event_types', input.eventTypes)
|
|
117
|
+
filters.push({ kind: 'type', values: input.eventTypes as SessionEventType[] })
|
|
118
|
+
}
|
|
119
|
+
if (input.surfaces !== undefined) {
|
|
120
|
+
assertNonEmptyArray('surfaces', input.surfaces)
|
|
121
|
+
filters.push({ kind: 'surface', values: input.surfaces })
|
|
122
|
+
}
|
|
123
|
+
return filters
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function normalizeQuery(value: string): string {
|
|
127
|
+
const query = value.trim().replace(/\s+/gu, ' ')
|
|
128
|
+
if (query.length === 0) {
|
|
129
|
+
throw new SessionQueryError(
|
|
130
|
+
'session-search query must contain non-whitespace text',
|
|
131
|
+
'SESSION_QUERY_INVALID_QUERY',
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
if (query.includes('\0')) {
|
|
135
|
+
throw new SessionQueryError(
|
|
136
|
+
'session-search query must not contain NUL',
|
|
137
|
+
'SESSION_QUERY_INVALID_QUERY',
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
return query
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function sequenceRange(
|
|
144
|
+
from: number | undefined,
|
|
145
|
+
to: number | undefined,
|
|
146
|
+
): { from?: number; to?: number } {
|
|
147
|
+
if (from !== undefined) assertNonNegativeSafeInteger('sequence lower bound', from)
|
|
148
|
+
if (to !== undefined) assertNonNegativeSafeInteger('sequence upper bound', to)
|
|
149
|
+
if (from !== undefined && to !== undefined && from > to) {
|
|
150
|
+
throw invalidRange('sequence', 'from must be less than or equal to to')
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
...from === undefined ? {} : { from },
|
|
154
|
+
...to === undefined ? {} : { to },
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function timestampRange(
|
|
159
|
+
name: string,
|
|
160
|
+
from: string | undefined,
|
|
161
|
+
to: string | undefined,
|
|
162
|
+
): { from?: number; to?: number } | undefined {
|
|
163
|
+
if (from === undefined && to === undefined) return undefined
|
|
164
|
+
const fromTimestamp = from === undefined ? undefined : parseIsoTimestamp(`${name}_from`, from)
|
|
165
|
+
const toTimestamp = to === undefined ? undefined : parseIsoTimestamp(`${name}_to`, to)
|
|
166
|
+
if (
|
|
167
|
+
fromTimestamp !== undefined
|
|
168
|
+
&& toTimestamp !== undefined
|
|
169
|
+
&& compareTimestamps(fromTimestamp, toTimestamp) > 0
|
|
170
|
+
) {
|
|
171
|
+
throw invalidRange(name, 'from must be less than or equal to to')
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
...fromTimestamp === undefined ? {} : { from: timestampLowerBound(fromTimestamp) },
|
|
175
|
+
...toTimestamp === undefined ? {} : { to: timestampUpperBound(toTimestamp) },
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const ISO_TIMESTAMP =
|
|
180
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|([+-])(\d{2}):(\d{2}))$/
|
|
181
|
+
|
|
182
|
+
interface ExactTimestamp {
|
|
183
|
+
readonly millisecond: number
|
|
184
|
+
/** Canonical decimal digits strictly below one millisecond; no trailing zeroes. */
|
|
185
|
+
readonly remainder: string
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function parseIsoTimestamp(name: string, value: string): ExactTimestamp {
|
|
189
|
+
const match = ISO_TIMESTAMP.exec(value)
|
|
190
|
+
if (match === null) {
|
|
191
|
+
throw invalidRange(name, 'must be an ISO 8601 timestamp with Z or a numeric offset')
|
|
192
|
+
}
|
|
193
|
+
const year = Number(match[1])
|
|
194
|
+
const month = Number(match[2])
|
|
195
|
+
const day = Number(match[3])
|
|
196
|
+
const hour = Number(match[4])
|
|
197
|
+
const minute = Number(match[5])
|
|
198
|
+
const second = Number(match[6] ?? 0)
|
|
199
|
+
const offsetHour = Number(match[10] ?? 0)
|
|
200
|
+
const offsetMinute = Number(match[11] ?? 0)
|
|
201
|
+
if (
|
|
202
|
+
month < 1 || month > 12
|
|
203
|
+
|| day < 1 || day > daysInMonth(year, month)
|
|
204
|
+
|| hour > 23 || minute > 59 || second > 59
|
|
205
|
+
|| offsetHour > 23 || offsetMinute > 59
|
|
206
|
+
) {
|
|
207
|
+
throw invalidRange(name, 'must be a valid ISO 8601 timestamp')
|
|
208
|
+
}
|
|
209
|
+
const fraction = match[7] ?? ''
|
|
210
|
+
const millisecondDigits = fraction.slice(0, 3).padEnd(3, '0')
|
|
211
|
+
const normalized = `${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}`
|
|
212
|
+
+ `:${match[6] ?? '00'}.${millisecondDigits}${match[8]}`
|
|
213
|
+
const timestamp = Date.parse(normalized)
|
|
214
|
+
if (!Number.isSafeInteger(timestamp)) {
|
|
215
|
+
throw invalidRange(name, 'must be a valid ISO 8601 timestamp')
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
millisecond: timestamp,
|
|
219
|
+
remainder: fraction.slice(3).replace(/0+$/u, ''),
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function compareTimestamps(left: ExactTimestamp, right: ExactTimestamp): number {
|
|
224
|
+
if (left.millisecond !== right.millisecond) {
|
|
225
|
+
return left.millisecond < right.millisecond ? -1 : 1
|
|
226
|
+
}
|
|
227
|
+
const length = Math.max(left.remainder.length, right.remainder.length)
|
|
228
|
+
for (let index = 0; index < length; index += 1) {
|
|
229
|
+
const leftDigit = left.remainder[index] ?? '0'
|
|
230
|
+
const rightDigit = right.remainder[index] ?? '0'
|
|
231
|
+
if (leftDigit !== rightDigit) return leftDigit < rightDigit ? -1 : 1
|
|
232
|
+
}
|
|
233
|
+
return 0
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function timestampLowerBound(timestamp: ExactTimestamp): number {
|
|
237
|
+
return timestamp.remainder.length === 0
|
|
238
|
+
? timestamp.millisecond
|
|
239
|
+
: nextUpFinite(timestamp.millisecond)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function timestampUpperBound(timestamp: ExactTimestamp): number {
|
|
243
|
+
return timestamp.remainder.length === 0
|
|
244
|
+
? timestamp.millisecond
|
|
245
|
+
: nextDownFinite(timestamp.millisecond + 1)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function nextUpFinite(value: number): number {
|
|
249
|
+
if (value === 0) return Number.MIN_VALUE
|
|
250
|
+
const view = new DataView(new ArrayBuffer(8))
|
|
251
|
+
view.setFloat64(0, value)
|
|
252
|
+
const bits = view.getBigUint64(0)
|
|
253
|
+
view.setBigUint64(0, value > 0 ? bits + 1n : bits - 1n)
|
|
254
|
+
return view.getFloat64(0)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function nextDownFinite(value: number): number {
|
|
258
|
+
if (value === 0) return -Number.MIN_VALUE
|
|
259
|
+
const view = new DataView(new ArrayBuffer(8))
|
|
260
|
+
view.setFloat64(0, value)
|
|
261
|
+
const bits = view.getBigUint64(0)
|
|
262
|
+
view.setBigUint64(0, value > 0 ? bits - 1n : bits + 1n)
|
|
263
|
+
return view.getFloat64(0)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function daysInMonth(year: number, month: number): number {
|
|
267
|
+
if (month === 2) return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28
|
|
268
|
+
return [4, 6, 9, 11].includes(month) ? 30 : 31
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function invalidRange(name: string, detail: string): SessionQueryError {
|
|
272
|
+
return new SessionQueryError(
|
|
273
|
+
`session ${name} range ${detail}`,
|
|
274
|
+
'SESSION_QUERY_INVALID_FILTER',
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function assertNonNegativeSafeInteger(name: string, value: number): void {
|
|
279
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
280
|
+
throw new SessionQueryError(
|
|
281
|
+
`${name} must be a non-negative safe integer`,
|
|
282
|
+
'SESSION_QUERY_INVALID_FILTER',
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function assertNonEmptyArray(name: string, values: readonly unknown[]): void {
|
|
288
|
+
if (values.length === 0) {
|
|
289
|
+
throw new SessionQueryError(
|
|
290
|
+
`${name} must contain at least one value when supplied`,
|
|
291
|
+
'SESSION_QUERY_INVALID_FILTER',
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Model schemas and model-owned value normalization shared by tool operations. */
|
|
297
|
+
export const toolInput = {
|
|
298
|
+
sessionSearchParameters,
|
|
299
|
+
eventSearchParameters,
|
|
300
|
+
targetSessionParameter,
|
|
301
|
+
buildSessionFilters,
|
|
302
|
+
materializeParentSessionIds,
|
|
303
|
+
buildEventFilters,
|
|
304
|
+
normalizeQuery,
|
|
305
|
+
sequenceRange,
|
|
306
|
+
assertNonNegativeSafeInteger,
|
|
307
|
+
}
|
|
@@ -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
|
+
}
|