modelfusion 0.100.0 → 0.102.0

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 (50) hide show
  1. package/CHANGELOG.md +1432 -0
  2. package/core/api/BaseUrlApiConfiguration.d.ts +7 -6
  3. package/core/api/BaseUrlPartsApiConfiguration.cjs +53 -0
  4. package/core/api/BaseUrlPartsApiConfiguration.d.ts +26 -0
  5. package/core/api/BaseUrlPartsApiConfiguration.js +49 -0
  6. package/core/api/index.cjs +1 -0
  7. package/core/api/index.d.ts +1 -0
  8. package/core/api/index.js +1 -0
  9. package/model-function/generate-image/ImageGenerationModel.d.ts +12 -2
  10. package/model-function/generate-image/PromptTemplateImageGenerationModel.cjs +3 -3
  11. package/model-function/generate-image/PromptTemplateImageGenerationModel.d.ts +2 -2
  12. package/model-function/generate-image/PromptTemplateImageGenerationModel.js +3 -3
  13. package/model-function/generate-image/generateImage.cjs +9 -7
  14. package/model-function/generate-image/generateImage.d.ts +2 -0
  15. package/model-function/generate-image/generateImage.js +9 -7
  16. package/model-provider/automatic1111/Automatic1111ApiConfiguration.cjs +8 -9
  17. package/model-provider/automatic1111/Automatic1111ApiConfiguration.d.ts +7 -9
  18. package/model-provider/automatic1111/Automatic1111ApiConfiguration.js +8 -9
  19. package/model-provider/automatic1111/Automatic1111Error.cjs +7 -31
  20. package/model-provider/automatic1111/Automatic1111Error.d.ts +2 -11
  21. package/model-provider/automatic1111/Automatic1111Error.js +6 -28
  22. package/model-provider/automatic1111/Automatic1111Facade.cjs +10 -1
  23. package/model-provider/automatic1111/Automatic1111Facade.d.ts +7 -0
  24. package/model-provider/automatic1111/Automatic1111Facade.js +8 -0
  25. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +26 -29
  26. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +24 -10
  27. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +26 -29
  28. package/model-provider/automatic1111/Automatic1111ImageGenerationPrompt.d.ts +0 -1
  29. package/model-provider/automatic1111/index.cjs +1 -3
  30. package/model-provider/automatic1111/index.d.ts +1 -1
  31. package/model-provider/automatic1111/index.js +0 -1
  32. package/model-provider/openai/OpenAIImageGenerationModel.cjs +25 -31
  33. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +2 -3
  34. package/model-provider/openai/OpenAIImageGenerationModel.js +25 -31
  35. package/model-provider/stability/StabilityApiConfiguration.cjs +12 -5
  36. package/model-provider/stability/StabilityApiConfiguration.d.ts +7 -8
  37. package/model-provider/stability/StabilityApiConfiguration.js +12 -5
  38. package/model-provider/stability/StabilityError.cjs +7 -31
  39. package/model-provider/stability/StabilityError.d.ts +2 -11
  40. package/model-provider/stability/StabilityError.js +6 -28
  41. package/model-provider/stability/StabilityFacade.cjs +11 -3
  42. package/model-provider/stability/StabilityFacade.d.ts +10 -2
  43. package/model-provider/stability/StabilityFacade.js +9 -2
  44. package/model-provider/stability/StabilityImageGenerationModel.cjs +39 -50
  45. package/model-provider/stability/StabilityImageGenerationModel.d.ts +37 -22
  46. package/model-provider/stability/StabilityImageGenerationModel.js +39 -50
  47. package/model-provider/stability/index.cjs +1 -3
  48. package/model-provider/stability/index.d.ts +1 -1
  49. package/model-provider/stability/index.js +0 -1
  50. package/package.json +15 -15
@@ -6,6 +6,28 @@ import { PromptTemplate } from "../../model-function/PromptTemplate.js";
6
6
  import { ImageGenerationModel, ImageGenerationModelSettings } from "../../model-function/generate-image/ImageGenerationModel.js";
7
7
  import { PromptTemplateImageGenerationModel } from "../../model-function/generate-image/PromptTemplateImageGenerationModel.js";
8
8
  import { Automatic1111ImageGenerationPrompt } from "./Automatic1111ImageGenerationPrompt.js";
9
+ export interface Automatic1111ImageGenerationSettings extends ImageGenerationModelSettings {
10
+ api?: ApiConfiguration;
11
+ /**
12
+ * Stable Diffusion checkpoint.
13
+ */
14
+ model: string;
15
+ height?: number;
16
+ width?: number;
17
+ /**
18
+ * Sampling method.
19
+ */
20
+ sampler?: string;
21
+ /**
22
+ * Sampling steps.
23
+ */
24
+ steps?: number;
25
+ /**
26
+ * CFG Scale.
27
+ */
28
+ cfgScale?: number;
29
+ seed?: number;
30
+ }
9
31
  /**
10
32
  * Create an image generation model that calls the AUTOMATIC1111 Stable Diffusion Web UI API.
11
33
  *
@@ -17,26 +39,18 @@ export declare class Automatic1111ImageGenerationModel extends AbstractModel<Aut
17
39
  get modelName(): string;
18
40
  callAPI(input: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<Automatic1111ImageGenerationResponse>;
19
41
  get settingsForEvent(): Partial<Automatic1111ImageGenerationSettings>;
20
- doGenerateImage(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
42
+ doGenerateImages(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
21
43
  response: {
22
44
  images: string[];
23
45
  parameters: {};
24
46
  info: string;
25
47
  };
26
- base64Image: string;
48
+ base64Images: string[];
27
49
  }>;
28
50
  withTextPrompt(): PromptTemplateImageGenerationModel<string, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;
29
51
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, Automatic1111ImageGenerationPrompt>): PromptTemplateImageGenerationModel<INPUT_PROMPT, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;
30
52
  withSettings(additionalSettings: Automatic1111ImageGenerationSettings): this;
31
53
  }
32
- export interface Automatic1111ImageGenerationSettings extends ImageGenerationModelSettings {
33
- api?: ApiConfiguration;
34
- model: string;
35
- height?: number;
36
- width?: number;
37
- sampler?: string;
38
- steps?: number;
39
- }
40
54
  declare const Automatic1111ImageGenerationResponseSchema: z.ZodObject<{
41
55
  images: z.ZodArray<z.ZodString, "many">;
42
56
  parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
@@ -25,13 +25,31 @@ export class Automatic1111ImageGenerationModel extends AbstractModel {
25
25
  return this.settings.model;
26
26
  }
27
27
  async callAPI(input, options) {
28
+ const api = this.settings.api ?? new Automatic1111ApiConfiguration();
29
+ const abortSignal = options?.run?.abortSignal;
28
30
  return callWithRetryAndThrottle({
29
- retry: this.settings.api?.retry,
30
- throttle: this.settings.api?.throttle,
31
- call: async () => callAutomatic1111ImageGenerationAPI({
32
- ...this.settings,
33
- abortSignal: options?.run?.abortSignal,
34
- prompt: input.prompt,
31
+ retry: api.retry,
32
+ throttle: api.throttle,
33
+ call: async () => postJsonToApi({
34
+ url: api.assembleUrl(`/txt2img`),
35
+ headers: api.headers,
36
+ body: {
37
+ prompt: input.prompt,
38
+ negative_prompt: input.negativePrompt,
39
+ seed: this.settings.seed,
40
+ batch_size: this.settings.numberOfGenerations,
41
+ height: this.settings.height,
42
+ width: this.settings.width,
43
+ cfg_scale: this.settings.cfgScale,
44
+ sampler_index: this.settings.sampler,
45
+ steps: this.settings.steps,
46
+ override_settings: {
47
+ sd_model_checkpoint: this.settings.model,
48
+ },
49
+ },
50
+ failedResponseHandler: failedAutomatic1111CallResponseHandler,
51
+ successfulResponseHandler: createJsonResponseHandler(Automatic1111ImageGenerationResponseSchema),
52
+ abortSignal,
35
53
  }),
36
54
  });
37
55
  }
@@ -43,11 +61,11 @@ export class Automatic1111ImageGenerationModel extends AbstractModel {
43
61
  steps: this.settings.steps,
44
62
  };
45
63
  }
46
- async doGenerateImage(prompt, options) {
64
+ async doGenerateImages(prompt, options) {
47
65
  const response = await this.callAPI(prompt, options);
48
66
  return {
49
67
  response,
50
- base64Image: response.images[0],
68
+ base64Images: response.images,
51
69
  };
52
70
  }
53
71
  withTextPrompt() {
@@ -68,24 +86,3 @@ const Automatic1111ImageGenerationResponseSchema = z.object({
68
86
  parameters: z.object({}),
69
87
  info: z.string(),
70
88
  });
71
- async function callAutomatic1111ImageGenerationAPI({ api = new Automatic1111ApiConfiguration(), abortSignal, height, width, prompt, negativePrompt, sampler, steps, seed, model, }) {
72
- return postJsonToApi({
73
- url: api.assembleUrl(`/txt2img`),
74
- headers: api.headers,
75
- body: {
76
- height,
77
- width,
78
- prompt,
79
- negative_prompt: negativePrompt,
80
- sampler_index: sampler,
81
- steps,
82
- seed,
83
- override_settings: {
84
- sd_model_checkpoint: model,
85
- },
86
- },
87
- failedResponseHandler: failedAutomatic1111CallResponseHandler,
88
- successfulResponseHandler: createJsonResponseHandler(Automatic1111ImageGenerationResponseSchema),
89
- abortSignal,
90
- });
91
- }
@@ -2,7 +2,6 @@ import { PromptTemplate } from "../../model-function/PromptTemplate.js";
2
2
  export type Automatic1111ImageGenerationPrompt = {
3
3
  prompt: string;
4
4
  negativePrompt?: string;
5
- seed?: number;
6
5
  };
7
6
  /**
8
7
  * Formats a basic text prompt as an Automatic1111 prompt.
@@ -26,10 +26,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.automatic1111 = exports.Automatic1111Error = void 0;
29
+ exports.automatic1111 = void 0;
30
30
  __exportStar(require("./Automatic1111ApiConfiguration.cjs"), exports);
31
- var Automatic1111Error_js_1 = require("./Automatic1111Error.cjs");
32
- Object.defineProperty(exports, "Automatic1111Error", { enumerable: true, get: function () { return Automatic1111Error_js_1.Automatic1111Error; } });
33
31
  exports.automatic1111 = __importStar(require("./Automatic1111Facade.cjs"));
34
32
  __exportStar(require("./Automatic1111ImageGenerationModel.cjs"), exports);
35
33
  __exportStar(require("./Automatic1111ImageGenerationPrompt.cjs"), exports);
@@ -1,5 +1,5 @@
1
1
  export * from "./Automatic1111ApiConfiguration.js";
2
- export { Automatic1111Error, Automatic1111ErrorData, } from "./Automatic1111Error.js";
2
+ export { Automatic1111ErrorData } from "./Automatic1111Error.js";
3
3
  export * as automatic1111 from "./Automatic1111Facade.js";
4
4
  export * from "./Automatic1111ImageGenerationModel.js";
5
5
  export * from "./Automatic1111ImageGenerationPrompt.js";
@@ -1,5 +1,4 @@
1
1
  export * from "./Automatic1111ApiConfiguration.js";
2
- export { Automatic1111Error, } from "./Automatic1111Error.js";
3
2
  export * as automatic1111 from "./Automatic1111Facade.js";
4
3
  export * from "./Automatic1111ImageGenerationModel.js";
5
4
  export * from "./Automatic1111ImageGenerationPrompt.js";
@@ -60,7 +60,7 @@ const calculateOpenAIImageGenerationCostInMillicents = ({ model, settings, }) =>
60
60
  if (cost == null) {
61
61
  return null;
62
62
  }
63
- return (settings.n ?? 1) * cost;
63
+ return (settings.numberOfGenerations ?? 1) * cost;
64
64
  };
65
65
  exports.calculateOpenAIImageGenerationCostInMillicents = calculateOpenAIImageGenerationCostInMillicents;
66
66
  /**
@@ -88,38 +88,48 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
88
88
  return this.settings.model;
89
89
  }
90
90
  async callAPI(prompt, options) {
91
- const run = options?.run;
91
+ const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
92
+ const abortSignal = options?.run?.abortSignal;
93
+ const userId = options?.run?.userId;
92
94
  const responseFormat = options?.responseFormat;
93
- const callSettings = {
94
- ...this.settings,
95
- user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
96
- abortSignal: run?.abortSignal,
97
- responseFormat,
98
- prompt,
99
- };
100
95
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
101
- retry: callSettings.api?.retry,
102
- throttle: callSettings.api?.throttle,
103
- call: async () => callOpenAIImageGenerationAPI(callSettings),
96
+ retry: api.retry,
97
+ throttle: api.throttle,
98
+ call: async () => {
99
+ return (0, postToApi_js_1.postJsonToApi)({
100
+ url: api.assembleUrl("/images/generations"),
101
+ headers: api.headers,
102
+ body: {
103
+ prompt,
104
+ n: this.settings.numberOfGenerations,
105
+ size: this.settings.size,
106
+ response_format: responseFormat.type,
107
+ user: this.settings.isUserIdForwardingEnabled ? userId : undefined,
108
+ },
109
+ failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
110
+ successfulResponseHandler: responseFormat?.handler,
111
+ abortSignal,
112
+ });
113
+ },
104
114
  });
105
115
  }
106
116
  get settingsForEvent() {
107
117
  const eventSettingProperties = [
108
- "n",
118
+ "numberOfGenerations",
109
119
  "size",
110
120
  "quality",
111
121
  "style",
112
122
  ];
113
123
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
114
124
  }
115
- async doGenerateImage(prompt, options) {
125
+ async doGenerateImages(prompt, options) {
116
126
  const response = await this.callAPI(prompt, {
117
127
  responseFormat: exports.OpenAIImageGenerationResponseFormat.base64Json,
118
128
  ...options,
119
129
  });
120
130
  return {
121
131
  response,
122
- base64Image: response.data[0].b64_json,
132
+ base64Images: response.data.map((item) => item.b64_json),
123
133
  };
124
134
  }
125
135
  withPromptTemplate(promptTemplate) {
@@ -155,19 +165,3 @@ exports.OpenAIImageGenerationResponseFormat = {
155
165
  handler: (0, postToApi_js_1.createJsonResponseHandler)(openAIImageGenerationBase64JsonSchema),
156
166
  },
157
167
  };
158
- async function callOpenAIImageGenerationAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, prompt, n, size, responseFormat, user, }) {
159
- return (0, postToApi_js_1.postJsonToApi)({
160
- url: api.assembleUrl("/images/generations"),
161
- headers: api.headers,
162
- body: {
163
- prompt,
164
- n,
165
- size,
166
- response_format: responseFormat.type,
167
- user,
168
- },
169
- failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
170
- successfulResponseHandler: responseFormat?.handler,
171
- abortSignal,
172
- });
173
- }
@@ -24,7 +24,6 @@ export declare const calculateOpenAIImageGenerationCostInMillicents: ({ model, s
24
24
  export type OpenAIImageModelType = keyof typeof OPENAI_IMAGE_MODELS;
25
25
  export interface OpenAIImageGenerationCallSettings {
26
26
  model: OpenAIImageModelType;
27
- n?: number;
28
27
  size?: "256x256" | "512x512" | "1024x1024" | "1792x1024" | "1024x1792";
29
28
  quality?: "standard" | "hd";
30
29
  style?: "vivid" | "natural";
@@ -52,14 +51,14 @@ export declare class OpenAIImageGenerationModel extends AbstractModel<OpenAIImag
52
51
  responseFormat: OpenAIImageGenerationResponseFormatType<RESULT>;
53
52
  } & FunctionOptions): Promise<RESULT>;
54
53
  get settingsForEvent(): Partial<OpenAIImageGenerationSettings>;
55
- doGenerateImage(prompt: string, options?: FunctionOptions): Promise<{
54
+ doGenerateImages(prompt: string, options?: FunctionOptions): Promise<{
56
55
  response: {
57
56
  data: {
58
57
  b64_json: string;
59
58
  }[];
60
59
  created: number;
61
60
  };
62
- base64Image: string;
61
+ base64Images: string[];
63
62
  }>;
64
63
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, string>): PromptTemplateImageGenerationModel<INPUT_PROMPT, string, OpenAIImageGenerationSettings, this>;
65
64
  withSettings(additionalSettings: Partial<OpenAIImageGenerationSettings>): this;
@@ -57,7 +57,7 @@ export const calculateOpenAIImageGenerationCostInMillicents = ({ model, settings
57
57
  if (cost == null) {
58
58
  return null;
59
59
  }
60
- return (settings.n ?? 1) * cost;
60
+ return (settings.numberOfGenerations ?? 1) * cost;
61
61
  };
62
62
  /**
63
63
  * Create an image generation model that calls the OpenAI AI image creation API.
@@ -84,38 +84,48 @@ export class OpenAIImageGenerationModel extends AbstractModel {
84
84
  return this.settings.model;
85
85
  }
86
86
  async callAPI(prompt, options) {
87
- const run = options?.run;
87
+ const api = this.settings.api ?? new OpenAIApiConfiguration();
88
+ const abortSignal = options?.run?.abortSignal;
89
+ const userId = options?.run?.userId;
88
90
  const responseFormat = options?.responseFormat;
89
- const callSettings = {
90
- ...this.settings,
91
- user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
92
- abortSignal: run?.abortSignal,
93
- responseFormat,
94
- prompt,
95
- };
96
91
  return callWithRetryAndThrottle({
97
- retry: callSettings.api?.retry,
98
- throttle: callSettings.api?.throttle,
99
- call: async () => callOpenAIImageGenerationAPI(callSettings),
92
+ retry: api.retry,
93
+ throttle: api.throttle,
94
+ call: async () => {
95
+ return postJsonToApi({
96
+ url: api.assembleUrl("/images/generations"),
97
+ headers: api.headers,
98
+ body: {
99
+ prompt,
100
+ n: this.settings.numberOfGenerations,
101
+ size: this.settings.size,
102
+ response_format: responseFormat.type,
103
+ user: this.settings.isUserIdForwardingEnabled ? userId : undefined,
104
+ },
105
+ failedResponseHandler: failedOpenAICallResponseHandler,
106
+ successfulResponseHandler: responseFormat?.handler,
107
+ abortSignal,
108
+ });
109
+ },
100
110
  });
101
111
  }
102
112
  get settingsForEvent() {
103
113
  const eventSettingProperties = [
104
- "n",
114
+ "numberOfGenerations",
105
115
  "size",
106
116
  "quality",
107
117
  "style",
108
118
  ];
109
119
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
110
120
  }
111
- async doGenerateImage(prompt, options) {
121
+ async doGenerateImages(prompt, options) {
112
122
  const response = await this.callAPI(prompt, {
113
123
  responseFormat: OpenAIImageGenerationResponseFormat.base64Json,
114
124
  ...options,
115
125
  });
116
126
  return {
117
127
  response,
118
- base64Image: response.data[0].b64_json,
128
+ base64Images: response.data.map((item) => item.b64_json),
119
129
  };
120
130
  }
121
131
  withPromptTemplate(promptTemplate) {
@@ -150,19 +160,3 @@ export const OpenAIImageGenerationResponseFormat = {
150
160
  handler: createJsonResponseHandler(openAIImageGenerationBase64JsonSchema),
151
161
  },
152
162
  };
153
- async function callOpenAIImageGenerationAPI({ api = new OpenAIApiConfiguration(), abortSignal, prompt, n, size, responseFormat, user, }) {
154
- return postJsonToApi({
155
- url: api.assembleUrl("/images/generations"),
156
- headers: api.headers,
157
- body: {
158
- prompt,
159
- n,
160
- size,
161
- response_format: responseFormat.type,
162
- user,
163
- },
164
- failedResponseHandler: failedOpenAICallResponseHandler,
165
- successfulResponseHandler: responseFormat?.handler,
166
- abortSignal,
167
- });
168
- }
@@ -1,13 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StabilityApiConfiguration = void 0;
4
- const BaseUrlApiConfiguration_js_1 = require("../../core/api/BaseUrlApiConfiguration.cjs");
4
+ const BaseUrlPartsApiConfiguration_js_1 = require("../../core/api/BaseUrlPartsApiConfiguration.cjs");
5
5
  const loadApiKey_js_1 = require("../../core/api/loadApiKey.cjs");
6
- class StabilityApiConfiguration extends BaseUrlApiConfiguration_js_1.BaseUrlApiConfiguration {
7
- constructor({ baseUrl = "https://api.stability.ai/v1", apiKey, retry, throttle, } = {}) {
6
+ /**
7
+ * Creates an API configuration for the Stability AI API.
8
+ * It calls the API at https://api.stability.ai/v1 by default.
9
+ */
10
+ class StabilityApiConfiguration extends BaseUrlPartsApiConfiguration_js_1.BaseUrlPartsApiConfiguration {
11
+ constructor({ protocol = "https", host = "api.stability.ai", port = "443", path = "/v1", apiKey, headers, retry, throttle, } = {}) {
8
12
  super({
9
- baseUrl,
10
- headers: {
13
+ protocol,
14
+ host,
15
+ port,
16
+ path,
17
+ headers: headers ?? {
11
18
  Authorization: `Bearer ${(0, loadApiKey_js_1.loadApiKey)({
12
19
  apiKey,
13
20
  environmentVariableName: "STABILITY_API_KEY",
@@ -1,11 +1,10 @@
1
- import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
2
- import { RetryFunction } from "../../core/api/RetryFunction.js";
3
- import { ThrottleFunction } from "../../core/api/ThrottleFunction.js";
4
- export declare class StabilityApiConfiguration extends BaseUrlApiConfiguration {
5
- constructor({ baseUrl, apiKey, retry, throttle, }?: {
6
- baseUrl?: string;
1
+ import { BaseUrlPartsApiConfiguration, BaseUrlPartsApiConfigurationOptions } from "../../core/api/BaseUrlPartsApiConfiguration.js";
2
+ /**
3
+ * Creates an API configuration for the Stability AI API.
4
+ * It calls the API at https://api.stability.ai/v1 by default.
5
+ */
6
+ export declare class StabilityApiConfiguration extends BaseUrlPartsApiConfiguration {
7
+ constructor({ protocol, host, port, path, apiKey, headers, retry, throttle, }?: Partial<BaseUrlPartsApiConfigurationOptions> & {
7
8
  apiKey?: string;
8
- retry?: RetryFunction;
9
- throttle?: ThrottleFunction;
10
9
  });
11
10
  }
@@ -1,10 +1,17 @@
1
- import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
1
+ import { BaseUrlPartsApiConfiguration, } from "../../core/api/BaseUrlPartsApiConfiguration.js";
2
2
  import { loadApiKey } from "../../core/api/loadApiKey.js";
3
- export class StabilityApiConfiguration extends BaseUrlApiConfiguration {
4
- constructor({ baseUrl = "https://api.stability.ai/v1", apiKey, retry, throttle, } = {}) {
3
+ /**
4
+ * Creates an API configuration for the Stability AI API.
5
+ * It calls the API at https://api.stability.ai/v1 by default.
6
+ */
7
+ export class StabilityApiConfiguration extends BaseUrlPartsApiConfiguration {
8
+ constructor({ protocol = "https", host = "api.stability.ai", port = "443", path = "/v1", apiKey, headers, retry, throttle, } = {}) {
5
9
  super({
6
- baseUrl,
7
- headers: {
10
+ protocol,
11
+ host,
12
+ port,
13
+ path,
14
+ headers: headers ?? {
8
15
  Authorization: `Bearer ${loadApiKey({
9
16
  apiKey,
10
17
  environmentVariableName: "STABILITY_API_KEY",
@@ -1,37 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.failedStabilityCallResponseHandler = exports.StabilityError = exports.stabilityErrorDataSchema = void 0;
3
+ exports.failedStabilityCallResponseHandler = void 0;
4
4
  const zod_1 = require("zod");
5
- const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
5
+ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
6
6
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
7
- const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
8
- exports.stabilityErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
7
+ const stabilityErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
9
8
  message: zod_1.z.string(),
10
9
  }));
11
- class StabilityError extends ApiCallError_js_1.ApiCallError {
12
- constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
13
- super({ message, statusCode, requestBodyValues, url });
14
- Object.defineProperty(this, "data", {
15
- enumerable: true,
16
- configurable: true,
17
- writable: true,
18
- value: void 0
19
- });
20
- this.data = data;
21
- }
22
- }
23
- exports.StabilityError = StabilityError;
24
- const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
25
- const responseBody = await response.text();
26
- const parsedError = (0, parseJSON_js_1.parseJSON)({
27
- text: responseBody,
28
- schema: exports.stabilityErrorDataSchema,
29
- });
30
- return new StabilityError({
31
- url,
32
- requestBodyValues,
33
- statusCode: response.status,
34
- data: parsedError,
35
- });
36
- };
37
- exports.failedStabilityCallResponseHandler = failedStabilityCallResponseHandler;
10
+ exports.failedStabilityCallResponseHandler = (0, postToApi_js_1.createJsonErrorResponseHandler)({
11
+ errorSchema: stabilityErrorDataSchema,
12
+ errorToMessage: (error) => error.message,
13
+ });
@@ -1,18 +1,9 @@
1
1
  import { ApiCallError } from "../../core/api/ApiCallError.js";
2
2
  import { ResponseHandler } from "../../core/api/postToApi.js";
3
3
  import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
- export declare const stabilityErrorDataSchema: ZodSchema<{
4
+ declare const stabilityErrorDataSchema: ZodSchema<{
5
5
  message: string;
6
6
  }>;
7
7
  export type StabilityErrorData = (typeof stabilityErrorDataSchema)["_type"];
8
- export declare class StabilityError extends ApiCallError {
9
- readonly data: StabilityErrorData;
10
- constructor({ data, statusCode, url, requestBodyValues, message, }: {
11
- message?: string;
12
- statusCode: number;
13
- url: string;
14
- requestBodyValues: unknown;
15
- data: StabilityErrorData;
16
- });
17
- }
18
8
  export declare const failedStabilityCallResponseHandler: ResponseHandler<ApiCallError>;
9
+ export {};
@@ -1,32 +1,10 @@
1
1
  import { z } from "zod";
2
- import { ApiCallError } from "../../core/api/ApiCallError.js";
2
+ import { createJsonErrorResponseHandler, } from "../../core/api/postToApi.js";
3
3
  import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
- import { parseJSON } from "../../core/schema/parseJSON.js";
5
- export const stabilityErrorDataSchema = new ZodSchema(z.object({
4
+ const stabilityErrorDataSchema = new ZodSchema(z.object({
6
5
  message: z.string(),
7
6
  }));
8
- export class StabilityError extends ApiCallError {
9
- constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
10
- super({ message, statusCode, requestBodyValues, url });
11
- Object.defineProperty(this, "data", {
12
- enumerable: true,
13
- configurable: true,
14
- writable: true,
15
- value: void 0
16
- });
17
- this.data = data;
18
- }
19
- }
20
- export const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
21
- const responseBody = await response.text();
22
- const parsedError = parseJSON({
23
- text: responseBody,
24
- schema: stabilityErrorDataSchema,
25
- });
26
- return new StabilityError({
27
- url,
28
- requestBodyValues,
29
- statusCode: response.status,
30
- data: parsedError,
31
- });
32
- };
7
+ export const failedStabilityCallResponseHandler = createJsonErrorResponseHandler({
8
+ errorSchema: stabilityErrorDataSchema,
9
+ errorToMessage: (error) => error.message,
10
+ });
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImageGenerator = void 0;
3
+ exports.Api = exports.ImageGenerator = void 0;
4
+ const StabilityApiConfiguration_js_1 = require("./StabilityApiConfiguration.cjs");
4
5
  const StabilityImageGenerationModel_js_1 = require("./StabilityImageGenerationModel.cjs");
5
6
  /**
6
7
  * Create an image generation model that calls the Stability AI image generation API.
@@ -10,12 +11,11 @@ const StabilityImageGenerationModel_js_1 = require("./StabilityImageGenerationMo
10
11
  * @example
11
12
  * const image = await generateImage(
12
13
  * stability.ImageGenerator({
13
- * model: "stable-diffusion-512-v2-1",
14
+ * model: "stable-diffusion-v1-6",
14
15
  * cfgScale: 7,
15
16
  * clipGuidancePreset: "FAST_BLUE",
16
17
  * height: 512,
17
18
  * width: 512,
18
- * samples: 1,
19
19
  * steps: 30,
20
20
  * })
21
21
  * [
@@ -30,3 +30,11 @@ function ImageGenerator(settings) {
30
30
  return new StabilityImageGenerationModel_js_1.StabilityImageGenerationModel(settings);
31
31
  }
32
32
  exports.ImageGenerator = ImageGenerator;
33
+ /**
34
+ * Creates an API configuration for the Stability AI API.
35
+ * It calls the API at https://api.stability.ai/v1 by default.
36
+ */
37
+ function Api(settings) {
38
+ return new StabilityApiConfiguration_js_1.StabilityApiConfiguration(settings);
39
+ }
40
+ exports.Api = Api;
@@ -1,3 +1,5 @@
1
+ import { BaseUrlPartsApiConfigurationOptions } from "../../core/api/BaseUrlPartsApiConfiguration.js";
2
+ import { StabilityApiConfiguration } from "./StabilityApiConfiguration.js";
1
3
  import { StabilityImageGenerationModel, StabilityImageGenerationSettings } from "./StabilityImageGenerationModel.js";
2
4
  /**
3
5
  * Create an image generation model that calls the Stability AI image generation API.
@@ -7,12 +9,11 @@ import { StabilityImageGenerationModel, StabilityImageGenerationSettings } from
7
9
  * @example
8
10
  * const image = await generateImage(
9
11
  * stability.ImageGenerator({
10
- * model: "stable-diffusion-512-v2-1",
12
+ * model: "stable-diffusion-v1-6",
11
13
  * cfgScale: 7,
12
14
  * clipGuidancePreset: "FAST_BLUE",
13
15
  * height: 512,
14
16
  * width: 512,
15
- * samples: 1,
16
17
  * steps: 30,
17
18
  * })
18
19
  * [
@@ -24,3 +25,10 @@ import { StabilityImageGenerationModel, StabilityImageGenerationSettings } from
24
25
  * @returns A new instance of {@link StabilityImageGenerationModel}.
25
26
  */
26
27
  export declare function ImageGenerator(settings: StabilityImageGenerationSettings): StabilityImageGenerationModel;
28
+ /**
29
+ * Creates an API configuration for the Stability AI API.
30
+ * It calls the API at https://api.stability.ai/v1 by default.
31
+ */
32
+ export declare function Api(settings: Partial<BaseUrlPartsApiConfigurationOptions> & {
33
+ apiKey?: string;
34
+ }): StabilityApiConfiguration;
@@ -1,3 +1,4 @@
1
+ import { StabilityApiConfiguration } from "./StabilityApiConfiguration.js";
1
2
  import { StabilityImageGenerationModel, } from "./StabilityImageGenerationModel.js";
2
3
  /**
3
4
  * Create an image generation model that calls the Stability AI image generation API.
@@ -7,12 +8,11 @@ import { StabilityImageGenerationModel, } from "./StabilityImageGenerationModel.
7
8
  * @example
8
9
  * const image = await generateImage(
9
10
  * stability.ImageGenerator({
10
- * model: "stable-diffusion-512-v2-1",
11
+ * model: "stable-diffusion-v1-6",
11
12
  * cfgScale: 7,
12
13
  * clipGuidancePreset: "FAST_BLUE",
13
14
  * height: 512,
14
15
  * width: 512,
15
- * samples: 1,
16
16
  * steps: 30,
17
17
  * })
18
18
  * [
@@ -26,3 +26,10 @@ import { StabilityImageGenerationModel, } from "./StabilityImageGenerationModel.
26
26
  export function ImageGenerator(settings) {
27
27
  return new StabilityImageGenerationModel(settings);
28
28
  }
29
+ /**
30
+ * Creates an API configuration for the Stability AI API.
31
+ * It calls the API at https://api.stability.ai/v1 by default.
32
+ */
33
+ export function Api(settings) {
34
+ return new StabilityApiConfiguration(settings);
35
+ }