@zyro-sec/schema 1.0.1 → 1.0.3

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.
Files changed (136) hide show
  1. package/AGENTS.md +95 -95
  2. package/package.json +5 -1
  3. package/script/build.ts +9 -9
  4. package/script/config-schema.ts +86 -86
  5. package/script/config-v1.json +1311 -1311
  6. package/script/pack.ts +5 -5
  7. package/src/agent.ts +68 -68
  8. package/src/artifact.ts +81 -81
  9. package/src/automation.ts +224 -224
  10. package/src/blob-store.ts +25 -25
  11. package/src/command.ts +18 -18
  12. package/src/config/agent.ts +26 -26
  13. package/src/config/command.ts +14 -14
  14. package/src/config/compaction.ts +27 -27
  15. package/src/config/experimental.ts +72 -72
  16. package/src/config/flag.ts +39 -39
  17. package/src/config/formatter.ts +13 -13
  18. package/src/config/lsp.ts +19 -19
  19. package/src/config/mcp.ts +22 -22
  20. package/src/config/media.ts +15 -15
  21. package/src/config/model.ts +37 -37
  22. package/src/config/plugin.ts +14 -14
  23. package/src/config/policy.ts +57 -57
  24. package/src/config/provider.ts +122 -122
  25. package/src/config/reference.ts +23 -23
  26. package/src/config/shell.ts +10 -10
  27. package/src/config/tool-output.ts +9 -9
  28. package/src/config/warming.ts +21 -21
  29. package/src/config/watcher.ts +8 -8
  30. package/src/config/websearch.ts +17 -17
  31. package/src/config/worktree.ts +10 -10
  32. package/src/config.ts +149 -149
  33. package/src/connection.ts +38 -38
  34. package/src/credential-legacy.ts +29 -29
  35. package/src/credential.ts +78 -78
  36. package/src/durable-event-manifest.ts +7 -7
  37. package/src/event-log.ts +21 -21
  38. package/src/event-manifest.ts +108 -108
  39. package/src/event.ts +205 -205
  40. package/src/file-diff.ts +25 -25
  41. package/src/filesystem-v1.ts +1 -1
  42. package/src/filesystem.ts +48 -48
  43. package/src/form.ts +177 -177
  44. package/src/generate.ts +292 -292
  45. package/src/goal.ts +46 -46
  46. package/src/identifier.ts +32 -32
  47. package/src/index.ts +51 -51
  48. package/src/installation-event.ts +20 -20
  49. package/src/instruction-entry.ts +40 -40
  50. package/src/instruction.ts +25 -25
  51. package/src/integration-id.ts +13 -13
  52. package/src/integration.ts +129 -129
  53. package/src/job.ts +79 -79
  54. package/src/legacy-event.ts +1 -1
  55. package/src/llm.ts +6 -6
  56. package/src/location-event.ts +8 -8
  57. package/src/location.ts +36 -36
  58. package/src/lsp-event.ts +7 -7
  59. package/src/mcp-event.ts +63 -63
  60. package/src/mcp.ts +171 -171
  61. package/src/model-compatibility.ts +55 -55
  62. package/src/model.ts +176 -176
  63. package/src/models-dev.ts +9 -9
  64. package/src/money.ts +18 -18
  65. package/src/permission-saved.ts +25 -25
  66. package/src/permission-v1.ts +1 -1
  67. package/src/permission.ts +67 -67
  68. package/src/persistent-pty.ts +73 -73
  69. package/src/plugin.ts +62 -62
  70. package/src/project-id.ts +9 -9
  71. package/src/project.ts +62 -62
  72. package/src/prompt-input.ts +33 -33
  73. package/src/prompt.ts +117 -117
  74. package/src/provider.ts +136 -136
  75. package/src/pty-ticket.ts +10 -10
  76. package/src/pty.ts +61 -61
  77. package/src/push.ts +86 -86
  78. package/src/question.ts +22 -22
  79. package/src/reference.ts +40 -40
  80. package/src/rpc.ts +167 -167
  81. package/src/schema.ts +41 -41
  82. package/src/server-event.ts +8 -8
  83. package/src/session-error.ts +14 -14
  84. package/src/session-event.ts +739 -739
  85. package/src/session-fork.ts +10 -10
  86. package/src/session-id.ts +12 -12
  87. package/src/session-inbox.ts +102 -102
  88. package/src/session-legacy.ts +117 -117
  89. package/src/session-message.ts +352 -352
  90. package/src/session-metadata.ts +11 -11
  91. package/src/session-revert.ts +86 -86
  92. package/src/session-stats.ts +68 -68
  93. package/src/session-status-event.ts +53 -53
  94. package/src/session-transfer.ts +11 -11
  95. package/src/session-v1.ts +1 -1
  96. package/src/session.ts +147 -147
  97. package/src/shell.ts +90 -90
  98. package/src/skill.ts +68 -68
  99. package/src/snapshot.ts +6 -6
  100. package/src/token-usage.ts +18 -18
  101. package/src/tool.ts +143 -143
  102. package/src/tui-event.ts +59 -59
  103. package/src/usage-limits.ts +76 -76
  104. package/src/v1/filesystem.ts +11 -11
  105. package/src/v1/legacy-event.ts +18 -18
  106. package/src/v1/permission.ts +66 -66
  107. package/src/v1/session.ts +667 -667
  108. package/src/vcs-event.ts +14 -14
  109. package/src/vcs.ts +41 -41
  110. package/src/websearch.ts +44 -44
  111. package/src/workspace-event.ts +32 -32
  112. package/src/workspace-id.ts +19 -19
  113. package/src/workspace.ts +20 -20
  114. package/src/worktree-event.ts +22 -22
  115. package/src/worktree.ts +109 -109
  116. package/tsconfig.build.json +11 -11
  117. package/tsconfig.json +14 -14
  118. package/test/agent.test.ts +0 -10
  119. package/test/automation.test.ts +0 -48
  120. package/test/compatibility.test.ts +0 -10
  121. package/test/config.test.ts +0 -194
  122. package/test/contract-hygiene.test.ts +0 -352
  123. package/test/credential-legacy.test.ts +0 -55
  124. package/test/event-manifest.test.ts +0 -228
  125. package/test/event.test.ts +0 -52
  126. package/test/legacy-event.test.ts +0 -46
  127. package/test/mcp.test.ts +0 -37
  128. package/test/model.test.ts +0 -104
  129. package/test/session-cursor.test.ts +0 -16
  130. package/test/session-error.test.ts +0 -51
  131. package/test/session-legacy.test.ts +0 -178
  132. package/test/session-terminal.test.ts +0 -50
  133. package/test/token-usage.test.ts +0 -18
  134. package/test/v1-isolation.test.ts +0 -26
  135. package/test/vcs.test.ts +0 -23
  136. package/test/worktree.test.ts +0 -45
@@ -1,108 +1,108 @@
1
- export * as EventManifest from "./event-manifest.js"
2
-
3
- import { Schema } from "effect"
4
- import { Agent } from "./agent.js"
5
- import { Artifact } from "./artifact.js"
6
- import { Command } from "./command.js"
7
- import { Config } from "./config.js"
8
- import { Credential } from "./credential.js"
9
- import { Durable } from "./durable-event-manifest.js"
10
- import { Event } from "./event.js"
11
- import { FileSystem } from "./filesystem.js"
12
- import { FileSystemV1 } from "./filesystem-v1.js"
13
- import { Form } from "./form.js"
14
- import { InstallationEvent } from "./installation-event.js"
15
- import { Job } from "./job.js"
16
- import { Integration } from "./integration.js"
17
- import { LegacyEventV1 } from "./legacy-event.js"
18
- import { LspEvent } from "./lsp-event.js"
19
- import { LocationEvent } from "./location-event.js"
20
- import { McpEvent } from "./mcp-event.js"
21
- import { Model } from "./model.js"
22
- import { ModelsDev } from "./models-dev.js"
23
- import { Permission } from "./permission.js"
24
- import { PersistentPty } from "./persistent-pty.js"
25
- import { Plugin } from "./plugin.js"
26
- import { Project } from "./project.js"
27
- import { Provider } from "./provider.js"
28
- import { Worktree } from "./worktree.js"
29
- import { Pty } from "./pty.js"
30
- import { Reference } from "./reference.js"
31
- import { ServerEvent } from "./server-event.js"
32
- import { Shell } from "./shell.js"
33
- import { Skill } from "./skill.js"
34
- import { SessionEvent } from "./session-event.js"
35
- import { SessionStatusEvent } from "./session-status-event.js"
36
- import { TuiEvent } from "./tui-event.js"
37
- import { VcsEvent } from "./vcs-event.js"
38
- import { WorkspaceEvent } from "./workspace-event.js"
39
- import { WorktreeEvent } from "./worktree-event.js"
40
- import { WebSearch } from "./websearch.js"
41
- import { Push } from "./push.js"
42
-
43
- const coreDefinitions = Event.inventory(...SessionEvent.Definitions)
44
-
45
- const foundationDefinitions = Event.inventory(
46
- ...LocationEvent.Definitions,
47
- ...ModelsDev.Event.Definitions,
48
- ...Credential.Event.Definitions,
49
- ...Integration.Event.Definitions,
50
- ...Provider.Event.Definitions,
51
- ...Model.Event.Definitions,
52
- ...Agent.Event.Definitions,
53
- ...coreDefinitions,
54
- )
55
-
56
- const featureDefinitions = Event.inventory(
57
- ...FileSystem.Event.Definitions,
58
- ...Reference.Event.Definitions,
59
- ...Permission.Event.Definitions,
60
- ...Plugin.Event.Definitions,
61
- ...Project.Event.Definitions,
62
- ...Worktree.Event.Definitions,
63
- ...Command.Event.Definitions,
64
- ...Config.Event.Definitions,
65
- ...Skill.Event.Definitions,
66
- ...Pty.Event.Definitions,
67
- ...PersistentPty.Event.Definitions,
68
- ...Shell.Event.Definitions,
69
- ...Job.Event.Definitions,
70
- ...Form.Event.Definitions,
71
- ...WebSearch.Event.Definitions,
72
- ...Push.Event.Definitions,
73
- ...Artifact.Event.Definitions,
74
- )
75
-
76
- export const ServerDefinitions = Event.inventory(
77
- ...foundationDefinitions,
78
- ...featureDefinitions,
79
- // Current events the TUI consumes from the public stream.
80
- ...SessionStatusEvent.Definitions,
81
- ...TuiEvent.Definitions,
82
- ...InstallationEvent.Definitions,
83
- ...VcsEvent.Definitions,
84
- McpEvent.StatusChanged,
85
- McpEvent.ResourcesChanged,
86
- McpEvent.ExperimentalEvent,
87
- )
88
- export const Server = Event.latest(ServerDefinitions)
89
- export type ServerEvent = Schema.Schema.Type<(typeof ServerDefinitions)[number]>
90
- export const isServer = (event: { readonly type: string }): event is ServerEvent => Server.has(event.type)
91
-
92
- export const Definitions = Event.inventory(
93
- ...foundationDefinitions,
94
- ...InstallationEvent.Definitions,
95
- ...featureDefinitions,
96
- ...LspEvent.Definitions,
97
- ...TuiEvent.Definitions,
98
- ...McpEvent.Definitions,
99
- ...LegacyEventV1.Definitions,
100
- ...FileSystemV1.Event.Definitions,
101
- ...SessionStatusEvent.Definitions,
102
- ...VcsEvent.Definitions,
103
- ...WorkspaceEvent.Definitions,
104
- ...WorktreeEvent.Definitions,
105
- ...ServerEvent.Definitions,
106
- )
107
- export const Latest = Event.latest(Definitions)
108
- export { Durable }
1
+ export * as EventManifest from "./event-manifest.js"
2
+
3
+ import { Schema } from "effect"
4
+ import { Agent } from "./agent.js"
5
+ import { Artifact } from "./artifact.js"
6
+ import { Command } from "./command.js"
7
+ import { Config } from "./config.js"
8
+ import { Credential } from "./credential.js"
9
+ import { Durable } from "./durable-event-manifest.js"
10
+ import { Event } from "./event.js"
11
+ import { FileSystem } from "./filesystem.js"
12
+ import { FileSystemV1 } from "./filesystem-v1.js"
13
+ import { Form } from "./form.js"
14
+ import { InstallationEvent } from "./installation-event.js"
15
+ import { Job } from "./job.js"
16
+ import { Integration } from "./integration.js"
17
+ import { LegacyEventV1 } from "./legacy-event.js"
18
+ import { LspEvent } from "./lsp-event.js"
19
+ import { LocationEvent } from "./location-event.js"
20
+ import { McpEvent } from "./mcp-event.js"
21
+ import { Model } from "./model.js"
22
+ import { ModelsDev } from "./models-dev.js"
23
+ import { Permission } from "./permission.js"
24
+ import { PersistentPty } from "./persistent-pty.js"
25
+ import { Plugin } from "./plugin.js"
26
+ import { Project } from "./project.js"
27
+ import { Provider } from "./provider.js"
28
+ import { Worktree } from "./worktree.js"
29
+ import { Pty } from "./pty.js"
30
+ import { Reference } from "./reference.js"
31
+ import { ServerEvent } from "./server-event.js"
32
+ import { Shell } from "./shell.js"
33
+ import { Skill } from "./skill.js"
34
+ import { SessionEvent } from "./session-event.js"
35
+ import { SessionStatusEvent } from "./session-status-event.js"
36
+ import { TuiEvent } from "./tui-event.js"
37
+ import { VcsEvent } from "./vcs-event.js"
38
+ import { WorkspaceEvent } from "./workspace-event.js"
39
+ import { WorktreeEvent } from "./worktree-event.js"
40
+ import { WebSearch } from "./websearch.js"
41
+ import { Push } from "./push.js"
42
+
43
+ const coreDefinitions = Event.inventory(...SessionEvent.Definitions)
44
+
45
+ const foundationDefinitions = Event.inventory(
46
+ ...LocationEvent.Definitions,
47
+ ...ModelsDev.Event.Definitions,
48
+ ...Credential.Event.Definitions,
49
+ ...Integration.Event.Definitions,
50
+ ...Provider.Event.Definitions,
51
+ ...Model.Event.Definitions,
52
+ ...Agent.Event.Definitions,
53
+ ...coreDefinitions,
54
+ )
55
+
56
+ const featureDefinitions = Event.inventory(
57
+ ...FileSystem.Event.Definitions,
58
+ ...Reference.Event.Definitions,
59
+ ...Permission.Event.Definitions,
60
+ ...Plugin.Event.Definitions,
61
+ ...Project.Event.Definitions,
62
+ ...Worktree.Event.Definitions,
63
+ ...Command.Event.Definitions,
64
+ ...Config.Event.Definitions,
65
+ ...Skill.Event.Definitions,
66
+ ...Pty.Event.Definitions,
67
+ ...PersistentPty.Event.Definitions,
68
+ ...Shell.Event.Definitions,
69
+ ...Job.Event.Definitions,
70
+ ...Form.Event.Definitions,
71
+ ...WebSearch.Event.Definitions,
72
+ ...Push.Event.Definitions,
73
+ ...Artifact.Event.Definitions,
74
+ )
75
+
76
+ export const ServerDefinitions = Event.inventory(
77
+ ...foundationDefinitions,
78
+ ...featureDefinitions,
79
+ // Current events the TUI consumes from the public stream.
80
+ ...SessionStatusEvent.Definitions,
81
+ ...TuiEvent.Definitions,
82
+ ...InstallationEvent.Definitions,
83
+ ...VcsEvent.Definitions,
84
+ McpEvent.StatusChanged,
85
+ McpEvent.ResourcesChanged,
86
+ McpEvent.ExperimentalEvent,
87
+ )
88
+ export const Server = Event.latest(ServerDefinitions)
89
+ export type ServerEvent = Schema.Schema.Type<(typeof ServerDefinitions)[number]>
90
+ export const isServer = (event: { readonly type: string }): event is ServerEvent => Server.has(event.type)
91
+
92
+ export const Definitions = Event.inventory(
93
+ ...foundationDefinitions,
94
+ ...InstallationEvent.Definitions,
95
+ ...featureDefinitions,
96
+ ...LspEvent.Definitions,
97
+ ...TuiEvent.Definitions,
98
+ ...McpEvent.Definitions,
99
+ ...LegacyEventV1.Definitions,
100
+ ...FileSystemV1.Event.Definitions,
101
+ ...SessionStatusEvent.Definitions,
102
+ ...VcsEvent.Definitions,
103
+ ...WorkspaceEvent.Definitions,
104
+ ...WorktreeEvent.Definitions,
105
+ ...ServerEvent.Definitions,
106
+ )
107
+ export const Latest = Event.latest(Definitions)
108
+ export { Durable }
package/src/event.ts CHANGED
@@ -1,205 +1,205 @@
1
- export * as Event from "./event.js"
2
-
3
- import { Schema, SchemaTransformation } from "effect"
4
- import { optional } from "./schema.js"
5
- import { ascending } from "./identifier.js"
6
- import { Location } from "./location.js"
7
- import { statics } from "./schema.js"
8
-
9
- export const ID = Schema.String.check(Schema.isStartingWith("evt_")).pipe(
10
- Schema.brand("Event.ID"),
11
- Schema.annotate({ identifier: "Event.ID" }),
12
- statics((schema) => ({ create: () => schema.make("evt_" + ascending()) })),
13
- )
14
- export type ID = typeof ID.Type
15
-
16
- /**
17
- * Position in one aggregate's durable log. Values originate from the durable
18
- * event envelope and synced markers;
19
- * `after` cursors accept only values that came from those sources.
20
- */
21
- export const Seq = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)).pipe(
22
- Schema.brand("Event.Seq"),
23
- Schema.annotate({ identifier: "Event.Seq" }),
24
- )
25
- export type Seq = typeof Seq.Type
26
-
27
- /** Durable schema version of one event type, from the event definition that committed it. */
28
- export const Version = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)).pipe(
29
- Schema.brand("Event.Version"),
30
- Schema.annotate({ identifier: "Event.Version" }),
31
- )
32
- export type Version = typeof Version.Type
33
-
34
- const DurableEnvelope = Schema.Struct({ aggregateID: Schema.String, seq: Seq, version: Version })
35
- export type DurableEnvelope = typeof DurableEnvelope.Type
36
-
37
- export type DurableDefinition<
38
- Type extends string = string,
39
- DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
40
- > = Schema.Top & {
41
- readonly type: Type
42
- readonly durability: "durable"
43
- readonly durable: {
44
- readonly version: number
45
- readonly aggregate: string
46
- }
47
- readonly data: DataSchema
48
- }
49
-
50
- export type EphemeralDefinition<
51
- Type extends string = string,
52
- DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
53
- > = Schema.Top & {
54
- readonly type: Type
55
- readonly durability: "ephemeral"
56
- readonly durable?: never
57
- readonly data: DataSchema
58
- }
59
-
60
- export type Definition<
61
- Type extends string = string,
62
- DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
63
- > = DurableDefinition<Type, DataSchema> | EphemeralDefinition<Type, DataSchema>
64
-
65
- export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
66
-
67
- type PayloadBase<D extends Definition> = {
68
- readonly id: ID
69
- readonly type: D["type"]
70
- readonly created: number
71
- readonly data: Data<D>
72
- readonly location?: Location.Ref
73
- readonly metadata?: Record<string, unknown>
74
- }
75
-
76
- export type Payload<D extends Definition = Definition> = D extends DurableDefinition
77
- ? PayloadBase<D> & { readonly durable: DurableEnvelope }
78
- : PayloadBase<D> & { readonly durable?: never }
79
-
80
- type Input<Type extends string, Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>> = {
81
- readonly type: Type
82
- readonly identifier?: string
83
- readonly durable?: {
84
- readonly version: number
85
- readonly aggregate: string
86
- }
87
- readonly schema: Fields
88
- }
89
-
90
- export function durable<
91
- const Type extends string,
92
- const Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,
93
- >(input: Input<Type, Fields> & { readonly durable: NonNullable<Input<Type, Fields>["durable"]> }) {
94
- const data = Schema.Struct(input.schema)
95
- const durable = Schema.Struct({
96
- aggregateID: DurableEnvelope.fields.aggregateID,
97
- seq: DurableEnvelope.fields.seq,
98
- version: Schema.Literal(input.durable.version).pipe(
99
- Schema.decodeTo(
100
- Schema.toType(Version),
101
- SchemaTransformation.transform({
102
- decode: () => Version.make(input.durable.version),
103
- encode: () => input.durable.version,
104
- }),
105
- ),
106
- ),
107
- })
108
- return Schema.Struct({
109
- id: ID,
110
- created: Schema.Finite,
111
- metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
112
- type: Schema.Literal(input.type),
113
- durable,
114
- location: optional(Location.Ref),
115
- data,
116
- })
117
- .annotate({ identifier: input.identifier ?? input.type })
118
- .pipe(
119
- statics(() => ({
120
- type: input.type,
121
- durability: "durable" as const,
122
- durable: input.durable,
123
- data,
124
- })),
125
- ) satisfies DurableDefinition<Type, typeof data>
126
- }
127
-
128
- export function ephemeral<
129
- const Type extends string,
130
- const Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,
131
- >(input: Omit<Input<Type, Fields>, "durable">) {
132
- const data = Schema.Struct(input.schema)
133
- return Schema.Struct({
134
- id: ID,
135
- created: Schema.Finite,
136
- metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
137
- type: Schema.Literal(input.type),
138
- location: optional(Location.Ref),
139
- data,
140
- })
141
- .annotate({ identifier: input.identifier ?? input.type })
142
- .pipe(
143
- statics(() => ({
144
- type: input.type,
145
- durability: "ephemeral" as const,
146
- durable: undefined,
147
- data,
148
- })),
149
- ) satisfies EphemeralDefinition<Type, typeof data>
150
- }
151
-
152
- export function inventory<const Definitions extends ReadonlyArray<Definition>>(...definitions: Definitions) {
153
- return Object.freeze(definitions)
154
- }
155
-
156
- export function latest(definitions: ReadonlyArray<Definition>) {
157
- return readonlyMap(
158
- definitions.reduce((result, definition) => {
159
- const existing = result.get(definition.type)
160
- if (!existing) {
161
- result.set(definition.type, definition)
162
- return result
163
- }
164
- if (definition.durable && existing.durable && definition.durable.version !== existing.durable.version) {
165
- if (definition.durable.version > existing.durable.version) result.set(definition.type, definition)
166
- return result
167
- }
168
- if (definition !== existing) throw new Error(`Duplicate latest event definition for ${definition.type}`)
169
- return result
170
- }, new Map<string, Definition>()),
171
- )
172
- }
173
-
174
- export function versionedType(type: string, version: number) {
175
- return `${type}.${version}`
176
- }
177
-
178
- export function durableMap<const Definitions extends ReadonlyArray<Definition>>(definitions: Definitions) {
179
- return readonlyMap(
180
- definitions.reduce((result, definition) => {
181
- if (definition.durability !== "durable") return result
182
- const key = versionedType(definition.type, definition.durable.version)
183
- if (result.has(key)) throw new Error(`Duplicate durable event definition for ${key}`)
184
- result.set(key, definition)
185
- return result
186
- }, new Map<string, DurableDefinition>()),
187
- )
188
- }
189
-
190
- function readonlyMap<Key, Value>(map: Map<Key, Value>): ReadonlyMap<Key, Value> {
191
- const result: ReadonlyMap<Key, Value> = Object.freeze({
192
- get size() {
193
- return map.size
194
- },
195
- entries: () => map.entries(),
196
- forEach: (callback: (value: Value, key: Key, map: ReadonlyMap<Key, Value>) => void, thisArg?: unknown) =>
197
- map.forEach((value, key) => callback.call(thisArg, value, key, result)),
198
- get: (key: Key) => map.get(key),
199
- has: (key: Key) => map.has(key),
200
- keys: () => map.keys(),
201
- values: () => map.values(),
202
- [Symbol.iterator]: () => map[Symbol.iterator](),
203
- })
204
- return result
205
- }
1
+ export * as Event from "./event.js"
2
+
3
+ import { Schema, SchemaTransformation } from "effect"
4
+ import { optional } from "./schema.js"
5
+ import { ascending } from "./identifier.js"
6
+ import { Location } from "./location.js"
7
+ import { statics } from "./schema.js"
8
+
9
+ export const ID = Schema.String.check(Schema.isStartingWith("evt_")).pipe(
10
+ Schema.brand("Event.ID"),
11
+ Schema.annotate({ identifier: "Event.ID" }),
12
+ statics((schema) => ({ create: () => schema.make("evt_" + ascending()) })),
13
+ )
14
+ export type ID = typeof ID.Type
15
+
16
+ /**
17
+ * Position in one aggregate's durable log. Values originate from the durable
18
+ * event envelope and synced markers;
19
+ * `after` cursors accept only values that came from those sources.
20
+ */
21
+ export const Seq = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)).pipe(
22
+ Schema.brand("Event.Seq"),
23
+ Schema.annotate({ identifier: "Event.Seq" }),
24
+ )
25
+ export type Seq = typeof Seq.Type
26
+
27
+ /** Durable schema version of one event type, from the event definition that committed it. */
28
+ export const Version = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)).pipe(
29
+ Schema.brand("Event.Version"),
30
+ Schema.annotate({ identifier: "Event.Version" }),
31
+ )
32
+ export type Version = typeof Version.Type
33
+
34
+ const DurableEnvelope = Schema.Struct({ aggregateID: Schema.String, seq: Seq, version: Version })
35
+ export type DurableEnvelope = typeof DurableEnvelope.Type
36
+
37
+ export type DurableDefinition<
38
+ Type extends string = string,
39
+ DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
40
+ > = Schema.Top & {
41
+ readonly type: Type
42
+ readonly durability: "durable"
43
+ readonly durable: {
44
+ readonly version: number
45
+ readonly aggregate: string
46
+ }
47
+ readonly data: DataSchema
48
+ }
49
+
50
+ export type EphemeralDefinition<
51
+ Type extends string = string,
52
+ DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
53
+ > = Schema.Top & {
54
+ readonly type: Type
55
+ readonly durability: "ephemeral"
56
+ readonly durable?: never
57
+ readonly data: DataSchema
58
+ }
59
+
60
+ export type Definition<
61
+ Type extends string = string,
62
+ DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,
63
+ > = DurableDefinition<Type, DataSchema> | EphemeralDefinition<Type, DataSchema>
64
+
65
+ export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
66
+
67
+ type PayloadBase<D extends Definition> = {
68
+ readonly id: ID
69
+ readonly type: D["type"]
70
+ readonly created: number
71
+ readonly data: Data<D>
72
+ readonly location?: Location.Ref
73
+ readonly metadata?: Record<string, unknown>
74
+ }
75
+
76
+ export type Payload<D extends Definition = Definition> = D extends DurableDefinition
77
+ ? PayloadBase<D> & { readonly durable: DurableEnvelope }
78
+ : PayloadBase<D> & { readonly durable?: never }
79
+
80
+ type Input<Type extends string, Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>> = {
81
+ readonly type: Type
82
+ readonly identifier?: string
83
+ readonly durable?: {
84
+ readonly version: number
85
+ readonly aggregate: string
86
+ }
87
+ readonly schema: Fields
88
+ }
89
+
90
+ export function durable<
91
+ const Type extends string,
92
+ const Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,
93
+ >(input: Input<Type, Fields> & { readonly durable: NonNullable<Input<Type, Fields>["durable"]> }) {
94
+ const data = Schema.Struct(input.schema)
95
+ const durable = Schema.Struct({
96
+ aggregateID: DurableEnvelope.fields.aggregateID,
97
+ seq: DurableEnvelope.fields.seq,
98
+ version: Schema.Literal(input.durable.version).pipe(
99
+ Schema.decodeTo(
100
+ Schema.toType(Version),
101
+ SchemaTransformation.transform({
102
+ decode: () => Version.make(input.durable.version),
103
+ encode: () => input.durable.version,
104
+ }),
105
+ ),
106
+ ),
107
+ })
108
+ return Schema.Struct({
109
+ id: ID,
110
+ created: Schema.Finite,
111
+ metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
112
+ type: Schema.Literal(input.type),
113
+ durable,
114
+ location: optional(Location.Ref),
115
+ data,
116
+ })
117
+ .annotate({ identifier: input.identifier ?? input.type })
118
+ .pipe(
119
+ statics(() => ({
120
+ type: input.type,
121
+ durability: "durable" as const,
122
+ durable: input.durable,
123
+ data,
124
+ })),
125
+ ) satisfies DurableDefinition<Type, typeof data>
126
+ }
127
+
128
+ export function ephemeral<
129
+ const Type extends string,
130
+ const Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,
131
+ >(input: Omit<Input<Type, Fields>, "durable">) {
132
+ const data = Schema.Struct(input.schema)
133
+ return Schema.Struct({
134
+ id: ID,
135
+ created: Schema.Finite,
136
+ metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
137
+ type: Schema.Literal(input.type),
138
+ location: optional(Location.Ref),
139
+ data,
140
+ })
141
+ .annotate({ identifier: input.identifier ?? input.type })
142
+ .pipe(
143
+ statics(() => ({
144
+ type: input.type,
145
+ durability: "ephemeral" as const,
146
+ durable: undefined,
147
+ data,
148
+ })),
149
+ ) satisfies EphemeralDefinition<Type, typeof data>
150
+ }
151
+
152
+ export function inventory<const Definitions extends ReadonlyArray<Definition>>(...definitions: Definitions) {
153
+ return Object.freeze(definitions)
154
+ }
155
+
156
+ export function latest(definitions: ReadonlyArray<Definition>) {
157
+ return readonlyMap(
158
+ definitions.reduce((result, definition) => {
159
+ const existing = result.get(definition.type)
160
+ if (!existing) {
161
+ result.set(definition.type, definition)
162
+ return result
163
+ }
164
+ if (definition.durable && existing.durable && definition.durable.version !== existing.durable.version) {
165
+ if (definition.durable.version > existing.durable.version) result.set(definition.type, definition)
166
+ return result
167
+ }
168
+ if (definition !== existing) throw new Error(`Duplicate latest event definition for ${definition.type}`)
169
+ return result
170
+ }, new Map<string, Definition>()),
171
+ )
172
+ }
173
+
174
+ export function versionedType(type: string, version: number) {
175
+ return `${type}.${version}`
176
+ }
177
+
178
+ export function durableMap<const Definitions extends ReadonlyArray<Definition>>(definitions: Definitions) {
179
+ return readonlyMap(
180
+ definitions.reduce((result, definition) => {
181
+ if (definition.durability !== "durable") return result
182
+ const key = versionedType(definition.type, definition.durable.version)
183
+ if (result.has(key)) throw new Error(`Duplicate durable event definition for ${key}`)
184
+ result.set(key, definition)
185
+ return result
186
+ }, new Map<string, DurableDefinition>()),
187
+ )
188
+ }
189
+
190
+ function readonlyMap<Key, Value>(map: Map<Key, Value>): ReadonlyMap<Key, Value> {
191
+ const result: ReadonlyMap<Key, Value> = Object.freeze({
192
+ get size() {
193
+ return map.size
194
+ },
195
+ entries: () => map.entries(),
196
+ forEach: (callback: (value: Value, key: Key, map: ReadonlyMap<Key, Value>) => void, thisArg?: unknown) =>
197
+ map.forEach((value, key) => callback.call(thisArg, value, key, result)),
198
+ get: (key: Key) => map.get(key),
199
+ has: (key: Key) => map.has(key),
200
+ keys: () => map.keys(),
201
+ values: () => map.values(),
202
+ [Symbol.iterator]: () => map[Symbol.iterator](),
203
+ })
204
+ return result
205
+ }