@midsummer-ai/sdk 0.1.0-staging.4159 → 0.1.0-staging.4165
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 +26 -1
- package/ai/index.d.mts +29 -3
- package/ai/index.mjs +1 -1
- package/ai/stand-in.mjs +1 -1
- package/{ai-BkbpMs2m.mjs → ai-BHK1-jsB.mjs} +204 -57
- 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-BHK1-jsB.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
|
|
@@ -77,6 +77,31 @@ const aiContract = [
|
|
|
77
77
|
})).text.length > 0, "the Model answers once it has the tool's result");
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
|
+
{
|
|
81
|
+
name: "an image comes back as the bytes of a PNG, JPEG or WebP of the size it says, with the Model that made it and what it cost",
|
|
82
|
+
async run(ai) {
|
|
83
|
+
const image = await ai.image({ prompt: "A clay bowl on a wooden table, morning light" });
|
|
84
|
+
const starts = {
|
|
85
|
+
"image/png": [
|
|
86
|
+
137,
|
|
87
|
+
80,
|
|
88
|
+
78,
|
|
89
|
+
71
|
|
90
|
+
],
|
|
91
|
+
"image/jpeg": [255, 216],
|
|
92
|
+
"image/webp": [
|
|
93
|
+
82,
|
|
94
|
+
73,
|
|
95
|
+
70,
|
|
96
|
+
70
|
|
97
|
+
]
|
|
98
|
+
}[image.type];
|
|
99
|
+
check(starts !== void 0 && starts.every((byte, index) => image.bytes[index] === byte), "the bytes are an image of the type it says");
|
|
100
|
+
check(Number.isInteger(image.width) && image.width > 0 && Number.isInteger(image.height) && image.height > 0, "its size is whole pixels");
|
|
101
|
+
check(image.model.length > 0 && image.credits >= 0 && image.catalogue.length > 0, "it names its Model and what it cost");
|
|
102
|
+
await refused(() => ai.image({ prompt: " " }), "invalid");
|
|
103
|
+
}
|
|
104
|
+
},
|
|
80
105
|
{
|
|
81
106
|
name: "a Model that is not offered is refused as invalid",
|
|
82
107
|
async run(ai) {
|
package/ai/index.d.mts
CHANGED
|
@@ -11,11 +11,16 @@
|
|
|
11
11
|
* for await (const text of stream) write(text)
|
|
12
12
|
* const { credits } = await stream.done
|
|
13
13
|
*
|
|
14
|
+
* `image` generates one picture from a prompt, for a page that needs one, as bytes to save with the project:
|
|
15
|
+
*
|
|
16
|
+
* const { bytes, type, width, height } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light" })
|
|
17
|
+
*
|
|
14
18
|
* A call can pass tools (its calls come back as `toolCalls`), ask for JSON to a schema
|
|
15
19
|
* (`responseFormat`), send images in a user message, set a reasoning effort, and name `fallback`
|
|
16
20
|
* Models asked in turn when one can't answer; `model` in what it spent is the one that replied.
|
|
17
21
|
*
|
|
18
|
-
* Under `midsummer dev` it returns the Stand-in's reply, an echo of the question,
|
|
22
|
+
* Under `midsummer dev` it returns the Stand-in's reply, an echo of the question, and a plain placeholder for an image
|
|
23
|
+
* (`model: "stand-in"`), unless given an engine. Given an engine,
|
|
19
24
|
* calls go there instead: the Stand-in (`@midsummer-ai/sdk/ai/stand-in`) in tests, or, from code outside Midsummer,
|
|
20
25
|
* the platform with an AI key (`ai(aiClient(origin, { key }))`). Every engine is held to the cases in `@midsummer-ai/sdk/ai/contract`.
|
|
21
26
|
* A Model is named as `midsummer ai model list` lists it; leaving it out picks the default.
|
|
@@ -112,9 +117,28 @@ type AiEvent = {
|
|
|
112
117
|
} | {
|
|
113
118
|
readonly done: Spent;
|
|
114
119
|
};
|
|
115
|
-
/**
|
|
120
|
+
/** What an image asks: what it shows (1 to 2,048 characters), and its shape and image Model when not the first. */
|
|
121
|
+
interface ImageAsk {
|
|
122
|
+
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
|
+
readonly model?: string;
|
|
126
|
+
}
|
|
127
|
+
/** A generated image: its bytes, type and size in pixels, the Model that made it, and what it cost in credits under `catalogue`. */
|
|
128
|
+
interface Image {
|
|
129
|
+
readonly bytes: Uint8Array;
|
|
130
|
+
readonly type: "image/png" | "image/jpeg" | "image/webp";
|
|
131
|
+
readonly width: number;
|
|
132
|
+
readonly height: number;
|
|
133
|
+
readonly model: string;
|
|
134
|
+
readonly credits: number;
|
|
135
|
+
readonly catalogue: string;
|
|
136
|
+
}
|
|
137
|
+
/** Where calls go. `ask` streams one call's events and `image` makes one image; both throw `AiError`. */
|
|
116
138
|
interface AiEngine {
|
|
117
139
|
ask(input: Ask): AsyncIterable<AiEvent>;
|
|
140
|
+
/** Absent where images aren't made, as from a deployment's own AI Gateway without a key. */
|
|
141
|
+
image?(input: ImageAsk): Promise<Image>;
|
|
118
142
|
}
|
|
119
143
|
interface Answer extends Spent {
|
|
120
144
|
readonly text: string;
|
|
@@ -129,6 +153,8 @@ interface AnswerStream extends AsyncIterable<string> {
|
|
|
129
153
|
interface Ai {
|
|
130
154
|
complete(input: Ask): Promise<Answer>;
|
|
131
155
|
stream(input: Ask): AnswerStream;
|
|
156
|
+
/** One generated image; save its bytes with the project and serve them from there. */
|
|
157
|
+
image(input: ImageAsk): Promise<Image>;
|
|
132
158
|
}
|
|
133
159
|
declare function ai(given?: AiEngine): Ai;
|
|
134
160
|
type Fetch = (url: string, init: RequestInit) => Promise<Response>;
|
|
@@ -150,4 +176,4 @@ declare function boundAi(handle: {
|
|
|
150
176
|
ask(input: Ask): Promise<ReadableStream<Uint8Array>>;
|
|
151
177
|
}): AiEngine;
|
|
152
178
|
//#endregion
|
|
153
|
-
export { Ai, AiEngine, AiError, AiErrorCode, AiEvent, Answer, AnswerStream, Ask, Message, Part, Spent, Tool, ToolCall, ToolCallPiece, ai, aiClient, boundAi };
|
|
179
|
+
export { Ai, AiEngine, AiError, AiErrorCode, AiEvent, Answer, AnswerStream, Ask, Image, ImageAsk, Message, Part, 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-BHK1-jsB.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-BHK1-jsB.mjs";
|
|
2
2
|
export { aiStandIn };
|
|
@@ -10,54 +10,141 @@ import { a as processEnv, n as here, r as late, t as clientHeaders } from "./her
|
|
|
10
10
|
*
|
|
11
11
|
* It offers the Models it is given (`stand-in` unless told), the first the default. Tokens are
|
|
12
12
|
* counted at about four characters a token, and cost nothing. Made to call a tool (`toolChoice`
|
|
13
|
-
* `required` or a name), it calls it with no arguments; a fallback is never needed.
|
|
13
|
+
* `required` or a name), it calls it with no arguments; a fallback is never needed. Asked for an image, it makes a
|
|
14
|
+
* plain sand-coloured PNG of the size asked, which says in its own text that it is Midsummer's dev stand-in, and is
|
|
15
|
+
* returned as made by `stand-in` at no cost: a page shows where the picture goes until it runs where images are made.
|
|
14
16
|
*/
|
|
15
17
|
/** A message's text, without its images. */
|
|
16
18
|
const textOf = (content) => typeof content === "string" ? content : content.map((part) => part.type === "text" ? part.text : "").join("");
|
|
19
|
+
/** The stand-in's image sizes, in pixels. */
|
|
20
|
+
const SIZES = {
|
|
21
|
+
square: [1024, 1024],
|
|
22
|
+
landscape: [1024, 576],
|
|
23
|
+
portrait: [576, 1024]
|
|
24
|
+
};
|
|
25
|
+
/** CRC-32 as PNG's chunks carry it (ISO 3309). */
|
|
26
|
+
function crc32(bytes) {
|
|
27
|
+
let crc = 4294967295;
|
|
28
|
+
for (const byte of bytes) {
|
|
29
|
+
crc ^= byte;
|
|
30
|
+
for (let bit = 0; bit < 8; bit++) crc = crc & 1 ? crc >>> 1 ^ 3988292384 : crc >>> 1;
|
|
31
|
+
}
|
|
32
|
+
return (crc ^ 4294967295) >>> 0;
|
|
33
|
+
}
|
|
34
|
+
/** A plain PNG of one colour, with a `Comment` saying it is the dev stand-in, its rows deflated by the runtime's own zlib. */
|
|
35
|
+
async function placeholder(width, height) {
|
|
36
|
+
const chunk = (type, data) => {
|
|
37
|
+
const out = new Uint8Array(12 + data.length);
|
|
38
|
+
const view = new DataView(out.buffer);
|
|
39
|
+
view.setUint32(0, data.length);
|
|
40
|
+
out.set(new TextEncoder().encode(type), 4);
|
|
41
|
+
out.set(data, 8);
|
|
42
|
+
view.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
|
|
43
|
+
return out;
|
|
44
|
+
};
|
|
45
|
+
const header = /* @__PURE__ */ new Uint8Array(13);
|
|
46
|
+
new DataView(header.buffer).setUint32(0, width);
|
|
47
|
+
new DataView(header.buffer).setUint32(4, height);
|
|
48
|
+
header.set([
|
|
49
|
+
8,
|
|
50
|
+
2,
|
|
51
|
+
0,
|
|
52
|
+
0,
|
|
53
|
+
0
|
|
54
|
+
], 8);
|
|
55
|
+
const row = new Uint8Array(1 + width * 3);
|
|
56
|
+
for (let x = 0; x < width; x++) row.set([
|
|
57
|
+
214,
|
|
58
|
+
200,
|
|
59
|
+
176
|
|
60
|
+
], 1 + x * 3);
|
|
61
|
+
const rows = new Uint8Array(row.length * height);
|
|
62
|
+
for (let y = 0; y < height; y++) rows.set(row, y * row.length);
|
|
63
|
+
const deflated = new Uint8Array(await new Response(new Blob([rows]).stream().pipeThrough(new CompressionStream("deflate"))).arrayBuffer());
|
|
64
|
+
const parts = [
|
|
65
|
+
new Uint8Array([
|
|
66
|
+
137,
|
|
67
|
+
80,
|
|
68
|
+
78,
|
|
69
|
+
71,
|
|
70
|
+
13,
|
|
71
|
+
10,
|
|
72
|
+
26,
|
|
73
|
+
10
|
|
74
|
+
]),
|
|
75
|
+
chunk("IHDR", header),
|
|
76
|
+
chunk("tEXt", new TextEncoder().encode("Comment\0Midsummer dev stand-in: a placeholder, not a generated image")),
|
|
77
|
+
chunk("IDAT", deflated),
|
|
78
|
+
chunk("IEND", /* @__PURE__ */ new Uint8Array())
|
|
79
|
+
];
|
|
80
|
+
const png = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0));
|
|
81
|
+
parts.reduce((at, part) => {
|
|
82
|
+
png.set(part, at);
|
|
83
|
+
return at + part.length;
|
|
84
|
+
}, 0);
|
|
85
|
+
return png;
|
|
86
|
+
}
|
|
17
87
|
function aiStandIn(options = {}) {
|
|
18
88
|
const models = options.models?.length ? options.models : ["stand-in"];
|
|
19
89
|
const tokens = (text) => Math.ceil(text.length / 4);
|
|
20
|
-
return {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
90
|
+
return {
|
|
91
|
+
async image(input) {
|
|
92
|
+
const model = input.model ?? models[0];
|
|
93
|
+
if (!models.includes(model)) throw new AiError("invalid", `There is no model ${model}.`);
|
|
94
|
+
if (!input.prompt.trim()) throw new AiError("invalid", "Describe the image in 1 to 2,048 characters.");
|
|
95
|
+
const [width, height] = SIZES[input.size ?? "square"];
|
|
96
|
+
return {
|
|
97
|
+
bytes: await placeholder(width, height),
|
|
98
|
+
type: "image/png",
|
|
99
|
+
width,
|
|
100
|
+
height,
|
|
101
|
+
model,
|
|
102
|
+
credits: 0,
|
|
103
|
+
catalogue: "stand-in"
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
async *ask(input) {
|
|
107
|
+
const model = input.model ?? models[0];
|
|
108
|
+
if (!models.includes(model)) throw new AiError("invalid", `There is no model ${model}.`);
|
|
109
|
+
const messages = [...input.system ? [{
|
|
110
|
+
role: "system",
|
|
111
|
+
content: input.system
|
|
112
|
+
}] : [], ...input.messages ?? [{
|
|
113
|
+
role: "user",
|
|
114
|
+
content: input.prompt ?? ""
|
|
115
|
+
}]];
|
|
116
|
+
const last = messages.filter((message) => message.role !== "system").at(-1);
|
|
117
|
+
if (!(last?.role === "tool" || last?.role === "user" && textOf(last.content).trim())) throw new AiError("invalid", "End with a user message that asks something, or with the results of the tools the model called.");
|
|
118
|
+
const inputTokens = tokens(messages.map((message) => textOf(message.content)).join(""));
|
|
119
|
+
const tool = typeof input.toolChoice === "object" ? input.toolChoice.name : input.toolChoice === "required" ? input.tools?.[0]?.name : void 0;
|
|
120
|
+
if (tool && last.role === "user") {
|
|
121
|
+
if (!input.tools?.some((given) => given.name === tool)) throw new AiError("invalid", `toolChoice names ${tool}, which isn't one of the tools given.`);
|
|
122
|
+
yield { toolCall: {
|
|
123
|
+
index: 0,
|
|
124
|
+
id: "call_stand_in_1",
|
|
125
|
+
name: tool,
|
|
126
|
+
arguments: "{}"
|
|
127
|
+
} };
|
|
128
|
+
yield { done: {
|
|
129
|
+
model,
|
|
130
|
+
inputTokens,
|
|
131
|
+
outputTokens: 1,
|
|
132
|
+
credits: 0,
|
|
133
|
+
catalogue: "stand-in"
|
|
134
|
+
} };
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const answer = `You asked: ${textOf(last.content)}`.slice(0, (input.maxTokens ?? 1024) * 4);
|
|
138
|
+
for (const piece of answer.match(/\S+\s*/g) ?? []) yield { text: piece };
|
|
42
139
|
yield { done: {
|
|
43
140
|
model,
|
|
44
141
|
inputTokens,
|
|
45
|
-
outputTokens:
|
|
142
|
+
outputTokens: tokens(answer),
|
|
46
143
|
credits: 0,
|
|
47
144
|
catalogue: "stand-in"
|
|
48
145
|
} };
|
|
49
|
-
return;
|
|
50
146
|
}
|
|
51
|
-
|
|
52
|
-
for (const piece of answer.match(/\S+\s*/g) ?? []) yield { text: piece };
|
|
53
|
-
yield { done: {
|
|
54
|
-
model,
|
|
55
|
-
inputTokens,
|
|
56
|
-
outputTokens: tokens(answer),
|
|
57
|
-
credits: 0,
|
|
58
|
-
catalogue: "stand-in"
|
|
59
|
-
} };
|
|
60
|
-
} };
|
|
147
|
+
};
|
|
61
148
|
}
|
|
62
149
|
//#endregion
|
|
63
150
|
//#region src/ai/index.ts
|
|
@@ -73,11 +160,16 @@ function aiStandIn(options = {}) {
|
|
|
73
160
|
* for await (const text of stream) write(text)
|
|
74
161
|
* const { credits } = await stream.done
|
|
75
162
|
*
|
|
163
|
+
* `image` generates one picture from a prompt, for a page that needs one, as bytes to save with the project:
|
|
164
|
+
*
|
|
165
|
+
* const { bytes, type, width, height } = await ai(aiClient(origin, { key })).image({ prompt: "A clay bowl, morning light" })
|
|
166
|
+
*
|
|
76
167
|
* A call can pass tools (its calls come back as `toolCalls`), ask for JSON to a schema
|
|
77
168
|
* (`responseFormat`), send images in a user message, set a reasoning effort, and name `fallback`
|
|
78
169
|
* Models asked in turn when one can't answer; `model` in what it spent is the one that replied.
|
|
79
170
|
*
|
|
80
|
-
* Under `midsummer dev` it returns the Stand-in's reply, an echo of the question,
|
|
171
|
+
* Under `midsummer dev` it returns the Stand-in's reply, an echo of the question, and a plain placeholder for an image
|
|
172
|
+
* (`model: "stand-in"`), unless given an engine. Given an engine,
|
|
81
173
|
* calls go there instead: the Stand-in (`@midsummer-ai/sdk/ai/stand-in`) in tests, or, from code outside Midsummer,
|
|
82
174
|
* the platform with an AI key (`ai(aiClient(origin, { key }))`). Every engine is held to the cases in `@midsummer-ai/sdk/ai/contract`.
|
|
83
175
|
* A Model is named as `midsummer ai model list` lists it; leaving it out picks the default.
|
|
@@ -87,7 +179,10 @@ function aiStandIn(options = {}) {
|
|
|
87
179
|
* `conflict` (a retry by `idempotencyKey` of a call still under way) or `unavailable` (try again).
|
|
88
180
|
*/
|
|
89
181
|
/** The Stand-in as `midsummer dev` answers with it: an echo of the question, for any Model named, costing nothing. */
|
|
90
|
-
const devAi = {
|
|
182
|
+
const devAi = {
|
|
183
|
+
ask: (input) => aiStandIn({ models: [input.model ?? "stand-in"] }).ask(input),
|
|
184
|
+
image: (input) => aiStandIn({ models: [input.model ?? "stand-in"] }).image(input)
|
|
185
|
+
};
|
|
91
186
|
var AiError = class extends Error {
|
|
92
187
|
code;
|
|
93
188
|
constructor(code, why) {
|
|
@@ -133,6 +228,15 @@ function ai(given) {
|
|
|
133
228
|
throw new AiError("forbidden", "This code runs outside a deployment, so it calls models with an AI key: ai(aiClient(origin, { key })).");
|
|
134
229
|
});
|
|
135
230
|
return {
|
|
231
|
+
async image(input) {
|
|
232
|
+
const prompt = input.prompt?.trim() ?? "";
|
|
233
|
+
if (!prompt || prompt.length > 2048) throw new AiError("invalid", "Describe the image in 1 to 2,048 characters.");
|
|
234
|
+
if (!engine.image) throw new AiError("invalid", "A deployment makes images with an AI key: ai(aiClient(origin, { key })).image(…).");
|
|
235
|
+
return engine.image({
|
|
236
|
+
...input,
|
|
237
|
+
prompt
|
|
238
|
+
});
|
|
239
|
+
},
|
|
136
240
|
async complete(input) {
|
|
137
241
|
let text = "";
|
|
138
242
|
const calls = toolCalls();
|
|
@@ -249,36 +353,79 @@ const CODE_NAMES = [
|
|
|
249
353
|
function aiClient(origin, options) {
|
|
250
354
|
const url = `${origin.replace(/\/+$/, "")}/v1/ai/ask`;
|
|
251
355
|
const send = options.fetch ?? ((input, init) => fetch(input, init));
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
356
|
+
/** The AI Gateway's error as an `AiError`, by its status and in its own words. */
|
|
357
|
+
const failed = async (response) => {
|
|
358
|
+
const why = await response.json().then((body) => typeof body.error === "string" ? body.error : void 0, () => void 0);
|
|
359
|
+
return new AiError(CODES[response.status] ?? "unavailable", why ?? `The AI Gateway returned ${response.status}.`);
|
|
360
|
+
};
|
|
361
|
+
return {
|
|
362
|
+
async image(input) {
|
|
363
|
+
const unreachable = () => new AiError("unavailable", "The AI Gateway could not be reached. Try again.");
|
|
364
|
+
const made = await send(`${origin.replace(/\/+$/, "")}/v1/ai/images`, {
|
|
257
365
|
method: "POST",
|
|
258
|
-
signal: leave.signal,
|
|
259
366
|
headers: {
|
|
260
367
|
"Content-Type": "application/json",
|
|
261
368
|
...clientHeaders("ai")
|
|
262
369
|
},
|
|
263
370
|
body: JSON.stringify({
|
|
264
371
|
...input,
|
|
265
|
-
key: options.key
|
|
266
|
-
stream: true
|
|
372
|
+
key: options.key
|
|
267
373
|
})
|
|
374
|
+
}).catch(() => {
|
|
375
|
+
throw unreachable();
|
|
268
376
|
});
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
377
|
+
if (!made.ok) throw await failed(made);
|
|
378
|
+
const answer = await made.json().catch(() => void 0);
|
|
379
|
+
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.");
|
|
380
|
+
return {
|
|
381
|
+
bytes: answer.url.startsWith("data:") ? Uint8Array.from(atob(answer.url.slice(answer.url.indexOf(",") + 1)), (char) => char.charCodeAt(0)) : await send(answer.url, {
|
|
382
|
+
method: "GET",
|
|
383
|
+
headers: clientHeaders("ai")
|
|
384
|
+
}).then(async (response) => {
|
|
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,
|
|
393
|
+
model: answer.model,
|
|
394
|
+
credits: answer.credits,
|
|
395
|
+
catalogue: answer.catalogue
|
|
396
|
+
};
|
|
397
|
+
},
|
|
398
|
+
async *ask(input) {
|
|
399
|
+
const leave = new AbortController();
|
|
400
|
+
let response;
|
|
401
|
+
try {
|
|
402
|
+
response = await send(url, {
|
|
403
|
+
method: "POST",
|
|
404
|
+
signal: leave.signal,
|
|
405
|
+
headers: {
|
|
406
|
+
"Content-Type": "application/json",
|
|
407
|
+
...clientHeaders("ai")
|
|
408
|
+
},
|
|
409
|
+
body: JSON.stringify({
|
|
410
|
+
...input,
|
|
411
|
+
key: options.key,
|
|
412
|
+
stream: true
|
|
413
|
+
})
|
|
414
|
+
});
|
|
415
|
+
} catch {
|
|
416
|
+
throw new AiError("unavailable", "The AI Gateway could not be reached. Try again.");
|
|
417
|
+
}
|
|
418
|
+
if (!response.ok || !response.body) {
|
|
419
|
+
const why = await response.json().then((body) => typeof body.error === "string" ? body.error : void 0, () => void 0);
|
|
420
|
+
throw new AiError(CODES[response.status] ?? "unavailable", why ?? `The AI Gateway returned ${response.status}.`);
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
yield* eventsOf(response.body);
|
|
424
|
+
} finally {
|
|
425
|
+
leave.abort();
|
|
426
|
+
}
|
|
280
427
|
}
|
|
281
|
-
}
|
|
428
|
+
};
|
|
282
429
|
}
|
|
283
430
|
/** The events of one call's newline-delimited JSON, as the AI Gateway streams them; cancelled when the reader leaves. */
|
|
284
431
|
async function* eventsOf(body) {
|
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.4165";
|
|
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.4165",
|
|
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.",
|