@lastbrain/ai-ui-core 1.0.44 → 1.0.46

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.
@@ -1 +1 @@
1
- {"version":3,"file":"enhanced-gateway.d.ts","sourceRoot":"","sources":["../../../src/route-handlers/nextjs/enhanced-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA2KxD,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,IAAI,CACxB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,MAAM,CAC1B,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD"}
1
+ {"version":3,"file":"enhanced-gateway.d.ts","sourceRoot":"","sources":["../../../src/route-handlers/nextjs/enhanced-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAqMxD,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,IAAI,CACxB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,GAAG,CACvB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD;AAED,wBAAsB,MAAM,CAC1B,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;CAAE,8BAGjD"}
@@ -20,6 +20,7 @@ function mapAuthRouteToPublicRoute(path) {
20
20
  const rawPath = path.startsWith("/") ? path.slice(1) : path;
21
21
  // Remove leading 'ai/' if present (for routes like ai/auth/prompts)
22
22
  const cleanPath = rawPath.startsWith("ai/") ? rawPath.slice(3) : rawPath;
23
+ console.log(`[Enhanced Gateway] Raw path: "${path}" -> Clean path: "${cleanPath}"`);
23
24
  // Route mapping: auth routes -> public routes (without api/public/v1 prefix)
24
25
  const routeMappings = {
25
26
  // Prompts
@@ -64,15 +65,21 @@ function mapAuthRouteToPublicRoute(path) {
64
65
  };
65
66
  // Try exact match first
66
67
  if (routeMappings[cleanPath]) {
68
+ console.log(`[Enhanced Gateway] Exact match: "${cleanPath}" -> "${routeMappings[cleanPath]}"`);
67
69
  return routeMappings[cleanPath];
68
70
  }
69
- // Try pattern matching
70
- for (const [pattern, target] of Object.entries(routeMappings)) {
71
- if (cleanPath.startsWith(pattern)) {
71
+ // Try pattern matching with priority to longer/more specific patterns first
72
+ const sortedPatterns = Object.entries(routeMappings)
73
+ .sort(([a], [b]) => b.length - a.length); // Longer patterns first
74
+ console.log(`[Enhanced Gateway] No exact match for "${cleanPath}", trying patterns...`);
75
+ for (const [pattern, target] of sortedPatterns) {
76
+ if (cleanPath.startsWith(pattern + "/") || cleanPath === pattern) {
77
+ console.log(`[Enhanced Gateway] Pattern match: "${pattern}" -> "${target}"`);
72
78
  return target + cleanPath.slice(pattern.length);
73
79
  }
74
80
  }
75
81
  // Fallback: return clean path as-is
82
+ console.log(`[Enhanced Gateway] No pattern match, using fallback: "${cleanPath}"`);
76
83
  return cleanPath;
77
84
  }
78
85
  async function handleRequest(request, context) {
@@ -82,7 +89,8 @@ async function handleRequest(request, context) {
82
89
  const params = await context.params;
83
90
  const originalPath = params.path.join("/");
84
91
  const mappedPath = mapAuthRouteToPublicRoute(originalPath);
85
- const url = `${LB_BASE_URL}/${mappedPath}`;
92
+ // Ajouter automatiquement le préfixe /api/ai pour le monorepo
93
+ const url = `${LB_BASE_URL}/api/ai/${mappedPath}`;
86
94
  // Preserve query params
87
95
  const searchParams = request.nextUrl.searchParams.toString();
88
96
  const finalUrl = searchParams ? `${url}?${searchParams}` : url;
@@ -90,10 +98,24 @@ async function handleRequest(request, context) {
90
98
  console.log(`[Enhanced Gateway] Proxying to: ${finalUrl}`);
91
99
  console.log("[Enhanced Gateway] Bearer token:", LB_API_KEY ? `${LB_API_KEY.substring(0, 10)}...` : "MISSING");
92
100
  try {
101
+ // Déterminer le type d'auth selon le path
102
+ const isAuthRoute = originalPath.startsWith('auth/') || mappedPath.startsWith('auth/');
93
103
  const headers = {
94
- Authorization: `Bearer ${LB_API_KEY}`,
95
104
  "Content-Type": "application/json",
96
105
  };
106
+ if (isAuthRoute) {
107
+ // Routes auth/* : forward les cookies de session
108
+ console.log("[Enhanced Gateway] Auth route - forwarding session cookies");
109
+ const cookies = request.headers.get('cookie');
110
+ if (cookies) {
111
+ headers['Cookie'] = cookies;
112
+ }
113
+ }
114
+ else {
115
+ // Routes v1/* : utiliser LB_API_KEY
116
+ console.log("[Enhanced Gateway] API route - using LB_API_KEY");
117
+ headers['Authorization'] = `Bearer ${LB_API_KEY}`;
118
+ }
97
119
  let body;
98
120
  if (request.method === "POST" ||
99
121
  request.method === "PUT" ||
@@ -110,6 +132,7 @@ async function handleRequest(request, context) {
110
132
  method: request.method,
111
133
  headers,
112
134
  body,
135
+ credentials: isAuthRoute ? 'include' : 'omit', // Include cookies for auth routes
113
136
  });
114
137
  if (!response.ok) {
115
138
  const errorText = await response.text();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lastbrain/ai-ui-core",
3
- "version": "1.0.44",
3
+ "version": "1.0.46",
4
4
  "description": "Framework-agnostic core library for LastBrain AI UI Kit",
5
5
  "private": false,
6
6
  "type": "module",
@@ -29,6 +29,8 @@ function mapAuthRouteToPublicRoute(path: string): string {
29
29
  // Remove leading 'ai/' if present (for routes like ai/auth/prompts)
30
30
  const cleanPath = rawPath.startsWith("ai/") ? rawPath.slice(3) : rawPath;
31
31
 
32
+ console.log(`[Enhanced Gateway] Raw path: "${path}" -> Clean path: "${cleanPath}"`);
33
+
32
34
  // Route mapping: auth routes -> public routes (without api/public/v1 prefix)
33
35
  const routeMappings: Record<string, string> = {
34
36
  // Prompts
@@ -81,17 +83,24 @@ function mapAuthRouteToPublicRoute(path: string): string {
81
83
 
82
84
  // Try exact match first
83
85
  if (routeMappings[cleanPath]) {
86
+ console.log(`[Enhanced Gateway] Exact match: "${cleanPath}" -> "${routeMappings[cleanPath]}"`);
84
87
  return routeMappings[cleanPath];
85
88
  }
86
89
 
87
- // Try pattern matching
88
- for (const [pattern, target] of Object.entries(routeMappings)) {
89
- if (cleanPath.startsWith(pattern)) {
90
+ // Try pattern matching with priority to longer/more specific patterns first
91
+ const sortedPatterns = Object.entries(routeMappings)
92
+ .sort(([a], [b]) => b.length - a.length); // Longer patterns first
93
+
94
+ console.log(`[Enhanced Gateway] No exact match for "${cleanPath}", trying patterns...`);
95
+ for (const [pattern, target] of sortedPatterns) {
96
+ if (cleanPath.startsWith(pattern + "/") || cleanPath === pattern) {
97
+ console.log(`[Enhanced Gateway] Pattern match: "${pattern}" -> "${target}"`);
90
98
  return target + cleanPath.slice(pattern.length);
91
99
  }
92
100
  }
93
101
 
94
102
  // Fallback: return clean path as-is
103
+ console.log(`[Enhanced Gateway] No pattern match, using fallback: "${cleanPath}"`);
95
104
  return cleanPath;
96
105
  }
97
106
 
@@ -110,7 +119,8 @@ async function handleRequest(
110
119
  const originalPath = params.path.join("/");
111
120
  const mappedPath = mapAuthRouteToPublicRoute(originalPath);
112
121
 
113
- const url = `${LB_BASE_URL}/${mappedPath}`;
122
+ // Ajouter automatiquement le préfixe /api/ai pour le monorepo
123
+ const url = `${LB_BASE_URL}/api/ai/${mappedPath}`;
114
124
 
115
125
  // Preserve query params
116
126
  const searchParams = request.nextUrl.searchParams.toString();
@@ -124,11 +134,26 @@ async function handleRequest(
124
134
  );
125
135
 
126
136
  try {
137
+ // Déterminer le type d'auth selon le path
138
+ const isAuthRoute = originalPath.startsWith('auth/') || mappedPath.startsWith('auth/');
139
+
127
140
  const headers: HeadersInit = {
128
- Authorization: `Bearer ${LB_API_KEY}`,
129
141
  "Content-Type": "application/json",
130
142
  };
131
143
 
144
+ if (isAuthRoute) {
145
+ // Routes auth/* : forward les cookies de session
146
+ console.log("[Enhanced Gateway] Auth route - forwarding session cookies");
147
+ const cookies = request.headers.get('cookie');
148
+ if (cookies) {
149
+ headers['Cookie'] = cookies;
150
+ }
151
+ } else {
152
+ // Routes v1/* : utiliser LB_API_KEY
153
+ console.log("[Enhanced Gateway] API route - using LB_API_KEY");
154
+ headers['Authorization'] = `Bearer ${LB_API_KEY}`;
155
+ }
156
+
132
157
  let body: string | undefined;
133
158
  if (
134
159
  request.method === "POST" ||
@@ -147,6 +172,7 @@ async function handleRequest(
147
172
  method: request.method,
148
173
  headers,
149
174
  body,
175
+ credentials: isAuthRoute ? 'include' : 'omit', // Include cookies for auth routes
150
176
  });
151
177
 
152
178
  if (!response.ok) {