modelfusion 0.130.0 → 0.130.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.130.1 - 2024-01-22
4
+
5
+ ### Fixed
6
+
7
+ - Duplex speech streaming works in Vercel Edge Functions.
8
+
3
9
  ## v0.130.0 - 2024-01-21
4
10
 
5
11
  ### Changed
package/README.md CHANGED
@@ -89,13 +89,16 @@ import { readFileSync } from "fs";
89
89
  const image = readFileSync("./image.png");
90
90
 
91
91
  const textStream = await streamText({
92
- model: openai.ChatTextGenerator({ model: "gpt-4-vision-preview" }),
93
- prompt: [
94
- openai.ChatMessage.user([
95
- { type: "text", text: "Describe the image in detail:" },
92
+ model: openai
93
+ .ChatTextGenerator({ model: "gpt-4-vision-preview" })
94
+ .withInstructionPrompt(),
95
+
96
+ prompt: {
97
+ instruction: [
98
+ { type: "text", text: "Describe the image in detail." },
96
99
  { type: "image", image, mimeType: "image/png" },
97
- ]),
98
- ],
100
+ ],
101
+ },
99
102
  });
100
103
 
101
104
  for await (const textPart of textStream) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "The TypeScript library for building AI applications.",
4
- "version": "0.130.0",
4
+ "version": "0.130.1",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -24,18 +24,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.createSimpleWebSocket = void 0;
27
+ const Runtime = __importStar(require("./detectRuntime.cjs"));
27
28
  /**
28
29
  * Creates a simplified websocket connection. This function works in both Node.js and browser.
29
30
  */
30
31
  async function createSimpleWebSocket(url) {
31
- if (typeof window === "undefined") {
32
- // Use ws library in Node.js:
33
- const { default: WebSocket } = await Promise.resolve().then(() => __importStar(require("ws")));
34
- return new WebSocket(url);
35
- }
36
- else {
37
- // Use native WebSocket in browser:
38
- return new WebSocket(url);
32
+ switch (Runtime.detectRuntime()) {
33
+ case "vercel-edge":
34
+ case "cloudflare-workers":
35
+ case "browser": {
36
+ return new WebSocket(url);
37
+ }
38
+ case "node": {
39
+ // Use ws library (for Node.js):
40
+ const { default: WebSocket } = await Promise.resolve().then(() => __importStar(require("ws")));
41
+ return new WebSocket(url);
42
+ }
43
+ default: {
44
+ throw new Error("Unknown runtime");
45
+ }
39
46
  }
40
47
  }
41
48
  exports.createSimpleWebSocket = createSimpleWebSocket;
@@ -1,14 +1,21 @@
1
+ import * as Runtime from "./detectRuntime.js";
1
2
  /**
2
3
  * Creates a simplified websocket connection. This function works in both Node.js and browser.
3
4
  */
4
5
  export async function createSimpleWebSocket(url) {
5
- if (typeof window === "undefined") {
6
- // Use ws library in Node.js:
7
- const { default: WebSocket } = await import("ws");
8
- return new WebSocket(url);
9
- }
10
- else {
11
- // Use native WebSocket in browser:
12
- return new WebSocket(url);
6
+ switch (Runtime.detectRuntime()) {
7
+ case "vercel-edge":
8
+ case "cloudflare-workers":
9
+ case "browser": {
10
+ return new WebSocket(url);
11
+ }
12
+ case "node": {
13
+ // Use ws library (for Node.js):
14
+ const { default: WebSocket } = await import("ws");
15
+ return new WebSocket(url);
16
+ }
17
+ default: {
18
+ throw new Error("Unknown runtime");
19
+ }
13
20
  }
14
21
  }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.detectRuntime = void 0;
4
+ function detectRuntime() {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ const globalThisAny = globalThis;
7
+ if (globalThisAny.EdgeRuntime) {
8
+ return "vercel-edge";
9
+ }
10
+ if (globalThis.navigator?.userAgent === "Cloudflare-Workers") {
11
+ return "cloudflare-workers";
12
+ }
13
+ if (globalThis.process?.release?.name === "node") {
14
+ return "node";
15
+ }
16
+ if (globalThis.window) {
17
+ return "browser";
18
+ }
19
+ return null;
20
+ }
21
+ exports.detectRuntime = detectRuntime;
@@ -0,0 +1 @@
1
+ export declare function detectRuntime(): "vercel-edge" | "cloudflare-workers" | "node" | "browser" | null;
@@ -0,0 +1,17 @@
1
+ export function detectRuntime() {
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ const globalThisAny = globalThis;
4
+ if (globalThisAny.EdgeRuntime) {
5
+ return "vercel-edge";
6
+ }
7
+ if (globalThis.navigator?.userAgent === "Cloudflare-Workers") {
8
+ return "cloudflare-workers";
9
+ }
10
+ if (globalThis.process?.release?.name === "node") {
11
+ return "node";
12
+ }
13
+ if (globalThis.window) {
14
+ return "browser";
15
+ }
16
+ return null;
17
+ }