create-claudius 1.8.0
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/LICENSE +21 -0
- package/README.md +68 -0
- package/dist/index.js +277 -0
- package/package.json +57 -0
- package/templates/next/README.md +23 -0
- package/templates/next/_gitignore +7 -0
- package/templates/next/app/ClaudiusWidget.tsx +16 -0
- package/templates/next/app/layout.tsx +15 -0
- package/templates/next/app/page.tsx +24 -0
- package/templates/next/next.config.mjs +4 -0
- package/templates/next/package.json +22 -0
- package/templates/next/tsconfig.json +21 -0
- package/templates/react/README.md +23 -0
- package/templates/react/_gitignore +4 -0
- package/templates/react/index.html +12 -0
- package/templates/react/package.json +23 -0
- package/templates/react/src/App.tsx +31 -0
- package/templates/react/src/main.tsx +9 -0
- package/templates/react/tsconfig.json +17 -0
- package/templates/react/vite.config.ts +6 -0
- package/templates/vanilla/README.md +19 -0
- package/templates/vanilla/_gitignore +4 -0
- package/templates/vanilla/index.html +60 -0
- package/templates/vanilla/package.json +14 -0
- package/templates/worker/.dev.vars.example +3 -0
- package/templates/worker/README.md +34 -0
- package/templates/worker/_gitignore +5 -0
- package/templates/worker/package.json +18 -0
- package/templates/worker/src/index.ts +92 -0
- package/templates/worker/tsconfig.json +16 -0
- package/templates/worker/wrangler.toml +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PMDevSolutions
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# create-claudius
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/create-claudius)
|
|
4
|
+
|
|
5
|
+
Scaffold a [Claudius](https://claudius-docs.pages.dev) AI chat widget project in
|
|
6
|
+
one command.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm create claudius@latest
|
|
10
|
+
# or
|
|
11
|
+
pnpm create claudius
|
|
12
|
+
# or
|
|
13
|
+
yarn create claudius
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
You'll be prompted for:
|
|
17
|
+
|
|
18
|
+
- **Project name** — the new directory / package name
|
|
19
|
+
- **Framework** — `vanilla` (CDN script embed), `react` (Vite), or `next` (Next.js App Router)
|
|
20
|
+
- **Theme** — `auto` / `light` / `dark`, or a built-in theme (`default`, `minimal`, `playful`, `corporate`)
|
|
21
|
+
- **Accent color** — a hex color for the widget
|
|
22
|
+
- **API URL** — your deployed Claudius worker endpoint
|
|
23
|
+
- Optionally, a **Cloudflare Worker** scaffold (`wrangler.toml` + KV stub + deploy runbook)
|
|
24
|
+
|
|
25
|
+
The generated project runs out of the box:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
cd my-app
|
|
29
|
+
pnpm install
|
|
30
|
+
pnpm dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Non-interactive
|
|
34
|
+
|
|
35
|
+
Pass flags to skip the prompts (handy for CI):
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm create claudius@latest my-app -- \
|
|
39
|
+
--template react \
|
|
40
|
+
--theme auto \
|
|
41
|
+
--accent "#4f46e5" \
|
|
42
|
+
--api-url https://my-worker.workers.dev \
|
|
43
|
+
--worker \
|
|
44
|
+
--yes
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
| Flag | Values | Description |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| `--template` | `vanilla` \| `react` \| `next` | Framework template |
|
|
50
|
+
| `--theme` | `auto` \| `light` \| `dark` \| `default` \| `minimal` \| `playful` \| `corporate` | Widget theme |
|
|
51
|
+
| `--accent` | `#rrggbb` | Accent color |
|
|
52
|
+
| `--api-url` | URL | Worker chat endpoint |
|
|
53
|
+
| `--worker` | — | Also scaffold a Cloudflare Worker |
|
|
54
|
+
| `--pm` | `npm` \| `pnpm` \| `yarn` \| `bun` | Package manager shown in next steps |
|
|
55
|
+
| `--yes`, `-y` | — | Accept defaults for any unspecified prompt |
|
|
56
|
+
|
|
57
|
+
## What you get
|
|
58
|
+
|
|
59
|
+
- **vanilla** — a static Vite site that loads the widget from the jsDelivr CDN via a
|
|
60
|
+
single `<script>` tag and `window.ClaudiusConfig`.
|
|
61
|
+
- **react** — a Vite + React + TypeScript app using the `claudius-chat-widget`
|
|
62
|
+
component and its stylesheet.
|
|
63
|
+
- **next** — a Next.js (App Router) + TypeScript app with a client-side widget wrapper.
|
|
64
|
+
|
|
65
|
+
See the [documentation](https://claudius-docs.pages.dev) for worker setup and
|
|
66
|
+
configuration.
|
|
67
|
+
|
|
68
|
+
MIT © PMDevSolutions
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { cancel as cancel2, confirm as confirm2, isCancel as isCancel2, log, note, outro } from "@clack/prompts";
|
|
5
|
+
import pc2 from "picocolors";
|
|
6
|
+
import { parseArgs } from "util";
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
import { dirname as dirname2, resolve as resolve2 } from "path";
|
|
9
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
10
|
+
|
|
11
|
+
// src/frameworks.ts
|
|
12
|
+
var FRAMEWORKS = [
|
|
13
|
+
{ id: "vanilla", label: "Vanilla", hint: "Static site + CDN script embed" },
|
|
14
|
+
{ id: "react", label: "React", hint: "Vite + React + TypeScript" },
|
|
15
|
+
{ id: "next", label: "Next.js", hint: "App Router + TypeScript" }
|
|
16
|
+
];
|
|
17
|
+
var TEMPLATE_IDS = FRAMEWORKS.map((f) => f.id);
|
|
18
|
+
var THEMES = [
|
|
19
|
+
{ value: "auto", label: "Auto (follows system)" },
|
|
20
|
+
{ value: "light", label: "Light" },
|
|
21
|
+
{ value: "dark", label: "Dark" },
|
|
22
|
+
{ value: "default", label: "Default (built-in)" },
|
|
23
|
+
{ value: "minimal", label: "Minimal" },
|
|
24
|
+
{ value: "playful", label: "Playful" },
|
|
25
|
+
{ value: "corporate", label: "Corporate" }
|
|
26
|
+
];
|
|
27
|
+
var THEME_VALUES = THEMES.map((t) => t.value);
|
|
28
|
+
var DEFAULTS = {
|
|
29
|
+
projectName: "my-claudius-app",
|
|
30
|
+
template: "react",
|
|
31
|
+
theme: "auto",
|
|
32
|
+
accent: "#4f46e5",
|
|
33
|
+
apiUrl: "https://your-worker.workers.dev"
|
|
34
|
+
};
|
|
35
|
+
function isTemplateId(value) {
|
|
36
|
+
return TEMPLATE_IDS.includes(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/prompts.ts
|
|
40
|
+
import { cancel, confirm, intro, isCancel, select, text } from "@clack/prompts";
|
|
41
|
+
import pc from "picocolors";
|
|
42
|
+
var PROJECT_NAME_RE = /^[a-zA-Z0-9._-]+$/;
|
|
43
|
+
var HEX_RE = /^#[0-9a-fA-F]{6}$/;
|
|
44
|
+
function unwrap(value) {
|
|
45
|
+
if (isCancel(value)) {
|
|
46
|
+
cancel("Scaffolding cancelled.");
|
|
47
|
+
process.exit(0);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
async function runPrompts(provided) {
|
|
52
|
+
intro(pc.bgCyan(pc.black(" create-claudius ")));
|
|
53
|
+
const yes = provided.yes ?? false;
|
|
54
|
+
const projectName = provided.projectName ?? (yes ? DEFAULTS.projectName : unwrap(
|
|
55
|
+
await text({
|
|
56
|
+
message: "Project name?",
|
|
57
|
+
placeholder: DEFAULTS.projectName,
|
|
58
|
+
defaultValue: DEFAULTS.projectName,
|
|
59
|
+
validate: (v) => v && !PROJECT_NAME_RE.test(v.trim()) ? "Use letters, numbers, dashes, dots, or underscores." : void 0
|
|
60
|
+
})
|
|
61
|
+
));
|
|
62
|
+
const template = provided.template ?? (yes ? DEFAULTS.template : unwrap(
|
|
63
|
+
await select({
|
|
64
|
+
message: "Which framework?",
|
|
65
|
+
options: FRAMEWORKS.map((f) => ({ value: f.id, label: f.label, hint: f.hint })),
|
|
66
|
+
initialValue: DEFAULTS.template
|
|
67
|
+
})
|
|
68
|
+
));
|
|
69
|
+
const theme = provided.theme ?? (yes ? DEFAULTS.theme : unwrap(
|
|
70
|
+
await select({
|
|
71
|
+
message: "Theme?",
|
|
72
|
+
options: THEMES,
|
|
73
|
+
initialValue: DEFAULTS.theme
|
|
74
|
+
})
|
|
75
|
+
));
|
|
76
|
+
const accent = provided.accent ?? (yes ? DEFAULTS.accent : unwrap(
|
|
77
|
+
await text({
|
|
78
|
+
message: "Accent color (hex)?",
|
|
79
|
+
placeholder: DEFAULTS.accent,
|
|
80
|
+
defaultValue: DEFAULTS.accent,
|
|
81
|
+
validate: (v) => v && !HEX_RE.test(v.trim()) ? "Use a 6-digit hex color like #4f46e5." : void 0
|
|
82
|
+
})
|
|
83
|
+
));
|
|
84
|
+
const apiUrl = provided.apiUrl ?? (yes ? DEFAULTS.apiUrl : unwrap(
|
|
85
|
+
await text({
|
|
86
|
+
message: "Worker API URL?",
|
|
87
|
+
placeholder: DEFAULTS.apiUrl,
|
|
88
|
+
defaultValue: DEFAULTS.apiUrl
|
|
89
|
+
})
|
|
90
|
+
));
|
|
91
|
+
const worker = provided.worker ?? (yes ? false : unwrap(
|
|
92
|
+
await confirm({
|
|
93
|
+
message: "Also scaffold a Cloudflare Worker?",
|
|
94
|
+
initialValue: false
|
|
95
|
+
})
|
|
96
|
+
));
|
|
97
|
+
return {
|
|
98
|
+
projectName: projectName.trim(),
|
|
99
|
+
template,
|
|
100
|
+
theme,
|
|
101
|
+
accent: accent.trim(),
|
|
102
|
+
apiUrl: apiUrl.trim(),
|
|
103
|
+
worker
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/scaffold.ts
|
|
108
|
+
import { mkdir, readdir, readFile, writeFile } from "fs/promises";
|
|
109
|
+
import { existsSync, readdirSync } from "fs";
|
|
110
|
+
import { dirname, join, resolve } from "path";
|
|
111
|
+
import { fileURLToPath } from "url";
|
|
112
|
+
function tokensFor(opts) {
|
|
113
|
+
return {
|
|
114
|
+
"{{PROJECT_NAME}}": opts.projectName,
|
|
115
|
+
"{{API_URL}}": opts.apiUrl,
|
|
116
|
+
"{{THEME}}": opts.theme,
|
|
117
|
+
"{{ACCENT_COLOR}}": opts.accent,
|
|
118
|
+
"{{WIDGET_VERSION}}": opts.widgetVersion
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function applyTokens(content, tokens) {
|
|
122
|
+
let out = content;
|
|
123
|
+
for (const [token, value] of Object.entries(tokens)) {
|
|
124
|
+
out = out.split(token).join(value);
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
function destFileName(name) {
|
|
129
|
+
return name === "_gitignore" ? ".gitignore" : name;
|
|
130
|
+
}
|
|
131
|
+
async function copyTemplate(srcDir, destDir, tokens) {
|
|
132
|
+
await mkdir(destDir, { recursive: true });
|
|
133
|
+
const entries = await readdir(srcDir, { withFileTypes: true });
|
|
134
|
+
for (const entry of entries) {
|
|
135
|
+
const srcPath = join(srcDir, entry.name);
|
|
136
|
+
const destPath = join(destDir, destFileName(entry.name));
|
|
137
|
+
if (entry.isDirectory()) {
|
|
138
|
+
await copyTemplate(srcPath, destPath, tokens);
|
|
139
|
+
} else {
|
|
140
|
+
const raw = await readFile(srcPath, "utf8");
|
|
141
|
+
await writeFile(destPath, applyTokens(raw, tokens));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function templatesRoot() {
|
|
146
|
+
return resolve(dirname(fileURLToPath(import.meta.url)), "../templates");
|
|
147
|
+
}
|
|
148
|
+
function isNonEmptyDir(dir) {
|
|
149
|
+
if (!existsSync(dir)) return false;
|
|
150
|
+
try {
|
|
151
|
+
return readdirSync(dir).length > 0;
|
|
152
|
+
} catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function scaffold(opts) {
|
|
157
|
+
const tokens = tokensFor(opts);
|
|
158
|
+
const root = templatesRoot();
|
|
159
|
+
await copyTemplate(join(root, opts.template), opts.projectDir, tokens);
|
|
160
|
+
if (opts.worker) {
|
|
161
|
+
await copyTemplate(join(root, "worker"), join(opts.projectDir, "worker"), tokens);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/index.ts
|
|
166
|
+
var HELP = `
|
|
167
|
+
${pc2.bold("create-claudius")} \u2014 scaffold a Claudius AI chat widget project
|
|
168
|
+
|
|
169
|
+
${pc2.bold("Usage")}
|
|
170
|
+
npm create claudius@latest [dir] -- [options]
|
|
171
|
+
|
|
172
|
+
${pc2.bold("Options")}
|
|
173
|
+
-t, --template <id> vanilla | react | next
|
|
174
|
+
--theme <name> auto | light | dark | default | minimal | playful | corporate
|
|
175
|
+
--accent <hex> accent color, e.g. #4f46e5
|
|
176
|
+
--api-url <url> worker chat endpoint
|
|
177
|
+
--worker also scaffold a Cloudflare Worker
|
|
178
|
+
--pm <name> package manager for the next-steps hint (npm|pnpm|yarn|bun)
|
|
179
|
+
-y, --yes accept defaults for anything not provided
|
|
180
|
+
-h, --help show this help
|
|
181
|
+
-v, --version show version
|
|
182
|
+
`;
|
|
183
|
+
function selfVersion() {
|
|
184
|
+
try {
|
|
185
|
+
const pkgPath = resolve2(dirname2(fileURLToPath2(import.meta.url)), "../package.json");
|
|
186
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
187
|
+
return pkg.version ?? "0.0.0";
|
|
188
|
+
} catch {
|
|
189
|
+
return "0.0.0";
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function detectPm(explicit) {
|
|
193
|
+
if (explicit) return explicit;
|
|
194
|
+
const ua = process.env.npm_config_user_agent ?? "";
|
|
195
|
+
if (ua.startsWith("pnpm")) return "pnpm";
|
|
196
|
+
if (ua.startsWith("yarn")) return "yarn";
|
|
197
|
+
if (ua.startsWith("bun")) return "bun";
|
|
198
|
+
return "npm";
|
|
199
|
+
}
|
|
200
|
+
async function main() {
|
|
201
|
+
const { values, positionals } = parseArgs({
|
|
202
|
+
allowPositionals: true,
|
|
203
|
+
options: {
|
|
204
|
+
template: { type: "string", short: "t" },
|
|
205
|
+
theme: { type: "string" },
|
|
206
|
+
accent: { type: "string" },
|
|
207
|
+
"api-url": { type: "string" },
|
|
208
|
+
worker: { type: "boolean" },
|
|
209
|
+
pm: { type: "string" },
|
|
210
|
+
yes: { type: "boolean", short: "y" },
|
|
211
|
+
help: { type: "boolean", short: "h" },
|
|
212
|
+
version: { type: "boolean", short: "v" }
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
if (values.help) {
|
|
216
|
+
console.log(HELP);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (values.version) {
|
|
220
|
+
console.log(selfVersion());
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const provided = {
|
|
224
|
+
projectName: positionals[0],
|
|
225
|
+
theme: values.theme,
|
|
226
|
+
accent: values.accent,
|
|
227
|
+
apiUrl: values["api-url"],
|
|
228
|
+
worker: values.worker,
|
|
229
|
+
yes: values.yes
|
|
230
|
+
};
|
|
231
|
+
if (values.template !== void 0) {
|
|
232
|
+
if (!isTemplateId(values.template)) {
|
|
233
|
+
console.error(
|
|
234
|
+
pc2.red(`Unknown template "${values.template}". Choose one of: ${TEMPLATE_IDS.join(", ")}`)
|
|
235
|
+
);
|
|
236
|
+
process.exit(1);
|
|
237
|
+
}
|
|
238
|
+
provided.template = values.template;
|
|
239
|
+
}
|
|
240
|
+
const result = await runPrompts(provided);
|
|
241
|
+
const projectDir = resolve2(process.cwd(), result.projectName);
|
|
242
|
+
if (isNonEmptyDir(projectDir)) {
|
|
243
|
+
if (provided.yes) {
|
|
244
|
+
log.error(`Target directory "${result.projectName}" already exists and is not empty.`);
|
|
245
|
+
process.exit(1);
|
|
246
|
+
}
|
|
247
|
+
const proceed = await confirm2({
|
|
248
|
+
message: `Directory "${result.projectName}" is not empty. Write into it anyway?`,
|
|
249
|
+
initialValue: false
|
|
250
|
+
});
|
|
251
|
+
if (isCancel2(proceed) || !proceed) {
|
|
252
|
+
cancel2("Aborted.");
|
|
253
|
+
process.exit(0);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
await scaffold({ ...result, projectDir, widgetVersion: `^${selfVersion()}` });
|
|
257
|
+
const pm = detectPm(values.pm);
|
|
258
|
+
const dev = pm === "npm" ? "npm run dev" : `${pm} dev`;
|
|
259
|
+
const steps = [`cd ${result.projectName}`, `${pm} install`, dev];
|
|
260
|
+
if (result.worker) {
|
|
261
|
+
steps.push(
|
|
262
|
+
"",
|
|
263
|
+
pc2.dim("# then deploy the worker"),
|
|
264
|
+
"cd worker",
|
|
265
|
+
`${pm} install`,
|
|
266
|
+
"npx wrangler kv namespace create RATE_LIMIT",
|
|
267
|
+
"npx wrangler secret put ANTHROPIC_API_KEY",
|
|
268
|
+
"npx wrangler deploy"
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
note(steps.join("\n"), "Next steps");
|
|
272
|
+
outro(pc2.green("Done! Happy building with Claudius."));
|
|
273
|
+
}
|
|
274
|
+
main().catch((err) => {
|
|
275
|
+
console.error(err);
|
|
276
|
+
process.exit(1);
|
|
277
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-claudius",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"description": "Scaffold a Claudius AI chat widget project in one command.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"claudius",
|
|
7
|
+
"create",
|
|
8
|
+
"create-claudius",
|
|
9
|
+
"scaffold",
|
|
10
|
+
"cli",
|
|
11
|
+
"chat-widget",
|
|
12
|
+
"claude",
|
|
13
|
+
"ai-chat"
|
|
14
|
+
],
|
|
15
|
+
"author": "PAMulligan",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"homepage": "https://claudius-docs.pages.dev",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/PMDevSolutions/Claudius.git",
|
|
21
|
+
"directory": "create-claudius"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/PMDevSolutions/Claudius/issues"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"bin": {
|
|
28
|
+
"create-claudius": "dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"templates"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"dev": "tsup --watch",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"test:watch": "vitest"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@clack/prompts": "^0.7.0",
|
|
49
|
+
"picocolors": "^1.1.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^22.0.0",
|
|
53
|
+
"tsup": "^8.3.0",
|
|
54
|
+
"typescript": "^5.8.0",
|
|
55
|
+
"vitest": "^4.1.0"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# {{PROJECT_NAME}}
|
|
2
|
+
|
|
3
|
+
A [Next.js](https://nextjs.org) (App Router) + TypeScript app using the
|
|
4
|
+
[`claudius-chat-widget`](https://www.npmjs.com/package/claudius-chat-widget)
|
|
5
|
+
component.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install
|
|
9
|
+
pnpm dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The widget is rendered by a client component (`app/ClaudiusWidget.tsx`) because it
|
|
13
|
+
relies on browser APIs:
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
"use client";
|
|
17
|
+
import { ChatWidget } from "claudius-chat-widget";
|
|
18
|
+
import "claudius-chat-widget/style.css";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You need a running Claudius worker for the chat to respond. See the
|
|
22
|
+
[worker setup guide](https://claudius-docs.pages.dev/deployment/worker/), or
|
|
23
|
+
re-run the scaffolder with `--worker` to generate one.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ChatWidget } from "claudius-chat-widget";
|
|
4
|
+
import "claudius-chat-widget/style.css";
|
|
5
|
+
|
|
6
|
+
// ChatWidget uses browser APIs and React state, so it must run on the client.
|
|
7
|
+
export function ClaudiusWidget() {
|
|
8
|
+
return (
|
|
9
|
+
<ChatWidget
|
|
10
|
+
apiUrl="{{API_URL}}"
|
|
11
|
+
title="{{PROJECT_NAME}}"
|
|
12
|
+
theme="{{THEME}}"
|
|
13
|
+
accentColor="{{ACCENT_COLOR}}"
|
|
14
|
+
/>
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
export const metadata: Metadata = {
|
|
5
|
+
title: "{{PROJECT_NAME}}",
|
|
6
|
+
description: "A Claudius-powered app",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export default function RootLayout({ children }: { children: ReactNode }) {
|
|
10
|
+
return (
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<body style={{ margin: 0 }}>{children}</body>
|
|
13
|
+
</html>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ClaudiusWidget } from "./ClaudiusWidget";
|
|
2
|
+
|
|
3
|
+
export default function Home() {
|
|
4
|
+
return (
|
|
5
|
+
<main
|
|
6
|
+
style={{
|
|
7
|
+
fontFamily: "system-ui, -apple-system, Segoe UI, Roboto, sans-serif",
|
|
8
|
+
display: "grid",
|
|
9
|
+
placeItems: "center",
|
|
10
|
+
minHeight: "100vh",
|
|
11
|
+
}}
|
|
12
|
+
>
|
|
13
|
+
<div style={{ maxWidth: "32rem", padding: "2rem", textAlign: "center" }}>
|
|
14
|
+
<h1>{{PROJECT_NAME}}</h1>
|
|
15
|
+
<p>
|
|
16
|
+
Edit <code>app/ClaudiusWidget.tsx</code> to configure the widget. The launcher
|
|
17
|
+
is in the corner.
|
|
18
|
+
</p>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<ClaudiusWidget />
|
|
22
|
+
</main>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{PROJECT_NAME}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "next dev",
|
|
7
|
+
"build": "next build",
|
|
8
|
+
"start": "next start"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"claudius-chat-widget": "{{WIDGET_VERSION}}",
|
|
12
|
+
"next": "^14.2.0",
|
|
13
|
+
"react": "^18.3.1",
|
|
14
|
+
"react-dom": "^18.3.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^22.0.0",
|
|
18
|
+
"@types/react": "^18.3.0",
|
|
19
|
+
"@types/react-dom": "^18.3.0",
|
|
20
|
+
"typescript": "^5.6.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"module": "esnext",
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"jsx": "preserve",
|
|
15
|
+
"incremental": true,
|
|
16
|
+
"plugins": [{ "name": "next" }],
|
|
17
|
+
"paths": { "@/*": ["./*"] }
|
|
18
|
+
},
|
|
19
|
+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
|
20
|
+
"exclude": ["node_modules"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# {{PROJECT_NAME}}
|
|
2
|
+
|
|
3
|
+
A [Vite](https://vite.dev) + React + TypeScript app using the
|
|
4
|
+
[`claudius-chat-widget`](https://www.npmjs.com/package/claudius-chat-widget)
|
|
5
|
+
component.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install
|
|
9
|
+
pnpm dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The widget lives in `src/App.tsx`:
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { ChatWidget } from "claudius-chat-widget";
|
|
16
|
+
import "claudius-chat-widget/style.css";
|
|
17
|
+
|
|
18
|
+
<ChatWidget apiUrl="{{API_URL}}" theme="{{THEME}}" accentColor="{{ACCENT_COLOR}}" />;
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You need a running Claudius worker for the chat to respond. See the
|
|
22
|
+
[worker setup guide](https://claudius-docs.pages.dev/deployment/worker/), or
|
|
23
|
+
re-run the scaffolder with `--worker` to generate one.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>{{PROJECT_NAME}}</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{PROJECT_NAME}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "tsc --noEmit && vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"claudius-chat-widget": "{{WIDGET_VERSION}}",
|
|
13
|
+
"react": "^18.3.1",
|
|
14
|
+
"react-dom": "^18.3.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/react": "^18.3.0",
|
|
18
|
+
"@types/react-dom": "^18.3.0",
|
|
19
|
+
"@vitejs/plugin-react": "^4.3.0",
|
|
20
|
+
"typescript": "^5.6.0",
|
|
21
|
+
"vite": "^6.0.0"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ChatWidget } from "claudius-chat-widget";
|
|
2
|
+
import "claudius-chat-widget/style.css";
|
|
3
|
+
|
|
4
|
+
const pageStyle: React.CSSProperties = {
|
|
5
|
+
fontFamily: "system-ui, -apple-system, Segoe UI, Roboto, sans-serif",
|
|
6
|
+
display: "grid",
|
|
7
|
+
placeItems: "center",
|
|
8
|
+
minHeight: "100vh",
|
|
9
|
+
margin: 0,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function App() {
|
|
13
|
+
return (
|
|
14
|
+
<main style={pageStyle}>
|
|
15
|
+
<div style={{ maxWidth: "32rem", padding: "2rem", textAlign: "center" }}>
|
|
16
|
+
<h1>{{PROJECT_NAME}}</h1>
|
|
17
|
+
<p>
|
|
18
|
+
Edit <code>src/App.tsx</code> and configure the widget via its props. The
|
|
19
|
+
launcher is in the corner.
|
|
20
|
+
</p>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
<ChatWidget
|
|
24
|
+
apiUrl="{{API_URL}}"
|
|
25
|
+
title="{{PROJECT_NAME}}"
|
|
26
|
+
theme="{{THEME}}"
|
|
27
|
+
accentColor="{{ACCENT_COLOR}}"
|
|
28
|
+
/>
|
|
29
|
+
</main>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true,
|
|
14
|
+
"useDefineForClassFields": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src", "vite.config.ts"]
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# {{PROJECT_NAME}}
|
|
2
|
+
|
|
3
|
+
A static site with the [Claudius](https://claudius-docs.pages.dev) chat widget
|
|
4
|
+
embedded via the CDN — no build step required for the widget itself.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pnpm install
|
|
8
|
+
pnpm dev
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The widget is configured in `index.html` via `window.ClaudiusConfig`:
|
|
12
|
+
|
|
13
|
+
- `apiUrl` — your deployed Claudius worker (currently `{{API_URL}}`)
|
|
14
|
+
- `theme` — `{{THEME}}`
|
|
15
|
+
- `accentColor` — `{{ACCENT_COLOR}}`
|
|
16
|
+
|
|
17
|
+
You need a running Claudius worker for the chat to respond. See the
|
|
18
|
+
[worker setup guide](https://claudius-docs.pages.dev/deployment/worker/), or
|
|
19
|
+
re-run the scaffolder with `--worker` to generate one.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>{{PROJECT_NAME}}</title>
|
|
7
|
+
<!-- Claudius widget styles (CDN, auto-updates within v1.x) -->
|
|
8
|
+
<link
|
|
9
|
+
rel="stylesheet"
|
|
10
|
+
href="https://cdn.jsdelivr.net/gh/PMDevSolutions/Claudius@1/cdn/claudius.css"
|
|
11
|
+
/>
|
|
12
|
+
<style>
|
|
13
|
+
:root {
|
|
14
|
+
font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
|
|
15
|
+
line-height: 1.5;
|
|
16
|
+
}
|
|
17
|
+
body {
|
|
18
|
+
margin: 0;
|
|
19
|
+
min-height: 100vh;
|
|
20
|
+
display: grid;
|
|
21
|
+
place-items: center;
|
|
22
|
+
background: #f8fafc;
|
|
23
|
+
color: #0f172a;
|
|
24
|
+
}
|
|
25
|
+
main {
|
|
26
|
+
max-width: 32rem;
|
|
27
|
+
padding: 2rem;
|
|
28
|
+
text-align: center;
|
|
29
|
+
}
|
|
30
|
+
code {
|
|
31
|
+
background: #e2e8f0;
|
|
32
|
+
padding: 0.15em 0.4em;
|
|
33
|
+
border-radius: 0.3em;
|
|
34
|
+
}
|
|
35
|
+
</style>
|
|
36
|
+
</head>
|
|
37
|
+
<body>
|
|
38
|
+
<main>
|
|
39
|
+
<h1>{{PROJECT_NAME}}</h1>
|
|
40
|
+
<p>
|
|
41
|
+
Your Claudius chat widget is live in the corner. Configure it by editing
|
|
42
|
+
<code>window.ClaudiusConfig</code> in <code>index.html</code>.
|
|
43
|
+
</p>
|
|
44
|
+
</main>
|
|
45
|
+
|
|
46
|
+
<!-- Claudius Chat Widget -->
|
|
47
|
+
<script>
|
|
48
|
+
window.ClaudiusConfig = {
|
|
49
|
+
apiUrl: "{{API_URL}}",
|
|
50
|
+
title: "{{PROJECT_NAME}}",
|
|
51
|
+
theme: "{{THEME}}",
|
|
52
|
+
accentColor: "{{ACCENT_COLOR}}",
|
|
53
|
+
};
|
|
54
|
+
</script>
|
|
55
|
+
<script
|
|
56
|
+
src="https://cdn.jsdelivr.net/gh/PMDevSolutions/Claudius@1/cdn/claudius.iife.js"
|
|
57
|
+
defer
|
|
58
|
+
></script>
|
|
59
|
+
</body>
|
|
60
|
+
</html>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# {{PROJECT_NAME}}-worker
|
|
2
|
+
|
|
3
|
+
A minimal [Cloudflare Worker](https://workers.cloudflare.com) that keeps your
|
|
4
|
+
Anthropic API key server-side and powers the Claudius widget. It exposes
|
|
5
|
+
`POST /api/chat` and `GET /api/health`, with CORS and a small KV-backed rate limit.
|
|
6
|
+
|
|
7
|
+
> This is a trimmed starter. For the full-featured worker (analytics, configurable
|
|
8
|
+
> rate limits, richer error handling), see the
|
|
9
|
+
> [Claudius worker](https://github.com/PMDevSolutions/Claudius/tree/main/worker).
|
|
10
|
+
|
|
11
|
+
## Local development
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm install
|
|
15
|
+
cp .dev.vars.example .dev.vars # then add your ANTHROPIC_API_KEY
|
|
16
|
+
pnpm dev # http://localhost:8787
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Deploy
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 1. Create the KV namespace and paste the id(s) into wrangler.toml
|
|
23
|
+
npx wrangler kv namespace create RATE_LIMIT
|
|
24
|
+
|
|
25
|
+
# 2. Store your API key as a secret
|
|
26
|
+
npx wrangler secret put ANTHROPIC_API_KEY
|
|
27
|
+
|
|
28
|
+
# 3. Deploy
|
|
29
|
+
npx wrangler deploy
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
After deploying, set `ALLOWED_ORIGIN` (in `wrangler.toml` or the Cloudflare
|
|
33
|
+
dashboard) to your site's origin, and point the widget's `apiUrl` at the worker
|
|
34
|
+
URL.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{PROJECT_NAME}}-worker",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "wrangler dev",
|
|
7
|
+
"deploy": "wrangler deploy"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@anthropic-ai/sdk": "^0.52.0",
|
|
11
|
+
"hono": "^4.7.0"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@cloudflare/workers-types": "^4.20240909.0",
|
|
15
|
+
"typescript": "^5.6.0",
|
|
16
|
+
"wrangler": "^4.6.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { cors } from "hono/cors";
|
|
3
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
4
|
+
|
|
5
|
+
interface Env {
|
|
6
|
+
ANTHROPIC_API_KEY: string;
|
|
7
|
+
ALLOWED_ORIGIN: string;
|
|
8
|
+
RATE_LIMIT: KVNamespace;
|
|
9
|
+
CLAUDE_MODEL?: string;
|
|
10
|
+
MAX_TOKENS?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ChatMessage {
|
|
14
|
+
role: "user" | "assistant";
|
|
15
|
+
content: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const DEFAULT_MODEL = "claude-haiku-4-5-20251001";
|
|
19
|
+
const MAX_MESSAGE_LENGTH = 2000;
|
|
20
|
+
const RATE_LIMIT_PER_MINUTE = 20;
|
|
21
|
+
|
|
22
|
+
// Customize your assistant's personality and knowledge here.
|
|
23
|
+
const SYSTEM_PROMPT =
|
|
24
|
+
"You are a helpful, concise assistant embedded on a website. " +
|
|
25
|
+
"Answer clearly and politely. If you don't know something, say so.";
|
|
26
|
+
|
|
27
|
+
const app = new Hono<{ Bindings: Env }>();
|
|
28
|
+
|
|
29
|
+
app.use(
|
|
30
|
+
"/api/*",
|
|
31
|
+
cors({
|
|
32
|
+
origin: (origin, c) => {
|
|
33
|
+
const allowed = (c.env.ALLOWED_ORIGIN || "http://localhost:5173")
|
|
34
|
+
.split(",")
|
|
35
|
+
.map((o: string) => o.trim())
|
|
36
|
+
.filter(Boolean);
|
|
37
|
+
if (origin?.startsWith("http://localhost:")) return origin;
|
|
38
|
+
return origin && allowed.includes(origin) ? origin : allowed[0];
|
|
39
|
+
},
|
|
40
|
+
allowMethods: ["POST", "OPTIONS"],
|
|
41
|
+
allowHeaders: ["Content-Type"],
|
|
42
|
+
maxAge: 86400,
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
app.get("/api/health", (c) => c.json({ ok: true }));
|
|
47
|
+
|
|
48
|
+
app.post("/api/chat", async (c) => {
|
|
49
|
+
let body: { messages?: ChatMessage[] };
|
|
50
|
+
try {
|
|
51
|
+
body = await c.req.json();
|
|
52
|
+
} catch {
|
|
53
|
+
return c.json({ error: "Invalid JSON body." }, 400);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const messages = body.messages;
|
|
57
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
58
|
+
return c.json({ error: "A non-empty messages array is required." }, 400);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Minimal per-IP, per-minute rate limit backed by the RATE_LIMIT KV namespace.
|
|
62
|
+
const ip = c.req.header("cf-connecting-ip") ?? "unknown";
|
|
63
|
+
const bucket = `rl:${ip}:${Math.floor(Date.now() / 60000)}`;
|
|
64
|
+
const count = parseInt((await c.env.RATE_LIMIT.get(bucket)) ?? "0", 10);
|
|
65
|
+
if (count >= RATE_LIMIT_PER_MINUTE) {
|
|
66
|
+
return c.json({ error: "Too many requests. Please wait a minute." }, 429, {
|
|
67
|
+
"Retry-After": "60",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
await c.env.RATE_LIMIT.put(bucket, String(count + 1), { expirationTtl: 120 });
|
|
71
|
+
|
|
72
|
+
const sanitized = messages.map((m) => ({
|
|
73
|
+
role: m.role,
|
|
74
|
+
content: String(m.content ?? "").slice(0, MAX_MESSAGE_LENGTH),
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const client = new Anthropic({ apiKey: c.env.ANTHROPIC_API_KEY });
|
|
79
|
+
const response = await client.messages.create({
|
|
80
|
+
model: c.env.CLAUDE_MODEL ?? DEFAULT_MODEL,
|
|
81
|
+
max_tokens: c.env.MAX_TOKENS ? parseInt(c.env.MAX_TOKENS, 10) : 1024,
|
|
82
|
+
system: SYSTEM_PROMPT,
|
|
83
|
+
messages: sanitized,
|
|
84
|
+
});
|
|
85
|
+
const textBlock = response.content.find((block) => block.type === "text");
|
|
86
|
+
return c.json({ reply: textBlock && textBlock.type === "text" ? textBlock.text : "" });
|
|
87
|
+
} catch {
|
|
88
|
+
return c.json({ error: "AI service temporarily unavailable. Please try again." }, 502);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export default app;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"types": ["@cloudflare/workers-types"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"isolatedModules": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src"]
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name = "{{PROJECT_NAME}}-worker"
|
|
2
|
+
main = "src/index.ts"
|
|
3
|
+
compatibility_date = "2024-09-23"
|
|
4
|
+
|
|
5
|
+
[vars]
|
|
6
|
+
# Comma-separated list of origins allowed to call this worker.
|
|
7
|
+
ALLOWED_ORIGIN = "http://localhost:5173"
|
|
8
|
+
# Optional overrides:
|
|
9
|
+
# CLAUDE_MODEL = "claude-haiku-4-5-20251001"
|
|
10
|
+
# MAX_TOKENS = "1024"
|
|
11
|
+
|
|
12
|
+
# Per-IP rate limiting is backed by this KV namespace. Create it with:
|
|
13
|
+
# npx wrangler kv namespace create RATE_LIMIT
|
|
14
|
+
# then paste the returned id (and preview_id) below.
|
|
15
|
+
[[kv_namespaces]]
|
|
16
|
+
binding = "RATE_LIMIT"
|
|
17
|
+
id = "placeholder"
|
|
18
|
+
preview_id = "placeholder"
|