File size: 2,772 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * `sessionMetadata` — typed session metadata. Mirrors
 * `agent-core-v2/session/sessionMetadata/sessionMetadata.ts`. The `ready`
 * promise property is excluded (not a wire method).
 */

import { z } from 'zod';

import { noResult } from '../helpers.js';
import type { ServiceContract } from '../types.js';

export const agentMetaSchema = z.object({
  homedir: z.string().optional(),
  type: z.enum(['main', 'sub', 'independent']).optional(),
  parentAgentId: z.union([z.string(), z.null()]).optional(),
  forkedFrom: z.string().optional(),
  labels: z.record(z.string(), z.string()).optional(),
  swarmItem: z.string().optional(),
});

export const sessionMetaSchema = z.object({
  id: z.string(),
  version: z.number().optional(),
  title: z.string().optional(),
  titleKind: z.enum(['replaceable', 'generated', 'custom']).optional(),
  lastPrompt: z.string().optional(),
  createdAt: z.number(),
  updatedAt: z.number(),
  archived: z.boolean(),
  archivedAt: z.number().optional(),
  cwd: z.string().optional(),
  forkedFrom: z.string().optional(),
  agents: z.record(z.string(), agentMetaSchema).optional(),
  custom: z.record(z.string(), z.unknown()).optional(),
  lastTurnReason: z.enum(['completed', 'cancelled', 'failed']).optional(),
});

/** `Partial<Omit<SessionMeta, 'id' | 'createdAt'>>` — every key optional. */
export const sessionMetaPatchSchema = z.object({
  version: z.number().optional(),
  title: z.string().optional(),
  titleKind: z.enum(['replaceable', 'generated', 'custom']).optional(),
  lastPrompt: z.string().optional(),
  updatedAt: z.number().optional(),
  archived: z.boolean().optional(),
  archivedAt: z.number().optional(),
  cwd: z.string().optional(),
  forkedFrom: z.string().optional(),
  agents: z.record(z.string(), agentMetaSchema).optional(),
  custom: z.record(z.string(), z.unknown()).optional(),
  lastTurnReason: z.enum(['completed', 'cancelled', 'failed']).optional(),
});

/** `keyof SessionMeta` — keep in sync with `sessionMetaSchema`. */
export const sessionMetaKeySchema = z.enum([
  'id',
  'version',
  'title',
  'titleKind',
  'lastPrompt',
  'createdAt',
  'updatedAt',
  'archived',
  'archivedAt',
  'cwd',
  'forkedFrom',
  'agents',
  'custom',
  'lastTurnReason',
]);

export const sessionMetadataChangedEventSchema = z.object({
  changed: z.array(sessionMetaKeySchema),
});

export const sessionMetadataContract = {
  read: { input: z.tuple([]), output: sessionMetaSchema },
  update: { input: z.tuple([sessionMetaPatchSchema]), output: noResult },
  setTitle: { input: z.tuple([z.string()]), output: noResult },
  setArchived: { input: z.tuple([z.boolean()]), output: noResult },
  registerAgent: { input: z.tuple([z.string(), agentMetaSchema]), output: noResult },
} satisfies ServiceContract;