@scalemule/nextjs 0.1.68 → 0.1.70
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/ai-Ccz-erh_.d.mts +253 -0
- package/dist/ai-Ccz-erh_.d.ts +253 -0
- package/dist/server/ai.d.mts +2 -0
- package/dist/server/ai.d.ts +2 -0
- package/dist/server/ai.js +371 -0
- package/dist/server/ai.mjs +367 -0
- package/dist/server/index.d.mts +79 -16
- package/dist/server/index.d.ts +79 -16
- package/dist/server/index.js +118 -4
- package/dist/server/index.mjs +117 -5
- package/package.json +7 -1
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { AiTransport, ScaleMuleAi } from '@scalemule/ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cookie Utilities for Secure Session Management
|
|
5
|
+
*
|
|
6
|
+
* Handles HTTP-only secure cookies for authentication.
|
|
7
|
+
* Tokens are never exposed to the browser.
|
|
8
|
+
*/
|
|
9
|
+
declare const SESSION_COOKIE_NAME = "sm_session";
|
|
10
|
+
declare const USER_ID_COOKIE_NAME = "sm_user_id";
|
|
11
|
+
/**
|
|
12
|
+
* Known accounts cookie — stores display metadata (email, name, avatar) for
|
|
13
|
+
* accounts that have logged in on this device. NOT httpOnly so client JS can
|
|
14
|
+
* read it to render the account switcher UI. Contains NO tokens or secrets.
|
|
15
|
+
*/
|
|
16
|
+
declare const KNOWN_ACCOUNTS_COOKIE_NAME = "sm_known_accounts";
|
|
17
|
+
interface SessionCookieOptions {
|
|
18
|
+
/** Isolate an embedded session in the top-level site cookie partition (CHIPS). */
|
|
19
|
+
partitioned?: boolean;
|
|
20
|
+
/** Cookie max age in seconds (default: 7 days) */
|
|
21
|
+
maxAge?: number;
|
|
22
|
+
/** Cookie domain (default: current domain) */
|
|
23
|
+
domain?: string;
|
|
24
|
+
/** Cookie path (default: '/') */
|
|
25
|
+
path?: string;
|
|
26
|
+
/** SameSite attribute (default: 'lax') */
|
|
27
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
28
|
+
/** Whether to use secure cookies (default: true in production) */
|
|
29
|
+
secure?: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface SessionData {
|
|
32
|
+
sessionToken: string;
|
|
33
|
+
userId: string;
|
|
34
|
+
expiresAt: Date;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a Response with session cookies set
|
|
38
|
+
*
|
|
39
|
+
* Use this after successful login to set HTTP-only cookies and return user data.
|
|
40
|
+
* The session token is stored in cookies, never sent to the browser in JSON.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const result = await sm.auth.login({ email, password })
|
|
45
|
+
* if (result.success) {
|
|
46
|
+
* return withSession(result.data, { user: result.data.user })
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function withSession<T extends Record<string, unknown>>(loginResponse: {
|
|
51
|
+
session_token: string;
|
|
52
|
+
user: {
|
|
53
|
+
id: string;
|
|
54
|
+
};
|
|
55
|
+
}, responseBody: T, options?: SessionCookieOptions): Response;
|
|
56
|
+
/**
|
|
57
|
+
* Create a Response that clears session cookies
|
|
58
|
+
*
|
|
59
|
+
* Use this after logout to clear HTTP-only cookies.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* await sm.auth.logout(sessionToken)
|
|
64
|
+
* return clearSession({ message: 'Logged out' })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function clearSession<T extends Record<string, unknown>>(responseBody: T, options?: SessionCookieOptions, status?: number): Response;
|
|
68
|
+
/**
|
|
69
|
+
* Get session data from request cookies
|
|
70
|
+
*
|
|
71
|
+
* Use this in API routes to get the current session.
|
|
72
|
+
* Returns null if no valid session cookie exists.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const session = await getSession()
|
|
77
|
+
* if (!session) {
|
|
78
|
+
* return Response.json({ error: 'Not authenticated' }, { status: 401 })
|
|
79
|
+
* }
|
|
80
|
+
* const user = await sm.auth.me(session.sessionToken)
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function getSession(options?: {
|
|
84
|
+
allowBearer?: boolean;
|
|
85
|
+
}): Promise<SessionData | null>;
|
|
86
|
+
/**
|
|
87
|
+
* Get session from a Request object (for edge/middleware)
|
|
88
|
+
*
|
|
89
|
+
* Use this when you need to read cookies from a Request directly.
|
|
90
|
+
*/
|
|
91
|
+
declare function getSessionFromRequest(request: Request): SessionData | null;
|
|
92
|
+
/**
|
|
93
|
+
* Known account entry stored in cookie.
|
|
94
|
+
* Contains display metadata ONLY — no tokens, no secrets.
|
|
95
|
+
*/
|
|
96
|
+
interface KnownAccountEntry {
|
|
97
|
+
userId: string;
|
|
98
|
+
email?: string;
|
|
99
|
+
fullName?: string;
|
|
100
|
+
avatarUrl?: string;
|
|
101
|
+
provider?: string;
|
|
102
|
+
lastActiveAt: string;
|
|
103
|
+
displayLabel?: string;
|
|
104
|
+
colorIndex?: number;
|
|
105
|
+
}
|
|
106
|
+
type AccountSwitcherPrivacy = 'full' | 'masked' | 'minimal';
|
|
107
|
+
/**
|
|
108
|
+
* Add an account to the known accounts cookie.
|
|
109
|
+
* Called after successful login. The cookie is NOT httpOnly so client JS
|
|
110
|
+
* can read it to render the account switcher UI.
|
|
111
|
+
*
|
|
112
|
+
* Appends Set-Cookie headers to an existing Headers object.
|
|
113
|
+
*/
|
|
114
|
+
declare function appendKnownAccountCookie(headers: Headers, account: KnownAccountEntry, existingCookie: string | null, options?: SessionCookieOptions, privacy?: AccountSwitcherPrivacy): void;
|
|
115
|
+
/**
|
|
116
|
+
* Remove a specific account from the known accounts cookie.
|
|
117
|
+
*/
|
|
118
|
+
declare function removeKnownAccountFromCookie(headers: Headers, userId: string, existingCookie: string | null, options?: SessionCookieOptions): void;
|
|
119
|
+
/**
|
|
120
|
+
* Clear the known accounts cookie entirely.
|
|
121
|
+
*/
|
|
122
|
+
declare function clearKnownAccountsCookie(headers: Headers, options?: SessionCookieOptions): void;
|
|
123
|
+
/**
|
|
124
|
+
* Read known accounts from a Request's cookies.
|
|
125
|
+
*/
|
|
126
|
+
declare function getKnownAccountsFromRequest(request: Request): KnownAccountEntry[];
|
|
127
|
+
/**
|
|
128
|
+
* Read the raw known accounts cookie value from a Request.
|
|
129
|
+
*/
|
|
130
|
+
declare function getKnownAccountsCookieRaw(request: Request): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Normalize all entries in the known accounts cookie to the given privacy level.
|
|
133
|
+
* Returns a Set-Cookie header string if any entries changed, or null if nothing changed.
|
|
134
|
+
* Used on /me requests to migrate legacy full-PII cookies to the configured privacy level.
|
|
135
|
+
*/
|
|
136
|
+
declare function normalizeKnownAccountsCookie(request: Request, privacy: AccountSwitcherPrivacy | undefined, options?: SessionCookieOptions): string | null;
|
|
137
|
+
/**
|
|
138
|
+
* Require authentication - throws Response if not authenticated
|
|
139
|
+
*
|
|
140
|
+
* Use this at the start of protected API routes.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* export async function GET() {
|
|
145
|
+
* const session = await requireSession()
|
|
146
|
+
* // session is guaranteed to exist here
|
|
147
|
+
* const files = await sm.storage.list(session.userId)
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function requireSession(): Promise<SessionData>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* AI Facade Proxy Routes — `@scalemule/nextjs/server/ai`
|
|
155
|
+
*
|
|
156
|
+
* A fail-closed, user-delegated proxy for the ScaleMule AI facade (`/v1/ai`).
|
|
157
|
+
* The browser never sees the server API key: the route attaches it together
|
|
158
|
+
* with the signed-in user's session, and the gateway runs the operation as
|
|
159
|
+
* that member within the configured account.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* // app/api/ai/[...action]/route.ts
|
|
164
|
+
* import { createAiRoutes } from '@scalemule/nextjs/server/ai'
|
|
165
|
+
*
|
|
166
|
+
* export const { GET, POST } = createAiRoutes({
|
|
167
|
+
* apiKey: process.env.SCALEMULE_API_KEY!,
|
|
168
|
+
* gatewayUrl: process.env.SCALEMULE_API_URL!,
|
|
169
|
+
* accountId: process.env.SCALEMULE_ACCOUNT_ID!,
|
|
170
|
+
* })
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* Browser side, point `@scalemule/ai` at the route:
|
|
174
|
+
* ```ts
|
|
175
|
+
* const ai = createAi({
|
|
176
|
+
* transport: createFetchTransport({
|
|
177
|
+
* baseUrl: window.location.origin,
|
|
178
|
+
* headers: () => ({ 'x-csrf-token': readCsrfCookie() }),
|
|
179
|
+
* }),
|
|
180
|
+
* basePath: '/api/ai',
|
|
181
|
+
* })
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* Rules (ADR-2026-11-08 D4/D7):
|
|
185
|
+
* - no session cookie → 401, nothing is forwarded
|
|
186
|
+
* - only `runs`, `runs/{id}`, `runs/{id}/events`, `runs/{id}/cancel`
|
|
187
|
+
* - the account comes from server config, never from the browser
|
|
188
|
+
* - forwarded from the browser: body, `x-idempotency-key`, `last-event-id`
|
|
189
|
+
* - never forwarded: `Origin`, `x-sm-workspace-id`, `x-requested-account-id`,
|
|
190
|
+
* any `x-app-*` / `x-sm-*` / internal header
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
type AiAccountResolver = (request: Request, session: SessionData) => string | null | undefined | Promise<string | null | undefined>;
|
|
194
|
+
interface AiRoutesConfig {
|
|
195
|
+
/** Server-side secret API key. Never shipped to the browser. */
|
|
196
|
+
apiKey: string;
|
|
197
|
+
/** Gateway URL (e.g. https://api.scalemule.com) */
|
|
198
|
+
gatewayUrl: string;
|
|
199
|
+
/**
|
|
200
|
+
* Account the signed-in user's runs belong to: a fixed id, or a resolver
|
|
201
|
+
* from the request and session (for multi-account apps). The gateway
|
|
202
|
+
* verifies the user's membership. Omit only when the API key is itself
|
|
203
|
+
* account-bound (workspace-site key).
|
|
204
|
+
*/
|
|
205
|
+
accountId?: string | AiAccountResolver;
|
|
206
|
+
/** Enable CSRF validation on POST (default: true) */
|
|
207
|
+
csrf?: boolean;
|
|
208
|
+
/** Custom fetch (tests). Defaults to `globalThis.fetch`. */
|
|
209
|
+
fetch?: typeof fetch;
|
|
210
|
+
}
|
|
211
|
+
type RouteHandler = (request: Request, context: {
|
|
212
|
+
params: Promise<{
|
|
213
|
+
action?: string[];
|
|
214
|
+
}>;
|
|
215
|
+
}) => Promise<Response>;
|
|
216
|
+
type Target = {
|
|
217
|
+
kind: 'create';
|
|
218
|
+
} | {
|
|
219
|
+
kind: 'get';
|
|
220
|
+
runId: string;
|
|
221
|
+
} | {
|
|
222
|
+
kind: 'events';
|
|
223
|
+
runId: string;
|
|
224
|
+
} | {
|
|
225
|
+
kind: 'cancel';
|
|
226
|
+
runId: string;
|
|
227
|
+
};
|
|
228
|
+
/** Allow-list the proxied surface. Anything else is a 404, never forwarded. */
|
|
229
|
+
declare function resolveAiTarget(method: string, segments: string[]): Target | null;
|
|
230
|
+
declare function createAiRoutes(config: AiRoutesConfig): {
|
|
231
|
+
GET: RouteHandler;
|
|
232
|
+
POST: RouteHandler;
|
|
233
|
+
};
|
|
234
|
+
interface ServiceAiClientConfig {
|
|
235
|
+
/** Server-side secret API key (service mode; account-bound keys need no accountId). */
|
|
236
|
+
apiKey: string;
|
|
237
|
+
/** Gateway URL */
|
|
238
|
+
gatewayUrl: string;
|
|
239
|
+
/** Account the runs belong to (sent as x-requested-account-id). */
|
|
240
|
+
accountId?: string;
|
|
241
|
+
/** Custom fetch (tests). */
|
|
242
|
+
fetch?: typeof fetch;
|
|
243
|
+
/** Custom transport (overrides gatewayUrl/fetch). */
|
|
244
|
+
transport?: AiTransport;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A `ScaleMuleAi` for server code (route handlers, server actions, jobs).
|
|
248
|
+
* Runs execute in service mode under the key's application and account.
|
|
249
|
+
* Never construct this where the key could reach a browser bundle.
|
|
250
|
+
*/
|
|
251
|
+
declare function createServiceAiClient(config: ServiceAiClientConfig): ScaleMuleAi;
|
|
252
|
+
|
|
253
|
+
export { type AiAccountResolver as A, KNOWN_ACCOUNTS_COOKIE_NAME as K, type SessionCookieOptions as S, USER_ID_COOKIE_NAME as U, type SessionData as a, type AiRoutesConfig as b, type KnownAccountEntry as c, SESSION_COOKIE_NAME as d, type ServiceAiClientConfig as e, appendKnownAccountCookie as f, clearKnownAccountsCookie as g, clearSession as h, createAiRoutes as i, createServiceAiClient as j, getKnownAccountsCookieRaw as k, getKnownAccountsFromRequest as l, getSession as m, getSessionFromRequest as n, normalizeKnownAccountsCookie as o, requireSession as p, resolveAiTarget as q, removeKnownAccountFromCookie as r, withSession as w };
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { AiTransport, ScaleMuleAi } from '@scalemule/ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cookie Utilities for Secure Session Management
|
|
5
|
+
*
|
|
6
|
+
* Handles HTTP-only secure cookies for authentication.
|
|
7
|
+
* Tokens are never exposed to the browser.
|
|
8
|
+
*/
|
|
9
|
+
declare const SESSION_COOKIE_NAME = "sm_session";
|
|
10
|
+
declare const USER_ID_COOKIE_NAME = "sm_user_id";
|
|
11
|
+
/**
|
|
12
|
+
* Known accounts cookie — stores display metadata (email, name, avatar) for
|
|
13
|
+
* accounts that have logged in on this device. NOT httpOnly so client JS can
|
|
14
|
+
* read it to render the account switcher UI. Contains NO tokens or secrets.
|
|
15
|
+
*/
|
|
16
|
+
declare const KNOWN_ACCOUNTS_COOKIE_NAME = "sm_known_accounts";
|
|
17
|
+
interface SessionCookieOptions {
|
|
18
|
+
/** Isolate an embedded session in the top-level site cookie partition (CHIPS). */
|
|
19
|
+
partitioned?: boolean;
|
|
20
|
+
/** Cookie max age in seconds (default: 7 days) */
|
|
21
|
+
maxAge?: number;
|
|
22
|
+
/** Cookie domain (default: current domain) */
|
|
23
|
+
domain?: string;
|
|
24
|
+
/** Cookie path (default: '/') */
|
|
25
|
+
path?: string;
|
|
26
|
+
/** SameSite attribute (default: 'lax') */
|
|
27
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
28
|
+
/** Whether to use secure cookies (default: true in production) */
|
|
29
|
+
secure?: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface SessionData {
|
|
32
|
+
sessionToken: string;
|
|
33
|
+
userId: string;
|
|
34
|
+
expiresAt: Date;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a Response with session cookies set
|
|
38
|
+
*
|
|
39
|
+
* Use this after successful login to set HTTP-only cookies and return user data.
|
|
40
|
+
* The session token is stored in cookies, never sent to the browser in JSON.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const result = await sm.auth.login({ email, password })
|
|
45
|
+
* if (result.success) {
|
|
46
|
+
* return withSession(result.data, { user: result.data.user })
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function withSession<T extends Record<string, unknown>>(loginResponse: {
|
|
51
|
+
session_token: string;
|
|
52
|
+
user: {
|
|
53
|
+
id: string;
|
|
54
|
+
};
|
|
55
|
+
}, responseBody: T, options?: SessionCookieOptions): Response;
|
|
56
|
+
/**
|
|
57
|
+
* Create a Response that clears session cookies
|
|
58
|
+
*
|
|
59
|
+
* Use this after logout to clear HTTP-only cookies.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* await sm.auth.logout(sessionToken)
|
|
64
|
+
* return clearSession({ message: 'Logged out' })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function clearSession<T extends Record<string, unknown>>(responseBody: T, options?: SessionCookieOptions, status?: number): Response;
|
|
68
|
+
/**
|
|
69
|
+
* Get session data from request cookies
|
|
70
|
+
*
|
|
71
|
+
* Use this in API routes to get the current session.
|
|
72
|
+
* Returns null if no valid session cookie exists.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const session = await getSession()
|
|
77
|
+
* if (!session) {
|
|
78
|
+
* return Response.json({ error: 'Not authenticated' }, { status: 401 })
|
|
79
|
+
* }
|
|
80
|
+
* const user = await sm.auth.me(session.sessionToken)
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
declare function getSession(options?: {
|
|
84
|
+
allowBearer?: boolean;
|
|
85
|
+
}): Promise<SessionData | null>;
|
|
86
|
+
/**
|
|
87
|
+
* Get session from a Request object (for edge/middleware)
|
|
88
|
+
*
|
|
89
|
+
* Use this when you need to read cookies from a Request directly.
|
|
90
|
+
*/
|
|
91
|
+
declare function getSessionFromRequest(request: Request): SessionData | null;
|
|
92
|
+
/**
|
|
93
|
+
* Known account entry stored in cookie.
|
|
94
|
+
* Contains display metadata ONLY — no tokens, no secrets.
|
|
95
|
+
*/
|
|
96
|
+
interface KnownAccountEntry {
|
|
97
|
+
userId: string;
|
|
98
|
+
email?: string;
|
|
99
|
+
fullName?: string;
|
|
100
|
+
avatarUrl?: string;
|
|
101
|
+
provider?: string;
|
|
102
|
+
lastActiveAt: string;
|
|
103
|
+
displayLabel?: string;
|
|
104
|
+
colorIndex?: number;
|
|
105
|
+
}
|
|
106
|
+
type AccountSwitcherPrivacy = 'full' | 'masked' | 'minimal';
|
|
107
|
+
/**
|
|
108
|
+
* Add an account to the known accounts cookie.
|
|
109
|
+
* Called after successful login. The cookie is NOT httpOnly so client JS
|
|
110
|
+
* can read it to render the account switcher UI.
|
|
111
|
+
*
|
|
112
|
+
* Appends Set-Cookie headers to an existing Headers object.
|
|
113
|
+
*/
|
|
114
|
+
declare function appendKnownAccountCookie(headers: Headers, account: KnownAccountEntry, existingCookie: string | null, options?: SessionCookieOptions, privacy?: AccountSwitcherPrivacy): void;
|
|
115
|
+
/**
|
|
116
|
+
* Remove a specific account from the known accounts cookie.
|
|
117
|
+
*/
|
|
118
|
+
declare function removeKnownAccountFromCookie(headers: Headers, userId: string, existingCookie: string | null, options?: SessionCookieOptions): void;
|
|
119
|
+
/**
|
|
120
|
+
* Clear the known accounts cookie entirely.
|
|
121
|
+
*/
|
|
122
|
+
declare function clearKnownAccountsCookie(headers: Headers, options?: SessionCookieOptions): void;
|
|
123
|
+
/**
|
|
124
|
+
* Read known accounts from a Request's cookies.
|
|
125
|
+
*/
|
|
126
|
+
declare function getKnownAccountsFromRequest(request: Request): KnownAccountEntry[];
|
|
127
|
+
/**
|
|
128
|
+
* Read the raw known accounts cookie value from a Request.
|
|
129
|
+
*/
|
|
130
|
+
declare function getKnownAccountsCookieRaw(request: Request): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Normalize all entries in the known accounts cookie to the given privacy level.
|
|
133
|
+
* Returns a Set-Cookie header string if any entries changed, or null if nothing changed.
|
|
134
|
+
* Used on /me requests to migrate legacy full-PII cookies to the configured privacy level.
|
|
135
|
+
*/
|
|
136
|
+
declare function normalizeKnownAccountsCookie(request: Request, privacy: AccountSwitcherPrivacy | undefined, options?: SessionCookieOptions): string | null;
|
|
137
|
+
/**
|
|
138
|
+
* Require authentication - throws Response if not authenticated
|
|
139
|
+
*
|
|
140
|
+
* Use this at the start of protected API routes.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* export async function GET() {
|
|
145
|
+
* const session = await requireSession()
|
|
146
|
+
* // session is guaranteed to exist here
|
|
147
|
+
* const files = await sm.storage.list(session.userId)
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function requireSession(): Promise<SessionData>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* AI Facade Proxy Routes — `@scalemule/nextjs/server/ai`
|
|
155
|
+
*
|
|
156
|
+
* A fail-closed, user-delegated proxy for the ScaleMule AI facade (`/v1/ai`).
|
|
157
|
+
* The browser never sees the server API key: the route attaches it together
|
|
158
|
+
* with the signed-in user's session, and the gateway runs the operation as
|
|
159
|
+
* that member within the configured account.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* // app/api/ai/[...action]/route.ts
|
|
164
|
+
* import { createAiRoutes } from '@scalemule/nextjs/server/ai'
|
|
165
|
+
*
|
|
166
|
+
* export const { GET, POST } = createAiRoutes({
|
|
167
|
+
* apiKey: process.env.SCALEMULE_API_KEY!,
|
|
168
|
+
* gatewayUrl: process.env.SCALEMULE_API_URL!,
|
|
169
|
+
* accountId: process.env.SCALEMULE_ACCOUNT_ID!,
|
|
170
|
+
* })
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* Browser side, point `@scalemule/ai` at the route:
|
|
174
|
+
* ```ts
|
|
175
|
+
* const ai = createAi({
|
|
176
|
+
* transport: createFetchTransport({
|
|
177
|
+
* baseUrl: window.location.origin,
|
|
178
|
+
* headers: () => ({ 'x-csrf-token': readCsrfCookie() }),
|
|
179
|
+
* }),
|
|
180
|
+
* basePath: '/api/ai',
|
|
181
|
+
* })
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* Rules (ADR-2026-11-08 D4/D7):
|
|
185
|
+
* - no session cookie → 401, nothing is forwarded
|
|
186
|
+
* - only `runs`, `runs/{id}`, `runs/{id}/events`, `runs/{id}/cancel`
|
|
187
|
+
* - the account comes from server config, never from the browser
|
|
188
|
+
* - forwarded from the browser: body, `x-idempotency-key`, `last-event-id`
|
|
189
|
+
* - never forwarded: `Origin`, `x-sm-workspace-id`, `x-requested-account-id`,
|
|
190
|
+
* any `x-app-*` / `x-sm-*` / internal header
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
type AiAccountResolver = (request: Request, session: SessionData) => string | null | undefined | Promise<string | null | undefined>;
|
|
194
|
+
interface AiRoutesConfig {
|
|
195
|
+
/** Server-side secret API key. Never shipped to the browser. */
|
|
196
|
+
apiKey: string;
|
|
197
|
+
/** Gateway URL (e.g. https://api.scalemule.com) */
|
|
198
|
+
gatewayUrl: string;
|
|
199
|
+
/**
|
|
200
|
+
* Account the signed-in user's runs belong to: a fixed id, or a resolver
|
|
201
|
+
* from the request and session (for multi-account apps). The gateway
|
|
202
|
+
* verifies the user's membership. Omit only when the API key is itself
|
|
203
|
+
* account-bound (workspace-site key).
|
|
204
|
+
*/
|
|
205
|
+
accountId?: string | AiAccountResolver;
|
|
206
|
+
/** Enable CSRF validation on POST (default: true) */
|
|
207
|
+
csrf?: boolean;
|
|
208
|
+
/** Custom fetch (tests). Defaults to `globalThis.fetch`. */
|
|
209
|
+
fetch?: typeof fetch;
|
|
210
|
+
}
|
|
211
|
+
type RouteHandler = (request: Request, context: {
|
|
212
|
+
params: Promise<{
|
|
213
|
+
action?: string[];
|
|
214
|
+
}>;
|
|
215
|
+
}) => Promise<Response>;
|
|
216
|
+
type Target = {
|
|
217
|
+
kind: 'create';
|
|
218
|
+
} | {
|
|
219
|
+
kind: 'get';
|
|
220
|
+
runId: string;
|
|
221
|
+
} | {
|
|
222
|
+
kind: 'events';
|
|
223
|
+
runId: string;
|
|
224
|
+
} | {
|
|
225
|
+
kind: 'cancel';
|
|
226
|
+
runId: string;
|
|
227
|
+
};
|
|
228
|
+
/** Allow-list the proxied surface. Anything else is a 404, never forwarded. */
|
|
229
|
+
declare function resolveAiTarget(method: string, segments: string[]): Target | null;
|
|
230
|
+
declare function createAiRoutes(config: AiRoutesConfig): {
|
|
231
|
+
GET: RouteHandler;
|
|
232
|
+
POST: RouteHandler;
|
|
233
|
+
};
|
|
234
|
+
interface ServiceAiClientConfig {
|
|
235
|
+
/** Server-side secret API key (service mode; account-bound keys need no accountId). */
|
|
236
|
+
apiKey: string;
|
|
237
|
+
/** Gateway URL */
|
|
238
|
+
gatewayUrl: string;
|
|
239
|
+
/** Account the runs belong to (sent as x-requested-account-id). */
|
|
240
|
+
accountId?: string;
|
|
241
|
+
/** Custom fetch (tests). */
|
|
242
|
+
fetch?: typeof fetch;
|
|
243
|
+
/** Custom transport (overrides gatewayUrl/fetch). */
|
|
244
|
+
transport?: AiTransport;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A `ScaleMuleAi` for server code (route handlers, server actions, jobs).
|
|
248
|
+
* Runs execute in service mode under the key's application and account.
|
|
249
|
+
* Never construct this where the key could reach a browser bundle.
|
|
250
|
+
*/
|
|
251
|
+
declare function createServiceAiClient(config: ServiceAiClientConfig): ScaleMuleAi;
|
|
252
|
+
|
|
253
|
+
export { type AiAccountResolver as A, KNOWN_ACCOUNTS_COOKIE_NAME as K, type SessionCookieOptions as S, USER_ID_COOKIE_NAME as U, type SessionData as a, type AiRoutesConfig as b, type KnownAccountEntry as c, SESSION_COOKIE_NAME as d, type ServiceAiClientConfig as e, appendKnownAccountCookie as f, clearKnownAccountsCookie as g, clearSession as h, createAiRoutes as i, createServiceAiClient as j, getKnownAccountsCookieRaw as k, getKnownAccountsFromRequest as l, getSession as m, getSessionFromRequest as n, normalizeKnownAccountsCookie as o, requireSession as p, resolveAiTarget as q, removeKnownAccountFromCookie as r, withSession as w };
|