@inferencesh/app 0.1.15 → 0.1.16
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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/schema.d.ts +2 -1
- package/dist/schema.js +4 -11
- package/dist/stream.d.ts +10 -3
- package/dist/stream.js +16 -39
- package/dist/zod-walk.d.ts +9 -0
- package/dist/zod-walk.js +40 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { File } from "./file.js";
|
|
|
2
2
|
export type { FileOptions, FileData } from "./file.js";
|
|
3
3
|
export { createFileSchema, isFileSchema, FILE_SCHEMA_MARKER } from "./schema.js";
|
|
4
4
|
export type { Socket, Frame } from "./socket.js";
|
|
5
|
-
export { createStreamSchema, media, pcm16, Live, liveFields, binaryField, STREAM_SCHEMA_MARKER, CLEAR_KEY, BINARY_SCHEMA_MARKER, } from "./stream.js";
|
|
5
|
+
export { createStreamSchema, media, pcm16, Live, liveFields, binaryField, STREAM_SCHEMA_MARKER, CLEAR_KEY, ERROR_KEY, BINARY_SCHEMA_MARKER, } from "./stream.js";
|
|
6
6
|
export type { Update } from "./stream.js";
|
|
7
7
|
export { StorageDir, ensureDir } from "./storage.js";
|
|
8
8
|
export type { StorageDirValue } from "./storage.js";
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export { File } from "./file.js";
|
|
3
3
|
// Zod schema utilities
|
|
4
4
|
export { createFileSchema, isFileSchema, FILE_SCHEMA_MARKER } from "./schema.js";
|
|
5
|
-
export { createStreamSchema, media, pcm16, Live, liveFields, binaryField, STREAM_SCHEMA_MARKER, CLEAR_KEY, BINARY_SCHEMA_MARKER, } from "./stream.js";
|
|
5
|
+
export { createStreamSchema, media, pcm16, Live, liveFields, binaryField, STREAM_SCHEMA_MARKER, CLEAR_KEY, ERROR_KEY, BINARY_SCHEMA_MARKER, } from "./stream.js";
|
|
6
6
|
// Storage directories
|
|
7
7
|
export { StorageDir, ensureDir } from "./storage.js";
|
|
8
8
|
// Download utility
|
package/dist/schema.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export declare const FILE_SCHEMA_MARKER: unique symbol;
|
|
|
28
28
|
*/
|
|
29
29
|
export declare function createFileSchema(z: any): any;
|
|
30
30
|
/**
|
|
31
|
-
* Check if a Zod schema is a file schema (created by createFileSchema)
|
|
31
|
+
* Check if a Zod schema is a file schema (created by createFileSchema),
|
|
32
|
+
* through any wrapper (optional, default, nullable, ...) in zod v3 or v4.
|
|
32
33
|
*/
|
|
33
34
|
export declare function isFileSchema(schema: any): boolean;
|
package/dist/schema.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* - Generates JSON Schema with format: "file"
|
|
8
8
|
*/
|
|
9
9
|
import { File } from "./file.js";
|
|
10
|
+
import { findMarker } from "./zod-walk.js";
|
|
10
11
|
// We don't import zod directly to avoid version conflicts.
|
|
11
12
|
// Instead, apps provide their own zod and we work with the schema structure.
|
|
12
13
|
/**
|
|
@@ -36,17 +37,9 @@ export function createFileSchema(z) {
|
|
|
36
37
|
return schema;
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
|
-
* Check if a Zod schema is a file schema (created by createFileSchema)
|
|
40
|
+
* Check if a Zod schema is a file schema (created by createFileSchema),
|
|
41
|
+
* through any wrapper (optional, default, nullable, ...) in zod v3 or v4.
|
|
40
42
|
*/
|
|
41
43
|
export function isFileSchema(schema) {
|
|
42
|
-
|
|
43
|
-
return false;
|
|
44
|
-
// Check for our marker
|
|
45
|
-
if (schema._def && schema._def[FILE_SCHEMA_MARKER])
|
|
46
|
-
return true;
|
|
47
|
-
// Check inner type for transforms/effects
|
|
48
|
-
if (schema._def?.typeName === "ZodEffects" && schema._def.schema) {
|
|
49
|
-
return isFileSchema(schema._def.schema);
|
|
50
|
-
}
|
|
51
|
-
return false;
|
|
44
|
+
return findMarker(schema, FILE_SCHEMA_MARKER) === true;
|
|
52
45
|
}
|
package/dist/stream.d.ts
CHANGED
|
@@ -51,11 +51,13 @@ import type { Socket } from "./socket.js";
|
|
|
51
51
|
* The kernel's zodToJsonSchema reads this and emits `format: "stream"`.
|
|
52
52
|
*/
|
|
53
53
|
export declare const STREAM_SCHEMA_MARKER: unique symbol;
|
|
54
|
+
/** `{"$clear": "audio"}`: drop what has been buffered of a live output field. */
|
|
55
|
+
export declare const CLEAR_KEY = "$clear";
|
|
54
56
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
+
* `{"$error": {"field": ..., "message": ...}}`: a refused frame, or anything
|
|
58
|
+
* else the caller should be told went wrong. The stream goes on.
|
|
57
59
|
*/
|
|
58
|
-
export declare const
|
|
60
|
+
export declare const ERROR_KEY = "$error";
|
|
59
61
|
/**
|
|
60
62
|
* Marks a binary item schema. The value on `_def` is the content type.
|
|
61
63
|
* The kernel's zodToJsonSchema reads this and emits
|
|
@@ -139,6 +141,11 @@ export declare class Live {
|
|
|
139
141
|
private frames;
|
|
140
142
|
private read;
|
|
141
143
|
private refuse;
|
|
144
|
+
/**
|
|
145
|
+
* Tell the caller something went wrong without ending the stream: a
|
|
146
|
+
* provider error, or why the session is about to end.
|
|
147
|
+
*/
|
|
148
|
+
error(message: string, field?: string | null): Promise<void>;
|
|
142
149
|
/**
|
|
143
150
|
* Send items of live output fields, or new values of ordinary ones.
|
|
144
151
|
*
|
package/dist/stream.js
CHANGED
|
@@ -45,16 +45,21 @@
|
|
|
45
45
|
* Zod is never imported: the app's own zod (v3 or v4) is passed in, and the
|
|
46
46
|
* schemas are read by duck-typing `_def`, the same way the kernel does.
|
|
47
47
|
*/
|
|
48
|
+
import { findMarker } from "./zod-walk.js";
|
|
48
49
|
/**
|
|
49
50
|
* Marks a live field's schema. The value on `_def` is the item schema.
|
|
50
51
|
* The kernel's zodToJsonSchema reads this and emits `format: "stream"`.
|
|
51
52
|
*/
|
|
52
53
|
export const STREAM_SCHEMA_MARKER = Symbol.for("inferencesh.streamSchema");
|
|
54
|
+
// Control frames. Reserved keys start with `$`, which no field name can, so a
|
|
55
|
+
// control frame is never mistaken for an output field.
|
|
56
|
+
/** `{"$clear": "audio"}`: drop what has been buffered of a live output field. */
|
|
57
|
+
export const CLEAR_KEY = "$clear";
|
|
53
58
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
59
|
+
* `{"$error": {"field": ..., "message": ...}}`: a refused frame, or anything
|
|
60
|
+
* else the caller should be told went wrong. The stream goes on.
|
|
56
61
|
*/
|
|
57
|
-
export const
|
|
62
|
+
export const ERROR_KEY = "$error";
|
|
58
63
|
/**
|
|
59
64
|
* Marks a binary item schema. The value on `_def` is the content type.
|
|
60
65
|
* The kernel's zodToJsonSchema reads this and emits
|
|
@@ -95,41 +100,6 @@ export function pcm16(z, rate = 16000, channels = 1) {
|
|
|
95
100
|
return media(z, `audio/pcm;format=s16le;rate=${rate};channels=${channels}`);
|
|
96
101
|
}
|
|
97
102
|
// --- schema walking (zod v3 and v4 `_def` shapes) ---------------------------
|
|
98
|
-
/** Inner schemas of a wrapper def, in the order worth checking. */
|
|
99
|
-
function innerSchemas(def) {
|
|
100
|
-
const out = [];
|
|
101
|
-
// v3 + v4: optional / default / nullable / catch / readonly
|
|
102
|
-
if (def.innerType)
|
|
103
|
-
out.push(def.innerType);
|
|
104
|
-
// v3: effects (preprocess / transform / refine), v4: some wrappers
|
|
105
|
-
if (def.schema)
|
|
106
|
-
out.push(def.schema);
|
|
107
|
-
if (def.inner)
|
|
108
|
-
out.push(def.inner);
|
|
109
|
-
// v4: pipe (preprocess, transform) — in, then out
|
|
110
|
-
if (def.in)
|
|
111
|
-
out.push(def.in);
|
|
112
|
-
if (def.out)
|
|
113
|
-
out.push(def.out);
|
|
114
|
-
return out;
|
|
115
|
-
}
|
|
116
|
-
/** The marker's value on the schema or any schema it wraps, or undefined. */
|
|
117
|
-
function findMarker(schema, marker, seen = new Set()) {
|
|
118
|
-
if (!schema || typeof schema !== "object" || seen.has(schema))
|
|
119
|
-
return undefined;
|
|
120
|
-
seen.add(schema);
|
|
121
|
-
const def = schema._def;
|
|
122
|
-
if (!def || typeof def !== "object")
|
|
123
|
-
return undefined;
|
|
124
|
-
if (Object.prototype.hasOwnProperty.call(def, marker))
|
|
125
|
-
return def[marker];
|
|
126
|
-
for (const inner of innerSchemas(def)) {
|
|
127
|
-
const found = findMarker(inner, marker, seen);
|
|
128
|
-
if (found !== undefined)
|
|
129
|
-
return found;
|
|
130
|
-
}
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
103
|
/** The fields of a zod object schema. v3: `_def.shape()`, v4: `_def.shape`. */
|
|
134
104
|
function shapeOf(objectSchema) {
|
|
135
105
|
const def = objectSchema?._def;
|
|
@@ -299,7 +269,14 @@ export class Live {
|
|
|
299
269
|
return updates;
|
|
300
270
|
}
|
|
301
271
|
async refuse(field, message) {
|
|
302
|
-
await this.
|
|
272
|
+
await this.error(message, field);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Tell the caller something went wrong without ending the stream: a
|
|
276
|
+
* provider error, or why the session is about to end.
|
|
277
|
+
*/
|
|
278
|
+
async error(message, field = null) {
|
|
279
|
+
await this.socket.send({ [ERROR_KEY]: { field, message } });
|
|
303
280
|
}
|
|
304
281
|
/**
|
|
305
282
|
* Send items of live output fields, or new values of ordinary ones.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walking a zod schema through its wrappers, the same way for zod v3 and v4:
|
|
3
|
+
* the app's own zod is passed in and never imported here, so schemas are read
|
|
4
|
+
* by duck-typing `_def`, as the kernel does.
|
|
5
|
+
*/
|
|
6
|
+
/** The schemas a wrapper (optional, default, nullable, effects, pipe, ...) wraps. */
|
|
7
|
+
export declare function innerSchemas(def: any): any[];
|
|
8
|
+
/** The value of a marker on a schema's `_def`, looking through its wrappers. */
|
|
9
|
+
export declare function findMarker(schema: any, marker: symbol, seen?: Set<any>): unknown;
|
package/dist/zod-walk.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walking a zod schema through its wrappers, the same way for zod v3 and v4:
|
|
3
|
+
* the app's own zod is passed in and never imported here, so schemas are read
|
|
4
|
+
* by duck-typing `_def`, as the kernel does.
|
|
5
|
+
*/
|
|
6
|
+
/** The schemas a wrapper (optional, default, nullable, effects, pipe, ...) wraps. */
|
|
7
|
+
export function innerSchemas(def) {
|
|
8
|
+
const out = [];
|
|
9
|
+
// v3 + v4: optional / default / nullable / catch / readonly
|
|
10
|
+
if (def.innerType)
|
|
11
|
+
out.push(def.innerType);
|
|
12
|
+
// v3: effects (preprocess / transform / refine), v4: some wrappers
|
|
13
|
+
if (def.schema)
|
|
14
|
+
out.push(def.schema);
|
|
15
|
+
if (def.inner)
|
|
16
|
+
out.push(def.inner);
|
|
17
|
+
// v4: pipe (preprocess, transform) — in, then out
|
|
18
|
+
if (def.in)
|
|
19
|
+
out.push(def.in);
|
|
20
|
+
if (def.out)
|
|
21
|
+
out.push(def.out);
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
/** The value of a marker on a schema's `_def`, looking through its wrappers. */
|
|
25
|
+
export function findMarker(schema, marker, seen = new Set()) {
|
|
26
|
+
if (!schema || typeof schema !== "object" || seen.has(schema))
|
|
27
|
+
return undefined;
|
|
28
|
+
seen.add(schema);
|
|
29
|
+
const def = schema._def;
|
|
30
|
+
if (!def || typeof def !== "object")
|
|
31
|
+
return undefined;
|
|
32
|
+
if (Object.prototype.hasOwnProperty.call(def, marker))
|
|
33
|
+
return def[marker];
|
|
34
|
+
for (const inner of innerSchemas(def)) {
|
|
35
|
+
const found = findMarker(inner, marker, seen);
|
|
36
|
+
if (found !== undefined)
|
|
37
|
+
return found;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
package/package.json
CHANGED