@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
package/lib/index.js
CHANGED
|
@@ -447,7 +447,7 @@ function callerOf(exec, ctx) {
|
|
|
447
447
|
return {
|
|
448
448
|
id: agent.session.id,
|
|
449
449
|
header: agent.session.header,
|
|
450
|
-
boundary: ctx.
|
|
450
|
+
boundary: ctx.agents.turnBoundaryOf(agent.session)
|
|
451
451
|
};
|
|
452
452
|
}
|
|
453
453
|
function targetId(args, caller) {
|
|
@@ -792,8 +792,7 @@ async function executeEventSearch(ctx, args, exec, maxResults) {
|
|
|
792
792
|
const query = toolInput.normalizeQuery(args.query);
|
|
793
793
|
const range = toolInput.sequenceRange(args.seq_from, args.seq_to);
|
|
794
794
|
if (sessionId === caller.id) {
|
|
795
|
-
const stepStartSeq = caller.boundary
|
|
796
|
-
if (stepStartSeq === void 0) throw new HarnessError("current-session search requires an active step boundary", "SESSION_QUERY_TOOL_NO_CURRENT_STEP");
|
|
795
|
+
const stepStartSeq = caller.boundary.lastStepStartSeq;
|
|
797
796
|
range.to = Math.min(range.to ?? Number.MAX_SAFE_INTEGER, (stepStartSeq ?? 0) - 1);
|
|
798
797
|
}
|
|
799
798
|
const title = await workspaceAccess.readTitle(ctx, caller, sessionId, exec.signal);
|
|
@@ -925,7 +924,7 @@ const inject = [
|
|
|
925
924
|
"tools",
|
|
926
925
|
"systemPrompt",
|
|
927
926
|
"sessionQuery",
|
|
928
|
-
"
|
|
927
|
+
"agents"
|
|
929
928
|
];
|
|
930
929
|
/** Default maximum number of authorized search hits returned by one call. */
|
|
931
930
|
const DEFAULT_MAX_SEARCH_RESULTS = 100;
|
|
@@ -12,7 +12,7 @@ interface Caller {
|
|
|
12
12
|
readonly id: SessionIdValue;
|
|
13
13
|
readonly header: SessionHeader;
|
|
14
14
|
/** The caller's own-session boundary fold (the `turnBoundary` projection). */
|
|
15
|
-
readonly boundary: TurnBoundaryProjection
|
|
15
|
+
readonly boundary: TurnBoundaryProjection;
|
|
16
16
|
}
|
|
17
17
|
interface TitleView {
|
|
18
18
|
readonly text: string;
|
package/locale/de.json
ADDED
package/locale/en.json
ADDED
package/locale/es.json
ADDED
package/locale/fr.json
ADDED
package/locale/ja.json
ADDED
package/locale/pt.json
ADDED
package/locale/ru.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-tool-session-query",
|
|
3
3
|
"description": "Workspace-authorized model-facing session history search, trace, and event read tools",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -14,27 +14,31 @@
|
|
|
14
14
|
"default": "./lib/index.js"
|
|
15
15
|
},
|
|
16
16
|
"./src/*": "./src/*",
|
|
17
|
+
"./locale/*.json": "./locale/*.json",
|
|
17
18
|
"./package.json": "./package.json"
|
|
18
19
|
},
|
|
19
20
|
"files": [
|
|
21
|
+
"locale/*.json",
|
|
20
22
|
"lib/index.js",
|
|
21
23
|
"lib/types/**/*.d.ts",
|
|
24
|
+
"lib/types/**/*.d.ts.map",
|
|
25
|
+
"lib/**/*.js.map",
|
|
26
|
+
"src",
|
|
22
27
|
"README.md"
|
|
23
28
|
],
|
|
24
29
|
"license": "MIT",
|
|
25
30
|
"peerDependencies": {
|
|
26
|
-
"@unieai/cordis": "4.0
|
|
27
|
-
"@unieai/rabi-agent": "0.2.
|
|
28
|
-
"@unieai/rabi-llm": "0.2.
|
|
29
|
-
"@unieai/rabi-session": "0.2.
|
|
30
|
-
"@unieai/rabi-session-
|
|
31
|
-
"@unieai/rabi-
|
|
32
|
-
"@unieai/rabi-
|
|
33
|
-
"@unieai/rabi-
|
|
34
|
-
"@unieai/rabi-tools": "0.2.1-rc.3"
|
|
31
|
+
"@unieai/cordis": "4.1.0",
|
|
32
|
+
"@unieai/rabi-agent": "0.2.2-rc.5",
|
|
33
|
+
"@unieai/rabi-llm": "0.2.2-rc.5",
|
|
34
|
+
"@unieai/rabi-session": "0.2.2-rc.5",
|
|
35
|
+
"@unieai/rabi-session-query": "0.2.2-rc.5",
|
|
36
|
+
"@unieai/rabi-system-prompt": "0.2.2-rc.5",
|
|
37
|
+
"@unieai/rabi-timeout": "0.2.2-rc.5",
|
|
38
|
+
"@unieai/rabi-tools": "0.2.2-rc.5"
|
|
35
39
|
},
|
|
36
40
|
"dependencies": {
|
|
37
|
-
"@unieai/rabi-brand": "0.2.
|
|
38
|
-
"@unieai/schemastery": "3.
|
|
41
|
+
"@unieai/rabi-brand": "0.2.2-rc.5",
|
|
42
|
+
"@unieai/schemastery": "3.19.0"
|
|
39
43
|
}
|
|
40
44
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing, workspace-authorized session-history search and read tools.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Context } from '@unieai/cordis'
|
|
8
|
+
import z from '@unieai/schemastery'
|
|
9
|
+
import { MAX_TIMER_DELAY_MS } from '@unieai/rabi-timeout'
|
|
10
|
+
import { defineTool } from '@unieai/rabi-tools'
|
|
11
|
+
import { toolInput } from './input.ts'
|
|
12
|
+
import { operations } from './operations.ts'
|
|
13
|
+
import { presentation } from './presentation.ts'
|
|
14
|
+
|
|
15
|
+
/** Cordis plugin name used by Loader diagnostics. */
|
|
16
|
+
export const name = 'tool-session-query'
|
|
17
|
+
|
|
18
|
+
/** Capability services required by the model-facing consumer. */
|
|
19
|
+
export const inject = ['tools', 'systemPrompt', 'sessionQuery', 'agents']
|
|
20
|
+
|
|
21
|
+
/** Default maximum number of authorized search hits returned by one call. */
|
|
22
|
+
export const DEFAULT_MAX_SEARCH_RESULTS = 100
|
|
23
|
+
|
|
24
|
+
/** Default cooperative deadline for either full-text search tool. */
|
|
25
|
+
export const DEFAULT_SEARCH_TIMEOUT_MS = 30_000
|
|
26
|
+
|
|
27
|
+
/** Deployment-owned search count and timeout bounds. */
|
|
28
|
+
export interface Config {
|
|
29
|
+
/** Maximum authorized hits returned by one search call. Defaults to 100. */
|
|
30
|
+
maxSearchResults?: number
|
|
31
|
+
/** Cooperative full-text search deadline in milliseconds. Defaults to 30000. */
|
|
32
|
+
searchTimeoutMs?: number
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Schemastery config for Loader defaults and generated configuration docs. */
|
|
36
|
+
export const Config: z<Config> = z.object({
|
|
37
|
+
maxSearchResults: z.number().step(1).min(1).default(DEFAULT_MAX_SEARCH_RESULTS),
|
|
38
|
+
searchTimeoutMs: z.number().step(1).min(1).max(MAX_TIMER_DELAY_MS).default(DEFAULT_SEARCH_TIMEOUT_MS),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
interface ResolvedConfig {
|
|
42
|
+
readonly maxSearchResults: number
|
|
43
|
+
readonly searchTimeoutMs: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const TEXT_OUTPUT = {
|
|
47
|
+
schema: { type: 'string' as const },
|
|
48
|
+
render: (_args: unknown, value: string) => [{ type: 'text' as const, text: value }],
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const PROMPT_TEXT =
|
|
52
|
+
'Use session_search to find relevant work from prior sessions, or session_event_search to search earlier '
|
|
53
|
+
+ 'events in one session. Search results are cursor-free and workspace-scoped. Follow a useful hit with '
|
|
54
|
+
+ 'session_trace, session_event_trace, or session_event_read when you need lineage, relationships, or exact data.'
|
|
55
|
+
|
|
56
|
+
/** Register all five tools and their shared model guidance. */
|
|
57
|
+
export function apply(ctx: Context, config: Config): void {
|
|
58
|
+
const resolved = resolveConfig(config)
|
|
59
|
+
ctx.systemPrompt.section({
|
|
60
|
+
name: 'tool:session-query',
|
|
61
|
+
order: ctx.systemPrompt.getSectionOrder('TOOL_SESSION_QUERY'),
|
|
62
|
+
text: PROMPT_TEXT,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
ctx.tools.register(defineTool({
|
|
66
|
+
name: 'session_search',
|
|
67
|
+
description: 'Search prior sessions in the caller workspace and return the strongest matching event from each session.',
|
|
68
|
+
parameters: toolInput.sessionSearchParameters,
|
|
69
|
+
output: TEXT_OUTPUT,
|
|
70
|
+
timeoutMs: resolved.searchTimeoutMs,
|
|
71
|
+
execute: (args, exec) => operations.executeSessionSearch(ctx, args, exec, resolved.maxSearchResults),
|
|
72
|
+
presentCall: presentation.presentSessionSearchCall,
|
|
73
|
+
}))
|
|
74
|
+
|
|
75
|
+
ctx.tools.register(defineTool({
|
|
76
|
+
name: 'session_event_search',
|
|
77
|
+
description: 'Search prior events in one authorized session; the current session excludes the step performing this call.',
|
|
78
|
+
parameters: toolInput.eventSearchParameters,
|
|
79
|
+
output: TEXT_OUTPUT,
|
|
80
|
+
timeoutMs: resolved.searchTimeoutMs,
|
|
81
|
+
execute: (args, exec) => operations.executeEventSearch(ctx, args, exec, resolved.maxSearchResults),
|
|
82
|
+
presentCall: presentation.presentEventSearchCall,
|
|
83
|
+
}))
|
|
84
|
+
|
|
85
|
+
ctx.tools.register(defineTool({
|
|
86
|
+
name: 'session_trace',
|
|
87
|
+
description: 'Read the authorized session lineage around one session, including complete visible ancestor and descendant relationships.',
|
|
88
|
+
parameters: toolInput.targetSessionParameter,
|
|
89
|
+
output: TEXT_OUTPUT,
|
|
90
|
+
isConcurrencySafe: () => true,
|
|
91
|
+
execute: (args, exec) => operations.executeSessionTrace(ctx, args, exec),
|
|
92
|
+
presentCall: presentation.presentSessionTraceCall,
|
|
93
|
+
}))
|
|
94
|
+
|
|
95
|
+
ctx.tools.register(defineTool({
|
|
96
|
+
name: 'session_event_trace',
|
|
97
|
+
description: 'Read every direct replacement and relationship to a cited source event for one event in an authorized session.',
|
|
98
|
+
parameters: {
|
|
99
|
+
...toolInput.targetSessionParameter,
|
|
100
|
+
seq: { type: 'integer', required: true, description: 'Target event sequence number.' },
|
|
101
|
+
},
|
|
102
|
+
output: TEXT_OUTPUT,
|
|
103
|
+
isConcurrencySafe: () => true,
|
|
104
|
+
execute: (args, exec) => operations.executeEventTrace(ctx, args, exec),
|
|
105
|
+
presentCall: args => presentation.presentEventTargetCall('Trace event', args),
|
|
106
|
+
}))
|
|
107
|
+
|
|
108
|
+
ctx.tools.register(defineTool({
|
|
109
|
+
name: 'session_event_read',
|
|
110
|
+
description: 'Read one full unabridged event and optional neighboring raw-event summaries from an authorized session.',
|
|
111
|
+
parameters: {
|
|
112
|
+
...toolInput.targetSessionParameter,
|
|
113
|
+
seq: { type: 'integer', required: true, description: 'Target event sequence number.' },
|
|
114
|
+
before: { type: 'integer', description: 'Number of preceding raw events to summarize. Omit for none.' },
|
|
115
|
+
after: { type: 'integer', description: 'Number of following raw events to summarize. Omit for none.' },
|
|
116
|
+
},
|
|
117
|
+
output: TEXT_OUTPUT,
|
|
118
|
+
isConcurrencySafe: () => true,
|
|
119
|
+
execute: (args, exec) => operations.executeEventRead(ctx, args, exec),
|
|
120
|
+
presentCall: args => presentation.presentEventTargetCall('Read event', args),
|
|
121
|
+
}))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function resolveConfig(config: Config): ResolvedConfig {
|
|
125
|
+
const maxSearchResults = config.maxSearchResults ?? DEFAULT_MAX_SEARCH_RESULTS
|
|
126
|
+
const searchTimeoutMs = config.searchTimeoutMs ?? DEFAULT_SEARCH_TIMEOUT_MS
|
|
127
|
+
if (!Number.isSafeInteger(maxSearchResults) || maxSearchResults < 1) {
|
|
128
|
+
throw new TypeError('tool-session-query: maxSearchResults must be a positive safe integer')
|
|
129
|
+
}
|
|
130
|
+
if (!Number.isInteger(searchTimeoutMs) || searchTimeoutMs < 1 || searchTimeoutMs > MAX_TIMER_DELAY_MS) {
|
|
131
|
+
throw new TypeError(
|
|
132
|
+
`tool-session-query: searchTimeoutMs must be a positive integer no greater than ${MAX_TIMER_DELAY_MS}`,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
return { maxSearchResults, searchTimeoutMs }
|
|
136
|
+
}
|
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
|
+
}
|