workers-ai-provider 0.2.0 → 0.2.1
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/README.md +1 -2
- package/dist/index.d.ts +30 -5
- package/dist/index.js +347 -52
- package/dist/index.js.map +1 -1
- package/package.json +40 -44
- package/src/convert-to-workersai-chat-messages.ts +94 -97
- package/src/index.ts +84 -82
- package/src/map-workersai-finish-reason.ts +12 -12
- package/src/map-workersai-usage.ts +8 -4
- package/src/utils.ts +89 -52
- package/src/workersai-chat-language-model.ts +313 -325
- package/src/workersai-chat-prompt.ts +18 -18
- package/src/workersai-chat-settings.ts +11 -11
- package/src/workersai-error.ts +10 -13
- package/src/workersai-image-config.ts +5 -0
- package/src/workersai-image-model.ts +114 -0
- package/src/workersai-image-settings.ts +3 -0
- package/src/workersai-models.ts +7 -2
@@ -1,33 +1,33 @@
|
|
1
1
|
export type WorkersAIChatPrompt = Array<WorkersAIChatMessage>;
|
2
2
|
|
3
3
|
export type WorkersAIChatMessage =
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
| WorkersAISystemMessage
|
5
|
+
| WorkersAIUserMessage
|
6
|
+
| WorkersAIAssistantMessage
|
7
|
+
| WorkersAIToolMessage;
|
8
8
|
|
9
9
|
export interface WorkersAISystemMessage {
|
10
|
-
|
11
|
-
|
10
|
+
role: "system";
|
11
|
+
content: string;
|
12
12
|
}
|
13
13
|
|
14
14
|
export interface WorkersAIUserMessage {
|
15
|
-
|
16
|
-
|
15
|
+
role: "user";
|
16
|
+
content: string;
|
17
17
|
}
|
18
18
|
|
19
19
|
export interface WorkersAIAssistantMessage {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
role: "assistant";
|
21
|
+
content: string;
|
22
|
+
tool_calls?: Array<{
|
23
|
+
id: string;
|
24
|
+
type: "function";
|
25
|
+
function: { name: string; arguments: string };
|
26
|
+
}>;
|
27
27
|
}
|
28
28
|
|
29
29
|
export interface WorkersAIToolMessage {
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
role: "tool";
|
31
|
+
name: string;
|
32
|
+
content: string;
|
33
33
|
}
|
@@ -1,13 +1,13 @@
|
|
1
1
|
export interface WorkersAIChatSettings {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
/**
|
3
|
+
* Whether to inject a safety prompt before all conversations.
|
4
|
+
* Defaults to `false`.
|
5
|
+
*/
|
6
|
+
safePrompt?: boolean;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Optionally set Cloudflare AI Gateway options.
|
10
|
+
* @deprecated
|
11
|
+
*/
|
12
|
+
gateway?: GatewayOptions;
|
13
13
|
}
|
package/src/workersai-error.ts
CHANGED
@@ -1,18 +1,15 @@
|
|
1
|
-
import {
|
2
|
-
createJsonErrorResponseHandler,
|
3
|
-
} from "@ai-sdk/provider-utils";
|
1
|
+
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
4
2
|
import { z } from "zod";
|
5
3
|
|
6
4
|
const workersAIErrorDataSchema = z.object({
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
object: z.literal("error"),
|
6
|
+
message: z.string(),
|
7
|
+
type: z.string(),
|
8
|
+
param: z.string().nullable(),
|
9
|
+
code: z.string().nullable(),
|
12
10
|
});
|
13
11
|
|
14
|
-
export const workersAIFailedResponseHandler =
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
});
|
12
|
+
export const workersAIFailedResponseHandler = createJsonErrorResponseHandler({
|
13
|
+
errorSchema: workersAIErrorDataSchema,
|
14
|
+
errorToMessage: (data) => data.message,
|
15
|
+
});
|
@@ -0,0 +1,114 @@
|
|
1
|
+
import type { ImageModelV1, ImageModelV1CallWarning } from "@ai-sdk/provider";
|
2
|
+
import type { WorkersAIImageConfig } from "./workersai-image-config";
|
3
|
+
import type { WorkersAIImageSettings } from "./workersai-image-settings";
|
4
|
+
import type { ImageGenerationModels } from "./workersai-models";
|
5
|
+
|
6
|
+
export class WorkersAIImageModel implements ImageModelV1 {
|
7
|
+
readonly specificationVersion = "v1";
|
8
|
+
|
9
|
+
get maxImagesPerCall(): number {
|
10
|
+
return this.settings.maxImagesPerCall ?? 1;
|
11
|
+
}
|
12
|
+
|
13
|
+
get provider(): string {
|
14
|
+
return this.config.provider;
|
15
|
+
}
|
16
|
+
constructor(
|
17
|
+
readonly modelId: ImageGenerationModels,
|
18
|
+
readonly settings: WorkersAIImageSettings,
|
19
|
+
readonly config: WorkersAIImageConfig,
|
20
|
+
) {}
|
21
|
+
|
22
|
+
async doGenerate({
|
23
|
+
prompt,
|
24
|
+
n,
|
25
|
+
size,
|
26
|
+
aspectRatio,
|
27
|
+
seed,
|
28
|
+
// headers,
|
29
|
+
// abortSignal,
|
30
|
+
}: Parameters<ImageModelV1["doGenerate"]>[0]): Promise<
|
31
|
+
Awaited<ReturnType<ImageModelV1["doGenerate"]>>
|
32
|
+
> {
|
33
|
+
const { width, height } = getDimensionsFromSizeString(size);
|
34
|
+
|
35
|
+
const warnings: Array<ImageModelV1CallWarning> = [];
|
36
|
+
|
37
|
+
if (aspectRatio != null) {
|
38
|
+
warnings.push({
|
39
|
+
type: "unsupported-setting",
|
40
|
+
setting: "aspectRatio",
|
41
|
+
details: "This model does not support aspect ratio. Use `size` instead.",
|
42
|
+
});
|
43
|
+
}
|
44
|
+
|
45
|
+
const generateImage = async () => {
|
46
|
+
const outputStream: ReadableStream<Uint8Array> = await this.config.binding.run(
|
47
|
+
this.modelId,
|
48
|
+
{
|
49
|
+
prompt,
|
50
|
+
seed,
|
51
|
+
width,
|
52
|
+
height,
|
53
|
+
},
|
54
|
+
);
|
55
|
+
|
56
|
+
// Convert the output stream to a Uint8Array.
|
57
|
+
return streamToUint8Array(outputStream);
|
58
|
+
};
|
59
|
+
|
60
|
+
const images: Uint8Array[] = await Promise.all(
|
61
|
+
Array.from({ length: n }, () => generateImage()),
|
62
|
+
);
|
63
|
+
|
64
|
+
// type AiTextToImageOutput = ReadableStream<Uint8Array>;
|
65
|
+
|
66
|
+
return {
|
67
|
+
images,
|
68
|
+
warnings,
|
69
|
+
response: {
|
70
|
+
timestamp: new Date(),
|
71
|
+
modelId: this.modelId,
|
72
|
+
headers: {},
|
73
|
+
},
|
74
|
+
};
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
function getDimensionsFromSizeString(size: string | undefined) {
|
79
|
+
const [width, height] = size?.split("x") ?? [undefined, undefined];
|
80
|
+
|
81
|
+
return {
|
82
|
+
width: parseInteger(width),
|
83
|
+
height: parseInteger(height),
|
84
|
+
};
|
85
|
+
}
|
86
|
+
|
87
|
+
function parseInteger(value?: string) {
|
88
|
+
if (value === "" || !value) return undefined;
|
89
|
+
const number = Number(value);
|
90
|
+
return Number.isInteger(number) ? number : undefined;
|
91
|
+
}
|
92
|
+
|
93
|
+
async function streamToUint8Array(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
|
94
|
+
const reader = stream.getReader();
|
95
|
+
const chunks: Uint8Array[] = [];
|
96
|
+
let totalLength = 0;
|
97
|
+
|
98
|
+
// Read the stream until it is finished.
|
99
|
+
while (true) {
|
100
|
+
const { done, value } = await reader.read();
|
101
|
+
if (done) break;
|
102
|
+
chunks.push(value);
|
103
|
+
totalLength += value.length;
|
104
|
+
}
|
105
|
+
|
106
|
+
// Allocate a new Uint8Array to hold all the data.
|
107
|
+
const result = new Uint8Array(totalLength);
|
108
|
+
let offset = 0;
|
109
|
+
for (const chunk of chunks) {
|
110
|
+
result.set(chunk, offset);
|
111
|
+
offset += chunk.length;
|
112
|
+
}
|
113
|
+
return result;
|
114
|
+
}
|
package/src/workersai-models.ts
CHANGED
@@ -2,8 +2,13 @@
|
|
2
2
|
* The names of the BaseAiTextGeneration models.
|
3
3
|
*/
|
4
4
|
export type TextGenerationModels = Exclude<
|
5
|
-
|
6
|
-
|
5
|
+
value2key<AiModels, BaseAiTextGeneration>,
|
6
|
+
value2key<AiModels, BaseAiTextToImage>
|
7
7
|
>;
|
8
8
|
|
9
|
+
/*
|
10
|
+
* The names of the BaseAiTextToImage models.
|
11
|
+
*/
|
12
|
+
export type ImageGenerationModels = value2key<AiModels, BaseAiTextToImage>;
|
13
|
+
|
9
14
|
type value2key<T, V> = { [K in keyof T]: T[K] extends V ? K : never }[keyof T];
|