katia-openai commited on
Commit
5c75232
·
1 Parent(s): a7d921d

enable attachments

Browse files
app/api/create-session/route.ts CHANGED
@@ -6,6 +6,11 @@ interface CreateSessionRequestBody {
6
  workflow?: { id?: string | null } | null;
7
  scope?: { user_id?: string | null } | null;
8
  workflowId?: string | null;
 
 
 
 
 
9
  }
10
 
11
  const DEFAULT_CHATKIT_BASE = "https://api.openai.com";
@@ -21,7 +26,9 @@ export async function POST(request: Request): Promise<Response> {
21
  const openaiApiKey = process.env.OPENAI_API_KEY;
22
  if (!openaiApiKey) {
23
  return new Response(
24
- JSON.stringify({ error: "Missing OPENAI_API_KEY environment variable" }),
 
 
25
  {
26
  status: 500,
27
  headers: { "Content-Type": "application/json" },
@@ -30,7 +37,8 @@ export async function POST(request: Request): Promise<Response> {
30
  }
31
 
32
  const parsedBody = await safeParseJson<CreateSessionRequestBody>(request);
33
- const { userId, sessionCookie: resolvedSessionCookie } = await resolveUserId(request);
 
34
  sessionCookie = resolvedSessionCookie;
35
  const resolvedWorkflowId =
36
  parsedBody?.workflow?.id ?? parsedBody?.workflowId ?? WORKFLOW_ID;
@@ -63,6 +71,12 @@ export async function POST(request: Request): Promise<Response> {
63
  body: JSON.stringify({
64
  workflow: { id: resolvedWorkflowId },
65
  user: userId,
 
 
 
 
 
 
66
  }),
67
  });
68
 
@@ -86,7 +100,9 @@ export async function POST(request: Request): Promise<Response> {
86
  });
87
  return buildJsonResponse(
88
  {
89
- error: upstreamError ?? `Failed to create session: ${upstreamResponse.statusText}`,
 
 
90
  details: upstreamJson,
91
  },
92
  upstreamResponse.status,
@@ -134,14 +150,18 @@ async function resolveUserId(request: Request): Promise<{
134
  userId: string;
135
  sessionCookie: string | null;
136
  }> {
137
- const existing = getCookieValue(request.headers.get("cookie"), SESSION_COOKIE_NAME);
 
 
 
138
  if (existing) {
139
  return { userId: existing, sessionCookie: null };
140
  }
141
 
142
- const generated = typeof crypto.randomUUID === "function"
143
- ? crypto.randomUUID()
144
- : Math.random().toString(36).slice(2);
 
145
 
146
  return {
147
  userId: generated,
@@ -149,7 +169,10 @@ async function resolveUserId(request: Request): Promise<{
149
  };
150
  }
151
 
152
- function getCookieValue(cookieHeader: string | null, name: string): string | null {
 
 
 
153
  if (!cookieHeader) {
154
  return null;
155
  }
@@ -157,8 +180,12 @@ function getCookieValue(cookieHeader: string | null, name: string): string | nul
157
  const cookies = cookieHeader.split(";");
158
  for (const cookie of cookies) {
159
  const [rawName, ...rest] = cookie.split("=");
160
- if (!rawName || rest.length === 0) { continue; }
161
- if (rawName.trim() === name) { return rest.join("=").trim(); }
 
 
 
 
162
  }
163
  return null;
164
  }
@@ -199,7 +226,9 @@ function buildJsonResponse(
199
  async function safeParseJson<T>(req: Request): Promise<T | null> {
200
  try {
201
  const text = await req.text();
202
- if (!text) { return null; }
 
 
203
  return JSON.parse(text) as T;
204
  } catch {
205
  return null;
 
6
  workflow?: { id?: string | null } | null;
7
  scope?: { user_id?: string | null } | null;
8
  workflowId?: string | null;
9
+ chatkit_configuration?: {
10
+ file_upload?: {
11
+ enabled?: boolean;
12
+ };
13
+ };
14
  }
15
 
16
  const DEFAULT_CHATKIT_BASE = "https://api.openai.com";
 
26
  const openaiApiKey = process.env.OPENAI_API_KEY;
27
  if (!openaiApiKey) {
28
  return new Response(
29
+ JSON.stringify({
30
+ error: "Missing OPENAI_API_KEY environment variable",
31
+ }),
32
  {
33
  status: 500,
34
  headers: { "Content-Type": "application/json" },
 
37
  }
38
 
39
  const parsedBody = await safeParseJson<CreateSessionRequestBody>(request);
40
+ const { userId, sessionCookie: resolvedSessionCookie } =
41
+ await resolveUserId(request);
42
  sessionCookie = resolvedSessionCookie;
43
  const resolvedWorkflowId =
44
  parsedBody?.workflow?.id ?? parsedBody?.workflowId ?? WORKFLOW_ID;
 
71
  body: JSON.stringify({
72
  workflow: { id: resolvedWorkflowId },
73
  user: userId,
74
+ chatkit_configuration: {
75
+ file_upload: {
76
+ enabled:
77
+ parsedBody?.chatkit_configuration?.file_upload?.enabled ?? false,
78
+ },
79
+ },
80
  }),
81
  });
82
 
 
100
  });
101
  return buildJsonResponse(
102
  {
103
+ error:
104
+ upstreamError ??
105
+ `Failed to create session: ${upstreamResponse.statusText}`,
106
  details: upstreamJson,
107
  },
108
  upstreamResponse.status,
 
150
  userId: string;
151
  sessionCookie: string | null;
152
  }> {
153
+ const existing = getCookieValue(
154
+ request.headers.get("cookie"),
155
+ SESSION_COOKIE_NAME
156
+ );
157
  if (existing) {
158
  return { userId: existing, sessionCookie: null };
159
  }
160
 
161
+ const generated =
162
+ typeof crypto.randomUUID === "function"
163
+ ? crypto.randomUUID()
164
+ : Math.random().toString(36).slice(2);
165
 
166
  return {
167
  userId: generated,
 
169
  };
170
  }
171
 
172
+ function getCookieValue(
173
+ cookieHeader: string | null,
174
+ name: string
175
+ ): string | null {
176
  if (!cookieHeader) {
177
  return null;
178
  }
 
180
  const cookies = cookieHeader.split(";");
181
  for (const cookie of cookies) {
182
  const [rawName, ...rest] = cookie.split("=");
183
+ if (!rawName || rest.length === 0) {
184
+ continue;
185
+ }
186
+ if (rawName.trim() === name) {
187
+ return rest.join("=").trim();
188
+ }
189
  }
190
  return null;
191
  }
 
226
  async function safeParseJson<T>(req: Request): Promise<T | null> {
227
  try {
228
  const text = await req.text();
229
+ if (!text) {
230
+ return null;
231
+ }
232
  return JSON.parse(text) as T;
233
  } catch {
234
  return null;
components/ChatKitPanel.tsx CHANGED
@@ -192,6 +192,12 @@ export function ChatKitPanel({
192
  },
193
  body: JSON.stringify({
194
  workflow: { id: WORKFLOW_ID },
 
 
 
 
 
 
195
  }),
196
  });
197
 
@@ -267,6 +273,10 @@ export function ChatKitPanel({
267
  },
268
  composer: {
269
  placeholder: PLACEHOLDER_INPUT,
 
 
 
 
270
  },
271
  threadItemActions: {
272
  feedback: false,
 
192
  },
193
  body: JSON.stringify({
194
  workflow: { id: WORKFLOW_ID },
195
+ chatkit_configuration: {
196
+ // enable attachments
197
+ file_upload: {
198
+ enabled: true,
199
+ },
200
+ },
201
  }),
202
  });
203
 
 
273
  },
274
  composer: {
275
  placeholder: PLACEHOLDER_INPUT,
276
+ attachments: {
277
+ // Enable attachments
278
+ enabled: true,
279
+ },
280
  },
281
  threadItemActions: {
282
  feedback: false,