@ragpipe/plugin-cloudflare 0.2.0 → 0.3.0-alpha.2

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/dist/index.cjs CHANGED
@@ -66,7 +66,7 @@ function cloudflareEmbedding(options) {
66
66
  // src/generation.ts
67
67
  function cloudflareGeneration(options) {
68
68
  const { model } = options;
69
- const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/run/${model}`;
69
+ const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/v1/chat/completions`;
70
70
  function buildMessages(question, context, opts) {
71
71
  const systemPrompt = opts?.systemPrompt ?? options.systemPrompt ?? "Answer based on the provided context.";
72
72
  let userPrompt = `Context:
@@ -95,7 +95,7 @@ ${userPrompt}`;
95
95
  Authorization: `Bearer ${options.apiToken}`,
96
96
  "Content-Type": "application/json"
97
97
  },
98
- body: JSON.stringify({ messages, stream: false })
98
+ body: JSON.stringify({ model, messages, stream: false })
99
99
  });
100
100
  if (!res.ok) {
101
101
  throw new Error(
@@ -103,12 +103,7 @@ ${userPrompt}`;
103
103
  );
104
104
  }
105
105
  const data = await res.json();
106
- if (!data.success) {
107
- throw new Error(
108
- "Cloudflare generation error: API returned success=false"
109
- );
110
- }
111
- return data.result.response;
106
+ return data.choices?.[0]?.message?.content ?? "";
112
107
  },
113
108
  async *generateStream(question, context, opts) {
114
109
  const messages = buildMessages(question, context, opts);
@@ -118,7 +113,7 @@ ${userPrompt}`;
118
113
  Authorization: `Bearer ${options.apiToken}`,
119
114
  "Content-Type": "application/json"
120
115
  },
121
- body: JSON.stringify({ messages, stream: true })
116
+ body: JSON.stringify({ model, messages, stream: true })
122
117
  });
123
118
  if (!res.ok) {
124
119
  throw new Error(
@@ -142,7 +137,8 @@ ${userPrompt}`;
142
137
  if (payload === "[DONE]") return;
143
138
  try {
144
139
  const data = JSON.parse(payload);
145
- if (data.response) yield data.response;
140
+ const chunk = data.choices?.[0]?.delta?.content;
141
+ if (chunk) yield chunk;
146
142
  } catch {
147
143
  }
148
144
  }
package/dist/index.js CHANGED
@@ -39,7 +39,7 @@ function cloudflareEmbedding(options) {
39
39
  // src/generation.ts
40
40
  function cloudflareGeneration(options) {
41
41
  const { model } = options;
42
- const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/run/${model}`;
42
+ const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/v1/chat/completions`;
43
43
  function buildMessages(question, context, opts) {
44
44
  const systemPrompt = opts?.systemPrompt ?? options.systemPrompt ?? "Answer based on the provided context.";
45
45
  let userPrompt = `Context:
@@ -68,7 +68,7 @@ ${userPrompt}`;
68
68
  Authorization: `Bearer ${options.apiToken}`,
69
69
  "Content-Type": "application/json"
70
70
  },
71
- body: JSON.stringify({ messages, stream: false })
71
+ body: JSON.stringify({ model, messages, stream: false })
72
72
  });
73
73
  if (!res.ok) {
74
74
  throw new Error(
@@ -76,12 +76,7 @@ ${userPrompt}`;
76
76
  );
77
77
  }
78
78
  const data = await res.json();
79
- if (!data.success) {
80
- throw new Error(
81
- "Cloudflare generation error: API returned success=false"
82
- );
83
- }
84
- return data.result.response;
79
+ return data.choices?.[0]?.message?.content ?? "";
85
80
  },
86
81
  async *generateStream(question, context, opts) {
87
82
  const messages = buildMessages(question, context, opts);
@@ -91,7 +86,7 @@ ${userPrompt}`;
91
86
  Authorization: `Bearer ${options.apiToken}`,
92
87
  "Content-Type": "application/json"
93
88
  },
94
- body: JSON.stringify({ messages, stream: true })
89
+ body: JSON.stringify({ model, messages, stream: true })
95
90
  });
96
91
  if (!res.ok) {
97
92
  throw new Error(
@@ -115,7 +110,8 @@ ${userPrompt}`;
115
110
  if (payload === "[DONE]") return;
116
111
  try {
117
112
  const data = JSON.parse(payload);
118
- if (data.response) yield data.response;
113
+ const chunk = data.choices?.[0]?.delta?.content;
114
+ if (chunk) yield chunk;
119
115
  } catch {
120
116
  }
121
117
  }
package/package.json CHANGED
@@ -1,64 +1,61 @@
1
1
  {
2
- "name": "@ragpipe/plugin-cloudflare",
3
- "version": "0.2.0",
4
- "description": "Cloudflare Workers AI embedding and generation plugin for ragpipe",
5
- "type": "module",
6
- "license": "MIT",
7
- "author": {
8
- "name": "yungblud",
9
- "url": "https://github.com/yungblud"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "https://github.com/yungblud/ragpipe",
14
- "directory": "packages/plugin-cloudflare"
15
- },
16
- "bugs": {
17
- "url": "https://github.com/yungblud/ragpipe/issues"
18
- },
19
- "homepage": "https://github.com/yungblud/ragpipe#readme",
20
- "publishConfig": {
21
- "registry": "https://registry.npmjs.org/",
22
- "access": "public"
23
- },
24
- "exports": {
25
- ".": {
26
- "types": "./dist/index.d.ts",
27
- "import": "./dist/index.js",
28
- "require": "./dist/index.cjs"
29
- }
30
- },
31
- "main": "./dist/index.cjs",
32
- "module": "./dist/index.js",
33
- "types": "./dist/index.d.ts",
34
- "files": [
35
- "dist",
36
- "README.md"
37
- ],
38
- "keywords": [
39
- "ragpipe",
40
- "rag",
41
- "cloudflare",
42
- "workers-ai",
43
- "embedding",
44
- "generation",
45
- "plugin"
46
- ],
47
- "peerDependencies": {
48
- "ragpipe": ">=0.3.0"
49
- },
50
- "devDependencies": {
51
- "tsup": "^8.4.0",
52
- "typescript": "^5.8.3",
53
- "vitest": "^3.1.1",
54
- "ragpipe": "0.3.0"
55
- },
56
- "scripts": {
57
- "build": "tsup",
58
- "dev": "tsup --watch",
59
- "typecheck": "tsc --noEmit",
60
- "test": "vitest run",
61
- "test:watch": "vitest",
62
- "test:coverage": "vitest run --coverage"
63
- }
64
- }
2
+ "name": "@ragpipe/plugin-cloudflare",
3
+ "version": "0.3.0-alpha.2",
4
+ "description": "Cloudflare Workers AI embedding and generation plugin for ragpipe",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "yungblud",
9
+ "url": "https://github.com/yungblud"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/yungblud/ragpipe",
14
+ "directory": "packages/plugin-cloudflare"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/yungblud/ragpipe/issues"
18
+ },
19
+ "homepage": "https://github.com/yungblud/ragpipe#readme",
20
+ "publishConfig": {
21
+ "registry": "https://registry.npmjs.org/",
22
+ "access": "public"
23
+ },
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/index.cjs"
29
+ }
30
+ },
31
+ "main": "./dist/index.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": ["dist", "README.md"],
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "dev": "tsup --watch",
38
+ "typecheck": "tsc --noEmit",
39
+ "test": "vitest run",
40
+ "test:watch": "vitest",
41
+ "test:coverage": "vitest run --coverage"
42
+ },
43
+ "keywords": [
44
+ "ragpipe",
45
+ "rag",
46
+ "cloudflare",
47
+ "workers-ai",
48
+ "embedding",
49
+ "generation",
50
+ "plugin"
51
+ ],
52
+ "peerDependencies": {
53
+ "ragpipe": ">=0.3.0"
54
+ },
55
+ "devDependencies": {
56
+ "ragpipe": "workspace:*",
57
+ "tsup": "^8.4.0",
58
+ "typescript": "^5.8.3",
59
+ "vitest": "^3.1.1"
60
+ }
61
+ }