abxbus 2.5.4 → 2.5.6

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 (39) hide show
  1. package/dist/cjs/BaseEvent.d.ts +68 -23
  2. package/dist/cjs/BaseEvent.js +131 -14
  3. package/dist/cjs/BaseEvent.js.map +2 -2
  4. package/dist/cjs/base_event.d.ts +2 -2
  5. package/dist/cjs/bridge_ipc.d.ts +45 -0
  6. package/dist/cjs/event_handler.d.ts +1 -0
  7. package/dist/cjs/events_suck.d.ts +1 -1
  8. package/dist/cjs/events_suck.js.map +2 -2
  9. package/dist/cjs/jsonschema.d.ts +6 -0
  10. package/dist/cjs/jsonschema.js +155 -0
  11. package/dist/cjs/jsonschema.js.map +7 -0
  12. package/dist/cjs/middleware_otel_tracing.d.ts +49 -0
  13. package/dist/cjs/types.d.ts +3 -4
  14. package/dist/cjs/types.js +8 -15
  15. package/dist/cjs/types.js.map +2 -2
  16. package/dist/esm/BaseEvent.js +131 -14
  17. package/dist/esm/BaseEvent.js.map +2 -2
  18. package/dist/esm/events_suck.js.map +2 -2
  19. package/dist/esm/jsonschema.js +135 -0
  20. package/dist/esm/jsonschema.js.map +7 -0
  21. package/dist/esm/types.js +7 -14
  22. package/dist/esm/types.js.map +2 -2
  23. package/dist/types/BaseEvent.d.ts +68 -23
  24. package/dist/types/base_event.d.ts +2 -2
  25. package/dist/types/bridge_ipc.d.ts +45 -0
  26. package/dist/types/event_handler.d.ts +1 -0
  27. package/dist/types/events_suck.d.ts +1 -1
  28. package/dist/types/jsonschema.d.ts +6 -0
  29. package/dist/types/middleware_otel_tracing.d.ts +49 -0
  30. package/dist/types/types.d.ts +3 -4
  31. package/package.json +1 -1
  32. package/src/BaseEvent.ts +277 -54
  33. package/src/events_suck.ts +3 -2
  34. package/src/jsonschema.ts +146 -0
  35. package/src/types.ts +6 -14
  36. package/dist/cjs/CoreClient.d.ts +0 -167
  37. package/dist/cjs/CoreEventBus.d.ts +0 -334
  38. package/dist/types/CoreClient.d.ts +0 -167
  39. package/dist/types/CoreEventBus.d.ts +0 -334
@@ -0,0 +1,146 @@
1
+ import { z } from 'zod'
2
+
3
+ export type JsonSchema = boolean | z.core.JSONSchema.JSONSchema
4
+ type JsonSchemaObject = z.core.JSONSchema.JSONSchema
5
+
6
+ const isJsonSchemaObject = (value: unknown): value is JsonSchemaObject => {
7
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
8
+ }
9
+
10
+ export const isJsonSchema = (value: unknown): value is JsonSchema => {
11
+ if (typeof value === 'boolean') {
12
+ return true
13
+ }
14
+ if (!isJsonSchemaObject(value)) {
15
+ return false
16
+ }
17
+ if ('_zod' in value || '_def' in value || '~standard' in value) {
18
+ return false
19
+ }
20
+ return true
21
+ }
22
+
23
+ const nullUnionCandidates = (schema: Record<string, unknown>): Record<string, unknown>[] | null => {
24
+ if (Array.isArray(schema.type) && schema.type.includes('null')) {
25
+ const non_null_types = schema.type.filter((item): item is string => typeof item === 'string' && item !== 'null')
26
+ if (non_null_types.length > 0) {
27
+ return [{ type: non_null_types.length === 1 ? non_null_types[0] : non_null_types }, { type: 'null' }]
28
+ }
29
+ }
30
+
31
+ return null
32
+ }
33
+
34
+ export const normalizeJsonSchema = (schema: JsonSchema): JsonSchema => {
35
+ const normalized = normalizeJsonSchemaValue(schema)
36
+ if (!isJsonSchemaObject(normalized)) {
37
+ return normalized as JsonSchema
38
+ }
39
+ const schema_record = { ...normalized } as JsonSchemaObject
40
+ const definitions = schema_record.$defs
41
+ const root_ref = rootRefForSchema(schema_record, definitions)
42
+ if (!root_ref || !definitions) {
43
+ schema_record.$schema ??= 'https://json-schema.org/draft/2020-12/schema'
44
+ return schema_record
45
+ }
46
+ const root_name = root_ref.slice('#/$defs/'.length)
47
+ const root_schema = definitions[root_name]
48
+ if (!isJsonSchemaObject(root_schema)) {
49
+ schema_record.$schema ??= 'https://json-schema.org/draft/2020-12/schema'
50
+ return schema_record
51
+ }
52
+ const rewritten_root = rewriteJsonSchemaRefs(root_schema, { [root_ref]: '#' }) as JsonSchemaObject
53
+ const remaining_defs = Object.fromEntries(Object.entries(definitions).filter(([name]) => name !== root_name))
54
+ if (Object.keys(remaining_defs).length > 0) {
55
+ rewritten_root.$defs = rewriteJsonSchemaRefs(remaining_defs, { [root_ref]: '#' }) as Record<string, JsonSchemaObject>
56
+ }
57
+ rewritten_root.$schema ??= schema_record.$schema ?? 'https://json-schema.org/draft/2020-12/schema'
58
+ setTitleFromInlinedRootDefinition(rewritten_root, root_name)
59
+ return rewritten_root
60
+ }
61
+
62
+ const rootRefForSchema = (schema: JsonSchemaObject, definitions: Record<string, JsonSchemaObject> | undefined): string | null => {
63
+ if (typeof schema.$ref === 'string' && schema.$ref.startsWith('#/$defs/')) {
64
+ return schema.$ref
65
+ }
66
+ if (!definitions) {
67
+ return null
68
+ }
69
+ const root = schemaWithoutSchemaAndDefinitions(schema)
70
+ for (const [name, definition] of Object.entries(definitions)) {
71
+ if (JSON.stringify(definition) === JSON.stringify(root)) {
72
+ return `#/$defs/${name}`
73
+ }
74
+ }
75
+ return null
76
+ }
77
+
78
+ const schemaWithoutSchemaAndDefinitions = (schema: JsonSchemaObject): Record<string, unknown> => {
79
+ const root: Record<string, unknown> = {}
80
+ for (const [key, value] of Object.entries(schema)) {
81
+ if (key !== '$schema' && key !== '$defs') {
82
+ root[key] = value
83
+ }
84
+ }
85
+ return root
86
+ }
87
+
88
+ const setTitleFromInlinedRootDefinition = (schema: JsonSchemaObject, root_name: string): void => {
89
+ if (root_name.startsWith('__schema')) return
90
+ schema.title ??= root_name
91
+ }
92
+
93
+ const rewriteJsonSchemaRefs = (schema: unknown, refs: Record<string, string>): unknown => {
94
+ if (Array.isArray(schema)) {
95
+ return schema.map((item) => rewriteJsonSchemaRefs(item, refs))
96
+ }
97
+ if (!isJsonSchemaObject(schema)) {
98
+ return schema
99
+ }
100
+ const rewritten: Record<string, unknown> = {}
101
+ for (const [key, value] of Object.entries(schema)) {
102
+ rewritten[key] = rewriteJsonSchemaRefs(value, refs)
103
+ }
104
+ if (typeof rewritten.$ref === 'string' && rewritten.$ref in refs) {
105
+ rewritten.$ref = refs[rewritten.$ref]
106
+ }
107
+ return rewritten
108
+ }
109
+
110
+ const normalizeJsonSchemaValue = (schema: unknown): unknown => {
111
+ if (Array.isArray(schema)) {
112
+ return schema.map((item) => normalizeJsonSchemaValue(item))
113
+ }
114
+ if (!isJsonSchemaObject(schema)) {
115
+ return schema
116
+ }
117
+
118
+ const normalized: Record<string, unknown> = {}
119
+ for (const [key, value] of Object.entries(schema)) {
120
+ normalized[key] = normalizeJsonSchemaValue(value)
121
+ }
122
+ if (Array.isArray(normalized.required) && normalized.required.every((item) => typeof item === 'string')) {
123
+ normalized.required = [...normalized.required].sort()
124
+ }
125
+
126
+ const null_union_candidates = nullUnionCandidates(normalized)
127
+ if (null_union_candidates !== null) {
128
+ const merged: Record<string, unknown> = { anyOf: normalizeJsonSchemaValue(null_union_candidates) }
129
+ for (const [key, value] of Object.entries(normalized)) {
130
+ if (key !== 'type') {
131
+ merged[key] = value
132
+ }
133
+ }
134
+ return merged
135
+ }
136
+
137
+ return normalized
138
+ }
139
+
140
+ export const toJsonSchema = (schema: z.core.$ZodType): JsonSchema => {
141
+ return normalizeJsonSchema(z.toJSONSchema(schema) as JsonSchema)
142
+ }
143
+
144
+ export const fromJsonSchema = (schema: JsonSchema): z.ZodTypeAny => {
145
+ return z.fromJSONSchema(schema)
146
+ }
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod'
2
2
  import type { BaseEvent } from './BaseEvent.js'
3
+ import { fromJsonSchema, isJsonSchema, type JsonSchema } from './jsonschema.js'
3
4
 
4
5
  export type EventStatus = 'pending' | 'started' | 'completed'
5
6
 
@@ -13,7 +14,7 @@ export type EventResultType<TEvent extends BaseEvent> = TEvent extends { __event
13
14
 
14
15
  export type EventResultTypeConstructor = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor
15
16
 
16
- export type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | unknown
17
+ export type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | JsonSchema
17
18
 
18
19
  export type EventHandlerReturn<T extends BaseEvent = BaseEvent> = EventResultType<T> | BaseEvent | null | void
19
20
 
@@ -107,19 +108,7 @@ export const extractZodShape = (raw: Record<string, unknown>): z.ZodRawShape =>
107
108
  return shape as z.ZodRawShape
108
109
  }
109
110
 
110
- export const toJsonSchema = (schema: unknown): unknown => {
111
- if (!schema || !isZodSchema(schema)) return schema
112
- const zod_any = z as unknown as { toJSONSchema: (input: z.ZodTypeAny) => unknown }
113
- // Cross-language roundtrips preserve core structural types; constraint keywords may not roundtrip exactly.
114
- return zod_any.toJSONSchema(schema)
115
- }
116
-
117
- export const fromJsonSchema = (schema: unknown): z.ZodTypeAny => {
118
- const zod_any = z as unknown as { fromJSONSchema: (input: unknown) => z.ZodTypeAny }
119
- return zod_any.fromJSONSchema(schema)
120
- }
121
-
122
- export const normalizeEventResultType = (value: EventResultTypeInput): z.ZodTypeAny | undefined => {
111
+ export function normalizeEventResultType(value: unknown): z.ZodTypeAny | undefined {
123
112
  if (value === undefined || value === null) {
124
113
  return undefined
125
114
  }
@@ -130,5 +119,8 @@ export const normalizeEventResultType = (value: EventResultTypeInput): z.ZodType
130
119
  if (constructor_schema) {
131
120
  return constructor_schema
132
121
  }
122
+ if (!isJsonSchema(value)) {
123
+ throw new Error(`event_result_type must be a Zod schema, constructor shorthand, or JSON Schema value, got: ${typeof value}`)
124
+ }
133
125
  return fromJsonSchema(value)
134
126
  }
@@ -1,167 +0,0 @@
1
- export declare const CORE_PROTOCOL_VERSION = 1;
2
- export type ProtocolEnvelope<TMessage = Record<string, unknown>> = {
3
- protocol_version: number;
4
- session_id: string;
5
- request_id?: string;
6
- last_patch_seq?: number;
7
- message: TMessage;
8
- };
9
- export type CoreMessage = Record<string, unknown> & {
10
- type?: string;
11
- };
12
- type FastCompletedHandlerOptions = {
13
- result_is_event_reference?: boolean;
14
- process_route_after?: boolean;
15
- process_available_after?: boolean;
16
- compact_response?: boolean;
17
- include_patches?: boolean;
18
- };
19
- type ForwardEventOptions = {
20
- parent_invocation_id?: string | null;
21
- block_parent_completion?: boolean;
22
- pause_parent_route?: boolean;
23
- event_timeout?: number | null;
24
- event_slow_timeout?: number | null;
25
- event_concurrency?: string | null;
26
- event_handler_timeout?: number | null;
27
- event_handler_slow_timeout?: number | null;
28
- event_handler_concurrency?: string | null;
29
- event_handler_completion?: string | null;
30
- event_blocks_parent_completion?: boolean | null;
31
- };
32
- export declare class RustCoreClient {
33
- private static shared_named_clients;
34
- private static cleanup_registered;
35
- static acquireNamed(bus_name: string): RustCoreClient;
36
- static releaseNamed(client: RustCoreClient, options?: {
37
- stopCore?: boolean;
38
- }): void;
39
- private static registerProcessCleanup;
40
- static stopIdleNamedClients(): void;
41
- readonly session_id: string;
42
- readonly command: string;
43
- readonly args: string[];
44
- readonly socket_path: string;
45
- private process;
46
- private rpc;
47
- private last_patch_seq;
48
- private request_seq;
49
- private release_transport_timer;
50
- private readonly kill_process_on_close;
51
- private readonly owns_socket_path;
52
- private readonly bus_name;
53
- private readonly session_id_bytes;
54
- constructor(options?: {
55
- command?: string;
56
- args?: string[];
57
- session_id?: string;
58
- socket_path?: string;
59
- bus_name?: string;
60
- });
61
- request(message: CoreMessage, options?: {
62
- includePatches?: boolean;
63
- advancePatchSeq?: boolean;
64
- advancePatchSeqWhenNoPatches?: boolean;
65
- }): ProtocolEnvelope<CoreMessage>[];
66
- private requestOnce;
67
- requestMessages(message: CoreMessage, options?: {
68
- includePatches?: boolean;
69
- advancePatchSeq?: boolean;
70
- advancePatchSeqWhenNoPatches?: boolean;
71
- }): CoreMessage[];
72
- getPatchSeq(): number;
73
- setPatchSeq(last_patch_seq: number): void;
74
- filterUnseenPatchMessages(messages: CoreMessage[]): CoreMessage[];
75
- ackPatchMessages(messages: CoreMessage | CoreMessage[]): void;
76
- registerBus(bus: Record<string, unknown>): CoreMessage[];
77
- unregisterBus(bus_id: string): CoreMessage[];
78
- registerHandler(handler: Record<string, unknown>): CoreMessage[];
79
- importBusSnapshot(snapshot: {
80
- bus: Record<string, unknown>;
81
- handlers: Record<string, unknown>[];
82
- events: Record<string, unknown>[];
83
- pending_event_ids: string[];
84
- }): CoreMessage[];
85
- unregisterHandler(handler_id: string): CoreMessage[];
86
- disconnectHost(host_id?: string | null): CoreMessage[];
87
- closeSession(): void;
88
- stopCore(): CoreMessage[];
89
- emitEvent(event: Record<string, unknown>, bus_id: string, defer_start?: boolean, compact_response?: boolean, options?: {
90
- parent_invocation_id?: string | null;
91
- block_parent_completion?: boolean;
92
- pause_parent_route?: boolean;
93
- }): CoreMessage[];
94
- forwardEvent(event_id: string, bus_id: string, defer_start?: boolean, compact_response?: boolean, options?: ForwardEventOptions): CoreMessage[];
95
- updateEventOptions(event_id: string, options: {
96
- event_handler_completion?: string | null;
97
- event_blocks_parent_completion?: boolean | null;
98
- }): CoreMessage[];
99
- processNextRoute(bus_id: string, limit?: number | null, compact_response?: boolean): CoreMessage[];
100
- waitInvocations(bus_id?: string | null, limit?: number | null): CoreMessage[];
101
- waitEventCompleted(event_id: string): CoreMessage[];
102
- waitEventEmitted(bus_id: string, event_pattern?: string, seen_event_ids?: string[], after_event_id?: string | null, after_created_at?: string | null): CoreMessage[];
103
- waitBusIdle(bus_id: string, timeout?: number | null): boolean;
104
- processRoute(route_id: string, limit?: number | null, compact_response?: boolean): CoreMessage[];
105
- awaitEvent(event_id: string, parent_invocation_id?: string | null): CoreMessage[];
106
- queueJumpEvent(event_id: string, parent_invocation_id: string, block_parent_completion?: boolean, pause_parent_route?: boolean): CoreMessage[];
107
- getEvent(event_id: string): Record<string, unknown> | null;
108
- listEvents(event_pattern?: string, limit?: number | null, bus_id?: string | null): Record<string, unknown>[];
109
- listEventIds(event_pattern?: string, limit?: number | null, bus_id?: string | null, statuses?: string[] | null): string[];
110
- listPendingEventIds(bus_id: string): string[];
111
- completeHandler(invocation: Record<string, unknown>, value: unknown, options?: FastCompletedHandlerOptions): CoreMessage[];
112
- completeHandlerNoPatches(invocation: Record<string, unknown>, value: unknown, options?: FastCompletedHandlerOptions): CoreMessage[];
113
- private requestFastCompletedHandler;
114
- private requestFastRegisterHandler;
115
- private requestFastUnregisterHandler;
116
- private requestFastQueueJumpEvent;
117
- private waitFastCoreAck;
118
- private waitFastCoreMessages;
119
- private readFastPatchSeq;
120
- private requiredString;
121
- private requiredUint64;
122
- completeHandlerOutcomes(outcomes: Record<string, unknown>[], options?: {
123
- compact_response?: boolean;
124
- }): CoreMessage[];
125
- completedHandlerOutcome(value: unknown, result_is_event_reference?: boolean): CoreMessage;
126
- handlerOutcomeRecord(invocation: Record<string, unknown>, outcome: CoreMessage, options?: {
127
- process_available_after?: boolean;
128
- }): CoreMessage;
129
- errorHandler(invocation: Record<string, unknown>, error: unknown, options?: {
130
- process_route_after?: boolean;
131
- process_available_after?: boolean;
132
- compact_response?: boolean;
133
- }): CoreMessage[];
134
- erroredHandlerOutcome(error: unknown): CoreMessage;
135
- close(): void;
136
- closeTransportOnly(options?: {
137
- closeSession?: boolean;
138
- }): void;
139
- disconnect(): void;
140
- stop(): void;
141
- private cleanupFailedSpawn;
142
- private connect;
143
- private tryConnect;
144
- private connectNamedBus;
145
- private requestNamedSession;
146
- private requestNamedStop;
147
- private waitForNamedDaemonStopped;
148
- private ensureNamedDaemon;
149
- private coreProcessEnv;
150
- private ensureRpc;
151
- private configureRpc;
152
- private closeTransport;
153
- private cleanupOwnedSocketPath;
154
- releaseTransportSoon(): void;
155
- private sendCloseSession;
156
- }
157
- export declare const defaultCoreCommand: (socket_path: string, options?: {
158
- daemon?: boolean;
159
- }) => {
160
- command: string;
161
- args: string[];
162
- };
163
- export declare const stableCoreSocketPath: (_bus_name: string) => string;
164
- export declare const coreNamespace: () => string;
165
- export declare const stableCoreSessionSocketPath: (bus_name: string) => string;
166
- export declare const namedCoreLockPath: (socket_path: string) => string;
167
- export {};