@umituz/web-ai 1.0.2 → 1.0.4

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 (47) hide show
  1. package/dist/{AIErrors-B2ogd1PB.d.ts → AIErrors-CLjXRty-.d.ts} +99 -92
  2. package/dist/{AIErrors-C5EasV_c.d.cts → AIErrors-DjZXUiLT.d.cts} +99 -92
  3. package/dist/PromptBuilder-B5Lm1znm.d.ts +215 -0
  4. package/dist/PromptBuilder-DIUZlfV6.d.cts +215 -0
  5. package/dist/{text-generation.service-BQO3Tvz2.d.cts → cancellation-error-Cj7fJyCg.d.cts} +66 -3
  6. package/dist/{text-generation.service-CcYQM6Ch.d.ts → cancellation-error-CkMn5Kft.d.ts} +66 -3
  7. package/dist/chat/index.cjs.map +1 -1
  8. package/dist/chat/index.d.cts +3 -3
  9. package/dist/chat/index.d.ts +3 -3
  10. package/dist/chat/index.js.map +1 -1
  11. package/dist/{chat.service-CL1qtYxe.d.cts → chat.service-9M-KpLo-.d.cts} +1 -1
  12. package/dist/{chat.service-q05GnhEm.d.ts → chat.service-B4FLFxR1.d.ts} +1 -1
  13. package/dist/content/index.cjs +1582 -452
  14. package/dist/content/index.cjs.map +1 -1
  15. package/dist/content/index.d.cts +165 -219
  16. package/dist/content/index.d.ts +165 -219
  17. package/dist/content/index.js +1525 -453
  18. package/dist/content/index.js.map +1 -1
  19. package/dist/{create-ai-9PW752z8.d.ts → create-ai-0eZFFqTw.d.ts} +1 -1
  20. package/dist/{create-ai-CXYKPK6G.d.cts → create-ai-BrKR_5fQ.d.cts} +1 -1
  21. package/dist/hooks/index.cjs +924 -31
  22. package/dist/hooks/index.cjs.map +1 -1
  23. package/dist/hooks/index.d.cts +44 -8
  24. package/dist/hooks/index.d.ts +44 -8
  25. package/dist/hooks/index.js +919 -32
  26. package/dist/hooks/index.js.map +1 -1
  27. package/dist/index-BUVULo3h.d.ts +177 -0
  28. package/dist/index-BulSkB2L.d.cts +177 -0
  29. package/dist/index.cjs +3057 -563
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +8 -5
  32. package/dist/index.d.ts +8 -5
  33. package/dist/index.js +3011 -564
  34. package/dist/index.js.map +1 -1
  35. package/dist/{provider.factory-DfDfk0dK.d.ts → provider.factory-BwPnqSEU.d.ts} +3 -1
  36. package/dist/{provider.factory-BxsfK4WW.d.cts → provider.factory-DyfJtR8M.d.cts} +3 -1
  37. package/dist/providers/index.cjs +818 -27
  38. package/dist/providers/index.cjs.map +1 -1
  39. package/dist/providers/index.d.cts +50 -8
  40. package/dist/providers/index.d.ts +50 -8
  41. package/dist/providers/index.js +806 -28
  42. package/dist/providers/index.js.map +1 -1
  43. package/dist/wizard/index.d.cts +2 -86
  44. package/dist/wizard/index.d.ts +2 -86
  45. package/package.json +21 -1
  46. package/dist/ContentGeneration-B6iK2B33.d.cts +0 -93
  47. package/dist/ContentGeneration-B6iK2B33.d.ts +0 -93
@@ -2627,39 +2627,814 @@ var FalProvider = class extends BaseProvider {
2627
2627
  }
2628
2628
  };
2629
2629
 
2630
- // src/providers/pruna/pruna-generate.ts
2631
- var prunaCorePromise = null;
2632
- async function loadPrunaCore() {
2633
- if (!prunaCorePromise) {
2634
- const specifier = "@umituz/pruna-provider/core";
2635
- prunaCorePromise = import(specifier);
2630
+ // src/providers/pruna/client/core/constants/limits.ts
2631
+ var DEFAULT_ASPECT_RATIO = "16:9";
2632
+ var P_VIDEO_DEFAULTS = {
2633
+ duration: 5,
2634
+ resolution: "720p",
2635
+ fps: 24,
2636
+ draft: false,
2637
+ promptUpsampling: true
2638
+ };
2639
+ var POLL_DEFAULTS = {
2640
+ intervalMs: 3e3,
2641
+ maxAttempts: 120,
2642
+ maxIntervalMs: 1e4,
2643
+ backoffBase: 1.5,
2644
+ jitterPercent: 0.1
2645
+ };
2646
+ var DEFAULT_DEDUPLICATOR_TTL_MS = 3e4;
2647
+ var DEDUPLICATOR_CLEANUP_GRACE_MS = 100;
2648
+
2649
+ // src/providers/pruna/client/core/constants/network.ts
2650
+ var HTTPS_PREFIX = "https://";
2651
+ var HTTP_PREFIX = "http://";
2652
+ var BASE64_MARKER = "base64,";
2653
+ var PRUNA_BASE_URL = "https://api.pruna.ai";
2654
+ var PRUNA_PREDICTIONS_URL = `${PRUNA_BASE_URL}/v1/predictions`;
2655
+ var PRUNA_FILES_URL = `${PRUNA_BASE_URL}/v1/files`;
2656
+ var AUTH_FAILURE_STATUSES = [401, 403];
2657
+ var MIN_API_KEY_LENGTH = 1;
2658
+ var MAX_API_KEY_LENGTH = 256;
2659
+
2660
+ // src/providers/pruna/client/core/constants/mime-types.ts
2661
+ var MIME_PNG = "image/png";
2662
+ var MIME_JPEG = "image/jpeg";
2663
+ var MIME_WEBP = "image/webp";
2664
+ var DEFAULT_IMAGE_MIME = MIME_PNG;
2665
+ var MIME_FILE_EXTENSION = {
2666
+ [MIME_PNG]: "png",
2667
+ [MIME_JPEG]: "jpg",
2668
+ [MIME_WEBP]: "webp"
2669
+ };
2670
+ var IMAGE_SIGNATURES = {
2671
+ PNG: [137, 80],
2672
+ JPEG: [255, 216],
2673
+ WEBP_RIFF: [82, 73]
2674
+ };
2675
+
2676
+ // src/providers/pruna/client/core/errors/cancellation-error.ts
2677
+ var CANCELLATION_MESSAGE = "Request cancelled by user";
2678
+ var CancellationError = class extends Error {
2679
+ constructor(message = CANCELLATION_MESSAGE) {
2680
+ super(message);
2681
+ this.name = "CancellationError";
2636
2682
  }
2637
- return prunaCorePromise;
2683
+ };
2684
+ function isCancellationError(value) {
2685
+ return value instanceof CancellationError;
2638
2686
  }
2639
- async function* generateWithPruna(request, apiKey, signal, onProgress) {
2640
- const prunaCore = await loadPrunaCore();
2641
- if (prunaCore.generateWithPruna) {
2642
- yield* prunaCore.generateWithPruna(request, apiKey, signal, onProgress);
2643
- return;
2644
- }
2645
- if (!prunaCore.submitPrediction || !prunaCore.pollForResult) {
2646
- throw new Error("@umituz/pruna-provider/core does not export required functions");
2647
- }
2648
- const modelId = request.model || (request.image ? "p-image-edit" : "p-image");
2649
- const prediction = await prunaCore.submitPrediction(modelId, request, apiKey, signal, onProgress);
2650
- if (prediction.output && typeof prediction.output === "object" && "url" in prediction.output) {
2651
- yield {
2652
- stage: "predicting",
2653
- result: { url: prediction.output.url, model: modelId }
2687
+
2688
+ // src/providers/pruna/client/core/errors/authentication-error.ts
2689
+ var AUTHENTICATION_MESSAGE = "Authentication failed. Please check your API key.";
2690
+ var AuthenticationError = class extends Error {
2691
+ constructor(message = AUTHENTICATION_MESSAGE) {
2692
+ super(message);
2693
+ this.name = "AuthenticationError";
2694
+ }
2695
+ };
2696
+
2697
+ // src/providers/pruna/client/core/errors/timeout-error.ts
2698
+ var TIMEOUT_MESSAGE = "Generation timed out. Maximum polling attempts reached.";
2699
+ var TimeoutError = class extends Error {
2700
+ constructor(message = TIMEOUT_MESSAGE) {
2701
+ super(message);
2702
+ this.name = "TimeoutError";
2703
+ }
2704
+ };
2705
+
2706
+ // src/providers/pruna/client/core/errors/validation-error.ts
2707
+ var ValidationError2 = class extends Error {
2708
+ field;
2709
+ reason;
2710
+ receivedValue;
2711
+ constructor(context) {
2712
+ super(`Invalid ${context.field}: ${context.reason}`);
2713
+ this.name = "ValidationError";
2714
+ this.field = context.field;
2715
+ this.reason = context.reason;
2716
+ this.receivedValue = context.receivedValue;
2717
+ }
2718
+ };
2719
+
2720
+ // src/providers/pruna/client/core/utils/base64.ts
2721
+ var globalScope = globalThis;
2722
+ var hasBuffer = typeof globalScope.Buffer?.from === "function";
2723
+ function base64ToBytes(raw) {
2724
+ if (hasBuffer) {
2725
+ const bufferFactory = globalScope.Buffer;
2726
+ if (!bufferFactory) {
2727
+ throw new Error("Buffer unavailable despite detection flag");
2728
+ }
2729
+ const buf = bufferFactory.from(raw, "base64");
2730
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
2731
+ }
2732
+ const decoded = atob(raw);
2733
+ return Uint8Array.from(decoded, (char) => char.charCodeAt(0));
2734
+ }
2735
+
2736
+ // src/providers/pruna/client/core/utils/mime-detector.ts
2737
+ var MIN_SIGNATURE_BYTES = 2;
2738
+ function matchesSignature(bytes, signature) {
2739
+ if (bytes.length < signature.length) return false;
2740
+ return signature.every((value, index) => bytes[index] === value);
2741
+ }
2742
+ function detectImageMime(bytes) {
2743
+ if (bytes.length < MIN_SIGNATURE_BYTES) return DEFAULT_IMAGE_MIME;
2744
+ if (matchesSignature(bytes, IMAGE_SIGNATURES.PNG)) return MIME_PNG;
2745
+ if (matchesSignature(bytes, IMAGE_SIGNATURES.JPEG)) return MIME_JPEG;
2746
+ if (matchesSignature(bytes, IMAGE_SIGNATURES.WEBP_RIFF)) return MIME_WEBP;
2747
+ return DEFAULT_IMAGE_MIME;
2748
+ }
2749
+
2750
+ // src/providers/pruna/client/core/utils/api-error-parser.ts
2751
+ async function parseApiError(response, fallback) {
2752
+ const payload = await response.json().catch(() => ({ message: response.statusText }));
2753
+ const message = payload.message ?? `${fallback}: ${response.status}`;
2754
+ const error = new Error(message);
2755
+ error.statusCode = response.status;
2756
+ return error;
2757
+ }
2758
+
2759
+ // src/providers/pruna/client/core/protocol/http-status.ts
2760
+ function isAuthFailureStatus(status) {
2761
+ return AUTH_FAILURE_STATUSES.includes(status);
2762
+ }
2763
+
2764
+ // src/providers/pruna/client/core/protocol/job-status.ts
2765
+ var TERMINAL_SUCCESS_STATUSES = ["succeeded", "completed"];
2766
+ var TERMINAL_FAILURE_STATUSES = ["failed"];
2767
+ function isTerminalSuccessStatus(status) {
2768
+ if (!status) return false;
2769
+ return TERMINAL_SUCCESS_STATUSES.includes(status);
2770
+ }
2771
+ function isTerminalFailureStatus(status) {
2772
+ if (!status) return false;
2773
+ return TERMINAL_FAILURE_STATUSES.includes(status);
2774
+ }
2775
+
2776
+ // src/providers/pruna/client/core/protocol/abort-signal-classifier.ts
2777
+ function isAbortError(err) {
2778
+ if (err instanceof DOMException && err.code === DOMException.ABORT_ERR) return true;
2779
+ if (err instanceof Error && err.name === "AbortError") return true;
2780
+ return false;
2781
+ }
2782
+
2783
+ // src/providers/pruna/client/core/protocol/pruna-url-builder.ts
2784
+ function isAbsoluteHttpUrl(value) {
2785
+ return value.startsWith(HTTPS_PREFIX) || value.startsWith(HTTP_PREFIX);
2786
+ }
2787
+ function isHttpsUrl(value) {
2788
+ return value.startsWith(HTTPS_PREFIX);
2789
+ }
2790
+ function buildPrunaApiUrl(pathOrUrl) {
2791
+ if (isAbsoluteHttpUrl(pathOrUrl)) return pathOrUrl;
2792
+ return `${PRUNA_BASE_URL}${pathOrUrl}`;
2793
+ }
2794
+ function buildFileUrlById(fileId) {
2795
+ return `${PRUNA_FILES_URL}/${fileId}`;
2796
+ }
2797
+
2798
+ // src/providers/pruna/client/core/math/exponential-backoff.ts
2799
+ function calculateExponentialBackoff2(inputs) {
2800
+ const {
2801
+ attempt,
2802
+ baseIntervalMs,
2803
+ maxIntervalMs = POLL_DEFAULTS.maxIntervalMs,
2804
+ jitterPercent = POLL_DEFAULTS.jitterPercent,
2805
+ growthBase = POLL_DEFAULTS.backoffBase
2806
+ } = inputs;
2807
+ const safeAttempt = Math.max(0, attempt);
2808
+ const safeBase = Math.max(0, baseIntervalMs);
2809
+ const exponentialDelay = Math.min(safeBase * Math.pow(growthBase, safeAttempt), maxIntervalMs);
2810
+ const jitter = exponentialDelay * jitterPercent * Math.random();
2811
+ return exponentialDelay + jitter;
2812
+ }
2813
+
2814
+ // src/providers/pruna/client/core/utils/uri-resolver.ts
2815
+ var uriCache = /* @__PURE__ */ new WeakMap();
2816
+ function stripBase64Prefix(image) {
2817
+ if (isHttpsUrl(image)) return image;
2818
+ const base64Index = image.indexOf(BASE64_MARKER);
2819
+ return base64Index !== -1 ? image.slice(base64Index + BASE64_MARKER.length) : image;
2820
+ }
2821
+ function readOutput(output) {
2822
+ if (typeof output === "string") return output;
2823
+ if (Array.isArray(output) && output.length > 0) return output[0] ?? null;
2824
+ if (output !== null && typeof output === "object" && "url" in output) {
2825
+ return output.url;
2826
+ }
2827
+ return null;
2828
+ }
2829
+ function extractUri(data) {
2830
+ const cached = uriCache.get(data);
2831
+ if (cached !== void 0) return cached;
2832
+ if (!isTerminalSuccessStatus(data.status)) {
2833
+ uriCache.set(data, null);
2834
+ return null;
2835
+ }
2836
+ const result = data.generation_url ?? readOutput(data.output) ?? data.video_url ?? data.data ?? null;
2837
+ uriCache.set(data, result);
2838
+ return result;
2839
+ }
2840
+ function resolveUri(uri) {
2841
+ return uri.startsWith("/") ? `${PRUNA_BASE_URL}${uri}` : uri;
2842
+ }
2843
+
2844
+ // src/providers/pruna/client/core/services/pruna-client.service.ts
2845
+ function buildFileExtension(mime) {
2846
+ return MIME_FILE_EXTENSION[mime] ?? "bin";
2847
+ }
2848
+ async function uploadImage(base64OrUrl, apiKey, onProgress) {
2849
+ if (isHttpsUrl(base64OrUrl)) return base64OrUrl;
2850
+ if (base64OrUrl.startsWith(HTTP_PREFIX)) {
2851
+ throw new Error("Only HTTPS URLs are supported for security.");
2852
+ }
2853
+ onProgress?.("uploading");
2854
+ const raw = stripBase64Prefix(base64OrUrl);
2855
+ let bytes;
2856
+ try {
2857
+ bytes = base64ToBytes(raw);
2858
+ } catch {
2859
+ throw new Error("Invalid image format. Provide base64 or a valid HTTPS URL.");
2860
+ }
2861
+ const mime = detectImageMime(bytes);
2862
+ const buffer = bytes.buffer.slice(
2863
+ bytes.byteOffset,
2864
+ bytes.byteOffset + bytes.byteLength
2865
+ );
2866
+ const blob = new Blob([buffer], { type: mime });
2867
+ const formData = new FormData();
2868
+ formData.append("content", blob, `upload.${buildFileExtension(mime)}`);
2869
+ const res = await fetch(PRUNA_FILES_URL, {
2870
+ method: "POST",
2871
+ headers: { apikey: apiKey },
2872
+ body: formData
2873
+ });
2874
+ if (!res.ok) {
2875
+ throw await parseApiError(res, "File upload error");
2876
+ }
2877
+ const data = await res.json();
2878
+ return data.urls?.get ?? buildFileUrlById(data.id ?? "");
2879
+ }
2880
+ async function submitPrediction(model, input, apiKey, signal, onProgress) {
2881
+ onProgress?.("predicting");
2882
+ const res = await fetch(PRUNA_PREDICTIONS_URL, {
2883
+ method: "POST",
2884
+ headers: {
2885
+ apikey: apiKey,
2886
+ Model: model,
2887
+ "Try-Sync": "true",
2888
+ "Content-Type": "application/json"
2889
+ },
2890
+ body: JSON.stringify({ input }),
2891
+ signal
2892
+ });
2893
+ if (!res.ok) {
2894
+ throw await parseApiError(res, "API error");
2895
+ }
2896
+ return res.json();
2897
+ }
2898
+ var GenerationFailedError = class extends Error {
2899
+ constructor(message) {
2900
+ super(message);
2901
+ this.name = "GenerationFailedError";
2902
+ }
2903
+ };
2904
+ async function attemptPoll(fullUrl, apiKey, signal) {
2905
+ let res;
2906
+ try {
2907
+ res = await fetch(fullUrl, { headers: { apikey: apiKey }, signal });
2908
+ } catch (err) {
2909
+ if (isAbortError(err)) throw new CancellationError();
2910
+ return { outcome: "transient-failure" };
2911
+ }
2912
+ if (!res.ok) {
2913
+ if (isAuthFailureStatus(res.status)) throw new AuthenticationError();
2914
+ return { outcome: "transient-failure" };
2915
+ }
2916
+ let data;
2917
+ try {
2918
+ data = await res.json();
2919
+ } catch {
2920
+ return { outcome: "transient-failure" };
2921
+ }
2922
+ if (isTerminalSuccessStatus(data.status)) {
2923
+ const uri = extractUri(data);
2924
+ if (uri) return { outcome: "succeeded", uri: resolveUri(uri) };
2925
+ return { outcome: "transient-failure" };
2926
+ }
2927
+ if (isTerminalFailureStatus(data.status)) {
2928
+ throw new GenerationFailedError(data.error ?? "Generation failed during processing.");
2929
+ }
2930
+ return { outcome: "continue" };
2931
+ }
2932
+ async function pollForResult(pollUrl, apiKey, maxAttempts = POLL_DEFAULTS.maxAttempts, intervalMs = POLL_DEFAULTS.intervalMs, signal, onProgress) {
2933
+ const fullUrl = isAbsoluteHttpUrl(pollUrl) ? pollUrl : buildPrunaApiUrl(pollUrl);
2934
+ const controller = new AbortController();
2935
+ const onExternalAbort = () => controller.abort();
2936
+ signal?.addEventListener("abort", onExternalAbort);
2937
+ try {
2938
+ for (let i = 0; i < maxAttempts; i++) {
2939
+ if (signal?.aborted) throw new CancellationError();
2940
+ const delay = calculateExponentialBackoff2({ attempt: i, baseIntervalMs: intervalMs });
2941
+ await new Promise((resolve) => setTimeout(resolve, delay));
2942
+ if (signal?.aborted) throw new CancellationError();
2943
+ onProgress?.("polling", i + 1);
2944
+ const result = await attemptPoll(fullUrl, apiKey, controller.signal);
2945
+ if (result.outcome === "succeeded" && result.uri) {
2946
+ return result.uri;
2947
+ }
2948
+ }
2949
+ throw new TimeoutError();
2950
+ } finally {
2951
+ signal?.removeEventListener("abort", onExternalAbort);
2952
+ controller.abort();
2953
+ }
2954
+ }
2955
+
2956
+ // src/providers/pruna/client/core/math/comparison-hash.ts
2957
+ function sortKeysDeep(value) {
2958
+ if (Array.isArray(value)) {
2959
+ return value.map(sortKeysDeep);
2960
+ }
2961
+ if (value !== null && typeof value === "object") {
2962
+ const record = value;
2963
+ return Object.keys(record).sort().reduce((acc, key) => {
2964
+ acc[key] = sortKeysDeep(record[key]);
2965
+ return acc;
2966
+ }, {});
2967
+ }
2968
+ return value;
2969
+ }
2970
+ function hashValue(value) {
2971
+ if (value === null || typeof value !== "object") {
2972
+ return String(value);
2973
+ }
2974
+ try {
2975
+ return JSON.stringify(sortKeysDeep(value));
2976
+ } catch {
2977
+ return String(value);
2978
+ }
2979
+ }
2980
+ function compositeHash(...values) {
2981
+ return values.map(hashValue).join("|");
2982
+ }
2983
+
2984
+ // src/providers/pruna/client/core/math/cache-key-derivation.ts
2985
+ function deriveGenerationCacheKey(apiKey, input) {
2986
+ return `${apiKey}:${compositeHash(input)}`;
2987
+ }
2988
+
2989
+ // src/providers/pruna/client/core/validation/api-key-validator.ts
2990
+ function assertValidApiKey(apiKey) {
2991
+ if (typeof apiKey !== "string") {
2992
+ throw new ValidationError2({
2993
+ field: "apiKey",
2994
+ reason: "must be a string",
2995
+ receivedValue: typeof apiKey
2996
+ });
2997
+ }
2998
+ if (apiKey.length < MIN_API_KEY_LENGTH) {
2999
+ throw new ValidationError2({
3000
+ field: "apiKey",
3001
+ reason: "must not be empty",
3002
+ receivedValue: apiKey
3003
+ });
3004
+ }
3005
+ if (apiKey.length > MAX_API_KEY_LENGTH) {
3006
+ throw new ValidationError2({
3007
+ field: "apiKey",
3008
+ reason: `exceeds maximum length of ${MAX_API_KEY_LENGTH}`,
3009
+ receivedValue: apiKey.length
3010
+ });
3011
+ }
3012
+ }
3013
+
3014
+ // src/providers/pruna/client/core/validation/image-source-validator.ts
3015
+ function classifyImageSource(source) {
3016
+ if (source.startsWith(HTTPS_PREFIX)) return "https-url";
3017
+ if (source.startsWith(HTTP_PREFIX)) return "http-url";
3018
+ if (source.includes(BASE64_MARKER) || /^[A-Za-z0-9+/=]+$/.test(source)) return "base64";
3019
+ return "unknown";
3020
+ }
3021
+ function assertSecureImageSource(image, fieldName = "image") {
3022
+ if (typeof image !== "string") {
3023
+ throw new ValidationError2({
3024
+ field: fieldName,
3025
+ reason: "must be a string",
3026
+ receivedValue: typeof image
3027
+ });
3028
+ }
3029
+ const kind = classifyImageSource(image);
3030
+ if (kind === "http-url") {
3031
+ throw new ValidationError2({
3032
+ field: fieldName,
3033
+ reason: "HTTP URLs are not supported; use HTTPS or a base64 data URI",
3034
+ receivedValue: image
3035
+ });
3036
+ }
3037
+ if (kind === "unknown") {
3038
+ throw new ValidationError2({
3039
+ field: fieldName,
3040
+ reason: "must be an HTTPS URL or a base64 data URI",
3041
+ receivedValue: typeof image
3042
+ });
3043
+ }
3044
+ }
3045
+
3046
+ // src/providers/pruna/client/core/validation/aspect-ratio-validator.ts
3047
+ var SUPPORTED_ASPECT_RATIOS = [
3048
+ "16:9",
3049
+ "9:16",
3050
+ "1:1",
3051
+ "4:3",
3052
+ "3:4",
3053
+ "3:2",
3054
+ "2:3"
3055
+ ];
3056
+ function isSupportedAspectRatio(value) {
3057
+ return typeof value === "string" && SUPPORTED_ASPECT_RATIOS.includes(value);
3058
+ }
3059
+ function assertValidAspectRatio(value, fieldName = "aspect_ratio") {
3060
+ if (value === void 0 || value === null) return;
3061
+ if (!isSupportedAspectRatio(value)) {
3062
+ throw new ValidationError2({
3063
+ field: fieldName,
3064
+ reason: `must be one of ${SUPPORTED_ASPECT_RATIOS.join(", ")}`,
3065
+ receivedValue: value
3066
+ });
3067
+ }
3068
+ }
3069
+
3070
+ // src/providers/pruna/client/core/validation/resolution-validator.ts
3071
+ var SUPPORTED_RESOLUTIONS = ["720p", "1080p"];
3072
+ function isSupportedResolution(value) {
3073
+ return typeof value === "string" && SUPPORTED_RESOLUTIONS.includes(value);
3074
+ }
3075
+ function assertValidResolution(value, fieldName = "resolution") {
3076
+ if (value === void 0 || value === null) return;
3077
+ if (!isSupportedResolution(value)) {
3078
+ throw new ValidationError2({
3079
+ field: fieldName,
3080
+ reason: `must be one of ${SUPPORTED_RESOLUTIONS.join(", ")}`,
3081
+ receivedValue: value
3082
+ });
3083
+ }
3084
+ }
3085
+
3086
+ // src/providers/pruna/client/core/validation/duration-validator.ts
3087
+ var MIN_VIDEO_DURATION_SECONDS = 1;
3088
+ var MAX_VIDEO_DURATION_SECONDS = 30;
3089
+ function isValidDuration(value) {
3090
+ return typeof value === "number" && Number.isFinite(value) && value >= MIN_VIDEO_DURATION_SECONDS && value <= MAX_VIDEO_DURATION_SECONDS && Number.isInteger(value);
3091
+ }
3092
+ function assertValidDuration(value, fieldName = "duration") {
3093
+ if (value === void 0 || value === null) return;
3094
+ if (!isValidDuration(value)) {
3095
+ throw new ValidationError2({
3096
+ field: fieldName,
3097
+ reason: `must be an integer between ${MIN_VIDEO_DURATION_SECONDS} and ${MAX_VIDEO_DURATION_SECONDS} seconds`,
3098
+ receivedValue: value
3099
+ });
3100
+ }
3101
+ }
3102
+
3103
+ // src/providers/pruna/client/core/validation/prompt-validator.ts
3104
+ var MIN_PROMPT_LENGTH = 1;
3105
+ var MAX_PROMPT_LENGTH = 4e3;
3106
+ function assertValidPrompt(value, fieldName = "prompt") {
3107
+ if (typeof value !== "string") {
3108
+ throw new ValidationError2({
3109
+ field: fieldName,
3110
+ reason: "must be a string",
3111
+ receivedValue: typeof value
3112
+ });
3113
+ }
3114
+ if (value.trim().length < MIN_PROMPT_LENGTH) {
3115
+ throw new ValidationError2({
3116
+ field: fieldName,
3117
+ reason: "must not be empty or whitespace-only",
3118
+ receivedValue: value
3119
+ });
3120
+ }
3121
+ if (value.length > MAX_PROMPT_LENGTH) {
3122
+ throw new ValidationError2({
3123
+ field: fieldName,
3124
+ reason: `exceeds maximum length of ${MAX_PROMPT_LENGTH} characters`,
3125
+ receivedValue: value.length
3126
+ });
3127
+ }
3128
+ }
3129
+
3130
+ // src/providers/pruna/client/core/validation/seed-validator.ts
3131
+ var MIN_SEED = -2147483648;
3132
+ var MAX_SEED = 2147483647;
3133
+ function isValidSeed(value) {
3134
+ return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value) && value >= MIN_SEED && value <= MAX_SEED;
3135
+ }
3136
+ function assertValidSeed(value, fieldName = "seed") {
3137
+ if (value === void 0 || value === null) return;
3138
+ if (!isValidSeed(value)) {
3139
+ throw new ValidationError2({
3140
+ field: fieldName,
3141
+ reason: `must be an integer in [${MIN_SEED}, ${MAX_SEED}]`,
3142
+ receivedValue: value
3143
+ });
3144
+ }
3145
+ }
3146
+
3147
+ // src/providers/pruna/client/core/protocol/input-type-guard.ts
3148
+ function isTextToImageInput(input) {
3149
+ return input.model === "p-image";
3150
+ }
3151
+ function isImageToImageInput(input) {
3152
+ return input.model === "p-image-edit";
3153
+ }
3154
+ function isImageToVideoInput(input) {
3155
+ return input.model === "p-video";
3156
+ }
3157
+
3158
+ // src/providers/pruna/client/core/utils/request-deduplicator.ts
3159
+ var RequestDeduplicator2 = class {
3160
+ pendingRequests = /* @__PURE__ */ new Map();
3161
+ ttl;
3162
+ constructor(ttl = DEFAULT_DEDUPLICATOR_TTL_MS) {
3163
+ this.ttl = ttl;
3164
+ }
3165
+ execute(key, fn) {
3166
+ const existing = this.pendingRequests.get(key);
3167
+ if (existing) {
3168
+ if (Date.now() - existing.timestamp < this.ttl) {
3169
+ return existing.promise;
3170
+ }
3171
+ this.pendingRequests.delete(key);
3172
+ }
3173
+ const promise = fn().finally(() => {
3174
+ setTimeout(() => {
3175
+ this.pendingRequests.delete(key);
3176
+ }, DEDUPLICATOR_CLEANUP_GRACE_MS);
3177
+ });
3178
+ this.pendingRequests.set(key, { promise, timestamp: Date.now() });
3179
+ return promise;
3180
+ }
3181
+ clear() {
3182
+ this.pendingRequests.clear();
3183
+ }
3184
+ size() {
3185
+ this.purgeExpired();
3186
+ return this.pendingRequests.size;
3187
+ }
3188
+ purgeExpired() {
3189
+ const now = Date.now();
3190
+ for (const [key, value] of this.pendingRequests) {
3191
+ if (now - value.timestamp >= this.ttl) {
3192
+ this.pendingRequests.delete(key);
3193
+ }
3194
+ }
3195
+ }
3196
+ };
3197
+ var globalDeduplicator = new RequestDeduplicator2();
3198
+
3199
+ // src/providers/pruna/client/generation/builders/text-to-image.builder.ts
3200
+ var TextToImageInputBuilder = class {
3201
+ model = "p-image";
3202
+ build(input, _context) {
3203
+ const payload = {
3204
+ prompt: input.prompt,
3205
+ aspect_ratio: input.aspect_ratio ?? DEFAULT_ASPECT_RATIO
3206
+ };
3207
+ if (input.seed !== void 0) {
3208
+ payload.seed = input.seed;
3209
+ }
3210
+ return Promise.resolve(payload);
3211
+ }
3212
+ };
3213
+
3214
+ // src/providers/pruna/client/generation/builders/image-to-image.builder.ts
3215
+ var ImageToImageInputBuilder = class {
3216
+ model = "p-image-edit";
3217
+ build(input, _context) {
3218
+ const payload = {
3219
+ images: [stripBase64Prefix(input.image)],
3220
+ prompt: input.prompt,
3221
+ aspect_ratio: input.aspect_ratio ?? DEFAULT_ASPECT_RATIO
3222
+ };
3223
+ if (input.seed !== void 0) {
3224
+ payload.seed = input.seed;
3225
+ }
3226
+ return Promise.resolve(payload);
3227
+ }
3228
+ };
3229
+
3230
+ // src/providers/pruna/client/generation/builders/image-to-video.builder.ts
3231
+ var ImageToVideoInputBuilder = class {
3232
+ model = "p-video";
3233
+ async build(input, context) {
3234
+ if (context.signal?.aborted) {
3235
+ throw new CancellationError();
3236
+ }
3237
+ const fileUrl = await uploadImage(
3238
+ input.image,
3239
+ context.apiKey,
3240
+ context.onProgress
3241
+ );
3242
+ return {
3243
+ image: fileUrl,
3244
+ prompt: input.prompt,
3245
+ duration: input.duration ?? P_VIDEO_DEFAULTS.duration,
3246
+ resolution: input.resolution ?? P_VIDEO_DEFAULTS.resolution,
3247
+ fps: P_VIDEO_DEFAULTS.fps,
3248
+ draft: input.draft ?? P_VIDEO_DEFAULTS.draft,
3249
+ aspect_ratio: input.aspect_ratio ?? DEFAULT_ASPECT_RATIO,
3250
+ prompt_upsampling: P_VIDEO_DEFAULTS.promptUpsampling
2654
3251
  };
2655
- return;
2656
3252
  }
2657
- const pollUrl = prediction.status_url || prediction.generation_url;
3253
+ };
3254
+
3255
+ // src/providers/pruna/client/generation/builders/model-input-builder.ts
3256
+ var ModelInputBuilderRegistry = class {
3257
+ builders = /* @__PURE__ */ new Map();
3258
+ register(builder) {
3259
+ this.builders.set(builder.model, builder);
3260
+ return this;
3261
+ }
3262
+ resolve(model) {
3263
+ const builder = this.builders.get(model);
3264
+ if (!builder) {
3265
+ throw new Error(`No input builder registered for model: ${model}`);
3266
+ }
3267
+ return builder;
3268
+ }
3269
+ };
3270
+
3271
+ // src/providers/pruna/client/generation/builders/builder-factory.ts
3272
+ function createDefaultBuilderRegistry() {
3273
+ return new ModelInputBuilderRegistry().register(new TextToImageInputBuilder()).register(new ImageToImageInputBuilder()).register(new ImageToVideoInputBuilder());
3274
+ }
3275
+
3276
+ // src/providers/pruna/client/generation/services/generation.service.ts
3277
+ var builderRegistry = createDefaultBuilderRegistry();
3278
+ function validateGenerateInput(input) {
3279
+ assertValidPrompt(input.prompt, `${input.model}.prompt`);
3280
+ assertValidAspectRatio(input.aspect_ratio, `${input.model}.aspect_ratio`);
3281
+ if (isTextToImageInput(input)) {
3282
+ assertValidSeed(input.seed, `${input.model}.seed`);
3283
+ } else if (isImageToImageInput(input)) {
3284
+ assertSecureImageSource(input.image, `${input.model}.image`);
3285
+ assertValidSeed(input.seed, `${input.model}.seed`);
3286
+ } else if (isImageToVideoInput(input)) {
3287
+ assertSecureImageSource(input.image, `${input.model}.image`);
3288
+ assertValidDuration(input.duration, `${input.model}.duration`);
3289
+ assertValidResolution(input.resolution, `${input.model}.resolution`);
3290
+ }
3291
+ }
3292
+ async function generate(apiKey, input, options = {}) {
3293
+ assertValidApiKey(apiKey);
3294
+ validateGenerateInput(input);
3295
+ const { signal, onProgress, useCache = true } = options;
3296
+ if (useCache) {
3297
+ const cacheKey = deriveGenerationCacheKey(apiKey, input);
3298
+ return globalDeduplicator.execute(
3299
+ cacheKey,
3300
+ () => generateInternal(apiKey, input, { signal, onProgress })
3301
+ );
3302
+ }
3303
+ return generateInternal(apiKey, input, { signal, onProgress });
3304
+ }
3305
+ async function generateInternal(apiKey, input, options) {
3306
+ const { signal, onProgress } = options;
3307
+ const builder = builderRegistry.resolve(input.model);
3308
+ const modelInput = await builder.build(input, { apiKey, signal, onProgress });
3309
+ const response = await submitPrediction(input.model, modelInput, apiKey, signal, onProgress);
3310
+ const syncUri = extractUri(response);
3311
+ if (syncUri) {
3312
+ return { url: resolveUri(syncUri), model: input.model };
3313
+ }
3314
+ const pollUrl = response.get_url ?? response.status_url;
2658
3315
  if (!pollUrl) {
2659
- throw new Error("Pruna prediction response missing polling URL");
3316
+ throw new Error("Pruna API returned no result and no polling URL.");
2660
3317
  }
2661
- const resultUrl = await prunaCore.pollForResult(pollUrl, apiKey, 60, 2e3, signal, onProgress);
2662
- yield { stage: "polling", result: { url: resultUrl, model: modelId } };
3318
+ const url = await pollForResult(
3319
+ pollUrl,
3320
+ apiKey,
3321
+ POLL_DEFAULTS.maxAttempts,
3322
+ POLL_DEFAULTS.intervalMs,
3323
+ signal,
3324
+ onProgress
3325
+ );
3326
+ return { url, model: input.model };
3327
+ }
3328
+ async function generateImageThenVideo(apiKey, input, options = {}) {
3329
+ assertValidApiKey(apiKey);
3330
+ assertValidPrompt(input.prompt, "text-to-video.prompt");
3331
+ assertValidAspectRatio(input.aspect_ratio, "text-to-video.aspect_ratio");
3332
+ assertValidDuration(input.duration, "text-to-video.duration");
3333
+ assertValidResolution(input.resolution, "text-to-video.resolution");
3334
+ const { signal, onProgress } = options;
3335
+ const aspectRatio = input.aspect_ratio ?? DEFAULT_ASPECT_RATIO;
3336
+ onProgress?.("predicting", 1);
3337
+ const imageResult = await generate(
3338
+ apiKey,
3339
+ { model: "p-image", prompt: input.prompt, aspect_ratio: aspectRatio },
3340
+ { signal }
3341
+ );
3342
+ onProgress?.("predicting", 2);
3343
+ return generate(
3344
+ apiKey,
3345
+ {
3346
+ model: "p-video",
3347
+ prompt: input.prompt,
3348
+ image: imageResult.url,
3349
+ duration: input.duration ?? P_VIDEO_DEFAULTS.duration,
3350
+ resolution: input.resolution ?? P_VIDEO_DEFAULTS.resolution,
3351
+ aspect_ratio: aspectRatio,
3352
+ draft: input.draft ?? P_VIDEO_DEFAULTS.draft
3353
+ },
3354
+ { signal, onProgress }
3355
+ );
3356
+ }
3357
+
3358
+ // src/providers/pruna/pruna-generate.ts
3359
+ async function* generateWithPruna(request, apiKey, signal, onProgress) {
3360
+ const stages = [];
3361
+ let notify = null;
3362
+ let done = false;
3363
+ const wake = () => {
3364
+ const fn = notify;
3365
+ if (fn) {
3366
+ notify = null;
3367
+ fn();
3368
+ }
3369
+ };
3370
+ const handleProgress = (stage) => {
3371
+ stages.push({ stage });
3372
+ onProgress?.(stage);
3373
+ wake();
3374
+ };
3375
+ const promise = (async () => {
3376
+ try {
3377
+ const model = request.model ?? (request.image ? "p-image-edit" : "p-image");
3378
+ if (model === "p-video" && !request.image && request.prompt) {
3379
+ const videoRes = await generateImageThenVideo(
3380
+ apiKey,
3381
+ {
3382
+ prompt: request.prompt,
3383
+ duration: request.duration ?? 5,
3384
+ aspect_ratio: request.aspectRatio ?? request.aspect_ratio ?? "16:9"
3385
+ },
3386
+ { signal, onProgress: handleProgress }
3387
+ );
3388
+ stages.push({
3389
+ stage: "polling",
3390
+ result: { url: videoRes.url, model: "p-video" }
3391
+ });
3392
+ return;
3393
+ }
3394
+ const input = {
3395
+ model,
3396
+ prompt: request.prompt ?? "",
3397
+ aspect_ratio: request.aspectRatio ?? request.aspect_ratio ?? "1:1",
3398
+ ...request.image ? { image: request.image } : {},
3399
+ ...request.duration ? { duration: request.duration } : {},
3400
+ ...request.resolution ? { resolution: request.resolution } : {},
3401
+ ...request.seed !== void 0 ? { seed: request.seed } : {}
3402
+ };
3403
+ const res = await generate(apiKey, input, {
3404
+ signal,
3405
+ onProgress: handleProgress
3406
+ });
3407
+ stages.push({
3408
+ stage: "polling",
3409
+ result: { url: res.url, model: res.model }
3410
+ });
3411
+ } catch (err) {
3412
+ stages.push({
3413
+ stage: "processing",
3414
+ error: err instanceof Error ? err : new Error(String(err))
3415
+ });
3416
+ } finally {
3417
+ done = true;
3418
+ wake();
3419
+ }
3420
+ })();
3421
+ while (!done || stages.length > 0) {
3422
+ if (stages.length === 0 && !done) {
3423
+ await new Promise((resolve) => {
3424
+ notify = resolve;
3425
+ });
3426
+ notify = null;
3427
+ }
3428
+ while (stages.length > 0) {
3429
+ const nextStage = stages.shift();
3430
+ if (!nextStage) continue;
3431
+ yield nextStage;
3432
+ if (nextStage.error) {
3433
+ throw nextStage.error;
3434
+ }
3435
+ }
3436
+ }
3437
+ await promise;
2663
3438
  }
2664
3439
 
2665
3440
  // src/providers/pruna/pruna.provider.ts
@@ -4970,7 +5745,7 @@ function useText(options = {}, deps) {
4970
5745
  (request, onChunk) => engine instanceof ProviderFactory ? engine.chatStream(request, { onChunk }) : engine.stream(request, { onChunk }),
4971
5746
  [engine]
4972
5747
  );
4973
- const generate = react.useCallback(
5748
+ const generate2 = react.useCallback(
4974
5749
  (prompt, config) => runWithState(async (signal) => {
4975
5750
  const response = await run(buildRequest(prompt, config, signal));
4976
5751
  setResult(response.text);
@@ -5026,7 +5801,7 @@ function useText(options = {}, deps) {
5026
5801
  isLoading,
5027
5802
  error,
5028
5803
  result,
5029
- generate,
5804
+ generate: generate2,
5030
5805
  generateJSON,
5031
5806
  stream,
5032
5807
  cancel,
@@ -5412,7 +6187,7 @@ function useGroq(options = {}, deps) {
5412
6187
  },
5413
6188
  [getOrCreateAbortController]
5414
6189
  );
5415
- const generate = react.useCallback(
6190
+ const generate2 = react.useCallback(
5416
6191
  (prompt, config) => runWithState(async () => {
5417
6192
  const response = await textGeneration.generateCompletion(prompt, {
5418
6193
  model: stableConfig.model,
@@ -5484,7 +6259,7 @@ function useGroq(options = {}, deps) {
5484
6259
  isLoading,
5485
6260
  error,
5486
6261
  result,
5487
- generate,
6262
+ generate: generate2,
5488
6263
  generateJSON,
5489
6264
  stream,
5490
6265
  cancel,
@@ -5492,15 +6267,133 @@ function useGroq(options = {}, deps) {
5492
6267
  clearError
5493
6268
  };
5494
6269
  }
6270
+ function isAbortError2(err) {
6271
+ if (!(err instanceof Error)) return false;
6272
+ if (err.name === "AbortError") return true;
6273
+ if (err instanceof DOMException && err.code === DOMException.ABORT_ERR) return true;
6274
+ return isCancellationError(err);
6275
+ }
6276
+ function toError(value) {
6277
+ return value instanceof Error ? value : new Error(String(value));
6278
+ }
6279
+ function useAsyncAction(executor, options = {}) {
6280
+ const [result, setResult] = react.useState(null);
6281
+ const [isLoading, setIsLoading] = react.useState(false);
6282
+ const [error, setError] = react.useState(null);
6283
+ const abortRef = react.useRef(null);
6284
+ const executorRef = react.useRef(executor);
6285
+ const optionsRef = react.useRef(options);
6286
+ const mountedRef = react.useRef(true);
6287
+ react.useEffect(() => {
6288
+ executorRef.current = executor;
6289
+ optionsRef.current = options;
6290
+ });
6291
+ react.useEffect(() => {
6292
+ mountedRef.current = true;
6293
+ return () => {
6294
+ mountedRef.current = false;
6295
+ abortRef.current?.abort();
6296
+ abortRef.current = null;
6297
+ };
6298
+ }, []);
6299
+ const run = react.useCallback(
6300
+ async (input) => {
6301
+ abortRef.current?.abort();
6302
+ const controller = new AbortController();
6303
+ abortRef.current = controller;
6304
+ setIsLoading(true);
6305
+ setError(null);
6306
+ setResult(null);
6307
+ try {
6308
+ const value = await executorRef.current(input, controller.signal);
6309
+ if (!mountedRef.current) return null;
6310
+ setResult(value);
6311
+ optionsRef.current.onSuccess?.(value);
6312
+ return value;
6313
+ } catch (err) {
6314
+ if (isAbortError2(err)) {
6315
+ if (mountedRef.current) {
6316
+ optionsRef.current.onCancelled?.();
6317
+ }
6318
+ return null;
6319
+ }
6320
+ if (!mountedRef.current) return null;
6321
+ const normalized = toError(err);
6322
+ setError(normalized);
6323
+ optionsRef.current.onError?.(normalized);
6324
+ return null;
6325
+ } finally {
6326
+ if (mountedRef.current) {
6327
+ setIsLoading(false);
6328
+ }
6329
+ }
6330
+ },
6331
+ []
6332
+ );
6333
+ const cancel = react.useCallback(() => {
6334
+ abortRef.current?.abort();
6335
+ abortRef.current = null;
6336
+ }, []);
6337
+ const reset = react.useCallback(() => {
6338
+ abortRef.current?.abort();
6339
+ abortRef.current = null;
6340
+ setResult(null);
6341
+ setError(null);
6342
+ setIsLoading(false);
6343
+ }, []);
6344
+ return react.useMemo(
6345
+ () => ({ result, isLoading, error, run, cancel, reset }),
6346
+ [result, isLoading, error, run, cancel, reset]
6347
+ );
6348
+ }
6349
+
6350
+ // src/hooks/usePrunaGeneration.ts
6351
+ function usePrunaGeneration(apiKey, options = {}) {
6352
+ return useAsyncAction(
6353
+ (input, signal) => generate(apiKey, input, { signal, onProgress: options.onProgress }),
6354
+ options
6355
+ );
6356
+ }
6357
+
6358
+ // src/hooks/usePrunaProxy.ts
6359
+ function createProxyExecutor(proxyUrl, executorOptions) {
6360
+ return async (input, signal) => {
6361
+ executorOptions.onProgress?.("uploading");
6362
+ const res = await fetch(proxyUrl, {
6363
+ method: "POST",
6364
+ headers: { "Content-Type": "application/json" },
6365
+ body: JSON.stringify(input),
6366
+ signal
6367
+ });
6368
+ if (!res.ok) {
6369
+ const payload = await res.json().catch(() => ({ message: res.statusText }));
6370
+ throw new Error(payload.message ?? `Proxy error: ${res.status}`);
6371
+ }
6372
+ return res.json();
6373
+ };
6374
+ }
6375
+ function usePrunaProxy(options) {
6376
+ const { proxyUrl, onProgress, onSuccess, onError } = options;
6377
+ return useAsyncAction(
6378
+ createProxyExecutor(proxyUrl, { onProgress }),
6379
+ { onSuccess, onError }
6380
+ );
6381
+ }
5495
6382
 
6383
+ exports.CancellationError = CancellationError;
6384
+ exports.GENERATION_COSTS = CREDIT_COSTS;
6385
+ exports.calculateCreditCost = calculateContentCreditCost;
5496
6386
  exports.useAIContent = useAIContent;
5497
6387
  exports.useAIGeneration = useAIGeneration;
6388
+ exports.useAsyncAction = useAsyncAction;
5498
6389
  exports.useBlogGenerator = useBlogGenerator;
5499
6390
  exports.useCacheStats = useCacheStats;
5500
6391
  exports.useChat = useChat;
5501
6392
  exports.useContentWizard = useContentWizard;
5502
6393
  exports.useGroq = useGroq;
5503
6394
  exports.useImageWizard = useImageWizard;
6395
+ exports.usePrunaGeneration = usePrunaGeneration;
6396
+ exports.usePrunaProxy = usePrunaProxy;
5504
6397
  exports.useSocialContentGenerator = useSocialContentGenerator;
5505
6398
  exports.useText = useText;
5506
6399
  exports.useVideoWizard = useVideoWizard;