bleam 0.0.5 → 0.0.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.
- package/dist/ai.cjs +187 -0
- package/dist/ai.d.cts +52 -0
- package/dist/ai.d.ts +52 -0
- package/dist/ai.js +182 -0
- package/dist/animated.cjs +286 -0
- package/dist/animated.d.cts +47 -0
- package/dist/animated.d.ts +47 -0
- package/dist/animated.js +281 -0
- package/dist/app.d.cts +1 -2
- package/dist/app.d.ts +1 -2
- package/dist/app.js +1 -2
- package/dist/bundler.d.cts +1 -2
- package/dist/bundler.d.ts +1 -2
- package/dist/chrome.cjs +265 -0
- package/dist/chrome.d.cts +19 -0
- package/dist/chrome.d.ts +19 -0
- package/dist/chrome.js +262 -0
- package/dist/cli.cjs +768 -31
- package/dist/cli.js +767 -34
- package/dist/config-Bb6SULnR.cjs +46 -0
- package/dist/config-CfWa3q-k.js +28 -0
- package/dist/config-ClDGHNb3.d.ts +39 -0
- package/dist/config-D3gPaVMb.d.cts +39 -0
- package/dist/config.cjs +4 -25
- package/dist/config.d.cts +2 -28
- package/dist/config.d.ts +2 -28
- package/dist/config.js +2 -24
- package/dist/crypto-BB92-Upx.js +7 -0
- package/dist/crypto-CT3VZ7lF.cjs +13 -0
- package/dist/crypto.cjs +2 -2
- package/dist/crypto.d.cts +8 -1
- package/dist/crypto.d.ts +8 -1
- package/dist/crypto.js +2 -2
- package/dist/dev-protocol.d.cts +3 -3
- package/dist/dev-protocol.d.ts +3 -3
- package/dist/dev-protocol.js +1 -2
- package/dist/fs.cjs +12 -2
- package/dist/fs.d.cts +7 -1
- package/dist/fs.d.ts +7 -1
- package/dist/fs.js +10 -1
- package/dist/index.cjs +4 -5
- package/dist/index.d.cts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -4
- package/dist/native-macos-D6p2SmUj.js +20 -0
- package/dist/native-macos-DLwo_ii9.cjs +31 -0
- package/dist/native-runtime-C85Nuc4F.js +10 -0
- package/dist/native-runtime-CbPzrs8j.d.cts +33 -0
- package/dist/{native-runtime-CdeT0JyQ.cjs → native-runtime-CsXnXkQn.cjs} +1 -14
- package/dist/native-runtime-DxHSrsTX.d.ts +33 -0
- package/dist/native.cjs +35 -32
- package/dist/native.d.cts +4 -34
- package/dist/native.d.ts +3 -33
- package/dist/native.js +36 -34
- package/dist/runtime.d.cts +1 -2
- package/dist/runtime.d.ts +1 -2
- package/dist/schema-BOuUHcqo.d.cts +58 -0
- package/dist/schema-DVmoD9JL.js +90 -0
- package/dist/schema-bxeeCyLn.cjs +102 -0
- package/dist/schema-ipsECAso.d.ts +58 -0
- package/dist/schema.cjs +4 -0
- package/dist/schema.d.cts +2 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +3 -0
- package/dist/state.cjs +765 -0
- package/dist/state.d.cts +129 -0
- package/dist/state.d.ts +129 -0
- package/dist/state.js +747 -0
- package/dist/styles.cjs +742 -0
- package/dist/styles.d.cts +701 -0
- package/dist/styles.d.ts +701 -0
- package/dist/styles.js +739 -0
- package/dist/ui.cjs +113 -0
- package/dist/ui.d.cts +30 -0
- package/dist/ui.d.ts +30 -0
- package/dist/ui.js +110 -0
- package/metro-config.cjs +18 -13
- package/package.json +47 -13
- package/templates/basic/app/index.tsx +130 -12
- package/templates/basic/app.config.ts +4 -2
- package/dist/app.d.cts.map +0 -1
- package/dist/app.d.ts.map +0 -1
- package/dist/app.js.map +0 -1
- package/dist/bundler.d.cts.map +0 -1
- package/dist/bundler.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/config.d.cts.map +0 -1
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/crypto-BNhWeXMj.cjs +0 -15
- package/dist/crypto-Bg8cD8wX.d.cts +0 -8
- package/dist/crypto-Bg8cD8wX.d.cts.map +0 -1
- package/dist/crypto-CCA_hvao.js +0 -11
- package/dist/crypto-CCA_hvao.js.map +0 -1
- package/dist/crypto-IOzbNoJD.d.ts +0 -8
- package/dist/crypto-IOzbNoJD.d.ts.map +0 -1
- package/dist/dev-protocol.d.cts.map +0 -1
- package/dist/dev-protocol.d.ts.map +0 -1
- package/dist/dev-protocol.js.map +0 -1
- package/dist/fs-BK7joD1g.js +0 -13
- package/dist/fs-BK7joD1g.js.map +0 -1
- package/dist/fs-CEhcjuN6.d.ts +0 -9
- package/dist/fs-CEhcjuN6.d.ts.map +0 -1
- package/dist/fs-CfE8CHf_.d.cts +0 -9
- package/dist/fs-CfE8CHf_.d.cts.map +0 -1
- package/dist/fs-DKHYTPua.cjs +0 -18
- package/dist/native-runtime-BoqybBBf.js +0 -18
- package/dist/native-runtime-BoqybBBf.js.map +0 -1
- package/dist/native.d.cts.map +0 -1
- package/dist/native.d.ts.map +0 -1
- package/dist/native.js.map +0 -1
- package/dist/runtime.d.cts.map +0 -1
- package/dist/runtime.d.ts.map +0 -1
package/dist/ai.cjs
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
let react_native = require("react-native");
|
|
3
|
+
react_native = require_chunk.__toESM(react_native);
|
|
4
|
+
|
|
5
|
+
//#region src/ai.ts
|
|
6
|
+
var AIError = class extends Error {
|
|
7
|
+
constructor(code, message, cause) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
this.name = "AIError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var ShapeError = class extends Error {
|
|
15
|
+
constructor(code, message, issues = []) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.issues = issues;
|
|
19
|
+
this.name = "ShapeError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
function issue(message, path = []) {
|
|
26
|
+
return {
|
|
27
|
+
message,
|
|
28
|
+
path
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function validateDefinition(definition, value, path = []) {
|
|
32
|
+
if (definition === "string" || definition === "number" || definition === "boolean") {
|
|
33
|
+
if (typeof value === definition) return { value };
|
|
34
|
+
return { issues: [issue(`Expected ${path.join(".") || "value"} to be ${definition}`, path)] };
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(definition)) {
|
|
37
|
+
if (!Array.isArray(value)) return { issues: [issue(`Expected ${path.join(".") || "value"} to be array`, path)] };
|
|
38
|
+
const itemDefinition = definition[0];
|
|
39
|
+
const issues$1 = [];
|
|
40
|
+
const output$1 = [];
|
|
41
|
+
for (const [index, item] of value.entries()) {
|
|
42
|
+
const result = validateDefinition(itemDefinition, item, [...path, index]);
|
|
43
|
+
if (result.issues !== void 0) issues$1.push(...result.issues);
|
|
44
|
+
else output$1.push(result.value);
|
|
45
|
+
}
|
|
46
|
+
if (issues$1.length > 0) return { issues: issues$1 };
|
|
47
|
+
return { value: output$1 };
|
|
48
|
+
}
|
|
49
|
+
if (!isRecord(value)) return { issues: [issue(`Expected ${path.join(".") || "value"} to be object`, path)] };
|
|
50
|
+
const output = {};
|
|
51
|
+
const issues = [];
|
|
52
|
+
for (const [key, field] of Object.entries(definition)) {
|
|
53
|
+
const result = validateDefinition(field, value[key], [...path, key]);
|
|
54
|
+
if (result.issues !== void 0) issues.push(...result.issues);
|
|
55
|
+
else output[key] = result.value;
|
|
56
|
+
}
|
|
57
|
+
if (issues.length > 0) return { issues };
|
|
58
|
+
return { value: output };
|
|
59
|
+
}
|
|
60
|
+
function bridgeSchema(definition) {
|
|
61
|
+
if (definition === "string" || definition === "number" || definition === "boolean") return { type: definition };
|
|
62
|
+
if (Array.isArray(definition)) return {
|
|
63
|
+
type: "array",
|
|
64
|
+
items: bridgeSchema(definition[0])
|
|
65
|
+
};
|
|
66
|
+
const properties = {};
|
|
67
|
+
const required = [];
|
|
68
|
+
for (const [key, value] of Object.entries(definition)) {
|
|
69
|
+
properties[key] = bridgeSchema(value);
|
|
70
|
+
required.push(key);
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
type: "object",
|
|
74
|
+
properties,
|
|
75
|
+
required
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function assertValidShape(definition, path = "shape") {
|
|
79
|
+
if (definition === "string" || definition === "number" || definition === "boolean") return;
|
|
80
|
+
if (Array.isArray(definition)) {
|
|
81
|
+
if (definition.length !== 1) throw new ShapeError("invalid_shape", `${path} arrays must contain exactly one item shape`);
|
|
82
|
+
assertValidShape(definition[0], `${path}[0]`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (!isRecord(definition)) throw new ShapeError("invalid_shape", `${path} must be a shape object`);
|
|
86
|
+
for (const [key, value] of Object.entries(definition)) assertValidShape(value, `${path}.${key}`);
|
|
87
|
+
}
|
|
88
|
+
function createShape(definition) {
|
|
89
|
+
assertValidShape(definition);
|
|
90
|
+
const api = {
|
|
91
|
+
"~standard": {
|
|
92
|
+
version: 1,
|
|
93
|
+
vendor: "bleam",
|
|
94
|
+
validate(value) {
|
|
95
|
+
return api.safeParse(value);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
parse(value) {
|
|
99
|
+
const result = api.safeParse(value);
|
|
100
|
+
if (result.issues !== void 0) throw new ShapeError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
101
|
+
return result.value;
|
|
102
|
+
},
|
|
103
|
+
safeParse(value) {
|
|
104
|
+
return validateDefinition(definition, value);
|
|
105
|
+
},
|
|
106
|
+
toJSONSchema() {
|
|
107
|
+
return bridgeSchema(definition);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
return api;
|
|
111
|
+
}
|
|
112
|
+
function shape(definition) {
|
|
113
|
+
return createShape(definition);
|
|
114
|
+
}
|
|
115
|
+
function schemaToBridgeSchema(schema) {
|
|
116
|
+
const schemaWithBridge = schema;
|
|
117
|
+
if (typeof schemaWithBridge.toJSONSchema === "function") return schemaWithBridge.toJSONSchema();
|
|
118
|
+
}
|
|
119
|
+
function validateStructuredInput(input) {
|
|
120
|
+
const result = input.shape["~standard"].validate(input.value);
|
|
121
|
+
if (result.issues !== void 0) throw new AIError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
122
|
+
return result.value;
|
|
123
|
+
}
|
|
124
|
+
function stableStringify(value) {
|
|
125
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
126
|
+
if (isRecord(value)) return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
127
|
+
return JSON.stringify(value) ?? "undefined";
|
|
128
|
+
}
|
|
129
|
+
function promptFromInput(options) {
|
|
130
|
+
const parts = [];
|
|
131
|
+
if (options.prompt) parts.push(options.prompt);
|
|
132
|
+
if (typeof options.input === "string") parts.push(options.input);
|
|
133
|
+
else if (options.input) parts.push(stableStringify(validateStructuredInput(options.input)));
|
|
134
|
+
if (parts.length === 0) throw new AIError("invalid_message", "Expected prompt or input");
|
|
135
|
+
return parts.join("\n\n");
|
|
136
|
+
}
|
|
137
|
+
function bridge() {
|
|
138
|
+
const nativeBridge = react_native.NativeModules.BleamAIBridge;
|
|
139
|
+
if (!nativeBridge) throw new AIError("unsupported_runtime", "bleam/ai requires the Bleam native runtime");
|
|
140
|
+
return nativeBridge;
|
|
141
|
+
}
|
|
142
|
+
function mapNativeError(error) {
|
|
143
|
+
const code = isRecord(error) && typeof error.code === "string" ? error.code : "";
|
|
144
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
145
|
+
if (code === "UNSUPPORTED_OS") return new AIError("unsupported_os", message, error);
|
|
146
|
+
if (code === "MODEL_UNAVAILABLE") return new AIError("model_unavailable", message, error);
|
|
147
|
+
if (code === "INVALID_SCHEMA") return new AIError("invalid_schema", message, error);
|
|
148
|
+
if (code === "INVALID_MESSAGE") return new AIError("invalid_message", message, error);
|
|
149
|
+
if (code === "CONTEXT_WINDOW_EXCEEDED") return new AIError("context_window_exceeded", message, error);
|
|
150
|
+
return new AIError("generation_failed", message, error);
|
|
151
|
+
}
|
|
152
|
+
async function isAvailable() {
|
|
153
|
+
try {
|
|
154
|
+
return await bridge().isAvailable?.() ?? false;
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async function generate(options) {
|
|
160
|
+
const output = options.output;
|
|
161
|
+
const request = {
|
|
162
|
+
system: options.system,
|
|
163
|
+
prompt: promptFromInput(options),
|
|
164
|
+
temperature: options.temperature,
|
|
165
|
+
maxTokens: options.maxTokens,
|
|
166
|
+
outputSchema: output ? schemaToBridgeSchema(output) : void 0
|
|
167
|
+
};
|
|
168
|
+
try {
|
|
169
|
+
const value = await bridge().generate?.(request);
|
|
170
|
+
if (output) {
|
|
171
|
+
const result = output["~standard"].validate(value);
|
|
172
|
+
if (result.issues !== void 0) throw new AIError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
173
|
+
return result.value;
|
|
174
|
+
}
|
|
175
|
+
if (typeof value !== "string") throw new AIError("generation_failed", "Expected generated text");
|
|
176
|
+
return value;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (error instanceof AIError) throw error;
|
|
179
|
+
throw mapNativeError(error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
exports.AIError = AIError;
|
|
185
|
+
exports.generate = generate;
|
|
186
|
+
exports.isAvailable = isAvailable;
|
|
187
|
+
exports.shape = shape;
|
package/dist/ai.d.cts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { h as StandardSchema, p as SchemaResult, s as InferOutput } from "./schema-BOuUHcqo.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/ai.d.ts
|
|
4
|
+
type PrimitiveToken = 'string' | 'number' | 'boolean';
|
|
5
|
+
type ShapeArray = readonly [ShapeDefinition];
|
|
6
|
+
type ShapeObject = {
|
|
7
|
+
readonly [key: string]: ShapeDefinition;
|
|
8
|
+
};
|
|
9
|
+
type ShapeDefinition = PrimitiveToken | ShapeObject | ShapeArray;
|
|
10
|
+
type InferShapeValue<Definition extends ShapeDefinition> = Definition extends 'string' ? string : Definition extends 'number' ? number : Definition extends 'boolean' ? boolean : Definition extends readonly [infer Item] ? Item extends ShapeDefinition ? Array<InferShapeValue<Item>> : never : Definition extends ShapeObject ? { [Key in keyof Definition]: InferShapeValue<Definition[Key]> } : never;
|
|
11
|
+
type InferShape<Definition extends ShapeObject> = { [Key in keyof Definition]: InferShapeValue<Definition[Key]> };
|
|
12
|
+
type BridgeSchema = {
|
|
13
|
+
type: PrimitiveToken;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'array';
|
|
16
|
+
items: BridgeSchema;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'object';
|
|
19
|
+
properties: Record<string, BridgeSchema>;
|
|
20
|
+
required: string[];
|
|
21
|
+
};
|
|
22
|
+
type GenerateOptions<Output$1 extends StructuredSchema | undefined = undefined> = {
|
|
23
|
+
system?: string;
|
|
24
|
+
prompt?: string;
|
|
25
|
+
input?: string | StructuredInput;
|
|
26
|
+
output?: Output$1;
|
|
27
|
+
temperature?: number;
|
|
28
|
+
maxTokens?: number;
|
|
29
|
+
};
|
|
30
|
+
type StructuredInput = {
|
|
31
|
+
shape: StructuredSchema;
|
|
32
|
+
value: unknown;
|
|
33
|
+
};
|
|
34
|
+
type StructuredSchema = BleamAIShape<unknown, unknown> | StandardSchema;
|
|
35
|
+
type AIErrorCode = 'unsupported_runtime' | 'unsupported_os' | 'model_unavailable' | 'generation_failed' | 'invalid_message' | 'invalid_schema' | 'validation_failed' | 'context_window_exceeded';
|
|
36
|
+
declare class AIError extends Error {
|
|
37
|
+
readonly code: AIErrorCode;
|
|
38
|
+
readonly cause?: unknown | undefined;
|
|
39
|
+
constructor(code: AIErrorCode, message: string, cause?: unknown | undefined);
|
|
40
|
+
}
|
|
41
|
+
interface BleamAIShape<Input, Output$1> extends StandardSchema<Input, Output$1> {
|
|
42
|
+
parse(value: unknown): Output$1;
|
|
43
|
+
safeParse(value: unknown): SchemaResult<Output$1>;
|
|
44
|
+
toJSONSchema(): BridgeSchema;
|
|
45
|
+
}
|
|
46
|
+
type InferShapeOutput<Value extends BleamAIShape<unknown, unknown>> = Value extends BleamAIShape<unknown, infer Output> ? Output : never;
|
|
47
|
+
type GenerateResult<Output$1 extends StructuredSchema | undefined> = Output$1 extends BleamAIShape<unknown, infer ShapeOutput> ? ShapeOutput : Output$1 extends StandardSchema ? InferOutput<Output$1> : string;
|
|
48
|
+
declare function shape<Definition extends ShapeObject>(definition: Definition): BleamAIShape<InferShape<Definition>, InferShape<Definition>>;
|
|
49
|
+
declare function isAvailable(): Promise<boolean>;
|
|
50
|
+
declare function generate<Output$1 extends StructuredSchema | undefined = undefined>(options: GenerateOptions<Output$1>): Promise<GenerateResult<Output$1>>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { AIError, AIErrorCode, BleamAIShape, InferShapeOutput, generate, isAvailable, shape };
|
package/dist/ai.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { h as StandardSchema, p as SchemaResult, s as InferOutput } from "./schema-ipsECAso.js";
|
|
2
|
+
|
|
3
|
+
//#region src/ai.d.ts
|
|
4
|
+
type PrimitiveToken = 'string' | 'number' | 'boolean';
|
|
5
|
+
type ShapeArray = readonly [ShapeDefinition];
|
|
6
|
+
type ShapeObject = {
|
|
7
|
+
readonly [key: string]: ShapeDefinition;
|
|
8
|
+
};
|
|
9
|
+
type ShapeDefinition = PrimitiveToken | ShapeObject | ShapeArray;
|
|
10
|
+
type InferShapeValue<Definition extends ShapeDefinition> = Definition extends 'string' ? string : Definition extends 'number' ? number : Definition extends 'boolean' ? boolean : Definition extends readonly [infer Item] ? Item extends ShapeDefinition ? Array<InferShapeValue<Item>> : never : Definition extends ShapeObject ? { [Key in keyof Definition]: InferShapeValue<Definition[Key]> } : never;
|
|
11
|
+
type InferShape<Definition extends ShapeObject> = { [Key in keyof Definition]: InferShapeValue<Definition[Key]> };
|
|
12
|
+
type BridgeSchema = {
|
|
13
|
+
type: PrimitiveToken;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'array';
|
|
16
|
+
items: BridgeSchema;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'object';
|
|
19
|
+
properties: Record<string, BridgeSchema>;
|
|
20
|
+
required: string[];
|
|
21
|
+
};
|
|
22
|
+
type GenerateOptions<Output$1 extends StructuredSchema | undefined = undefined> = {
|
|
23
|
+
system?: string;
|
|
24
|
+
prompt?: string;
|
|
25
|
+
input?: string | StructuredInput;
|
|
26
|
+
output?: Output$1;
|
|
27
|
+
temperature?: number;
|
|
28
|
+
maxTokens?: number;
|
|
29
|
+
};
|
|
30
|
+
type StructuredInput = {
|
|
31
|
+
shape: StructuredSchema;
|
|
32
|
+
value: unknown;
|
|
33
|
+
};
|
|
34
|
+
type StructuredSchema = BleamAIShape<unknown, unknown> | StandardSchema;
|
|
35
|
+
type AIErrorCode = 'unsupported_runtime' | 'unsupported_os' | 'model_unavailable' | 'generation_failed' | 'invalid_message' | 'invalid_schema' | 'validation_failed' | 'context_window_exceeded';
|
|
36
|
+
declare class AIError extends Error {
|
|
37
|
+
readonly code: AIErrorCode;
|
|
38
|
+
readonly cause?: unknown | undefined;
|
|
39
|
+
constructor(code: AIErrorCode, message: string, cause?: unknown | undefined);
|
|
40
|
+
}
|
|
41
|
+
interface BleamAIShape<Input, Output$1> extends StandardSchema<Input, Output$1> {
|
|
42
|
+
parse(value: unknown): Output$1;
|
|
43
|
+
safeParse(value: unknown): SchemaResult<Output$1>;
|
|
44
|
+
toJSONSchema(): BridgeSchema;
|
|
45
|
+
}
|
|
46
|
+
type InferShapeOutput<Value extends BleamAIShape<unknown, unknown>> = Value extends BleamAIShape<unknown, infer Output> ? Output : never;
|
|
47
|
+
type GenerateResult<Output$1 extends StructuredSchema | undefined> = Output$1 extends BleamAIShape<unknown, infer ShapeOutput> ? ShapeOutput : Output$1 extends StandardSchema ? InferOutput<Output$1> : string;
|
|
48
|
+
declare function shape<Definition extends ShapeObject>(definition: Definition): BleamAIShape<InferShape<Definition>, InferShape<Definition>>;
|
|
49
|
+
declare function isAvailable(): Promise<boolean>;
|
|
50
|
+
declare function generate<Output$1 extends StructuredSchema | undefined = undefined>(options: GenerateOptions<Output$1>): Promise<GenerateResult<Output$1>>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { AIError, AIErrorCode, BleamAIShape, InferShapeOutput, generate, isAvailable, shape };
|
package/dist/ai.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { NativeModules } from "react-native";
|
|
2
|
+
|
|
3
|
+
//#region src/ai.ts
|
|
4
|
+
var AIError = class extends Error {
|
|
5
|
+
constructor(code, message, cause) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.cause = cause;
|
|
9
|
+
this.name = "AIError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var ShapeError = class extends Error {
|
|
13
|
+
constructor(code, message, issues = []) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.issues = issues;
|
|
17
|
+
this.name = "ShapeError";
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function isRecord(value) {
|
|
21
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
function issue(message, path = []) {
|
|
24
|
+
return {
|
|
25
|
+
message,
|
|
26
|
+
path
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function validateDefinition(definition, value, path = []) {
|
|
30
|
+
if (definition === "string" || definition === "number" || definition === "boolean") {
|
|
31
|
+
if (typeof value === definition) return { value };
|
|
32
|
+
return { issues: [issue(`Expected ${path.join(".") || "value"} to be ${definition}`, path)] };
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(definition)) {
|
|
35
|
+
if (!Array.isArray(value)) return { issues: [issue(`Expected ${path.join(".") || "value"} to be array`, path)] };
|
|
36
|
+
const itemDefinition = definition[0];
|
|
37
|
+
const issues$1 = [];
|
|
38
|
+
const output$1 = [];
|
|
39
|
+
for (const [index, item] of value.entries()) {
|
|
40
|
+
const result = validateDefinition(itemDefinition, item, [...path, index]);
|
|
41
|
+
if (result.issues !== void 0) issues$1.push(...result.issues);
|
|
42
|
+
else output$1.push(result.value);
|
|
43
|
+
}
|
|
44
|
+
if (issues$1.length > 0) return { issues: issues$1 };
|
|
45
|
+
return { value: output$1 };
|
|
46
|
+
}
|
|
47
|
+
if (!isRecord(value)) return { issues: [issue(`Expected ${path.join(".") || "value"} to be object`, path)] };
|
|
48
|
+
const output = {};
|
|
49
|
+
const issues = [];
|
|
50
|
+
for (const [key, field] of Object.entries(definition)) {
|
|
51
|
+
const result = validateDefinition(field, value[key], [...path, key]);
|
|
52
|
+
if (result.issues !== void 0) issues.push(...result.issues);
|
|
53
|
+
else output[key] = result.value;
|
|
54
|
+
}
|
|
55
|
+
if (issues.length > 0) return { issues };
|
|
56
|
+
return { value: output };
|
|
57
|
+
}
|
|
58
|
+
function bridgeSchema(definition) {
|
|
59
|
+
if (definition === "string" || definition === "number" || definition === "boolean") return { type: definition };
|
|
60
|
+
if (Array.isArray(definition)) return {
|
|
61
|
+
type: "array",
|
|
62
|
+
items: bridgeSchema(definition[0])
|
|
63
|
+
};
|
|
64
|
+
const properties = {};
|
|
65
|
+
const required = [];
|
|
66
|
+
for (const [key, value] of Object.entries(definition)) {
|
|
67
|
+
properties[key] = bridgeSchema(value);
|
|
68
|
+
required.push(key);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
type: "object",
|
|
72
|
+
properties,
|
|
73
|
+
required
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function assertValidShape(definition, path = "shape") {
|
|
77
|
+
if (definition === "string" || definition === "number" || definition === "boolean") return;
|
|
78
|
+
if (Array.isArray(definition)) {
|
|
79
|
+
if (definition.length !== 1) throw new ShapeError("invalid_shape", `${path} arrays must contain exactly one item shape`);
|
|
80
|
+
assertValidShape(definition[0], `${path}[0]`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (!isRecord(definition)) throw new ShapeError("invalid_shape", `${path} must be a shape object`);
|
|
84
|
+
for (const [key, value] of Object.entries(definition)) assertValidShape(value, `${path}.${key}`);
|
|
85
|
+
}
|
|
86
|
+
function createShape(definition) {
|
|
87
|
+
assertValidShape(definition);
|
|
88
|
+
const api = {
|
|
89
|
+
"~standard": {
|
|
90
|
+
version: 1,
|
|
91
|
+
vendor: "bleam",
|
|
92
|
+
validate(value) {
|
|
93
|
+
return api.safeParse(value);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
parse(value) {
|
|
97
|
+
const result = api.safeParse(value);
|
|
98
|
+
if (result.issues !== void 0) throw new ShapeError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
99
|
+
return result.value;
|
|
100
|
+
},
|
|
101
|
+
safeParse(value) {
|
|
102
|
+
return validateDefinition(definition, value);
|
|
103
|
+
},
|
|
104
|
+
toJSONSchema() {
|
|
105
|
+
return bridgeSchema(definition);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return api;
|
|
109
|
+
}
|
|
110
|
+
function shape(definition) {
|
|
111
|
+
return createShape(definition);
|
|
112
|
+
}
|
|
113
|
+
function schemaToBridgeSchema(schema) {
|
|
114
|
+
const schemaWithBridge = schema;
|
|
115
|
+
if (typeof schemaWithBridge.toJSONSchema === "function") return schemaWithBridge.toJSONSchema();
|
|
116
|
+
}
|
|
117
|
+
function validateStructuredInput(input) {
|
|
118
|
+
const result = input.shape["~standard"].validate(input.value);
|
|
119
|
+
if (result.issues !== void 0) throw new AIError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
120
|
+
return result.value;
|
|
121
|
+
}
|
|
122
|
+
function stableStringify(value) {
|
|
123
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
124
|
+
if (isRecord(value)) return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
125
|
+
return JSON.stringify(value) ?? "undefined";
|
|
126
|
+
}
|
|
127
|
+
function promptFromInput(options) {
|
|
128
|
+
const parts = [];
|
|
129
|
+
if (options.prompt) parts.push(options.prompt);
|
|
130
|
+
if (typeof options.input === "string") parts.push(options.input);
|
|
131
|
+
else if (options.input) parts.push(stableStringify(validateStructuredInput(options.input)));
|
|
132
|
+
if (parts.length === 0) throw new AIError("invalid_message", "Expected prompt or input");
|
|
133
|
+
return parts.join("\n\n");
|
|
134
|
+
}
|
|
135
|
+
function bridge() {
|
|
136
|
+
const nativeBridge = NativeModules.BleamAIBridge;
|
|
137
|
+
if (!nativeBridge) throw new AIError("unsupported_runtime", "bleam/ai requires the Bleam native runtime");
|
|
138
|
+
return nativeBridge;
|
|
139
|
+
}
|
|
140
|
+
function mapNativeError(error) {
|
|
141
|
+
const code = isRecord(error) && typeof error.code === "string" ? error.code : "";
|
|
142
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
143
|
+
if (code === "UNSUPPORTED_OS") return new AIError("unsupported_os", message, error);
|
|
144
|
+
if (code === "MODEL_UNAVAILABLE") return new AIError("model_unavailable", message, error);
|
|
145
|
+
if (code === "INVALID_SCHEMA") return new AIError("invalid_schema", message, error);
|
|
146
|
+
if (code === "INVALID_MESSAGE") return new AIError("invalid_message", message, error);
|
|
147
|
+
if (code === "CONTEXT_WINDOW_EXCEEDED") return new AIError("context_window_exceeded", message, error);
|
|
148
|
+
return new AIError("generation_failed", message, error);
|
|
149
|
+
}
|
|
150
|
+
async function isAvailable() {
|
|
151
|
+
try {
|
|
152
|
+
return await bridge().isAvailable?.() ?? false;
|
|
153
|
+
} catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function generate(options) {
|
|
158
|
+
const output = options.output;
|
|
159
|
+
const request = {
|
|
160
|
+
system: options.system,
|
|
161
|
+
prompt: promptFromInput(options),
|
|
162
|
+
temperature: options.temperature,
|
|
163
|
+
maxTokens: options.maxTokens,
|
|
164
|
+
outputSchema: output ? schemaToBridgeSchema(output) : void 0
|
|
165
|
+
};
|
|
166
|
+
try {
|
|
167
|
+
const value = await bridge().generate?.(request);
|
|
168
|
+
if (output) {
|
|
169
|
+
const result = output["~standard"].validate(value);
|
|
170
|
+
if (result.issues !== void 0) throw new AIError("validation_failed", result.issues.map((nextIssue) => nextIssue.message).join("\n"), result.issues);
|
|
171
|
+
return result.value;
|
|
172
|
+
}
|
|
173
|
+
if (typeof value !== "string") throw new AIError("generation_failed", "Expected generated text");
|
|
174
|
+
return value;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (error instanceof AIError) throw error;
|
|
177
|
+
throw mapNativeError(error);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
export { AIError, generate, isAvailable, shape };
|