@midsummer-ai/sdk 0.1.0-staging.4174 → 0.1.0-staging.4177
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/ai/contract.mjs +11 -1
- package/ai/index.d.mts +36 -9
- package/ai/index.mjs +1 -1
- package/ai/stand-in.mjs +1 -1
- package/{ai-BHK1-jsB.mjs → ai-2T_Qya-j.mjs} +38 -18
- package/here-C2jhTheQ.mjs +1 -1
- package/package.json +1 -1
package/ai/contract.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as AiError } from "../ai-
|
|
1
|
+
import { t as AiError } from "../ai-2T_Qya-j.mjs";
|
|
2
2
|
//#region src/ai/contract.ts
|
|
3
3
|
/**
|
|
4
4
|
* AI's contract: what every engine promises, as cases any test runner can run. The Stand-in runs
|
|
@@ -99,7 +99,17 @@ const aiContract = [
|
|
|
99
99
|
check(starts !== void 0 && starts.every((byte, index) => image.bytes[index] === byte), "the bytes are an image of the type it says");
|
|
100
100
|
check(Number.isInteger(image.width) && image.width > 0 && Number.isInteger(image.height) && image.height > 0, "its size is whole pixels");
|
|
101
101
|
check(image.model.length > 0 && image.credits >= 0 && image.catalogue.length > 0, "it names its Model and what it cost");
|
|
102
|
+
check(image.pictures.length === 1 && image.pictures[0].width === image.width, "it is the one picture made");
|
|
103
|
+
const two = await ai.image({
|
|
104
|
+
prompt: "A clay bowl on a wooden table, morning light",
|
|
105
|
+
count: 2
|
|
106
|
+
});
|
|
107
|
+
check(two.pictures.length === 2 && two.pictures.every((picture) => picture.bytes.length > 0), "two asked are two pictures");
|
|
102
108
|
await refused(() => ai.image({ prompt: " " }), "invalid");
|
|
109
|
+
await refused(() => ai.image({
|
|
110
|
+
prompt: "A bowl",
|
|
111
|
+
count: 9
|
|
112
|
+
}), "invalid");
|
|
103
113
|
}
|
|
104
114
|
},
|
|
105
115
|
{
|
package/ai/index.d.mts
CHANGED
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
* for await (const text of stream) write(text)
|
|
12
12
|
* const { credits } = await stream.done
|
|
13
13
|
*
|
|
14
|
-
* `image` generates
|
|
14
|
+
* `image` generates a picture from a prompt, for a page that needs one, as bytes to save with the project, with the
|
|
15
|
+
* image Model, shape or size and the other options its Model takes (`ImageAsk`):
|
|
15
16
|
*
|
|
16
|
-
* const { bytes, type, width, height } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light" })
|
|
17
|
+
* const { bytes, type, width, height, seed } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light", model: "flux-2-dev", shape: "landscape" })
|
|
17
18
|
*
|
|
18
19
|
* A call can pass tools (its calls come back as `toolCalls`), ask for JSON to a schema
|
|
19
20
|
* (`responseFormat`), send images in a user message, set a reasoning effort, and name `fallback`
|
|
@@ -117,19 +118,45 @@ type AiEvent = {
|
|
|
117
118
|
} | {
|
|
118
119
|
readonly done: Spent;
|
|
119
120
|
};
|
|
120
|
-
/**
|
|
121
|
+
/**
|
|
122
|
+
* What an image asks: what it shows (1 to 2,048 characters), and the rest only where its Model takes it, as
|
|
123
|
+
* `midsummer ai model list` shows each one's: the image Model (the default image Model unless given), its `shape` or an
|
|
124
|
+
* exact `width` and `height`, how many pictures (`count`, 1 to 4), a `seed` to make the same picture again, what to
|
|
125
|
+
* leave out (`negative`), its file type (`format`), and `steps` or `quality`. A Model that doesn't take one refuses.
|
|
126
|
+
*
|
|
127
|
+
* await ai(aiClient(origin, { key })).image({ prompt: "A quiet harbour at dawn, film photo", model: "flux-2-dev", shape: "wide", seed: 42 })
|
|
128
|
+
*/
|
|
121
129
|
interface ImageAsk {
|
|
122
130
|
readonly prompt: string;
|
|
123
|
-
/** `ai model list` shows the sizes each image model makes; its first unless given. */
|
|
124
|
-
readonly size?: "square" | "landscape" | "portrait";
|
|
125
131
|
readonly model?: string;
|
|
132
|
+
readonly shape?: "square" | "landscape" | "portrait" | "wide";
|
|
133
|
+
/** The same as `shape`, kept for older callers. */
|
|
134
|
+
readonly size?: "square" | "landscape" | "portrait";
|
|
135
|
+
readonly width?: number;
|
|
136
|
+
readonly height?: number;
|
|
137
|
+
readonly count?: number;
|
|
138
|
+
readonly seed?: number;
|
|
139
|
+
readonly negative?: string;
|
|
140
|
+
readonly format?: "png" | "jpeg" | "webp";
|
|
141
|
+
readonly steps?: number;
|
|
142
|
+
readonly quality?: "low" | "medium" | "high";
|
|
126
143
|
}
|
|
127
|
-
/**
|
|
128
|
-
interface
|
|
144
|
+
/** One picture: its bytes, type and real size in pixels, and the seed that makes it again where its Model takes one. */
|
|
145
|
+
interface Picture {
|
|
129
146
|
readonly bytes: Uint8Array;
|
|
130
147
|
readonly type: "image/png" | "image/jpeg" | "image/webp";
|
|
131
148
|
readonly width: number;
|
|
132
149
|
readonly height: number;
|
|
150
|
+
readonly seed?: number;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A generated image: its first picture, every picture it made (`pictures`, the first included), the steps or quality it
|
|
154
|
+
* was made at, the Model that made it, and what they all cost in credits under `catalogue`.
|
|
155
|
+
*/
|
|
156
|
+
interface Image extends Picture {
|
|
157
|
+
readonly pictures: ReadonlyArray<Picture>;
|
|
158
|
+
readonly steps?: number;
|
|
159
|
+
readonly quality?: "low" | "medium" | "high";
|
|
133
160
|
readonly model: string;
|
|
134
161
|
readonly credits: number;
|
|
135
162
|
readonly catalogue: string;
|
|
@@ -153,7 +180,7 @@ interface AnswerStream extends AsyncIterable<string> {
|
|
|
153
180
|
interface Ai {
|
|
154
181
|
complete(input: Ask): Promise<Answer>;
|
|
155
182
|
stream(input: Ask): AnswerStream;
|
|
156
|
-
/**
|
|
183
|
+
/** A generated image, one picture or several (`count`); save their bytes with the project and serve them from there. */
|
|
157
184
|
image(input: ImageAsk): Promise<Image>;
|
|
158
185
|
}
|
|
159
186
|
declare function ai(given?: AiEngine): Ai;
|
|
@@ -176,4 +203,4 @@ declare function boundAi(handle: {
|
|
|
176
203
|
ask(input: Ask): Promise<ReadableStream<Uint8Array>>;
|
|
177
204
|
}): AiEngine;
|
|
178
205
|
//#endregion
|
|
179
|
-
export { Ai, AiEngine, AiError, AiErrorCode, AiEvent, Answer, AnswerStream, Ask, Image, ImageAsk, Message, Part, Spent, Tool, ToolCall, ToolCallPiece, ai, aiClient, boundAi };
|
|
206
|
+
export { Ai, AiEngine, AiError, AiErrorCode, AiEvent, Answer, AnswerStream, Ask, Image, ImageAsk, Message, Part, Picture, Spent, Tool, ToolCall, ToolCallPiece, ai, aiClient, boundAi };
|
package/ai/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as boundAi, n as ai, r as aiClient, t as AiError } from "../ai-
|
|
1
|
+
import { i as boundAi, n as ai, r as aiClient, t as AiError } from "../ai-2T_Qya-j.mjs";
|
|
2
2
|
export { AiError, ai, aiClient, boundAi };
|
package/ai/stand-in.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as aiStandIn } from "../ai-
|
|
1
|
+
import { a as aiStandIn } from "../ai-2T_Qya-j.mjs";
|
|
2
2
|
export { aiStandIn };
|
|
@@ -19,8 +19,9 @@ const textOf = (content) => typeof content === "string" ? content : content.map(
|
|
|
19
19
|
/** The stand-in's image sizes, in pixels. */
|
|
20
20
|
const SIZES = {
|
|
21
21
|
square: [1024, 1024],
|
|
22
|
-
landscape: [
|
|
23
|
-
portrait: [
|
|
22
|
+
landscape: [1536, 1024],
|
|
23
|
+
portrait: [1024, 1536],
|
|
24
|
+
wide: [1792, 1008]
|
|
24
25
|
};
|
|
25
26
|
/** CRC-32 as PNG's chunks carry it (ISO 3309). */
|
|
26
27
|
function crc32(bytes) {
|
|
@@ -92,12 +93,21 @@ function aiStandIn(options = {}) {
|
|
|
92
93
|
const model = input.model ?? models[0];
|
|
93
94
|
if (!models.includes(model)) throw new AiError("invalid", `There is no model ${model}.`);
|
|
94
95
|
if (!input.prompt.trim()) throw new AiError("invalid", "Describe the image in 1 to 2,048 characters.");
|
|
95
|
-
const
|
|
96
|
-
|
|
96
|
+
const count = input.count ?? 1;
|
|
97
|
+
if (!Number.isInteger(count) || count < 1 || count > 4) throw new AiError("invalid", "Ask for 1 to 4 pictures a call.");
|
|
98
|
+
if (input.width === void 0 !== (input.height === void 0)) throw new AiError("invalid", "Give both a width and a height, in pixels.");
|
|
99
|
+
const [width, height] = input.width !== void 0 && input.height !== void 0 ? [input.width, input.height] : SIZES[input.shape ?? input.size ?? "square"];
|
|
100
|
+
const seed = input.seed ?? 0;
|
|
101
|
+
const pictures = await Promise.all(Array.from({ length: count }, async (_, at) => ({
|
|
97
102
|
bytes: await placeholder(width, height),
|
|
98
103
|
type: "image/png",
|
|
99
104
|
width,
|
|
100
105
|
height,
|
|
106
|
+
seed: seed + at
|
|
107
|
+
})));
|
|
108
|
+
return {
|
|
109
|
+
...pictures[0],
|
|
110
|
+
pictures,
|
|
101
111
|
model,
|
|
102
112
|
credits: 0,
|
|
103
113
|
catalogue: "stand-in"
|
|
@@ -160,9 +170,10 @@ function aiStandIn(options = {}) {
|
|
|
160
170
|
* for await (const text of stream) write(text)
|
|
161
171
|
* const { credits } = await stream.done
|
|
162
172
|
*
|
|
163
|
-
* `image` generates
|
|
173
|
+
* `image` generates a picture from a prompt, for a page that needs one, as bytes to save with the project, with the
|
|
174
|
+
* image Model, shape or size and the other options its Model takes (`ImageAsk`):
|
|
164
175
|
*
|
|
165
|
-
* const { bytes, type, width, height } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light" })
|
|
176
|
+
* const { bytes, type, width, height, seed } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light", model: "flux-2-dev", shape: "landscape" })
|
|
166
177
|
*
|
|
167
178
|
* A call can pass tools (its calls come back as `toolCalls`), ask for JSON to a schema
|
|
168
179
|
* (`responseFormat`), send images in a user message, set a reasoning effort, and name `fallback`
|
|
@@ -377,19 +388,28 @@ function aiClient(origin, options) {
|
|
|
377
388
|
if (!made.ok) throw await failed(made);
|
|
378
389
|
const answer = await made.json().catch(() => void 0);
|
|
379
390
|
if (!answer || typeof answer.url !== "string" || !answer.type || typeof answer.width !== "number" || typeof answer.height !== "number" || typeof answer.model !== "string" || typeof answer.credits !== "number" || typeof answer.catalogue !== "string") throw new AiError("unavailable", "The answer could not be read. Try again.");
|
|
391
|
+
const bytesOf = (url) => url.startsWith("data:") ? Promise.resolve(Uint8Array.from(atob(url.slice(url.indexOf(",") + 1)), (char) => char.charCodeAt(0))) : send(url, {
|
|
392
|
+
method: "GET",
|
|
393
|
+
headers: clientHeaders("ai")
|
|
394
|
+
}).then(async (response) => {
|
|
395
|
+
if (!response.ok) throw await failed(response);
|
|
396
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
397
|
+
}, () => {
|
|
398
|
+
throw unreachable();
|
|
399
|
+
});
|
|
400
|
+
const shown = answer.images?.length ? answer.images : [answer];
|
|
401
|
+
const pictures = await Promise.all(shown.map(async ({ url, type, width, height, seed }) => ({
|
|
402
|
+
bytes: await bytesOf(url),
|
|
403
|
+
type,
|
|
404
|
+
width,
|
|
405
|
+
height,
|
|
406
|
+
...seed === void 0 ? {} : { seed }
|
|
407
|
+
})));
|
|
380
408
|
return {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (!response.ok) throw await failed(response);
|
|
386
|
-
return new Uint8Array(await response.arrayBuffer());
|
|
387
|
-
}, () => {
|
|
388
|
-
throw unreachable();
|
|
389
|
-
}),
|
|
390
|
-
type: answer.type,
|
|
391
|
-
width: answer.width,
|
|
392
|
-
height: answer.height,
|
|
409
|
+
...pictures[0],
|
|
410
|
+
pictures,
|
|
411
|
+
...answer.steps === void 0 ? {} : { steps: answer.steps },
|
|
412
|
+
...answer.quality === void 0 ? {} : { quality: answer.quality },
|
|
393
413
|
model: answer.model,
|
|
394
414
|
credits: answer.credits,
|
|
395
415
|
catalogue: answer.catalogue
|
package/here-C2jhTheQ.mjs
CHANGED
|
@@ -12,7 +12,7 @@ function processEnv() {
|
|
|
12
12
|
* This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
|
|
13
13
|
* release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
|
|
14
14
|
*/
|
|
15
|
-
const SDK_VERSION = "0.1.0-staging.
|
|
15
|
+
const SDK_VERSION = "0.1.0-staging.4177";
|
|
16
16
|
/**
|
|
17
17
|
* The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
|
|
18
18
|
* <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-staging.
|
|
3
|
+
"version": "0.1.0-staging.4177",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
|