@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
package/src/push.ts CHANGED
@@ -1,86 +1,86 @@
1
- export * as Push from "./push.js"
2
-
3
- import { Schema } from "effect"
4
- import { ephemeral, inventory } from "./event.js"
5
- import { NonNegativeInt, optional } from "./schema.js"
6
-
7
- export const DeviceID = Schema.String.check(
8
- Schema.isPattern(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/),
9
- ).pipe(
10
- Schema.brand("Push.DeviceID"),
11
- Schema.annotate({ identifier: "Push.DeviceID", description: "A lowercase canonical UUID chosen by the phone" }),
12
- )
13
- export type DeviceID = typeof DeviceID.Type
14
-
15
- export const Time = NonNegativeInt.annotate({
16
- description: "Milliseconds since the Unix epoch",
17
- })
18
-
19
- export const Level = Schema.Literals(["all", "attention", "none"]).annotate({ identifier: "Push.Level" })
20
- export type Level = typeof Level.Type
21
-
22
- export interface Bell extends Schema.Schema.Type<typeof Bell> {}
23
- export const Bell = Schema.Struct({
24
- level: Level,
25
- until: Time.pipe(optional),
26
- }).annotate({ identifier: "Push.Bell" })
27
-
28
- export interface Settings extends Schema.Schema.Type<typeof Settings> {}
29
- export const Settings = Schema.Struct({
30
- paused: Schema.Boolean,
31
- until: Time.pipe(optional),
32
- }).annotate({ identifier: "Push.Settings" })
33
-
34
- export interface DeviceSettings extends Schema.Schema.Type<typeof DeviceSettings> {}
35
- export const DeviceSettings = Schema.Struct({
36
- default: Level,
37
- }).annotate({ identifier: "Push.DeviceSettings" })
38
-
39
- export const Platform = Schema.Literals(["android", "ios"]).annotate({ identifier: "Push.Platform" })
40
- export type Platform = typeof Platform.Type
41
-
42
- export interface Register extends Schema.Schema.Type<typeof Register> {}
43
- export const Register = Schema.Struct({
44
- callbackURL: Schema.String,
45
- expiresAt: Time,
46
- publicKey: Schema.String,
47
- platform: Platform,
48
- name: Schema.String,
49
- settings: DeviceSettings.pipe(optional),
50
- }).annotate({ identifier: "Push.Register" })
51
-
52
- export interface Registered extends Schema.Schema.Type<typeof Registered> {}
53
- export const Registered = Schema.Struct({
54
- publicKey: Schema.String,
55
- }).annotate({ identifier: "Push.Registered" })
56
-
57
- export interface DevicePatch extends Schema.Schema.Type<typeof DevicePatch> {}
58
- export const DevicePatch = Schema.Struct({
59
- paused: Schema.Boolean.pipe(optional),
60
- settings: DeviceSettings.pipe(optional),
61
- }).annotate({ identifier: "Push.DevicePatch" })
62
-
63
- export interface Device extends Schema.Schema.Type<typeof Device> {}
64
- export const Device = Schema.Struct({
65
- id: DeviceID,
66
- name: Schema.String,
67
- platform: Platform,
68
- expiresAt: Time,
69
- settings: DeviceSettings,
70
- paused: Schema.Boolean,
71
- sessions: Schema.Record(Schema.String, Bell),
72
- time: Schema.Struct({
73
- created: Time,
74
- updated: Time,
75
- }),
76
- }).annotate({ identifier: "Push.Device" })
77
-
78
- export interface Info extends Schema.Schema.Type<typeof Info> {}
79
- export const Info = Schema.Struct({
80
- settings: Settings,
81
- devices: Schema.Array(Device),
82
- sessions: Schema.Record(Schema.String, Bell),
83
- }).annotate({ identifier: "Push.Info" })
84
-
85
- const Updated = ephemeral({ type: "experimental.push.updated", schema: {} })
86
- export const Event = { Updated, Definitions: inventory(Updated) }
1
+ export * as Push from "./push.js"
2
+
3
+ import { Schema } from "effect"
4
+ import { ephemeral, inventory } from "./event.js"
5
+ import { NonNegativeInt, optional } from "./schema.js"
6
+
7
+ export const DeviceID = Schema.String.check(
8
+ Schema.isPattern(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/),
9
+ ).pipe(
10
+ Schema.brand("Push.DeviceID"),
11
+ Schema.annotate({ identifier: "Push.DeviceID", description: "A lowercase canonical UUID chosen by the phone" }),
12
+ )
13
+ export type DeviceID = typeof DeviceID.Type
14
+
15
+ export const Time = NonNegativeInt.annotate({
16
+ description: "Milliseconds since the Unix epoch",
17
+ })
18
+
19
+ export const Level = Schema.Literals(["all", "attention", "none"]).annotate({ identifier: "Push.Level" })
20
+ export type Level = typeof Level.Type
21
+
22
+ export interface Bell extends Schema.Schema.Type<typeof Bell> {}
23
+ export const Bell = Schema.Struct({
24
+ level: Level,
25
+ until: Time.pipe(optional),
26
+ }).annotate({ identifier: "Push.Bell" })
27
+
28
+ export interface Settings extends Schema.Schema.Type<typeof Settings> {}
29
+ export const Settings = Schema.Struct({
30
+ paused: Schema.Boolean,
31
+ until: Time.pipe(optional),
32
+ }).annotate({ identifier: "Push.Settings" })
33
+
34
+ export interface DeviceSettings extends Schema.Schema.Type<typeof DeviceSettings> {}
35
+ export const DeviceSettings = Schema.Struct({
36
+ default: Level,
37
+ }).annotate({ identifier: "Push.DeviceSettings" })
38
+
39
+ export const Platform = Schema.Literals(["android", "ios"]).annotate({ identifier: "Push.Platform" })
40
+ export type Platform = typeof Platform.Type
41
+
42
+ export interface Register extends Schema.Schema.Type<typeof Register> {}
43
+ export const Register = Schema.Struct({
44
+ callbackURL: Schema.String,
45
+ expiresAt: Time,
46
+ publicKey: Schema.String,
47
+ platform: Platform,
48
+ name: Schema.String,
49
+ settings: DeviceSettings.pipe(optional),
50
+ }).annotate({ identifier: "Push.Register" })
51
+
52
+ export interface Registered extends Schema.Schema.Type<typeof Registered> {}
53
+ export const Registered = Schema.Struct({
54
+ publicKey: Schema.String,
55
+ }).annotate({ identifier: "Push.Registered" })
56
+
57
+ export interface DevicePatch extends Schema.Schema.Type<typeof DevicePatch> {}
58
+ export const DevicePatch = Schema.Struct({
59
+ paused: Schema.Boolean.pipe(optional),
60
+ settings: DeviceSettings.pipe(optional),
61
+ }).annotate({ identifier: "Push.DevicePatch" })
62
+
63
+ export interface Device extends Schema.Schema.Type<typeof Device> {}
64
+ export const Device = Schema.Struct({
65
+ id: DeviceID,
66
+ name: Schema.String,
67
+ platform: Platform,
68
+ expiresAt: Time,
69
+ settings: DeviceSettings,
70
+ paused: Schema.Boolean,
71
+ sessions: Schema.Record(Schema.String, Bell),
72
+ time: Schema.Struct({
73
+ created: Time,
74
+ updated: Time,
75
+ }),
76
+ }).annotate({ identifier: "Push.Device" })
77
+
78
+ export interface Info extends Schema.Schema.Type<typeof Info> {}
79
+ export const Info = Schema.Struct({
80
+ settings: Settings,
81
+ devices: Schema.Array(Device),
82
+ sessions: Schema.Record(Schema.String, Bell),
83
+ }).annotate({ identifier: "Push.Info" })
84
+
85
+ const Updated = ephemeral({ type: "experimental.push.updated", schema: {} })
86
+ export const Event = { Updated, Definitions: inventory(Updated) }
package/src/question.ts CHANGED
@@ -1,22 +1,22 @@
1
- export * as Question from "./question.js"
2
-
3
- import { Schema } from "effect"
4
- import { optional } from "./schema.js"
5
-
6
- const Option = Schema.Struct({
7
- label: Schema.String.annotate({ description: "Display text (1-5 words, concise)" }),
8
- description: Schema.String.annotate({ description: "Explanation of choice" }),
9
- })
10
-
11
- const base = {
12
- question: Schema.String.annotate({ description: "Complete question" }),
13
- header: Schema.String.annotate({ description: "Very short label (max 30 chars)" }),
14
- options: Schema.Array(Option).annotate({ description: "Available choices" }),
15
- multiple: Schema.Boolean.pipe(optional).annotate({ description: "Allow selecting multiple choices" }),
16
- }
17
-
18
- export const Prompt = Schema.Struct(base).annotate({ identifier: "Question.Prompt" })
19
- export interface Prompt extends Schema.Schema.Type<typeof Prompt> {}
20
-
21
- export const Answer = Schema.Array(Schema.String).annotate({ identifier: "Question.Answer" })
22
- export type Answer = typeof Answer.Type
1
+ export * as Question from "./question.js"
2
+
3
+ import { Schema } from "effect"
4
+ import { optional } from "./schema.js"
5
+
6
+ const Option = Schema.Struct({
7
+ label: Schema.String.annotate({ description: "Display text (1-5 words, concise)" }),
8
+ description: Schema.String.annotate({ description: "Explanation of choice" }),
9
+ })
10
+
11
+ const base = {
12
+ question: Schema.String.annotate({ description: "Complete question" }),
13
+ header: Schema.String.annotate({ description: "Very short label (max 30 chars)" }),
14
+ options: Schema.Array(Option).annotate({ description: "Available choices" }),
15
+ multiple: Schema.Boolean.pipe(optional).annotate({ description: "Allow selecting multiple choices" }),
16
+ }
17
+
18
+ export const Prompt = Schema.Struct(base).annotate({ identifier: "Question.Prompt" })
19
+ export interface Prompt extends Schema.Schema.Type<typeof Prompt> {}
20
+
21
+ export const Answer = Schema.Array(Schema.String).annotate({ identifier: "Question.Answer" })
22
+ export type Answer = typeof Answer.Type
package/src/reference.ts CHANGED
@@ -1,40 +1,40 @@
1
- export * as Reference from "./reference.js"
2
-
3
- import { Schema } from "effect"
4
- import { optional } from "./schema.js"
5
- import { ephemeral, inventory } from "./event.js"
6
- import { AbsolutePath } from "./schema.js"
7
-
8
- const Updated = ephemeral({ type: "reference.updated", schema: {} })
9
- export const Event = { Updated, Definitions: inventory(Updated) }
10
-
11
- export interface LocalSource extends Schema.Schema.Type<typeof LocalSource> {}
12
- export const LocalSource = Schema.Struct({
13
- type: Schema.Literal("local"),
14
- path: AbsolutePath,
15
- description: Schema.String.pipe(optional),
16
- hidden: Schema.Boolean.pipe(optional),
17
- }).annotate({ identifier: "Reference.LocalSource" })
18
-
19
- export interface GitSource extends Schema.Schema.Type<typeof GitSource> {}
20
- export const GitSource = Schema.Struct({
21
- type: Schema.Literal("git"),
22
- repository: Schema.String,
23
- branch: Schema.String.pipe(optional),
24
- description: Schema.String.pipe(optional),
25
- hidden: Schema.Boolean.pipe(optional),
26
- }).annotate({ identifier: "Reference.GitSource" })
27
-
28
- export const Source = Schema.Union([LocalSource, GitSource])
29
- .pipe(Schema.toTaggedUnion("type"))
30
- .annotate({ identifier: "Reference.Source" })
31
- export type Source = typeof Source.Type
32
-
33
- export const Info = Schema.Struct({
34
- name: Schema.String,
35
- path: AbsolutePath,
36
- description: Schema.String.pipe(optional),
37
- hidden: Schema.Boolean.pipe(optional),
38
- source: Source,
39
- }).annotate({ identifier: "Reference.Info" })
40
- export interface Info extends Schema.Schema.Type<typeof Info> {}
1
+ export * as Reference from "./reference.js"
2
+
3
+ import { Schema } from "effect"
4
+ import { optional } from "./schema.js"
5
+ import { ephemeral, inventory } from "./event.js"
6
+ import { AbsolutePath } from "./schema.js"
7
+
8
+ const Updated = ephemeral({ type: "reference.updated", schema: {} })
9
+ export const Event = { Updated, Definitions: inventory(Updated) }
10
+
11
+ export interface LocalSource extends Schema.Schema.Type<typeof LocalSource> {}
12
+ export const LocalSource = Schema.Struct({
13
+ type: Schema.Literal("local"),
14
+ path: AbsolutePath,
15
+ description: Schema.String.pipe(optional),
16
+ hidden: Schema.Boolean.pipe(optional),
17
+ }).annotate({ identifier: "Reference.LocalSource" })
18
+
19
+ export interface GitSource extends Schema.Schema.Type<typeof GitSource> {}
20
+ export const GitSource = Schema.Struct({
21
+ type: Schema.Literal("git"),
22
+ repository: Schema.String,
23
+ branch: Schema.String.pipe(optional),
24
+ description: Schema.String.pipe(optional),
25
+ hidden: Schema.Boolean.pipe(optional),
26
+ }).annotate({ identifier: "Reference.GitSource" })
27
+
28
+ export const Source = Schema.Union([LocalSource, GitSource])
29
+ .pipe(Schema.toTaggedUnion("type"))
30
+ .annotate({ identifier: "Reference.Source" })
31
+ export type Source = typeof Source.Type
32
+
33
+ export const Info = Schema.Struct({
34
+ name: Schema.String,
35
+ path: AbsolutePath,
36
+ description: Schema.String.pipe(optional),
37
+ hidden: Schema.Boolean.pipe(optional),
38
+ source: Source,
39
+ }).annotate({ identifier: "Reference.Info" })
40
+ export interface Info extends Schema.Schema.Type<typeof Info> {}
package/src/rpc.ts CHANGED
@@ -1,167 +1,167 @@
1
- export * as Rpc from "./rpc.js"
2
-
3
- import type { StandardSchemaV1 } from "@standard-schema/spec"
4
- import type { JsonSchema, Schema } from "effect"
5
- import type { Event } from "./event.js"
6
- import type { Location } from "./location.js"
7
- import type { Tool } from "./tool.js"
8
-
9
- export type ErrorMap = Readonly<Record<string, Tool.ValueSchema>> & {
10
- readonly [Name in `rpc.${string}`]?: never
11
- }
12
-
13
- export interface Method {
14
- readonly input: Tool.ValueSchema
15
- readonly output: Tool.ValueSchema
16
- readonly errors?: ErrorMap
17
- }
18
-
19
- export type PortableValueSchema = StandardSchemaV1<unknown, unknown> | JsonSchema.JsonSchema
20
-
21
- export interface PortableMethod extends Method {
22
- readonly input: PortableValueSchema
23
- readonly output: PortableValueSchema
24
- readonly errors?: Readonly<Record<string, PortableValueSchema>> & {
25
- readonly [Name in `rpc.${string}`]?: never
26
- }
27
- }
28
-
29
- type EventDataObject = Readonly<Record<string, unknown>>
30
- type EventValueSchema =
31
- | Schema.Codec<EventDataObject, EventDataObject>
32
- | StandardSchemaV1<unknown, EventDataObject>
33
- | (JsonSchema.JsonSchema & { readonly type: "object" })
34
- type PortableEventValueSchema =
35
- | StandardSchemaV1<unknown, EventDataObject>
36
- | (JsonSchema.JsonSchema & { readonly type: "object" })
37
-
38
- export interface EventDefinition {
39
- readonly schema: EventValueSchema
40
- }
41
- export type PortableEventDefinition = EventDefinition & { readonly schema: PortableEventValueSchema }
42
-
43
- export interface Definition {
44
- readonly id: string
45
- readonly methods: Readonly<Record<string, Method>> & { readonly events?: never }
46
- readonly events: Readonly<Record<string, EventDefinition>>
47
- }
48
-
49
- export interface PortableDefinition extends Definition {
50
- readonly methods: Readonly<Record<string, PortableMethod>> & { readonly events?: never }
51
- readonly events: Readonly<Record<string, PortableEventDefinition>>
52
- }
53
-
54
- export function define<const D extends Definition>(definition: D) {
55
- const reserved = Object.values(definition.methods)
56
- .flatMap((method) => Object.keys(method.errors ?? {}))
57
- .find((name) => name.startsWith("rpc."))
58
- if (reserved) throw new Error(`RPC error names starting with "rpc." are reserved: ${reserved}`)
59
- return definition
60
- }
61
-
62
- export type Input<S extends Tool.ValueSchema> = S extends Schema.Top
63
- ? S["Encoded"]
64
- : S extends StandardSchemaV1
65
- ? StandardSchemaV1.InferInput<S>
66
- : unknown
67
-
68
- export type Output<S extends Tool.ValueSchema> = S extends Schema.Top
69
- ? S["Type"]
70
- : S extends StandardSchemaV1
71
- ? StandardSchemaV1.InferOutput<S>
72
- : unknown
73
-
74
- // Effect codecs encode handler results; Standard Schema parses them forward.
75
- export type HandlerOutput<S extends Tool.ValueSchema> = S extends Schema.Top ? Output<S> : Input<S>
76
-
77
- type MethodErrors<M extends Method> = M extends {
78
- readonly errors: infer Errors extends ErrorMap
79
- }
80
- ? Errors
81
- : never
82
- type ErrorSchema<M extends Method, Name extends ErrorName<M>> = MethodErrors<M>[Name]
83
- type ErrorData<Data> = unknown extends Data
84
- ? { readonly data: Data }
85
- : undefined extends Data
86
- ? { readonly data?: Data }
87
- : { readonly data: Data }
88
- type ErrorDataArguments<Data> = unknown extends Data
89
- ? [data: Data]
90
- : undefined extends Data
91
- ? [data?: Data]
92
- : [data: Data]
93
- type Simplify<A> = { readonly [K in keyof A]: A[K] }
94
- declare const HandlerErrorTypeId: unique symbol
95
-
96
- export interface Failure<Type extends string = string, Data = unknown> {
97
- readonly type: Type
98
- readonly message: string
99
- readonly data?: Data
100
- }
101
-
102
- export type SystemError = Failure<
103
- | "rpc.unavailable"
104
- | "rpc.method_not_found"
105
- | "rpc.invalid_input"
106
- | "rpc.invalid_output"
107
- | "rpc.internal",
108
- never
109
- >
110
-
111
- export type ErrorName<M extends Method> = M extends {
112
- readonly errors: infer Errors extends ErrorMap
113
- }
114
- ? Exclude<keyof Errors & string, `rpc.${string}`>
115
- : never
116
- export type HandlerErrorFor<M extends Method, Name extends ErrorName<M>> = Simplify<
117
- {
118
- readonly type: Name
119
- readonly message: string
120
- readonly [HandlerErrorTypeId]: true
121
- } & ErrorData<HandlerOutput<ErrorSchema<M, Name>>>
122
- >
123
- export type HandlerError<M extends Method> = {
124
- readonly [Name in ErrorName<M>]: HandlerErrorFor<M, Name>
125
- }[ErrorName<M>]
126
- export type MethodErrorFor<M extends Method, Name extends ErrorName<M>> = Simplify<
127
- {
128
- readonly type: Name
129
- readonly message: string
130
- } & ErrorData<Output<ErrorSchema<M, Name>>>
131
- >
132
- export type MethodError<M extends Method> = {
133
- readonly [Name in ErrorName<M>]: MethodErrorFor<M, Name>
134
- }[ErrorName<M>]
135
- export type ErrorArguments<M extends Method, Name extends ErrorName<M>> = [
136
- type: Name,
137
- message: string,
138
- ...data: ErrorDataArguments<HandlerOutput<ErrorSchema<M, Name>>>,
139
- ]
140
- export type ErrorFactory<M extends Method> = <Name extends ErrorName<M>>(
141
- ...args: ErrorArguments<M, Name>
142
- ) => HandlerErrorFor<M, Name>
143
-
144
- export type EventInputData<S extends EventValueSchema> = S extends JsonSchema.JsonSchema
145
- ? EventDataObject
146
- : HandlerOutput<S>
147
- export type EventData<S extends EventValueSchema> = S extends JsonSchema.JsonSchema
148
- ? EventDataObject
149
- : Output<S>
150
-
151
- // Keep the event name correlated with its payload even when callers use unions.
152
- export type EventInput<D extends Definition> = {
153
- [Name in keyof D["events"] & string]: [name: Name, data: EventInputData<D["events"][Name]["schema"]>]
154
- }[keyof D["events"] & string]
155
-
156
- type EventPayloadFor<
157
- D extends Definition,
158
- Name extends keyof D["events"] & string,
159
- > = Omit<Event.Payload<Event.EphemeralDefinition>, "type" | "data" | "durable" | "location"> & {
160
- readonly type: `rpc.${D["id"]}.${Name}`
161
- readonly data: EventData<D["events"][Name]["schema"]>
162
- readonly location: Location.Ref
163
- }
164
-
165
- export type EventPayload<D extends Definition, Name extends keyof D["events"] & string = keyof D["events"] & string> = {
166
- readonly [K in Name]: EventPayloadFor<D, K>
167
- }[Name]
1
+ export * as Rpc from "./rpc.js"
2
+
3
+ import type { StandardSchemaV1 } from "@standard-schema/spec"
4
+ import type { JsonSchema, Schema } from "effect"
5
+ import type { Event } from "./event.js"
6
+ import type { Location } from "./location.js"
7
+ import type { Tool } from "./tool.js"
8
+
9
+ export type ErrorMap = Readonly<Record<string, Tool.ValueSchema>> & {
10
+ readonly [Name in `rpc.${string}`]?: never
11
+ }
12
+
13
+ export interface Method {
14
+ readonly input: Tool.ValueSchema
15
+ readonly output: Tool.ValueSchema
16
+ readonly errors?: ErrorMap
17
+ }
18
+
19
+ export type PortableValueSchema = StandardSchemaV1<unknown, unknown> | JsonSchema.JsonSchema
20
+
21
+ export interface PortableMethod extends Method {
22
+ readonly input: PortableValueSchema
23
+ readonly output: PortableValueSchema
24
+ readonly errors?: Readonly<Record<string, PortableValueSchema>> & {
25
+ readonly [Name in `rpc.${string}`]?: never
26
+ }
27
+ }
28
+
29
+ type EventDataObject = Readonly<Record<string, unknown>>
30
+ type EventValueSchema =
31
+ | Schema.Codec<EventDataObject, EventDataObject>
32
+ | StandardSchemaV1<unknown, EventDataObject>
33
+ | (JsonSchema.JsonSchema & { readonly type: "object" })
34
+ type PortableEventValueSchema =
35
+ | StandardSchemaV1<unknown, EventDataObject>
36
+ | (JsonSchema.JsonSchema & { readonly type: "object" })
37
+
38
+ export interface EventDefinition {
39
+ readonly schema: EventValueSchema
40
+ }
41
+ export type PortableEventDefinition = EventDefinition & { readonly schema: PortableEventValueSchema }
42
+
43
+ export interface Definition {
44
+ readonly id: string
45
+ readonly methods: Readonly<Record<string, Method>> & { readonly events?: never }
46
+ readonly events: Readonly<Record<string, EventDefinition>>
47
+ }
48
+
49
+ export interface PortableDefinition extends Definition {
50
+ readonly methods: Readonly<Record<string, PortableMethod>> & { readonly events?: never }
51
+ readonly events: Readonly<Record<string, PortableEventDefinition>>
52
+ }
53
+
54
+ export function define<const D extends Definition>(definition: D) {
55
+ const reserved = Object.values(definition.methods)
56
+ .flatMap((method) => Object.keys(method.errors ?? {}))
57
+ .find((name) => name.startsWith("rpc."))
58
+ if (reserved) throw new Error(`RPC error names starting with "rpc." are reserved: ${reserved}`)
59
+ return definition
60
+ }
61
+
62
+ export type Input<S extends Tool.ValueSchema> = S extends Schema.Top
63
+ ? S["Encoded"]
64
+ : S extends StandardSchemaV1
65
+ ? StandardSchemaV1.InferInput<S>
66
+ : unknown
67
+
68
+ export type Output<S extends Tool.ValueSchema> = S extends Schema.Top
69
+ ? S["Type"]
70
+ : S extends StandardSchemaV1
71
+ ? StandardSchemaV1.InferOutput<S>
72
+ : unknown
73
+
74
+ // Effect codecs encode handler results; Standard Schema parses them forward.
75
+ export type HandlerOutput<S extends Tool.ValueSchema> = S extends Schema.Top ? Output<S> : Input<S>
76
+
77
+ type MethodErrors<M extends Method> = M extends {
78
+ readonly errors: infer Errors extends ErrorMap
79
+ }
80
+ ? Errors
81
+ : never
82
+ type ErrorSchema<M extends Method, Name extends ErrorName<M>> = MethodErrors<M>[Name]
83
+ type ErrorData<Data> = unknown extends Data
84
+ ? { readonly data: Data }
85
+ : undefined extends Data
86
+ ? { readonly data?: Data }
87
+ : { readonly data: Data }
88
+ type ErrorDataArguments<Data> = unknown extends Data
89
+ ? [data: Data]
90
+ : undefined extends Data
91
+ ? [data?: Data]
92
+ : [data: Data]
93
+ type Simplify<A> = { readonly [K in keyof A]: A[K] }
94
+ declare const HandlerErrorTypeId: unique symbol
95
+
96
+ export interface Failure<Type extends string = string, Data = unknown> {
97
+ readonly type: Type
98
+ readonly message: string
99
+ readonly data?: Data
100
+ }
101
+
102
+ export type SystemError = Failure<
103
+ | "rpc.unavailable"
104
+ | "rpc.method_not_found"
105
+ | "rpc.invalid_input"
106
+ | "rpc.invalid_output"
107
+ | "rpc.internal",
108
+ never
109
+ >
110
+
111
+ export type ErrorName<M extends Method> = M extends {
112
+ readonly errors: infer Errors extends ErrorMap
113
+ }
114
+ ? Exclude<keyof Errors & string, `rpc.${string}`>
115
+ : never
116
+ export type HandlerErrorFor<M extends Method, Name extends ErrorName<M>> = Simplify<
117
+ {
118
+ readonly type: Name
119
+ readonly message: string
120
+ readonly [HandlerErrorTypeId]: true
121
+ } & ErrorData<HandlerOutput<ErrorSchema<M, Name>>>
122
+ >
123
+ export type HandlerError<M extends Method> = {
124
+ readonly [Name in ErrorName<M>]: HandlerErrorFor<M, Name>
125
+ }[ErrorName<M>]
126
+ export type MethodErrorFor<M extends Method, Name extends ErrorName<M>> = Simplify<
127
+ {
128
+ readonly type: Name
129
+ readonly message: string
130
+ } & ErrorData<Output<ErrorSchema<M, Name>>>
131
+ >
132
+ export type MethodError<M extends Method> = {
133
+ readonly [Name in ErrorName<M>]: MethodErrorFor<M, Name>
134
+ }[ErrorName<M>]
135
+ export type ErrorArguments<M extends Method, Name extends ErrorName<M>> = [
136
+ type: Name,
137
+ message: string,
138
+ ...data: ErrorDataArguments<HandlerOutput<ErrorSchema<M, Name>>>,
139
+ ]
140
+ export type ErrorFactory<M extends Method> = <Name extends ErrorName<M>>(
141
+ ...args: ErrorArguments<M, Name>
142
+ ) => HandlerErrorFor<M, Name>
143
+
144
+ export type EventInputData<S extends EventValueSchema> = S extends JsonSchema.JsonSchema
145
+ ? EventDataObject
146
+ : HandlerOutput<S>
147
+ export type EventData<S extends EventValueSchema> = S extends JsonSchema.JsonSchema
148
+ ? EventDataObject
149
+ : Output<S>
150
+
151
+ // Keep the event name correlated with its payload even when callers use unions.
152
+ export type EventInput<D extends Definition> = {
153
+ [Name in keyof D["events"] & string]: [name: Name, data: EventInputData<D["events"][Name]["schema"]>]
154
+ }[keyof D["events"] & string]
155
+
156
+ type EventPayloadFor<
157
+ D extends Definition,
158
+ Name extends keyof D["events"] & string,
159
+ > = Omit<Event.Payload<Event.EphemeralDefinition>, "type" | "data" | "durable" | "location"> & {
160
+ readonly type: `rpc.${D["id"]}.${Name}`
161
+ readonly data: EventData<D["events"][Name]["schema"]>
162
+ readonly location: Location.Ref
163
+ }
164
+
165
+ export type EventPayload<D extends Definition, Name extends keyof D["events"] & string = keyof D["events"] & string> = {
166
+ readonly [K in Name]: EventPayloadFor<D, K>
167
+ }[Name]