@kuratchi/js 0.0.9 → 0.0.10

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
@@ -23,7 +23,7 @@ bun run dev
23
23
  | File | Purpose |
24
24
  |---|---|
25
25
  | `.kuratchi/routes.js` | Compiled routes, actions, RPC handlers, and render functions |
26
- | `.kuratchi/worker.js` | Stable wrangler entry - re-exports the fetch handler and all Durable Object classes |
26
+ | `.kuratchi/worker.js` | Stable wrangler entry - re-exports the fetch handler plus all Durable Object and Agent classes |
27
27
  | `.kuratchi/do/*.js` | Generated Durable Object RPC proxy modules for `$durable-objects/*` imports |
28
28
 
29
29
  Point wrangler at the entry and you're done. **No `src/index.ts` needed.**
@@ -211,7 +211,7 @@ export async function addItem(formData: FormData): Promise<void> {
211
211
 
212
212
  ### Redirect after action
213
213
 
214
- Call `redirect()` inside an action to send the user to a different URL after the POST:
214
+ Call `redirect()` inside an action or `load()` to immediately exit and send the user to a different URL. `throw redirect()` also works, but is redundant because `redirect()` already throws:
215
215
 
216
216
  ```ts
217
217
  import { redirect } from '@kuratchi/js';
@@ -470,6 +470,44 @@ Declare it in `kuratchi.config.ts` and in `wrangler.jsonc`. The compiler exports
470
470
  ]
471
471
  }
472
472
  ```
473
+
474
+ ## Agents
475
+
476
+ Kuratchi treats `src/server/**/*.agent.ts` as a first-class Worker export convention.
477
+
478
+ - Any `.agent.ts` file under `src/server/` is scanned during build.
479
+ - The file must export a class with either `export class MyAgent` or `export default class MyAgent`.
480
+ - The compiler re-exports that class from `.kuratchi/worker.js`, so Wrangler can bind it directly.
481
+ - `.agent.ts` files are not route modules and are not converted into `$durable-objects/*` RPC proxies.
482
+
483
+ ```ts
484
+ // src/server/ai/session.agent.ts
485
+ import { Agent } from 'agents';
486
+
487
+ export class SessionAgent extends Agent {
488
+ async onRequest() {
489
+ return Response.json({ ok: true });
490
+ }
491
+ }
492
+ ```
493
+
494
+ ```jsonc
495
+ // wrangler.jsonc
496
+ {
497
+ "durable_objects": {
498
+ "bindings": [{ "name": "AI_SESSION", "class_name": "SessionAgent" }]
499
+ },
500
+ "migrations": [
501
+ { "tag": "v1", "new_sqlite_classes": ["SessionAgent"] }
502
+ ]
503
+ }
504
+ ```
505
+
506
+ Failure and edge behavior:
507
+
508
+ - If a `.agent.ts` file does not export a class, the build fails.
509
+ - Kuratchi only auto-discovers `.agent.ts` files under `src/server/`.
510
+ - You still need Wrangler Durable Object bindings and migrations because Agents run as Durable Objects.
473
511
  ## Runtime APIs
474
512
 
475
513
  These are available anywhere in server-side route code:
@@ -481,11 +519,29 @@ import {
481
519
  getLocals, // mutable locals bag for the current request
482
520
  getParams, // URL params ({ slug: 'foo' })
483
521
  getParam, // getParam('slug')
484
- redirect, // redirect('/path', 302)
522
+ RedirectError, // redirect signal thrown by redirect()\r\n redirect, // redirect('/path', 302)\r\n goto, // same as redirect()
485
523
  goto, // same as redirect — alias
486
524
  } from '@kuratchi/js';
487
525
  ```
488
526
 
527
+ ### Request helpers
528
+
529
+ For a batteries-included request layer, import pre-parsed request state from `@kuratchi/js/request`:
530
+
531
+ ```ts
532
+ import { url, pathname, searchParams, slug } from '@kuratchi/js/request';
533
+
534
+ const page = pathname;
535
+ const tab = searchParams.get('tab');
536
+ const postSlug = slug;
537
+ ```
538
+
539
+ - `url` is the parsed `URL` for the current request.
540
+ - `pathname` is the full path, like `/blog/hello-world`.
541
+ - `searchParams` is `url.searchParams` for the current request.
542
+ - `slug` is `params.slug` when the matched route defines a `slug` param.
543
+ - `headers`, `method`, and `params` are also exported from `@kuratchi/js/request`.
544
+ - Use `getRequest()` when you want the raw native `Request` object.
489
545
  ## Environment bindings
490
546
 
491
547
  Cloudflare env is server-only.
package/dist/cli.js CHANGED
@@ -123,11 +123,19 @@ async function startWranglerDev() {
123
123
  wranglerArgs.push('--port', String(port));
124
124
  console.log(`[kuratchi] Starting wrangler dev on port ${port}`);
125
125
  }
126
- const wrangler = spawn('npx', wranglerArgs, {
127
- cwd: projectDir,
128
- stdio: 'inherit',
129
- shell: process.platform === 'win32',
130
- });
126
+ // Use 'pipe' for stdin so wrangler doesn't detect stdin EOF and exit
127
+ // prematurely when launched via a script runner (e.g. `bun run dev`).
128
+ const isWin = process.platform === 'win32';
129
+ const wrangler = isWin
130
+ ? spawn('npx ' + wranglerArgs.join(' '), {
131
+ cwd: projectDir,
132
+ stdio: ['pipe', 'inherit', 'inherit'],
133
+ shell: true,
134
+ })
135
+ : spawn('npx', wranglerArgs, {
136
+ cwd: projectDir,
137
+ stdio: ['pipe', 'inherit', 'inherit'],
138
+ });
131
139
  const cleanup = () => {
132
140
  if (!wrangler.killed)
133
141
  wrangler.kill();
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Compiler â€" scans a project's routes/ directory, parses .html files,
2
+ * Compiler " scans a project's routes/ directory, parses .html files,
3
3
  * and generates a single Worker entry point.
4
4
  */
5
5
  export { parseFile } from './parser.js';
@@ -27,9 +27,9 @@ export interface CompiledRoute {
27
27
  /**
28
28
  * Compile a project's src/routes/ into .kuratchi/routes.js
29
29
  *
30
- * The generated module exports { app } â€" an object with a fetch() method
30
+ * The generated module exports { app } " an object with a fetch() method
31
31
  * that handles routing, load functions, form actions, and rendering.
32
- * Returns the path to .kuratchi/worker.js the stable wrangler entry point that
32
+ * Returns the path to .kuratchi/worker.js the stable wrangler entry point that
33
33
  * re-exports everything from routes.js (default fetch handler + named DO class exports).
34
34
  * No src/index.ts is needed in user projects.
35
35
  */