@an-sdk/nextjs 0.0.1
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/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/index.cjs +25 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/server.cjs +71 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +23 -0
- package/dist/server.d.ts +23 -0
- package/dist/server.js +45 -0
- package/dist/server.js.map +1 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AN Dev (an.dev)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @an-sdk/nextjs
|
|
2
|
+
|
|
3
|
+
Next.js integration for [AN](https://an.dev) AI agent chat. Provides a server-side token handler so your API key never reaches the client.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @an-sdk/nextjs @an-sdk/react ai @ai-sdk/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Set your API key
|
|
14
|
+
|
|
15
|
+
```env
|
|
16
|
+
# .env.local
|
|
17
|
+
AN_API_KEY=an_sk_your_key_here
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Get your API key from [an.dev/agents/dashboard/api](https://an.dev/agents/dashboard/api).
|
|
21
|
+
|
|
22
|
+
### 2. Create the token route (one line)
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// app/api/an/token/route.ts
|
|
26
|
+
import { createAnTokenHandler } from "@an-sdk/nextjs/server"
|
|
27
|
+
|
|
28
|
+
export const POST = createAnTokenHandler({
|
|
29
|
+
apiKey: process.env.AN_API_KEY!,
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 3. Use in your page
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
// app/page.tsx
|
|
37
|
+
"use client"
|
|
38
|
+
|
|
39
|
+
import { useChat } from "@ai-sdk/react"
|
|
40
|
+
import { AnAgentChat, createAnChat } from "@an-sdk/nextjs"
|
|
41
|
+
import "@an-sdk/react/styles.css"
|
|
42
|
+
import { useMemo } from "react"
|
|
43
|
+
|
|
44
|
+
export default function Chat() {
|
|
45
|
+
const chat = useMemo(
|
|
46
|
+
() => createAnChat({
|
|
47
|
+
agent: "your-agent-slug",
|
|
48
|
+
tokenUrl: "/api/an/token",
|
|
49
|
+
}),
|
|
50
|
+
[],
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const { messages, sendMessage, status, stop, error } = useChat({ chat })
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<AnAgentChat
|
|
57
|
+
messages={messages}
|
|
58
|
+
onSend={(msg) =>
|
|
59
|
+
sendMessage({ parts: [{ type: "text", text: msg.content }] })
|
|
60
|
+
}
|
|
61
|
+
status={status}
|
|
62
|
+
onStop={stop}
|
|
63
|
+
error={error}
|
|
64
|
+
/>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
That's it. Your `an_sk_` API key stays on the server. The client only receives short-lived JWTs.
|
|
70
|
+
|
|
71
|
+
## How It Works
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
Browser Your Next.js Server AN Relay
|
|
75
|
+
| | |
|
|
76
|
+
|-- POST /api/an/token --------->| |
|
|
77
|
+
| |-- POST /v1/tokens -------->|
|
|
78
|
+
| | (with an_sk_ key) |
|
|
79
|
+
| |<-- { token, expiresAt } ---|
|
|
80
|
+
|<-- { token, expiresAt } ------| |
|
|
81
|
+
| |
|
|
82
|
+
|-- POST /v1/chat/:agent ------(with short-lived JWT)------->|
|
|
83
|
+
|<-- streaming response -----(SSE)---------------------------|
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Adding Your Own Auth
|
|
87
|
+
|
|
88
|
+
For custom auth (Clerk, NextAuth, etc.), use `exchangeToken` directly:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
// app/api/an/token/route.ts
|
|
92
|
+
import { auth } from "@clerk/nextjs/server"
|
|
93
|
+
import { exchangeToken } from "@an-sdk/nextjs/server"
|
|
94
|
+
|
|
95
|
+
export async function POST(req: Request) {
|
|
96
|
+
const { userId } = await auth()
|
|
97
|
+
if (!userId) {
|
|
98
|
+
return Response.json({ error: "Unauthorized" }, { status: 401 })
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const { agent } = await req.json()
|
|
102
|
+
|
|
103
|
+
const data = await exchangeToken({
|
|
104
|
+
apiKey: process.env.AN_API_KEY!,
|
|
105
|
+
agent,
|
|
106
|
+
userId,
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
return Response.json(data)
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API
|
|
114
|
+
|
|
115
|
+
### `createAnTokenHandler(options)` — Server
|
|
116
|
+
|
|
117
|
+
Creates a Next.js `POST` route handler.
|
|
118
|
+
|
|
119
|
+
| Option | Type | Default | Description |
|
|
120
|
+
|--------|------|---------|-------------|
|
|
121
|
+
| `apiKey` | `string` | required | Your `an_sk_` API key |
|
|
122
|
+
| `relayUrl` | `string` | `"https://relay.an.dev"` | Relay API URL |
|
|
123
|
+
| `expiresIn` | `string` | `"1h"` | Token expiry |
|
|
124
|
+
|
|
125
|
+
### `exchangeToken(options)` — Server
|
|
126
|
+
|
|
127
|
+
Low-level function to exchange an API key for a JWT.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const { token, expiresAt } = await exchangeToken({
|
|
131
|
+
apiKey: process.env.AN_API_KEY!,
|
|
132
|
+
agent: "my-agent",
|
|
133
|
+
userId: "user-123",
|
|
134
|
+
})
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Re-exports
|
|
138
|
+
|
|
139
|
+
`@an-sdk/nextjs` re-exports everything from `@an-sdk/react`:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { AnAgentChat, createAnChat, applyTheme } from "@an-sdk/nextjs"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
|
|
17
|
+
// src/index.ts
|
|
18
|
+
var src_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(src_exports);
|
|
20
|
+
__reExport(src_exports, require("@an-sdk/react"), module.exports);
|
|
21
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
+
0 && (module.exports = {
|
|
23
|
+
...require("@an-sdk/react")
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Re-export everything from @an-sdk/react for convenience\nexport * from \"@an-sdk/react\"\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AACA,wBAAc,0BADd;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@an-sdk/react';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@an-sdk/react';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Re-export everything from @an-sdk/react for convenience\nexport * from \"@an-sdk/react\"\n"],"mappings":";AACA,cAAc;","names":[]}
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
createAnTokenHandler: () => createAnTokenHandler,
|
|
24
|
+
exchangeToken: () => exchangeToken
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(server_exports);
|
|
27
|
+
async function exchangeToken(options) {
|
|
28
|
+
const {
|
|
29
|
+
apiKey,
|
|
30
|
+
relayUrl = "https://relay.an.dev",
|
|
31
|
+
expiresIn = "1h",
|
|
32
|
+
agent,
|
|
33
|
+
userId
|
|
34
|
+
} = options;
|
|
35
|
+
const res = await fetch(`${relayUrl}/v1/tokens`, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: {
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
Authorization: `Bearer ${apiKey}`
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
userId,
|
|
43
|
+
agents: agent ? [agent] : void 0,
|
|
44
|
+
expiresIn
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
const err = await res.json().catch(() => ({ error: "Failed to exchange token" }));
|
|
49
|
+
throw new Error(err.error || `Token exchange failed: ${res.status}`);
|
|
50
|
+
}
|
|
51
|
+
return res.json();
|
|
52
|
+
}
|
|
53
|
+
function createAnTokenHandler(options) {
|
|
54
|
+
return async function POST(req) {
|
|
55
|
+
try {
|
|
56
|
+
const body = await req.json().catch(() => ({}));
|
|
57
|
+
const { agent, userId } = body;
|
|
58
|
+
const data = await exchangeToken({ ...options, agent, userId });
|
|
59
|
+
return Response.json(data);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
const message = err instanceof Error ? err.message : "Internal error";
|
|
62
|
+
return Response.json({ error: message }, { status: 500 });
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {
|
|
68
|
+
createAnTokenHandler,
|
|
69
|
+
exchangeToken
|
|
70
|
+
});
|
|
71
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server.ts"],"sourcesContent":["export interface TokenHandlerOptions {\n /** Your an_sk_ API key — keep in process.env, never expose to client */\n apiKey: string\n /** Relay URL. Default: \"https://relay.an.dev\" */\n relayUrl?: string\n /** Token expiry. Default: \"1h\" */\n expiresIn?: string\n}\n\nexport interface ExchangeTokenOptions extends TokenHandlerOptions {\n /** Agent slug to scope the token to */\n agent?: string\n /** User identifier for the token */\n userId?: string\n}\n\n/** Exchange an an_sk_ API key for a short-lived JWT via the relay */\nexport async function exchangeToken(options: ExchangeTokenOptions) {\n const {\n apiKey,\n relayUrl = \"https://relay.an.dev\",\n expiresIn = \"1h\",\n agent,\n userId,\n } = options\n\n const res = await fetch(`${relayUrl}/v1/tokens`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({\n userId,\n agents: agent ? [agent] : undefined,\n expiresIn,\n }),\n })\n\n if (!res.ok) {\n const err = await res.json().catch(() => ({ error: \"Failed to exchange token\" }))\n throw new Error(err.error || `Token exchange failed: ${res.status}`)\n }\n\n return res.json() as Promise<{ token: string; expiresAt: string }>\n}\n\n/** Create a Next.js API route handler that exchanges an_sk_ keys for JWTs */\nexport function createAnTokenHandler(options: TokenHandlerOptions) {\n return async function POST(req: Request) {\n try {\n const body = await req.json().catch(() => ({}))\n const { agent, userId } = body as { agent?: string; userId?: string }\n\n const data = await exchangeToken({ ...options, agent, userId })\n return Response.json(data)\n } catch (err) {\n const message = err instanceof Error ? err.message : \"Internal error\"\n return Response.json({ error: message }, { status: 500 })\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,eAAsB,cAAc,SAA+B;AACjE,QAAM;AAAA,IACJ;AAAA,IACA,WAAW;AAAA,IACX,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,MAAM,MAAM,MAAM,GAAG,QAAQ,cAAc;AAAA,IAC/C,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,eAAe,UAAU,MAAM;AAAA,IACjC;AAAA,IACA,MAAM,KAAK,UAAU;AAAA,MACnB;AAAA,MACA,QAAQ,QAAQ,CAAC,KAAK,IAAI;AAAA,MAC1B;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,MAAM,MAAM,IAAI,KAAK,EAAE,MAAM,OAAO,EAAE,OAAO,2BAA2B,EAAE;AAChF,UAAM,IAAI,MAAM,IAAI,SAAS,0BAA0B,IAAI,MAAM,EAAE;AAAA,EACrE;AAEA,SAAO,IAAI,KAAK;AAClB;AAGO,SAAS,qBAAqB,SAA8B;AACjE,SAAO,eAAe,KAAK,KAAc;AACvC,QAAI;AACF,YAAM,OAAO,MAAM,IAAI,KAAK,EAAE,MAAM,OAAO,CAAC,EAAE;AAC9C,YAAM,EAAE,OAAO,OAAO,IAAI;AAE1B,YAAM,OAAO,MAAM,cAAc,EAAE,GAAG,SAAS,OAAO,OAAO,CAAC;AAC9D,aAAO,SAAS,KAAK,IAAI;AAAA,IAC3B,SAAS,KAAK;AACZ,YAAM,UAAU,eAAe,QAAQ,IAAI,UAAU;AACrD,aAAO,SAAS,KAAK,EAAE,OAAO,QAAQ,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface TokenHandlerOptions {
|
|
2
|
+
/** Your an_sk_ API key — keep in process.env, never expose to client */
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Relay URL. Default: "https://relay.an.dev" */
|
|
5
|
+
relayUrl?: string;
|
|
6
|
+
/** Token expiry. Default: "1h" */
|
|
7
|
+
expiresIn?: string;
|
|
8
|
+
}
|
|
9
|
+
interface ExchangeTokenOptions extends TokenHandlerOptions {
|
|
10
|
+
/** Agent slug to scope the token to */
|
|
11
|
+
agent?: string;
|
|
12
|
+
/** User identifier for the token */
|
|
13
|
+
userId?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Exchange an an_sk_ API key for a short-lived JWT via the relay */
|
|
16
|
+
declare function exchangeToken(options: ExchangeTokenOptions): Promise<{
|
|
17
|
+
token: string;
|
|
18
|
+
expiresAt: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** Create a Next.js API route handler that exchanges an_sk_ keys for JWTs */
|
|
21
|
+
declare function createAnTokenHandler(options: TokenHandlerOptions): (req: Request) => Promise<Response>;
|
|
22
|
+
|
|
23
|
+
export { type ExchangeTokenOptions, type TokenHandlerOptions, createAnTokenHandler, exchangeToken };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface TokenHandlerOptions {
|
|
2
|
+
/** Your an_sk_ API key — keep in process.env, never expose to client */
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Relay URL. Default: "https://relay.an.dev" */
|
|
5
|
+
relayUrl?: string;
|
|
6
|
+
/** Token expiry. Default: "1h" */
|
|
7
|
+
expiresIn?: string;
|
|
8
|
+
}
|
|
9
|
+
interface ExchangeTokenOptions extends TokenHandlerOptions {
|
|
10
|
+
/** Agent slug to scope the token to */
|
|
11
|
+
agent?: string;
|
|
12
|
+
/** User identifier for the token */
|
|
13
|
+
userId?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Exchange an an_sk_ API key for a short-lived JWT via the relay */
|
|
16
|
+
declare function exchangeToken(options: ExchangeTokenOptions): Promise<{
|
|
17
|
+
token: string;
|
|
18
|
+
expiresAt: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** Create a Next.js API route handler that exchanges an_sk_ keys for JWTs */
|
|
21
|
+
declare function createAnTokenHandler(options: TokenHandlerOptions): (req: Request) => Promise<Response>;
|
|
22
|
+
|
|
23
|
+
export { type ExchangeTokenOptions, type TokenHandlerOptions, createAnTokenHandler, exchangeToken };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
async function exchangeToken(options) {
|
|
3
|
+
const {
|
|
4
|
+
apiKey,
|
|
5
|
+
relayUrl = "https://relay.an.dev",
|
|
6
|
+
expiresIn = "1h",
|
|
7
|
+
agent,
|
|
8
|
+
userId
|
|
9
|
+
} = options;
|
|
10
|
+
const res = await fetch(`${relayUrl}/v1/tokens`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
Authorization: `Bearer ${apiKey}`
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify({
|
|
17
|
+
userId,
|
|
18
|
+
agents: agent ? [agent] : void 0,
|
|
19
|
+
expiresIn
|
|
20
|
+
})
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const err = await res.json().catch(() => ({ error: "Failed to exchange token" }));
|
|
24
|
+
throw new Error(err.error || `Token exchange failed: ${res.status}`);
|
|
25
|
+
}
|
|
26
|
+
return res.json();
|
|
27
|
+
}
|
|
28
|
+
function createAnTokenHandler(options) {
|
|
29
|
+
return async function POST(req) {
|
|
30
|
+
try {
|
|
31
|
+
const body = await req.json().catch(() => ({}));
|
|
32
|
+
const { agent, userId } = body;
|
|
33
|
+
const data = await exchangeToken({ ...options, agent, userId });
|
|
34
|
+
return Response.json(data);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
const message = err instanceof Error ? err.message : "Internal error";
|
|
37
|
+
return Response.json({ error: message }, { status: 500 });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
createAnTokenHandler,
|
|
43
|
+
exchangeToken
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server.ts"],"sourcesContent":["export interface TokenHandlerOptions {\n /** Your an_sk_ API key — keep in process.env, never expose to client */\n apiKey: string\n /** Relay URL. Default: \"https://relay.an.dev\" */\n relayUrl?: string\n /** Token expiry. Default: \"1h\" */\n expiresIn?: string\n}\n\nexport interface ExchangeTokenOptions extends TokenHandlerOptions {\n /** Agent slug to scope the token to */\n agent?: string\n /** User identifier for the token */\n userId?: string\n}\n\n/** Exchange an an_sk_ API key for a short-lived JWT via the relay */\nexport async function exchangeToken(options: ExchangeTokenOptions) {\n const {\n apiKey,\n relayUrl = \"https://relay.an.dev\",\n expiresIn = \"1h\",\n agent,\n userId,\n } = options\n\n const res = await fetch(`${relayUrl}/v1/tokens`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({\n userId,\n agents: agent ? [agent] : undefined,\n expiresIn,\n }),\n })\n\n if (!res.ok) {\n const err = await res.json().catch(() => ({ error: \"Failed to exchange token\" }))\n throw new Error(err.error || `Token exchange failed: ${res.status}`)\n }\n\n return res.json() as Promise<{ token: string; expiresAt: string }>\n}\n\n/** Create a Next.js API route handler that exchanges an_sk_ keys for JWTs */\nexport function createAnTokenHandler(options: TokenHandlerOptions) {\n return async function POST(req: Request) {\n try {\n const body = await req.json().catch(() => ({}))\n const { agent, userId } = body as { agent?: string; userId?: string }\n\n const data = await exchangeToken({ ...options, agent, userId })\n return Response.json(data)\n } catch (err) {\n const message = err instanceof Error ? err.message : \"Internal error\"\n return Response.json({ error: message }, { status: 500 })\n }\n }\n}\n"],"mappings":";AAiBA,eAAsB,cAAc,SAA+B;AACjE,QAAM;AAAA,IACJ;AAAA,IACA,WAAW;AAAA,IACX,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,MAAM,MAAM,MAAM,GAAG,QAAQ,cAAc;AAAA,IAC/C,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,eAAe,UAAU,MAAM;AAAA,IACjC;AAAA,IACA,MAAM,KAAK,UAAU;AAAA,MACnB;AAAA,MACA,QAAQ,QAAQ,CAAC,KAAK,IAAI;AAAA,MAC1B;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,MAAM,MAAM,IAAI,KAAK,EAAE,MAAM,OAAO,EAAE,OAAO,2BAA2B,EAAE;AAChF,UAAM,IAAI,MAAM,IAAI,SAAS,0BAA0B,IAAI,MAAM,EAAE;AAAA,EACrE;AAEA,SAAO,IAAI,KAAK;AAClB;AAGO,SAAS,qBAAqB,SAA8B;AACjE,SAAO,eAAe,KAAK,KAAc;AACvC,QAAI;AACF,YAAM,OAAO,MAAM,IAAI,KAAK,EAAE,MAAM,OAAO,CAAC,EAAE;AAC9C,YAAM,EAAE,OAAO,OAAO,IAAI;AAE1B,YAAM,OAAO,MAAM,cAAc,EAAE,GAAG,SAAS,OAAO,OAAO,CAAC;AAC9D,aAAO,SAAS,KAAK,IAAI;AAAA,IAC3B,SAAS,KAAK;AACZ,YAAM,UAAU,eAAe,QAAQ,IAAI,UAAU;AACrD,aAAO,SAAS,KAAK,EAAE,OAAO,QAAQ,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC1D;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@an-sdk/nextjs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Next.js integration for AN AI agent chat — server-side token handler",
|
|
6
|
+
"homepage": "https://an.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/21st-dev/an-sdk"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"require": {
|
|
22
|
+
"types": "./dist/index.d.cts",
|
|
23
|
+
"default": "./dist/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"./server": {
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/server.d.ts",
|
|
29
|
+
"default": "./dist/server.js"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/server.d.cts",
|
|
33
|
+
"default": "./dist/server.cjs"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"keywords": [
|
|
43
|
+
"nextjs",
|
|
44
|
+
"react",
|
|
45
|
+
"ai",
|
|
46
|
+
"agent",
|
|
47
|
+
"chat",
|
|
48
|
+
"ai-sdk",
|
|
49
|
+
"an",
|
|
50
|
+
"an.dev"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup",
|
|
54
|
+
"lint": "tsc --noEmit"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@an-sdk/react": "^0.1.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"next": ">=14.0.0",
|
|
61
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
62
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@types/react": "^19.0.0",
|
|
66
|
+
"next": "^15.0.0",
|
|
67
|
+
"react": "^19.0.0",
|
|
68
|
+
"react-dom": "^19.0.0",
|
|
69
|
+
"tsup": "^8.0.0",
|
|
70
|
+
"typescript": "^5.4.5"
|
|
71
|
+
}
|
|
72
|
+
}
|