Skip to content

Commit 753a629

Browse files
authored
Merge pull request #124 from openai/docs-ia-redo
[docs] IA redo first pass
2 parents 92649b3 + 429233a commit 753a629

24 files changed

+807
-726
lines changed

packages/docs/astro.config.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,55 @@ const typeDocConfig: StarlightTypeDocOptions['typeDoc'] = {
4141
const sidebar = [
4242
{ label: 'Overview', link: '/' },
4343
{
44-
label: 'Guides',
44+
label: 'Authenticate users',
45+
collapsed: true,
4546
items: [
46-
{ label: 'Authentication', link: '/guides/authentication' },
4747
{
48-
label: 'Theming & Customization',
49-
link: '/guides/theming-customization',
48+
label: 'Hosted backend (client secrets)',
49+
link: '/guides/authenticate-hosted',
5050
},
51-
{ label: 'Methods', link: '/guides/methods' },
52-
{ label: 'Events', link: '/guides/events' },
53-
{ label: 'Client Tools', link: '/guides/client-tools' },
54-
{ label: 'Entity tagging', link: '/guides/entities' },
55-
{ label: 'Custom Backends', link: '/guides/custom-backends' },
51+
{
52+
label: 'Custom backend (custom fetch)',
53+
link: '/guides/authenticate-custom',
54+
},
55+
],
56+
},
57+
{
58+
label: 'Customize',
59+
collapsed: true,
60+
items: [
61+
{ label: 'Overview', link: '/guides/customize' },
62+
{ label: 'Theme', link: '/guides/customize-theme' },
63+
{ label: 'Header & history', link: '/guides/customize-header-history' },
64+
{ label: 'Start screen', link: '/guides/customize-start-screen' },
65+
{ label: 'Composer', link: '/guides/customize-composer' },
66+
{
67+
label: 'Thread item actions',
68+
link: '/guides/customize-thread-item-actions',
69+
},
70+
],
71+
},
72+
{
73+
label: 'Connect to your app',
74+
collapsed: true,
75+
items: [
76+
{ label: 'Register callbacks', link: '/guides/register-callbacks' },
77+
{ label: 'Call ChatKit methods', link: '/guides/methods' },
78+
{
79+
label: 'Handle runtime events',
80+
link: '/guides/runtime-events',
81+
},
82+
],
83+
},
84+
{
85+
label: 'Go live',
86+
collapsed: true,
87+
items: [
88+
{ label: 'Overview', link: '/guides/go-live' },
89+
{ label: 'Provide domain keys', link: '/guides/provide-domain-keys' },
90+
{ label: 'Monitor logs', link: '/guides/monitor-logs' },
91+
// { label: 'Update CSP settings', link: '/guides/update-csp-settings' },
92+
// { label: 'Scope user data', link: '/guides/scope-user-data' },
5693
{ label: 'Localization', link: '/guides/localization' },
5794
],
5895
},

packages/docs/src/content/docs/guides/custom-backends.mdx renamed to packages/docs/src/content/docs/guides/authenticate-custom.mdx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
---
2-
title: Custom backends
3-
description: Build a bespoke backend for ChatKit using your own stack.
2+
title: Authenticate users with a custom backend
3+
description: Inject headers and metadata with a custom fetch function.
44
---
55

66
import { TabItem, Tabs } from '@astrojs/starlight/components';
77

8-
Use a custom backend when you need full control over routing, tools, memory, or security. Provide a custom fetch function to use for API requests and orchestrate model calls yourself.
8+
:::note[Note]
9+
This guide is for a **custom backend** using the ChatKit Python SDK.
10+
For the hosted backend, see [Authenticate users with a hosted backend](/chatkit-js/guides/authenticate-hosted).
11+
:::
912

10-
## Approaches
13+
To protect the ChatKit endpoints exposed by your server using the ChatKit Python SDK, use a custom fetch function to inject your own auth headers and metadata on every request.
1114

12-
- Use the ChatKit Python SDK for fast integration
13-
- Or integrate directly with your model provider and implement compatible events
1415

15-
## Configure ChatKit
16+
## Inject headers with `api.fetch`
17+
18+
- Point ChatKit at your endpoint with `api.url`.
19+
- Add auth headers through the `fetch` override.
1620

1721
<Tabs syncKey="language">
1822
<TabItem label="React">
@@ -33,17 +37,9 @@ Use a custom backend when you need full control over routing, tools, memory, or
3337
...options.headers,
3438
"Authorization": `Bearer ${auth}`,
3539
},
36-
37-
// You can override any options here
3840
});
3941
},
4042

41-
// Required when attachments are enabled.
42-
uploadStrategy: {
43-
type: "direct",
44-
uploadUrl: "https://your-domain.com/your/chatkit/api/upload",
45-
}
46-
4743
// Register your domain in the dashboard at
4844
// https://platform.openai.com/settings/organization/security/domain-allowlist
4945
domainKey: "your-domain-key",
@@ -70,23 +66,19 @@ Use a custom backend when you need full control over routing, tools, memory, or
7066
// Anything you do in this callback is invisible to ChatKit.
7167
"Authorization": `Bearer ${auth}`,
7268
},
73-
// You can override any options here
7469
});
7570
},
7671
// Register your domain in the dashboard at
7772
// https://platform.openai.com/settings/organization/security/domain-allowlist
7873
domainKey: "your-domain-key",
79-
// Required when attachments are enabled.
80-
uploadStrategy: {
81-
type: "direct",
82-
uploadUrl: "https://your-domain.com/your/chatkit/api/upload",
83-
}
8474
},
8575
});
8676
```
8777

8878
</TabItem>
8979
</Tabs>
9080

81+
## Backend responsibilities
9182

92-
83+
- Validate every request (auth headers, cookies, or signed params) before invoking your agent.
84+
- Scope data to the requesting user and session; the ChatKit Python SDK does not define the concept of a "user" or "session".
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: Authenticate users with a hosted backend
3+
description: Issue client secrets from your own server and pass them to ChatKit at runtime.
4+
---
5+
6+
import { TabItem, Tabs } from '@astrojs/starlight/components';
7+
8+
:::note[Note]
9+
This guide is for the **hosted backend**.
10+
For a custom backend, see [Authenticate users with a custom backend](/chatkit-js/guides/authenticate-custom).
11+
:::
12+
13+
This guide shows how to mint short-lived ChatKit client secrets after authenticating users on your server and deliver them to the hosted backend at runtime.
14+
15+
16+
17+
## Expose a ChatKit session endpoint from your server
18+
19+
Your server issues client secrets after authenticating the user. Use the ChatKit Sessions API to create (and later refresh) secrets and return only the `client_secret` value to the browser.
20+
21+
Your server should:
22+
- Authenticate the user (cookies, headers, tokens, etc.)
23+
- Call the [ChatKit sessions API](https://platform.openai.com/docs/api-reference/chatkit/sessions/create) to create a session
24+
- Return `client_secret` to your app/browser
25+
26+
**FastAPI example**
27+
28+
```py
29+
from fastapi import FastAPI, Depends
30+
from openai import OpenAI
31+
import os
32+
33+
app = FastAPI()
34+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
35+
36+
def require_user():
37+
# Replace with your auth logic. Raise if unauthenticated.
38+
return {"user_id": "user_123"}
39+
40+
@app.post("/chatkit/session")
41+
def create_chatkit_session(user=Depends(require_user)):
42+
session = client.beta.chatkit.sessions.create(
43+
workflow={"id": workflow_id}, # your published Agent Builder workflow id
44+
user=user["user_id"],
45+
# chatkit_configuration, expires_after, rate_limits, ...
46+
)
47+
return {"client_secret": session.client_secret}
48+
```
49+
50+
## Configure ChatKit with `getClientSecret`
51+
52+
Wire your session endpoint into ChatKit. The callback pulls a fresh `client_secret` on mount and whenever ChatKit asks for a refresh.
53+
54+
<Tabs syncKey="language">
55+
<TabItem label="React">
56+
```jsx
57+
const chatkit = useChatKit({
58+
api: {
59+
async getClientSecret() {
60+
const res = await fetch('/chatkit/session', { method: 'POST' });
61+
const { client_secret } = await res.json();
62+
return client_secret;
63+
},
64+
},
65+
});
66+
```
67+
68+
</TabItem>
69+
70+
<TabItem label="Vanilla JS">
71+
72+
```js
73+
const chatkit = document.getElementById('my-chat');
74+
75+
chatkit.setOptions({
76+
api: {
77+
async getClientSecret() {
78+
const res = await fetch('/api/chatkit/start', { method: 'POST' });
79+
const { client_secret } = await res.json();
80+
return client_secret;
81+
},
82+
},
83+
});
84+
```
85+
86+
</TabItem>
87+
</Tabs>
88+
89+
## Operational notes
90+
91+
- Keep the ChatKit sessions API settings (history, attachments, etc.) in sync with the options you pass to ChatKit.
92+
- `getClientSecret(currentClientSecret)` runs on mount and before expiry; use the presence of `currentClientSecret` to tell refreshes apart from first-time requests.

packages/docs/src/content/docs/guides/authentication.mdx

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)