@soulcraft/sdk 2.6.1 → 3.0.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.
Files changed (59) hide show
  1. package/dist/modules/auth/backchannel.d.ts +13 -39
  2. package/dist/modules/auth/backchannel.d.ts.map +1 -1
  3. package/dist/modules/auth/backchannel.js +12 -144
  4. package/dist/modules/auth/backchannel.js.map +1 -1
  5. package/dist/modules/auth/middleware.d.ts +45 -157
  6. package/dist/modules/auth/middleware.d.ts.map +1 -1
  7. package/dist/modules/auth/middleware.js +40 -322
  8. package/dist/modules/auth/middleware.js.map +1 -1
  9. package/dist/modules/auth/products.d.ts +1 -1
  10. package/dist/modules/auth/products.js +1 -1
  11. package/dist/modules/auth/request-backchannel.d.ts +94 -0
  12. package/dist/modules/auth/request-backchannel.d.ts.map +1 -0
  13. package/dist/modules/auth/request-backchannel.js +206 -0
  14. package/dist/modules/auth/request-backchannel.js.map +1 -0
  15. package/dist/modules/auth/request-middleware.d.ts +438 -0
  16. package/dist/modules/auth/request-middleware.d.ts.map +1 -0
  17. package/dist/modules/auth/request-middleware.js +650 -0
  18. package/dist/modules/auth/request-middleware.js.map +1 -0
  19. package/dist/modules/auth/service-token.d.ts +8 -7
  20. package/dist/modules/auth/service-token.d.ts.map +1 -1
  21. package/dist/modules/auth/service-token.js +8 -7
  22. package/dist/modules/auth/service-token.js.map +1 -1
  23. package/dist/modules/auth/sveltekit.d.ts +1 -1
  24. package/dist/modules/auth/sveltekit.d.ts.map +1 -1
  25. package/dist/modules/auth/sveltekit.js +1 -1
  26. package/dist/modules/auth/sveltekit.js.map +1 -1
  27. package/dist/namespaces.d.ts +1 -1
  28. package/dist/server/handlers/export.js +1 -1
  29. package/dist/server/handlers/export.js.map +1 -1
  30. package/dist/server/handlers/workspace.d.ts +1 -1
  31. package/dist/server/handlers/workspace.d.ts.map +1 -1
  32. package/dist/server/handlers/workspace.js +3 -4
  33. package/dist/server/handlers/workspace.js.map +1 -1
  34. package/dist/server/index.d.ts +5 -12
  35. package/dist/server/index.d.ts.map +1 -1
  36. package/dist/server/index.js +4 -9
  37. package/dist/server/index.js.map +1 -1
  38. package/dist/server/instance-pool.d.ts.map +1 -1
  39. package/dist/server/instance-pool.js +1 -0
  40. package/dist/server/instance-pool.js.map +1 -1
  41. package/dist/server/namespace-router.d.ts +1 -1
  42. package/dist/server/namespace-router.js +1 -1
  43. package/dist/server/rpc-handler.d.ts +2 -9
  44. package/dist/server/rpc-handler.d.ts.map +1 -1
  45. package/dist/server/rpc-handler.js +2 -9
  46. package/dist/server/rpc-handler.js.map +1 -1
  47. package/docs/ADR-001-sdk-design.md +3 -3
  48. package/docs/ADR-004-product-registry.md +1 -1
  49. package/docs/ADR-005-hall-integration.md +1 -1
  50. package/docs/ADR-006-rpc-cache.md +2 -2
  51. package/docs/IMPLEMENTATION-PLAN.md +7 -7
  52. package/docs/KIT-APP-GUIDE.md +100 -99
  53. package/docs/USAGE.md +30 -40
  54. package/docs/kit-sdk-guide.md +59 -60
  55. package/package.json +2 -7
  56. package/dist/server/hono-router.d.ts +0 -70
  57. package/dist/server/hono-router.d.ts.map +0 -1
  58. package/dist/server/hono-router.js +0 -167
  59. package/dist/server/hono-router.js.map +0 -1
@@ -1,70 +0,0 @@
1
- /**
2
- * @module server/hono-router
3
- * @description Hono integration factory for the Soulcraft SDK namespace router.
4
- *
5
- * Creates a Hono app with the unified RPC endpoint:
6
- *
7
- * - `POST /api/rpc` — Unified namespace RPC endpoint. Accepts the
8
- * {@link SoulcraftRPC} envelope and dispatches via the namespace router.
9
- * Streaming responses are delivered as SSE (`text/event-stream`).
10
- *
11
- * Products mount this router in their Hono app with `app.route('', sdkRouter)`.
12
- *
13
- * @example Workshop server-hono.ts
14
- * ```typescript
15
- * import { createSoulcraftRouter } from '@soulcraft/sdk/server'
16
- *
17
- * const sdkRouter = createSoulcraftRouter({
18
- * resolveBrain: async (ctx) => getUserBrainy(ctx.user!.emailHash, ctx.workspaceId),
19
- * authenticate: async (ctx) => verifySession(ctx.request),
20
- * providers: {
21
- * chat: chatHandler,
22
- * graph: graphHandler,
23
- * search: searchHandler,
24
- * },
25
- * })
26
- *
27
- * app.route('', sdkRouter)
28
- * ```
29
- */
30
- import { Hono } from 'hono';
31
- import type { NamespaceRouterConfig } from './namespace-router.js';
32
- import type { RpcCacheConfig } from './rpc-cache.js';
33
- /**
34
- * Configuration for {@link createSoulcraftRouter}.
35
- *
36
- * Extends {@link NamespaceRouterConfig} with optional RPC response caching.
37
- */
38
- export interface SoulcraftRouterConfig extends NamespaceRouterConfig {
39
- /**
40
- * Optional RPC response cache configuration.
41
- *
42
- * When provided, cacheable read methods are served from an in-process LRU cache
43
- * with singleflight deduplication. Write methods flush the scope automatically.
44
- *
45
- * @see {@link RpcCacheConfig}
46
- */
47
- cache?: RpcCacheConfig;
48
- }
49
- /**
50
- * Creates a Hono app with the unified Soulcraft RPC routes.
51
- *
52
- * The returned Hono app should be mounted at the root of your product server
53
- * (not under a prefix) since it defines its own `/api/rpc` path.
54
- *
55
- * @param config - Namespace router configuration.
56
- * @returns A Hono app ready to be mounted with `app.route('', sdkRouter)`.
57
- *
58
- * @example
59
- * ```typescript
60
- * const sdkRouter = createSoulcraftRouter({
61
- * resolveBrain: async (ctx) => pool.forUser(ctx.user!.emailHash, ctx.workspaceId),
62
- * authenticate: async (ctx) => verifySession(ctx.request),
63
- * providers: { chat: chatHandler },
64
- * })
65
- *
66
- * app.route('', sdkRouter)
67
- * ```
68
- */
69
- export declare function createSoulcraftRouter(config: SoulcraftRouterConfig): Hono;
70
- //# sourceMappingURL=hono-router.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hono-router.d.ts","sourceRoot":"","sources":["../../src/server/hono-router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAG3B,OAAO,KAAK,EAAE,qBAAqB,EAAkB,MAAM,uBAAuB,CAAA;AAClF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAGpD;;;;GAIG;AACH,MAAM,WAAW,qBAAsB,SAAQ,qBAAqB;IAClE;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,cAAc,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAyCzE"}
@@ -1,167 +0,0 @@
1
- /**
2
- * @module server/hono-router
3
- * @description Hono integration factory for the Soulcraft SDK namespace router.
4
- *
5
- * Creates a Hono app with the unified RPC endpoint:
6
- *
7
- * - `POST /api/rpc` — Unified namespace RPC endpoint. Accepts the
8
- * {@link SoulcraftRPC} envelope and dispatches via the namespace router.
9
- * Streaming responses are delivered as SSE (`text/event-stream`).
10
- *
11
- * Products mount this router in their Hono app with `app.route('', sdkRouter)`.
12
- *
13
- * @example Workshop server-hono.ts
14
- * ```typescript
15
- * import { createSoulcraftRouter } from '@soulcraft/sdk/server'
16
- *
17
- * const sdkRouter = createSoulcraftRouter({
18
- * resolveBrain: async (ctx) => getUserBrainy(ctx.user!.emailHash, ctx.workspaceId),
19
- * authenticate: async (ctx) => verifySession(ctx.request),
20
- * providers: {
21
- * chat: chatHandler,
22
- * graph: graphHandler,
23
- * search: searchHandler,
24
- * },
25
- * })
26
- *
27
- * app.route('', sdkRouter)
28
- * ```
29
- */
30
- import { Hono } from 'hono';
31
- import { createNamespaceRouter } from './namespace-router.js';
32
- import { createCachedDispatch } from './rpc-cache.js';
33
- /**
34
- * Creates a Hono app with the unified Soulcraft RPC routes.
35
- *
36
- * The returned Hono app should be mounted at the root of your product server
37
- * (not under a prefix) since it defines its own `/api/rpc` path.
38
- *
39
- * @param config - Namespace router configuration.
40
- * @returns A Hono app ready to be mounted with `app.route('', sdkRouter)`.
41
- *
42
- * @example
43
- * ```typescript
44
- * const sdkRouter = createSoulcraftRouter({
45
- * resolveBrain: async (ctx) => pool.forUser(ctx.user!.emailHash, ctx.workspaceId),
46
- * authenticate: async (ctx) => verifySession(ctx.request),
47
- * providers: { chat: chatHandler },
48
- * })
49
- *
50
- * app.route('', sdkRouter)
51
- * ```
52
- */
53
- export function createSoulcraftRouter(config) {
54
- const baseRouter = createNamespaceRouter(config);
55
- const router = config.cache
56
- ? createCachedDispatch(baseRouter, config.cache)
57
- : baseRouter;
58
- const app = new Hono();
59
- // ── POST /api/rpc — Unified namespace RPC ────────────────────────────────
60
- app.post('/api/rpc', async (c) => {
61
- let body;
62
- try {
63
- body = await c.req.json();
64
- }
65
- catch {
66
- return c.json({ error: { code: 'BAD_REQUEST', message: 'Request body must be valid JSON' } }, 400);
67
- }
68
- // Validate required fields.
69
- if (typeof body.ns !== 'string' || typeof body.method !== 'string' || !Array.isArray(body.args)) {
70
- return c.json({ error: { code: 'BAD_REQUEST', message: '{ ns: string, method: string, args: unknown[] } required' } }, 400);
71
- }
72
- const rpc = {
73
- id: typeof body.id === 'string' ? body.id : crypto.randomUUID(),
74
- ns: body.ns,
75
- method: body.method,
76
- args: body.args,
77
- stream: body.stream ?? false,
78
- };
79
- const result = await router.dispatch(rpc, c.req.raw);
80
- return _sendResult(c, result);
81
- });
82
- return app;
83
- }
84
- // ─────────────────────────────────────────────────────────────────────────────
85
- // Response helpers
86
- // ─────────────────────────────────────────────────────────────────────────────
87
- /**
88
- * Converts a {@link DispatchResult} into a Hono Response.
89
- *
90
- * For `response` results, returns JSON.
91
- * For `stream` results, returns an SSE stream.
92
- *
93
- * @param c - Hono context.
94
- * @param result - The dispatch result from the namespace router.
95
- * @returns A Response.
96
- */
97
- function _sendResult(c, result) {
98
- if (result.type === 'response') {
99
- const { response } = result;
100
- const status = response.error ? _errorStatusCode(response.error.code) : 200;
101
- const headers = { 'Content-Type': 'application/json' };
102
- // Merge cache headers (Cache-Control, ETag, X-Cache) when present.
103
- if (result.headers) {
104
- for (const [key, value] of Object.entries(result.headers)) {
105
- headers[key] = value;
106
- }
107
- }
108
- return new Response(JSON.stringify(response), { status, headers });
109
- }
110
- // Streaming result — deliver as SSE.
111
- const { id, iterable } = result;
112
- const stream = new ReadableStream({
113
- async start(controller) {
114
- const encoder = new TextEncoder();
115
- try {
116
- for await (const chunk of iterable) {
117
- const frame = { id, chunk };
118
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(frame)}\n\n`));
119
- }
120
- // Send done signal.
121
- const doneFrame = { id, done: true };
122
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(doneFrame)}\n\n`));
123
- }
124
- catch (err) {
125
- const message = err instanceof Error ? err.message : String(err);
126
- const errorFrame = { id, error: message };
127
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(errorFrame)}\n\n`));
128
- }
129
- finally {
130
- controller.close();
131
- }
132
- },
133
- });
134
- return new Response(stream, {
135
- status: 200,
136
- headers: {
137
- 'Content-Type': 'text/event-stream',
138
- 'Cache-Control': 'no-cache',
139
- 'Connection': 'keep-alive',
140
- },
141
- });
142
- }
143
- /**
144
- * Maps error codes to HTTP status codes for JSON responses.
145
- *
146
- * @param code - The error code from the RPC response.
147
- * @returns The appropriate HTTP status code.
148
- */
149
- function _errorStatusCode(code) {
150
- switch (code) {
151
- case 'UNAUTHORIZED':
152
- case 'AUTH_ERROR':
153
- return 401;
154
- case 'FORBIDDEN':
155
- return 403;
156
- case 'NAMESPACE_NOT_FOUND':
157
- case 'METHOD_NOT_FOUND':
158
- return 404;
159
- case 'BRAIN_UNAVAILABLE':
160
- return 503;
161
- case 'BAD_REQUEST':
162
- return 400;
163
- default:
164
- return 500;
165
- }
166
- }
167
- //# sourceMappingURL=hono-router.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hono-router.js","sourceRoot":"","sources":["../../src/server/hono-router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAC3B,OAAO,EAAE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