@saturn-universe/protocol 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,215 @@
1
+ import type { EventPosition, HostCommand, ModelPricing, SessionDetails, SessionExportPage, SessionHistoryMessage, SessionHistoryPage, SessionListItem, SessionUsageBucket, SessionUsageCounts, SessionUsageSummary } from '@saturn-universe/core';
2
+ import { z } from 'zod';
3
+ import type { Assert, Equal, Wire } from './wire-shapes.js';
4
+ export type SessionSummary = {
5
+ readonly id: string;
6
+ readonly workspaceId?: string;
7
+ readonly mainTurn?: {
8
+ readonly ordinal: number;
9
+ readonly status: string;
10
+ };
11
+ } & Partial<Pick<SessionDetails, 'member' | 'runCount' | 'turnCount' | 'stepCount' | 'cursor' | 'latestRun' | 'todos'>>;
12
+ export interface SessionUsageReport extends SessionUsageSummary {
13
+ readonly modelId?: string;
14
+ readonly contextWindow?: number;
15
+ readonly pricing?: ModelPricing;
16
+ readonly pricingSource?: 'builtin';
17
+ }
18
+ type UsageBucketResult = SessionUsageCounts & Partial<Pick<SessionUsageBucket, 'requests' | 'unknownUsageRequests'>>;
19
+ export type SessionUsageResult = Omit<SessionUsageReport, 'main' | 'subagent' | 'total'> & {
20
+ readonly main: UsageBucketResult;
21
+ readonly subagent: UsageBucketResult;
22
+ readonly total: UsageBucketResult;
23
+ };
24
+ declare const sessionUsage: z.ZodObject<{
25
+ sessionId: z.ZodString;
26
+ model: z.ZodOptional<z.ZodObject<{
27
+ providerId: z.ZodString;
28
+ modelId: z.ZodString;
29
+ profileRef: z.ZodString;
30
+ }, z.core.$strip>>;
31
+ latest: z.ZodOptional<z.ZodObject<{
32
+ outputTokensPerSec: z.ZodOptional<z.ZodNumber>;
33
+ inputTokens: z.ZodNumber;
34
+ cachedInputTokens: z.ZodNumber;
35
+ outputTokens: z.ZodNumber;
36
+ reasoningTokens: z.ZodNumber;
37
+ }, z.core.$strip>>;
38
+ main: z.ZodObject<{
39
+ requests: z.ZodOptional<z.ZodNumber>;
40
+ unknownUsageRequests: z.ZodOptional<z.ZodNumber>;
41
+ inputTokens: z.ZodNumber;
42
+ cachedInputTokens: z.ZodNumber;
43
+ outputTokens: z.ZodNumber;
44
+ reasoningTokens: z.ZodNumber;
45
+ }, z.core.$strip>;
46
+ subagent: z.ZodObject<{
47
+ requests: z.ZodOptional<z.ZodNumber>;
48
+ unknownUsageRequests: z.ZodOptional<z.ZodNumber>;
49
+ inputTokens: z.ZodNumber;
50
+ cachedInputTokens: z.ZodNumber;
51
+ outputTokens: z.ZodNumber;
52
+ reasoningTokens: z.ZodNumber;
53
+ }, z.core.$strip>;
54
+ total: z.ZodObject<{
55
+ requests: z.ZodOptional<z.ZodNumber>;
56
+ unknownUsageRequests: z.ZodOptional<z.ZodNumber>;
57
+ inputTokens: z.ZodNumber;
58
+ cachedInputTokens: z.ZodNumber;
59
+ outputTokens: z.ZodNumber;
60
+ reasoningTokens: z.ZodNumber;
61
+ }, z.core.$strip>;
62
+ avgOutputTokensPerSec: z.ZodOptional<z.ZodNumber>;
63
+ modelId: z.ZodOptional<z.ZodString>;
64
+ contextWindow: z.ZodOptional<z.ZodNumber>;
65
+ pricing: z.ZodOptional<z.ZodObject<{
66
+ inputPerMillion: z.ZodNumber;
67
+ cachedInputPerMillion: z.ZodOptional<z.ZodNumber>;
68
+ outputPerMillion: z.ZodNumber;
69
+ }, z.core.$strip>>;
70
+ pricingSource: z.ZodOptional<z.ZodLiteral<"builtin">>;
71
+ }, z.core.$strip>;
72
+ export declare function parseSessionUsageResult(value: unknown): SessionUsageResult | undefined;
73
+ declare const sessionSummary: z.ZodObject<{
74
+ id: z.ZodString;
75
+ workspaceId: z.ZodOptional<z.ZodString>;
76
+ member: z.ZodOptional<z.ZodObject<{
77
+ id: z.ZodString;
78
+ agentSessionId: z.ZodString;
79
+ parentSessionId: z.ZodString;
80
+ }, z.core.$strip>>;
81
+ runCount: z.ZodOptional<z.ZodNumber>;
82
+ turnCount: z.ZodOptional<z.ZodNumber>;
83
+ stepCount: z.ZodOptional<z.ZodNumber>;
84
+ cursor: z.ZodOptional<z.ZodCustom<string, string>>;
85
+ mainTurn: z.ZodOptional<z.ZodObject<{
86
+ ordinal: z.ZodNumber;
87
+ status: z.ZodString;
88
+ }, z.core.$strip>>;
89
+ latestRun: z.ZodOptional<z.ZodObject<{
90
+ id: z.ZodString;
91
+ status: z.ZodString;
92
+ pauseReason: z.ZodOptional<z.ZodString>;
93
+ }, z.core.$strip>>;
94
+ todos: z.ZodOptional<z.ZodArray<z.ZodObject<{
95
+ id: z.ZodString;
96
+ content: z.ZodString;
97
+ status: z.ZodEnum<{
98
+ pending: "pending";
99
+ in_progress: "in_progress";
100
+ completed: "completed";
101
+ cancelled: "cancelled";
102
+ }>;
103
+ }, z.core.$strip>>>;
104
+ }, z.core.$strip>;
105
+ export declare function parseSessionSummaryResult(value: unknown, requestedId: string): SessionSummary | undefined;
106
+ declare const historyMessage: z.ZodObject<{
107
+ id: z.ZodString;
108
+ runId: z.ZodString;
109
+ position: z.ZodCustom<string, string>;
110
+ role: z.ZodEnum<{
111
+ user: "user";
112
+ assistant: "assistant";
113
+ tool: "tool";
114
+ thinking: "thinking";
115
+ }>;
116
+ text: z.ZodString;
117
+ requestId: z.ZodOptional<z.ZodString>;
118
+ toolCallId: z.ZodOptional<z.ZodString>;
119
+ toolName: z.ZodOptional<z.ZodString>;
120
+ toolStatus: z.ZodOptional<z.ZodEnum<{
121
+ error: "error";
122
+ success: "success";
123
+ cancelled: "cancelled";
124
+ denied: "denied";
125
+ }>>;
126
+ toolArguments: z.ZodOptional<z.ZodString>;
127
+ toolOutput: z.ZodOptional<z.ZodString>;
128
+ toolData: z.ZodOptional<z.ZodJSONSchema>;
129
+ error: z.ZodOptional<z.ZodObject<{
130
+ code: z.ZodString;
131
+ message: z.ZodOptional<z.ZodString>;
132
+ }, z.core.$strip>>;
133
+ nextOffset: z.ZodOptional<z.ZodNumber>;
134
+ }, z.core.$strip>;
135
+ declare const historyPage: z.ZodObject<{
136
+ through: z.ZodCustom<EventPosition, EventPosition>;
137
+ messages: z.ZodArray<z.ZodObject<{
138
+ id: z.ZodString;
139
+ runId: z.ZodString;
140
+ position: z.ZodCustom<string, string>;
141
+ role: z.ZodEnum<{
142
+ user: "user";
143
+ assistant: "assistant";
144
+ tool: "tool";
145
+ thinking: "thinking";
146
+ }>;
147
+ text: z.ZodString;
148
+ requestId: z.ZodOptional<z.ZodString>;
149
+ toolCallId: z.ZodOptional<z.ZodString>;
150
+ toolName: z.ZodOptional<z.ZodString>;
151
+ toolStatus: z.ZodOptional<z.ZodEnum<{
152
+ error: "error";
153
+ success: "success";
154
+ cancelled: "cancelled";
155
+ denied: "denied";
156
+ }>>;
157
+ toolArguments: z.ZodOptional<z.ZodString>;
158
+ toolOutput: z.ZodOptional<z.ZodString>;
159
+ toolData: z.ZodOptional<z.ZodJSONSchema>;
160
+ error: z.ZodOptional<z.ZodObject<{
161
+ code: z.ZodString;
162
+ message: z.ZodOptional<z.ZodString>;
163
+ }, z.core.$strip>>;
164
+ nextOffset: z.ZodOptional<z.ZodNumber>;
165
+ }, z.core.$strip>>;
166
+ nextBefore: z.ZodOptional<z.ZodCustom<string, string>>;
167
+ }, z.core.$strip>;
168
+ export declare function validSessionHistoryResult(command: Extract<HostCommand, {
169
+ type: 'session.history' | 'session.history.message';
170
+ }>, value: unknown): boolean;
171
+ declare const sessionList: z.ZodArray<z.ZodObject<{
172
+ id: z.ZodString;
173
+ workspaceId: z.ZodString;
174
+ title: z.ZodString;
175
+ status: z.ZodString;
176
+ createdAt: z.ZodString;
177
+ updatedAt: z.ZodString;
178
+ modeRef: z.ZodOptional<z.ZodString>;
179
+ firstUserMessage: z.ZodOptional<z.ZodString>;
180
+ runCount: z.ZodNumber;
181
+ latestRunId: z.ZodOptional<z.ZodString>;
182
+ latestRunStatus: z.ZodOptional<z.ZodString>;
183
+ }, z.core.$strip>>;
184
+ declare const sessionExportPage: z.ZodObject<{
185
+ sessionId: z.ZodString;
186
+ through: z.ZodCustom<EventPosition, EventPosition>;
187
+ records: z.ZodArray<z.ZodObject<{
188
+ position: z.ZodCustom<string, string>;
189
+ ts: z.ZodString;
190
+ sessionId: z.ZodString;
191
+ runId: z.ZodNullable<z.ZodString>;
192
+ agentId: z.ZodNullable<z.ZodString>;
193
+ agentRole: z.ZodEnum<{
194
+ main: "main";
195
+ worker: "worker";
196
+ }>;
197
+ memberId: z.ZodOptional<z.ZodString>;
198
+ kind: z.ZodString;
199
+ payload: z.ZodUnknown;
200
+ }, z.core.$strip>>;
201
+ nextAfter: z.ZodOptional<z.ZodCustom<string, string>>;
202
+ }, z.core.$strip>;
203
+ export declare function parseSessionListResult(value: unknown, workspaceId?: string, limit?: number): readonly SessionListItem[] | undefined;
204
+ export declare function parseSessionExportResult(value: unknown, request: Pick<Extract<HostCommand, {
205
+ type: 'session.export';
206
+ }>, 'after' | 'through' | 'limit'>): SessionExportPage | undefined;
207
+ export type SessionResultSchemasMatchCore = [
208
+ Assert<Equal<Wire<z.output<typeof sessionUsage>>, Wire<SessionUsageResult>>>,
209
+ Assert<Equal<Wire<z.output<typeof sessionSummary>>, Wire<SessionSummary>>>,
210
+ Assert<Equal<Wire<z.output<typeof sessionList>>, Wire<readonly SessionListItem[]>>>,
211
+ Assert<Equal<Wire<z.output<typeof sessionExportPage>>, Wire<SessionExportPage>>>,
212
+ Assert<Equal<Wire<z.output<typeof historyMessage>>, Wire<SessionHistoryMessage>>>,
213
+ Assert<Equal<Wire<z.output<typeof historyPage>>, Wire<SessionHistoryPage>>>
214
+ ];
215
+ export {};
@@ -0,0 +1,174 @@
1
+ import { DEFAULT_SESSION_EXPORT_LIMIT, isEventPosition, MAX_SESSION_EXPORT_LIMIT, MAX_SESSION_LIST_LIMIT, todoStatuses, } from '@saturn-universe/core';
2
+ import { z } from 'zod';
3
+ import { modelPricingSchema, runStopDetailsSchema } from './wire-shapes.js';
4
+ const id = z.string().min(1);
5
+ const count = z.number().int().nonnegative();
6
+ const usageCounts = {
7
+ inputTokens: count,
8
+ cachedInputTokens: count,
9
+ outputTokens: count,
10
+ reasoningTokens: count,
11
+ };
12
+ const usageBucket = z.object({
13
+ ...usageCounts,
14
+ requests: count.optional(),
15
+ unknownUsageRequests: count.optional(),
16
+ });
17
+ const sessionUsage = z.object({
18
+ sessionId: id,
19
+ model: z.object({ providerId: z.string(), modelId: z.string(), profileRef: z.string() }).optional(),
20
+ latest: z.object({ ...usageCounts, outputTokensPerSec: z.number().nonnegative().optional() }).optional(),
21
+ main: usageBucket,
22
+ subagent: usageBucket,
23
+ total: usageBucket,
24
+ avgOutputTokensPerSec: z.number().nonnegative().optional(),
25
+ modelId: z.string().optional(),
26
+ contextWindow: z.number().optional(),
27
+ pricing: modelPricingSchema.optional(),
28
+ pricingSource: z.literal('builtin').optional(),
29
+ });
30
+ export function parseSessionUsageResult(value) {
31
+ return sessionUsage.safeParse(value).success ? value : undefined;
32
+ }
33
+ const sessionSummary = z.object({
34
+ id,
35
+ workspaceId: id.optional(),
36
+ member: z.object({ id, agentSessionId: id, parentSessionId: id }).optional(),
37
+ runCount: count.optional(),
38
+ turnCount: count.optional(),
39
+ stepCount: count.optional(),
40
+ cursor: z.custom(isEventPosition).optional(),
41
+ mainTurn: z.object({ ordinal: count, status: z.string() }).optional(),
42
+ latestRun: z
43
+ .object({ id, status: z.string(), pauseReason: runStopDetailsSchema.shape.pauseReason })
44
+ .optional(),
45
+ todos: z.array(z.object({ id: z.string(), content: z.string(), status: z.enum(todoStatuses) })).optional(),
46
+ });
47
+ export function parseSessionSummaryResult(value, requestedId) {
48
+ const result = sessionSummary.safeParse(value);
49
+ if (!result.success)
50
+ return undefined;
51
+ const { id, member } = result.data;
52
+ const matches = member
53
+ ? member.agentSessionId === requestedId && member.parentSessionId === id
54
+ : id === requestedId;
55
+ return matches ? value : undefined;
56
+ }
57
+ const resultPosition = z.custom(isEventPosition);
58
+ const historyMessage = z.object({
59
+ id: z.string(),
60
+ runId: z.string(),
61
+ position: resultPosition,
62
+ role: z.enum(['user', 'assistant', 'tool', 'thinking']),
63
+ text: z.string(),
64
+ requestId: z.string().optional(),
65
+ toolCallId: z.string().optional(),
66
+ toolName: z.string().optional(),
67
+ toolStatus: z.enum(['success', 'error', 'denied', 'cancelled']).optional(),
68
+ toolArguments: z.string().optional(),
69
+ toolOutput: z.string().optional(),
70
+ toolData: z.json().optional(),
71
+ error: z.object({ code: z.string(), message: z.string().optional() }).optional(),
72
+ nextOffset: z.number().int().positive().optional(),
73
+ });
74
+ const historyPage = z.object({
75
+ through: z.custom(isEventPosition),
76
+ messages: z.array(historyMessage),
77
+ nextBefore: resultPosition.optional(),
78
+ });
79
+ function validHistoryOffset(message, offset) {
80
+ const { text, nextOffset } = message;
81
+ return nextOffset === undefined || (text.length > 0 && nextOffset === offset + text.length);
82
+ }
83
+ export function validSessionHistoryResult(command, value) {
84
+ if (command.type === 'session.history.message') {
85
+ const chunk = historyMessage.safeParse(value);
86
+ return (chunk.success &&
87
+ chunk.data.position === command.position &&
88
+ validHistoryOffset(chunk.data, command.offset));
89
+ }
90
+ const result = historyPage.safeParse(value);
91
+ if (!result.success)
92
+ return false;
93
+ const { through, messages, nextBefore } = result.data;
94
+ if ((command.through !== undefined && through !== command.through) ||
95
+ messages.length > command.limit ||
96
+ (nextBefore !== undefined && nextBefore !== messages[0]?.position))
97
+ return false;
98
+ const snapshot = BigInt(through);
99
+ const before = command.before === undefined ? undefined : BigInt(command.before);
100
+ let previous = 0n;
101
+ for (const message of messages) {
102
+ const position = BigInt(message.position);
103
+ if (position <= previous || position > snapshot || (before !== undefined && position >= before))
104
+ return false;
105
+ if (!validHistoryOffset(message, 0))
106
+ return false;
107
+ previous = position;
108
+ }
109
+ return true;
110
+ }
111
+ const sessionList = z
112
+ .array(z.object({
113
+ id,
114
+ workspaceId: id,
115
+ title: z.string(),
116
+ status: z.string(),
117
+ createdAt: z.string(),
118
+ updatedAt: z.string(),
119
+ modeRef: z.string().optional(),
120
+ firstUserMessage: z.string().optional(),
121
+ runCount: count,
122
+ latestRunId: z.string().optional(),
123
+ latestRunStatus: z.string().optional(),
124
+ }))
125
+ .max(MAX_SESSION_LIST_LIMIT);
126
+ const sessionExportPage = z.object({
127
+ sessionId: z.string().min(1),
128
+ through: z.custom(isEventPosition),
129
+ records: z
130
+ .array(z.object({
131
+ position: resultPosition,
132
+ ts: z.string(),
133
+ sessionId: z.string(),
134
+ runId: z.string().nullable(),
135
+ agentId: z.string().nullable(),
136
+ agentRole: z.enum(['main', 'worker']),
137
+ memberId: z.string().optional(),
138
+ kind: z.string(),
139
+ payload: z.unknown(),
140
+ }))
141
+ .max(MAX_SESSION_EXPORT_LIMIT),
142
+ nextAfter: resultPosition.optional(),
143
+ });
144
+ export function parseSessionListResult(value, workspaceId, limit) {
145
+ const result = sessionList.safeParse(value);
146
+ if (!result.success || (limit !== undefined && result.data.length > limit))
147
+ return undefined;
148
+ if (workspaceId !== undefined && result.data.some((session) => session.workspaceId !== workspaceId))
149
+ return undefined;
150
+ return result.data;
151
+ }
152
+ export function parseSessionExportResult(value, request) {
153
+ const result = sessionExportPage.safeParse(value);
154
+ if (!result.success)
155
+ return undefined;
156
+ const { sessionId, through, records, nextAfter } = result.data;
157
+ if (records.length > (request.limit ?? DEFAULT_SESSION_EXPORT_LIMIT))
158
+ return undefined;
159
+ if (request.through !== undefined && through !== request.through)
160
+ return undefined;
161
+ const snapshot = BigInt(through);
162
+ let previous = BigInt(request.after ?? '0');
163
+ for (const record of records) {
164
+ if (record.sessionId !== sessionId)
165
+ return undefined;
166
+ const position = BigInt(record.position);
167
+ if (position <= previous || position > snapshot)
168
+ return undefined;
169
+ previous = position;
170
+ }
171
+ if (nextAfter !== undefined && records.at(-1)?.position !== nextAfter)
172
+ return undefined;
173
+ return result.data;
174
+ }
@@ -0,0 +1,68 @@
1
+ export type SkillScope = 'project' | 'user' | 'mode' | 'config';
2
+ export interface DiscoveredSkillSummary {
3
+ readonly name: string;
4
+ readonly description: string;
5
+ readonly scope: SkillScope;
6
+ readonly source: string;
7
+ readonly resources: readonly string[];
8
+ readonly ref?: string;
9
+ readonly license?: string;
10
+ readonly compatibility?: string;
11
+ readonly allowedTools?: string;
12
+ readonly indexedBySecurityPlugin?: boolean;
13
+ }
14
+ export interface DiscoveredSkill extends DiscoveredSkillSummary {
15
+ readonly rootDir: string;
16
+ readonly skillFilePath: string;
17
+ }
18
+ export interface ModeSkillDirectory {
19
+ readonly dir: string;
20
+ readonly source: string;
21
+ readonly indexedBySecurityPlugin?: boolean;
22
+ readonly allowedSkills?: readonly string[];
23
+ }
24
+ export interface ExplicitSkillEntry {
25
+ readonly path: string;
26
+ readonly resources?: readonly string[] | undefined;
27
+ }
28
+ export declare const skillNamePattern: RegExp;
29
+ export declare function userSkillsDir(env?: NodeJS.ProcessEnv): string;
30
+ export declare function agentsSkillsDir(env?: NodeJS.ProcessEnv): string;
31
+ interface SkillDirectory {
32
+ readonly dir: string;
33
+ readonly source: string;
34
+ }
35
+ export declare function projectSkillsDir(workspaceRoot: string): string;
36
+ export declare function projectSkillsDirs(workspaceRoot: string): readonly SkillDirectory[];
37
+ export declare function localSkillSearchDirectories(workspaceRoot: string, env?: NodeJS.ProcessEnv): readonly (SkillDirectory & {
38
+ readonly scope: 'project' | 'user';
39
+ })[];
40
+ export declare function isInsideSkillDir(rootDir: string, targetPath: string): boolean;
41
+ export declare function validateRelativeSkillResourcePath(rawPath: unknown): {
42
+ readonly ok: true;
43
+ readonly normalized: string;
44
+ } | {
45
+ readonly ok: false;
46
+ readonly code: string;
47
+ readonly message: string;
48
+ };
49
+ export declare function parseSkillFrontmatter(markdown: string): Record<string, string>;
50
+ export declare function rewriteSkillFrontmatterName(markdown: string, alias: string): string;
51
+ export declare function inspectSkillDirectory(skillDir: string): Promise<{
52
+ readonly name: string;
53
+ readonly description: string;
54
+ readonly rootDir: string;
55
+ readonly skillFilePath: string;
56
+ readonly resources: readonly string[];
57
+ readonly frontmatter: Readonly<Record<string, string>>;
58
+ }>;
59
+ export declare function discoverAgentSkills(workspaceRoot: string, options?: {
60
+ readonly env?: NodeJS.ProcessEnv;
61
+ readonly modeSkillDirs?: readonly ModeSkillDirectory[];
62
+ readonly explicitSkills?: Readonly<Record<string, ExplicitSkillEntry>>;
63
+ }): Promise<DiscoveredSkill[]>;
64
+ export declare function formatCompactSkillIndex(skills: readonly DiscoveredSkillSummary[], options?: {
65
+ readonly maxTotalChars?: number;
66
+ readonly maxDescriptionChars?: number;
67
+ }): string;
68
+ export {};