nuxt-edge-ai 0.1.2 → 0.1.4

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 CHANGED
@@ -1,141 +1,507 @@
1
- # nuxt-edge-ai
2
-
3
- `nuxt-edge-ai` is a Nuxt module for building local-first AI applications with a real server-side WASM inference runtime.
4
-
5
- It ships:
6
-
7
- - a Nuxt module install surface
8
- - Nitro API routes for health, model pull, and generation
9
- - a client composable for app-side usage
10
- - a vendored `transformers.js` + `onnxruntime-web` runtime inside the package
11
- - no Ollama, no `llama.cpp`, no Rust/C++/native runtime dependency for consumers
12
-
13
- The model weights are not bundled. Users either point the module at a local model directory or allow it to download and cache the model on first run.
14
-
15
- ## Why this exists
16
-
17
- The goal is to make `nuxt-edge-ai` a credible, publishable Nuxt module:
18
-
19
- - installable in a regular Nuxt app
20
- - able to run a real local model
21
- - packaged as JS/TS + WASM only
22
- - suitable as a strong portfolio / resume project
23
-
24
- ## Current runtime
25
-
26
- Current real runtime path:
27
-
28
- - `transformers.js` web build
29
- - `onnxruntime-web` WASM backend
30
- - server-side execution through Nitro
31
-
32
- Recommended first demo model:
33
-
34
- - `Xenova/distilgpt2` for quick validation
35
-
36
- Recommended next upgrade target:
37
-
38
- - `onnx-community/Qwen2.5-0.5B-Instruct-ONNX`
39
-
40
- ## Install
41
-
42
- ```bash
43
- pnpm add nuxt-edge-ai
44
- ```
45
-
46
- ```ts
47
- // nuxt.config.ts
48
- export default defineNuxtConfig({
49
- modules: ['nuxt-edge-ai'],
50
- edgeAI: {
51
- runtime: 'transformers-wasm',
52
- cacheDir: './.cache/nuxt-edge-ai',
53
- model: {
54
- id: 'Xenova/distilgpt2',
55
- task: 'text-generation',
56
- allowRemote: true,
57
- dtype: 'q8',
58
- generation: {
59
- maxNewTokens: 96,
60
- temperature: 0.7,
61
- topP: 0.9,
62
- doSample: true,
63
- repetitionPenalty: 1.05,
64
- },
65
- },
66
- },
67
- })
68
- ```
69
-
70
- ```vue
71
- <script setup lang="ts">
72
- const edgeAI = useEdgeAI()
73
-
74
- await edgeAI.pull()
75
-
76
- const result = await edgeAI.generate({
77
- prompt: 'Write a pitch for a local-first Nuxt AI module.',
78
- })
79
- </script>
80
- ```
81
-
82
- ## Consumer runtime guarantees
83
-
84
- Consumers do not need to install:
85
-
86
- - Ollama
87
- - Rust
88
- - C++
89
- - Python
90
- - `llama.cpp`
91
- - extra runtime npm packages beyond this module
92
-
93
- What consumers do need:
94
-
95
- - a Node/Nitro server runtime
96
- - a model path or permission to download a compatible model
97
-
98
- ## API surface
99
-
100
- - `GET /api/edge-ai/health`
101
- - `POST /api/edge-ai/pull`
102
- - `POST /api/edge-ai/generate`
103
- - `useEdgeAI().health()`
104
- - `useEdgeAI().pull()`
105
- - `useEdgeAI().generate()`
106
-
107
- ## Local development
108
-
109
- ```bash
110
- pnpm install
111
- pnpm dev
112
- ```
113
-
114
- Useful commands:
115
-
116
- ```bash
117
- pnpm vendor:runtime
118
- pnpm lint
119
- pnpm test
120
- pnpm test:types
121
- pnpm prepack
122
- ```
123
-
124
- ## Docs
125
-
126
- See [`docs/index.md`](./docs/index.md) for the project docs tree.
127
-
128
- ## Repository shape
129
-
130
- This repository follows a Nuxt modules-style layout:
131
-
132
- - `src/module.ts`: module entry and runtime config wiring
133
- - `src/runtime/`: composables, plugin, and Nitro runtime code
134
- - `playground/`: interactive demo app
135
- - `test/fixtures/`: module consumer fixtures
136
- - `docs/`: module documentation
137
- - `scripts/vendor-runtime.mjs`: vendored runtime generation
138
-
139
- ## Status
140
-
141
- This is an MVP, but it now runs a real model instead of mock text when `runtime: 'transformers-wasm'` is enabled.
1
+ # nuxt-edge-ai
2
+
3
+ [![npm version](https://img.shields.io/npm/v/nuxt-edge-ai/latest.svg)](https://www.npmjs.com/package/nuxt-edge-ai)
4
+ [![npm downloads](https://img.shields.io/npm/dm/nuxt-edge-ai.svg)](https://www.npmjs.com/package/nuxt-edge-ai)
5
+ [![license](https://img.shields.io/npm/l/nuxt-edge-ai.svg)](./LICENSE)
6
+ [![nuxt](https://img.shields.io/badge/Nuxt-4.x-00DC82?logo=nuxt.js&logoColor=white)](https://nuxt.com/)
7
+ [![ci](https://github.com/otadk/nuxt-edge-ai/actions/workflows/ci.yml/badge.svg)](https://github.com/otadk/nuxt-edge-ai/actions/workflows/ci.yml)
8
+
9
+ `nuxt-edge-ai` is a Nuxt module for building local-first AI applications with a real server-side WASM inference runtime and an optional remote API fallback.
10
+
11
+ It ships:
12
+
13
+ - a Nuxt module install surface
14
+ - Nitro API routes for health, model pull, and generation
15
+ - a client composable for app-side usage
16
+ - an `EdgeAI` SDK with an OpenAI-like `chat.completions.create()` surface
17
+ - switchable `local`, `remote`, and `mock` providers behind one module API
18
+ - a vendored `transformers.js` + `onnxruntime-web` runtime inside the package
19
+ - no Ollama, no `llama.cpp`, no Rust/C++/native runtime dependency for consumers
20
+
21
+ The model weights are not bundled. Users either point the module at a local model directory or allow it to download and cache the model on first run.
22
+
23
+ ## Demo
24
+
25
+ - Minimal local-only StackBlitz demo: https://stackblitz.com/edit/nuxt-starter-t2mavk3t?file=package.json
26
+
27
+ The StackBlitz example keeps the setup intentionally small and uses the OpenAI-style `chat.completions.create()` call shape against the local provider only.
28
+
29
+ ## Features
30
+
31
+ - Nuxt module install surface designed for app integration
32
+ - Nitro endpoints for health, pull, and generate workflows
33
+ - local-first server-side inference with bundled WASM runtime assets
34
+ - optional OpenAI-compatible remote provider for stronger hosted models
35
+ - OpenAI-compatible `chat/completions` endpoint for SDK-style integration
36
+ - **streaming chat completions** with SSE (Server-Sent Events) for real-time typewriter effect
37
+ - compatible with `@ai-sdk/vue`'s `useChat()` for seamless integration
38
+ - published package includes vendored inference runtime files
39
+ - no consumer requirement for Ollama, Rust, C++, Python, or native AI runtimes
40
+
41
+ ## Why this exists
42
+
43
+ The goal is to make `nuxt-edge-ai` a credible, publishable Nuxt module:
44
+
45
+ - installable in a regular Nuxt app
46
+ - able to run a real local model
47
+ - packaged as JS/TS + WASM only
48
+
49
+ ## Current runtime
50
+
51
+ Current local runtime path:
52
+
53
+ - `transformers.js` web build
54
+ - `onnxruntime-web` WASM backend
55
+ - server-side execution through Nitro
56
+
57
+ Built-in local preset:
58
+
59
+ - `distilgpt2`
60
+
61
+ The local path is intentionally conservative now. When local inference is not enough, the module can fall back to a remote OpenAI-compatible API.
62
+
63
+ ## Support Matrix
64
+
65
+ | Surface | Status | Notes |
66
+ | --- | --- | --- |
67
+ | Nuxt | Supported | `^4.4.0` and newer Nuxt 4 releases |
68
+ | Runtime | Supported | Node/Nitro server runtime |
69
+ | Local inference | Supported | Bundled Transformers.js + ONNX Runtime WASM |
70
+ | Remote inference | Supported | OpenAI-compatible `chat/completions` providers |
71
+ | Mock mode | Supported | Fixture tests, CI, and integration smoke checks |
72
+ | Streaming | **Supported** | SSE-based streaming with AI SDK-compatible protocol |
73
+ | Edge runtime workers | Not yet supported | The local WASM runtime currently assumes a Node server process |
74
+
75
+ ## Validation
76
+
77
+ This module is validated through:
78
+
79
+ - fixture-based Nuxt module tests in `test/`
80
+ - type checks for both the module and the playground app
81
+ - a local playground app in `playground/`
82
+ - a published-style external consumer smoke test before release
83
+
84
+ That keeps the package focused on the real consumer path: install the module, register it in `nuxt.config.ts`, and call the exposed Nitro routes or injected client.
85
+
86
+ ## Install
87
+
88
+ ```bash
89
+ pnpm add nuxt-edge-ai
90
+ ```
91
+
92
+ ```ts
93
+ // nuxt.config.ts
94
+ export default defineNuxtConfig({
95
+ modules: ['nuxt-edge-ai'],
96
+ edgeAI: {
97
+ provider: 'local',
98
+ cacheDir: './.cache/nuxt-edge-ai',
99
+ preset: 'distilgpt2',
100
+ remote: {
101
+ enabled: true,
102
+ fallback: true,
103
+ baseUrl: 'https://api.openai.com/v1',
104
+ apiKey: process.env.OPENAI_API_KEY,
105
+ model: 'gpt-4o-mini',
106
+ },
107
+ },
108
+ })
109
+ ```
110
+
111
+ ```vue
112
+ <script setup lang="ts">
113
+ const edgeAI = useEdgeAI()
114
+
115
+ await edgeAI.pull()
116
+
117
+ const completion = await edgeAI.client.chat.completions.create({
118
+ model: edgeAI.defaultModel,
119
+ messages: [
120
+ {
121
+ role: 'user',
122
+ content: 'Write a pitch for a local-first Nuxt AI module.',
123
+ },
124
+ ],
125
+ })
126
+
127
+ const text = String(completion.choices[0]?.message.content ?? '')
128
+ </script>
129
+ ```
130
+
131
+ If you prefer the lower-level route wrapper, `useEdgeAI().chatCompletions()` accepts the same OpenAI-style payload shape.
132
+
133
+ ## Configuration
134
+
135
+ Top-level module options:
136
+
137
+ | Option | Type | Default | Notes |
138
+ | --- | --- | --- | --- |
139
+ | `routeBase` | `string` | `/api/edge-ai` | Base path for module endpoints |
140
+ | `provider` | `'local' \| 'remote' \| 'mock'` | `local` | Runtime backend selector |
141
+ | `runtime` | `'transformers-wasm' \| 'mock'` | legacy | Backward-compatible alias for older configs |
142
+ | `cacheDir` | `string` | `./.cache/nuxt-edge-ai` | Cache and model asset directory |
143
+ | `warmup` | `boolean` | `false` | Warm the runtime on health checks |
144
+ | `preset` | `string` | `distilgpt2` | Local model preset |
145
+ | `presets` | `Record<string, ...>` | `undefined` | Register additional local presets |
146
+ | `model` | `object` | see below | Override the local model preset |
147
+ | `remote` | `object` | see below | Remote provider and fallback settings |
148
+
149
+ Local model options:
150
+
151
+ | Option | Type | Default | Notes |
152
+ | --- | --- | --- | --- |
153
+ | `id` | `string` | `Xenova/distilgpt2` | Model identifier used when no local path is set |
154
+ | `task` | `'text-generation'` | `text-generation` | Current supported task |
155
+ | `localPath` | `string \| undefined` | `undefined` | Local model directory |
156
+ | `allowRemote` | `boolean` | `true` | Allow first-run download from remote model source |
157
+ | `dtype` | `string \| undefined` | `q8` | Runtime dtype passed to Transformers.js |
158
+ | `generation.maxNewTokens` | `number` | `96` | Max generated tokens |
159
+ | `generation.temperature` | `number` | `0.7` | Sampling temperature |
160
+ | `generation.topP` | `number` | `0.9` | Top-p sampling |
161
+ | `generation.doSample` | `boolean` | `true` | Enable sampling |
162
+ | `generation.repetitionPenalty` | `number` | `1.05` | Repetition penalty |
163
+
164
+ Remote provider options:
165
+
166
+ | Option | Type | Default | Notes |
167
+ | --- | --- | --- | --- |
168
+ | `enabled` | `boolean` | `false` | Enable remote provider settings |
169
+ | `fallback` | `boolean` | `true` | Fall back to remote if local pull/generate fails |
170
+ | `baseUrl` | `string` | `https://api.openai.com/v1` | Remote API base URL |
171
+ | `path` | `string` | `/chat/completions` | OpenAI-compatible endpoint path |
172
+ | `model` | `string` | `gpt-4o-mini` | Default remote model ID |
173
+ | `apiKey` | `string \| undefined` | `undefined` | Inline API key |
174
+ | `headers` | `Record<string, string> \| undefined` | `undefined` | Extra request headers |
175
+ | `systemPrompt` | `string \| undefined` | `undefined` | Optional system instruction |
176
+
177
+ ## Provider examples
178
+
179
+ Local-only mode:
180
+
181
+ ```ts
182
+ export default defineNuxtConfig({
183
+ modules: ['nuxt-edge-ai'],
184
+ edgeAI: {
185
+ provider: 'local',
186
+ preset: 'distilgpt2',
187
+ remote: {
188
+ enabled: false,
189
+ },
190
+ },
191
+ })
192
+ ```
193
+
194
+ Local with automatic remote fallback:
195
+
196
+ ```ts
197
+ export default defineNuxtConfig({
198
+ modules: ['nuxt-edge-ai'],
199
+ edgeAI: {
200
+ provider: 'local',
201
+ preset: 'distilgpt2',
202
+ remote: {
203
+ enabled: true,
204
+ fallback: true,
205
+ baseUrl: 'https://api.openai.com/v1',
206
+ apiKey: process.env.OPENAI_API_KEY,
207
+ model: 'gpt-4o-mini',
208
+ },
209
+ },
210
+ })
211
+ ```
212
+
213
+ Custom preset registration:
214
+
215
+ ```ts
216
+ export default defineNuxtConfig({
217
+ modules: ['nuxt-edge-ai'],
218
+ edgeAI: {
219
+ presets: {
220
+ 'team-default': {
221
+ label: 'Team Default',
222
+ description: 'Project-specific local preset',
223
+ model: {
224
+ id: 'Xenova/distilgpt2',
225
+ dtype: 'q8',
226
+ generation: {
227
+ maxNewTokens: 120,
228
+ },
229
+ },
230
+ },
231
+ },
232
+ preset: 'team-default',
233
+ },
234
+ })
235
+ ```
236
+
237
+ ## Consumer runtime guarantees
238
+
239
+ Consumers do not need to install:
240
+
241
+ - Ollama
242
+ - Rust
243
+ - C++
244
+ - Python
245
+ - `llama.cpp`
246
+ - extra runtime npm packages beyond this module
247
+
248
+ What consumers do need:
249
+
250
+ - a Node/Nitro server runtime
251
+ - a model path or permission to download a compatible model
252
+
253
+ ## API surface
254
+
255
+ - `GET /api/edge-ai/health`
256
+ - `POST /api/edge-ai/pull`
257
+ - `POST /api/edge-ai/generate`
258
+ - `POST /api/edge-ai/chat/completions`
259
+ - `useEdgeAI().health()`
260
+ - `useEdgeAI().pull()`
261
+ - `useEdgeAI().chatCompletions()`
262
+ - `useEdgeAI().client.chat.completions.create()`
263
+
264
+ Health responses also expose:
265
+
266
+ - `provider`
267
+ - `presets`
268
+ - `remoteFallback`
269
+ - `engine.ready`
270
+ - `engine.lastError`
271
+
272
+ ## OpenAI-compatible chat completions
273
+
274
+ You can either point the official OpenAI client at the module's Nitro route, or use the package's own `EdgeAI` client with the same calling style.
275
+
276
+ Using `EdgeAI` directly:
277
+
278
+ ```ts
279
+ import { EdgeAI } from 'nuxt-edge-ai'
280
+
281
+ const client = new EdgeAI({
282
+ baseURL: 'http://localhost:3000/api/edge-ai',
283
+ })
284
+
285
+ const response = await client.chat.completions.create({
286
+ model: 'openai/gpt-oss-20b:free',
287
+ messages: [
288
+ {
289
+ role: 'user',
290
+ content: "How many r's are in strawberry?",
291
+ },
292
+ ],
293
+ reasoning: { enabled: true },
294
+ })
295
+ ```
296
+
297
+ Using `useEdgeAI()` inside a Nuxt app with the same calling style:
298
+
299
+ ```ts
300
+ const edgeAI = useEdgeAI()
301
+
302
+ await edgeAI.pull()
303
+
304
+ const response = await edgeAI.client.chat.completions.create({
305
+ model: edgeAI.defaultModel,
306
+ messages: [
307
+ {
308
+ role: 'user',
309
+ content: 'Summarize the module in one sentence.',
310
+ },
311
+ ],
312
+ })
313
+ ```
314
+
315
+ Using the OpenAI SDK against the same route:
316
+
317
+ ```ts
318
+ import OpenAI from 'openai'
319
+
320
+ const client = new OpenAI({
321
+ baseURL: 'http://localhost:3000/api/edge-ai',
322
+ apiKey: 'local-dev-token',
323
+ })
324
+
325
+ const response = await client.chat.completions.create({
326
+ model: 'openai/gpt-oss-20b:free',
327
+ messages: [
328
+ {
329
+ role: 'user',
330
+ content: "How many r's are in strawberry?",
331
+ },
332
+ ],
333
+ reasoning: { enabled: true },
334
+ })
335
+ ```
336
+
337
+ If you want to call the route wrapper directly, `useEdgeAI().chatCompletions(...)` maps to the same `/chat/completions` endpoint.
338
+
339
+ ## Streaming chat completions
340
+
341
+ The module now supports real-time streaming responses with Server-Sent Events (SSE). This enables the typewriter effect that modern AI applications expect.
342
+
343
+ ### Using `useEdgeAI()` with streaming
344
+
345
+ ```vue
346
+ <script setup lang="ts">
347
+ const edgeAI = useEdgeAI()
348
+ const messages = ref<Array<{ role: 'user' | 'assistant', content: string }>>([])
349
+ const input = ref('')
350
+
351
+ async function handleSubmit() {
352
+ const text = input.value.trim()
353
+ if (!text) return
354
+
355
+ // Add user message
356
+ messages.value.push({ role: 'user', content: text })
357
+ input.value = ''
358
+
359
+ // Add placeholder for assistant response
360
+ messages.value.push({ role: 'assistant', content: '' })
361
+
362
+ // Stream the response
363
+ try {
364
+ for await (const token of edgeAI.streamChatCompletionsGenerator({
365
+ model: edgeAI.defaultModel,
366
+ messages: messages.value.slice(0, -1),
367
+ stream: true,
368
+ })) {
369
+ // Update the last message with each token
370
+ const lastMessage = messages.value[messages.value.length - 1]
371
+ lastMessage.content += token
372
+ }
373
+ }
374
+ catch (error) {
375
+ console.error('Stream error:', error)
376
+ }
377
+ }
378
+
379
+ // Stop streaming if needed
380
+ function stop() {
381
+ edgeAI.stop()
382
+ }
383
+ </script>
384
+ ```
385
+
386
+ ### Using the EdgeAI client with streaming
387
+
388
+ ```ts
389
+ import { EdgeAI } from 'nuxt-edge-ai'
390
+
391
+ const client = new EdgeAI({
392
+ baseURL: 'http://localhost:3000/api/edge-ai',
393
+ })
394
+
395
+ // Stream with callbacks
396
+ await client.chat.completions.stream(
397
+ {
398
+ model: 'distilgpt2',
399
+ messages: [{ role: 'user', content: 'Hello!' }],
400
+ stream: true,
401
+ },
402
+ {
403
+ onToken: (token) => console.log(token),
404
+ onCompletion: (text) => console.log('Done:', text),
405
+ onError: (error) => console.error(error),
406
+ }
407
+ )
408
+
409
+ // Or use async generator
410
+ for await (const token of client.streamChatCompletionGenerator({
411
+ model: 'distilgpt2',
412
+ messages: [{ role: 'user', content: 'Hello!' }],
413
+ stream: true,
414
+ })) {
415
+ console.log(token)
416
+ }
417
+ ```
418
+
419
+ ### Compatible with `@ai-sdk/vue`
420
+
421
+ The streaming protocol is compatible with Vercel AI SDK's `useChat()` composable. You can use `@ai-sdk/vue` with this module:
422
+
423
+ ```ts
424
+ import { useChat } from '@ai-sdk/vue'
425
+
426
+ const { messages, input, handleSubmit, isLoading, stop } = useChat({
427
+ api: '/api/edge-ai/chat/completions',
428
+ })
429
+ ```
430
+
431
+ When the module is using a remote OpenAI-compatible backend, it forwards `messages`, `reasoning`, and any extra `remoteBody` fields. If the upstream provider returns `reasoning_details`, the module preserves them on `choices[0].message`.
432
+
433
+ Example OpenRouter-style config:
434
+
435
+ ```ts
436
+ export default defineNuxtConfig({
437
+ modules: ['nuxt-edge-ai'],
438
+ edgeAI: {
439
+ provider: 'remote',
440
+ remote: {
441
+ enabled: true,
442
+ baseUrl: 'https://openrouter.ai/api/v1',
443
+ apiKey: process.env.OPENROUTER_API_KEY,
444
+ model: 'openai/gpt-oss-20b:free',
445
+ },
446
+ },
447
+ })
448
+ ```
449
+
450
+ ## Troubleshooting
451
+
452
+ Common checks:
453
+
454
+ - Run `POST /api/edge-ai/health` first to confirm route wiring and runtime config.
455
+ - Use `provider: 'mock'` to separate module wiring issues from model/runtime issues.
456
+ - Remote fallback requires `edgeAI.remote.enabled: true` plus `edgeAI.remote.apiKey`.
457
+ - If `pull` fails, inspect server logs first. Most early failures are model-path or packaged-runtime issues.
458
+ - After changing vendored runtime files, always run `pnpm prepack` before validating a published-style install.
459
+
460
+ ## Known limitations
461
+
462
+ - The local provider currently targets `text-generation` only.
463
+ - The local WASM runtime is designed for a Node/Nitro server process, not edge-worker runtimes.
464
+ - Model quality and latency depend heavily on the selected preset or upstream remote model.
465
+ - Model weights are not bundled in the npm package; local-first usage still requires either a local model path or a first-run download.
466
+
467
+ ## Local development
468
+
469
+ ```bash
470
+ pnpm install
471
+ pnpm dev
472
+ ```
473
+
474
+ Useful commands:
475
+
476
+ ```bash
477
+ pnpm vendor:runtime
478
+ pnpm lint
479
+ pnpm test
480
+ pnpm test:types
481
+ pnpm prepack
482
+ ```
483
+
484
+ ## Docs
485
+
486
+ See [`docs/index.md`](./docs/index.md) for the project docs tree.
487
+
488
+ Key docs:
489
+
490
+ - [`docs/getting-started.md`](./docs/getting-started.md)
491
+ - [`docs/api.md`](./docs/api.md)
492
+ - [`docs/models.md`](./docs/models.md)
493
+ - [`docs/architecture.md`](./docs/architecture.md)
494
+ - [`docs/third-party.md`](./docs/third-party.md)
495
+
496
+ ## Repository shape
497
+
498
+ - `src/module.ts`: module entry and runtime config wiring
499
+ - `src/runtime/`: composables, plugin, and Nitro runtime code
500
+ - `playground/`: interactive demo app
501
+ - `test/fixtures/`: module consumer fixtures
502
+ - `docs/`: module documentation
503
+ - `scripts/vendor-runtime.mjs`: vendored runtime generation
504
+
505
+ ## Status
506
+
507
+ This module is ready for community consumption, but it is still intentionally scoped. The stable contract today is a Nuxt module that exposes one AI surface across three execution modes: `local`, `remote`, and `mock`.