@gram-ai/elements 1.0.5 → 1.0.7
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/elements.css +1 -1
- package/dist/elements.js +1643 -1594
- package/dist/index.d.ts +4 -1
- package/dist/lib/chatHandler.d.ts +33 -0
- package/dist/lib/fetchAdapter.d.ts +18 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +66 -0
- package/dist/types/index.d.ts +94 -2
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export { ElementsProvider as GramElementsProvider } from './contexts/ElementsProvider';
|
|
2
2
|
export { useElements as useGramElements } from './contexts/ElementsProvider';
|
|
3
|
-
export { Chat
|
|
3
|
+
export { Chat } from './components/Chat';
|
|
4
|
+
export { createFetchAdapter } from './lib/fetchAdapter';
|
|
5
|
+
export type { FetchAdapterOptions } from './lib/fetchAdapter';
|
|
6
|
+
export type { ElementsConfig, ComposerConfig, ModalConfig, ToolsConfig, ModelConfig, WelcomeConfig, Suggestion, GramRuntimeApi, CustomRuntimeApi, } from './types';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface ChatHandlerOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The API key for the OpenAI-compatible provider.
|
|
4
|
+
* Defaults to OPENAI_API_KEY environment variable.
|
|
5
|
+
*/
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
/**
|
|
8
|
+
* The base URL for the OpenAI-compatible API.
|
|
9
|
+
* Use this for OpenRouter, Azure, or other compatible providers.
|
|
10
|
+
* @example 'https://openrouter.ai/api/v1'
|
|
11
|
+
*/
|
|
12
|
+
baseURL?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The model to use for chat completions.
|
|
15
|
+
* @default 'gpt-4o-mini'
|
|
16
|
+
*/
|
|
17
|
+
model?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a chat handler for Next.js API routes.
|
|
21
|
+
* Returns OpenAI-compatible SSE streaming format for use with assistant-ui.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // app/api/chat/route.ts
|
|
25
|
+
* import { createChatHandler } from '@gram-ai/elements/server'
|
|
26
|
+
*
|
|
27
|
+
* export const POST = createChatHandler({
|
|
28
|
+
* apiKey: process.env.OPENROUTER_API_KEY,
|
|
29
|
+
* baseURL: 'https://openrouter.ai/api/v1',
|
|
30
|
+
* model: 'anthropic/claude-3.5-sonnet',
|
|
31
|
+
* })
|
|
32
|
+
*/
|
|
33
|
+
export declare function createChatHandler(options?: ChatHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ChatModelAdapter } from '@assistant-ui/react';
|
|
2
|
+
export interface FetchAdapterOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The API endpoint to send chat requests to.
|
|
5
|
+
* @default '/api/chat'
|
|
6
|
+
*/
|
|
7
|
+
endpoint?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates a chat adapter that connects to an OpenAI-compatible API endpoint.
|
|
11
|
+
* Handles the SSE streaming format used by OpenAI and compatible providers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { createFetchAdapter } from '@gram-ai/elements'
|
|
15
|
+
*
|
|
16
|
+
* const adapter = createFetchAdapter({ endpoint: '/api/chat' })
|
|
17
|
+
*/
|
|
18
|
+
export declare function createFetchAdapter(options?: FetchAdapterOptions): ChatModelAdapter;
|
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import y from "openai";
|
|
2
|
+
function l(o) {
|
|
3
|
+
return o.map((e) => {
|
|
4
|
+
let t;
|
|
5
|
+
return typeof e.content == "string" ? t = e.content : Array.isArray(e.content) ? t = e.content.filter((n) => n.type === "text" && n.text).map((n) => n.text).join("") : t = "", {
|
|
6
|
+
role: e.role,
|
|
7
|
+
content: t
|
|
8
|
+
};
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function w(o = {}) {
|
|
12
|
+
const {
|
|
13
|
+
apiKey: e = process.env.OPENAI_API_KEY,
|
|
14
|
+
baseURL: t,
|
|
15
|
+
model: n = "gpt-4o-mini"
|
|
16
|
+
} = o, i = new y({
|
|
17
|
+
apiKey: e,
|
|
18
|
+
baseURL: t
|
|
19
|
+
});
|
|
20
|
+
return async function(p) {
|
|
21
|
+
try {
|
|
22
|
+
const { messages: r } = await p.json(), d = await i.chat.completions.create({
|
|
23
|
+
model: n,
|
|
24
|
+
messages: l(r),
|
|
25
|
+
stream: !0
|
|
26
|
+
}), c = new TextEncoder(), u = new ReadableStream({
|
|
27
|
+
async start(a) {
|
|
28
|
+
try {
|
|
29
|
+
for await (const s of d) {
|
|
30
|
+
const f = JSON.stringify(s);
|
|
31
|
+
a.enqueue(c.encode(`data: ${f}
|
|
32
|
+
|
|
33
|
+
`));
|
|
34
|
+
}
|
|
35
|
+
a.enqueue(c.encode(`data: [DONE]
|
|
36
|
+
|
|
37
|
+
`)), a.close();
|
|
38
|
+
} catch (s) {
|
|
39
|
+
a.error(s);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return new Response(u, {
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "text/event-stream",
|
|
46
|
+
"Cache-Control": "no-cache",
|
|
47
|
+
Connection: "keep-alive"
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
} catch (r) {
|
|
51
|
+
return new Response(
|
|
52
|
+
JSON.stringify({
|
|
53
|
+
error: String(r),
|
|
54
|
+
stack: r instanceof Error ? r.stack : void 0
|
|
55
|
+
}),
|
|
56
|
+
{
|
|
57
|
+
status: 500,
|
|
58
|
+
headers: { "Content-Type": "application/json" }
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
w as createChatHandler
|
|
66
|
+
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ChatModelAdapter } from '@assistant-ui/react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
2
|
+
import { ComponentType, ReactNode } from 'react';
|
|
3
3
|
export interface ElementsProviderProps {
|
|
4
4
|
/**
|
|
5
5
|
* The children to render.
|
|
@@ -19,11 +19,103 @@ export interface ElementsConfig {
|
|
|
19
19
|
/**
|
|
20
20
|
* Whether to render the chat window inside of an expandable modal or a standalone chat window.
|
|
21
21
|
*/
|
|
22
|
-
variant?: '
|
|
22
|
+
variant?: 'widget' | 'standalone' | 'sidecar';
|
|
23
|
+
/**
|
|
24
|
+
* LLM model configuration.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const config: ElementsConfig = {
|
|
28
|
+
* model: {
|
|
29
|
+
* availableModels: ['gpt-4o-mini', 'gpt-4o'],
|
|
30
|
+
* showModelPicker: true,
|
|
31
|
+
* },
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
model?: ModelConfig;
|
|
35
|
+
/**
|
|
36
|
+
* The theme to use for the Elements library.
|
|
37
|
+
*/
|
|
38
|
+
theme?: 'light' | 'dark' | 'system';
|
|
39
|
+
/**
|
|
40
|
+
* The configuration for the welcome message and initial suggestions.
|
|
41
|
+
*/
|
|
42
|
+
welcome: WelcomeConfig;
|
|
23
43
|
/**
|
|
24
44
|
* The configuration for the composer.
|
|
25
45
|
*/
|
|
26
46
|
composer?: ComposerConfig;
|
|
47
|
+
/**
|
|
48
|
+
* The configuration for the modal window.
|
|
49
|
+
* Does not apply if variant is 'standalone'.
|
|
50
|
+
*/
|
|
51
|
+
modal?: ModalConfig;
|
|
52
|
+
/**
|
|
53
|
+
* The configuration for the tools.
|
|
54
|
+
*/
|
|
55
|
+
tools?: ToolsConfig;
|
|
56
|
+
}
|
|
57
|
+
export interface ModelConfig {
|
|
58
|
+
/**
|
|
59
|
+
* The models available to the user.
|
|
60
|
+
* TODO: Update type
|
|
61
|
+
*/
|
|
62
|
+
availableModels: string[];
|
|
63
|
+
/**
|
|
64
|
+
* Whether to show the model picker in the composer.
|
|
65
|
+
*/
|
|
66
|
+
showModelPicker?: boolean;
|
|
67
|
+
}
|
|
68
|
+
export interface ToolsConfig {
|
|
69
|
+
/**
|
|
70
|
+
* `components` can be used to override the default components used by the
|
|
71
|
+
* Elements library for a given tool result.
|
|
72
|
+
*
|
|
73
|
+
* Please ensure that the tool name directly matches the tool name in your Gram toolset.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const config: ElementsConfig = {
|
|
77
|
+
* tools: {
|
|
78
|
+
* components: {
|
|
79
|
+
* "get_current_weather": WeatherComponent,
|
|
80
|
+
* },
|
|
81
|
+
* },
|
|
82
|
+
* }
|
|
83
|
+
*/
|
|
84
|
+
components?: Record<string, ComponentType>;
|
|
85
|
+
}
|
|
86
|
+
export interface WelcomeConfig {
|
|
87
|
+
/**
|
|
88
|
+
* The welcome message to display when the thread is empty.
|
|
89
|
+
*/
|
|
90
|
+
title: string;
|
|
91
|
+
/**
|
|
92
|
+
* The subtitle to display when the thread is empty.
|
|
93
|
+
*/
|
|
94
|
+
subtitle: string;
|
|
95
|
+
/**
|
|
96
|
+
* The suggestions to display when the thread is empty.
|
|
97
|
+
*/
|
|
98
|
+
suggestions: Suggestion[];
|
|
99
|
+
}
|
|
100
|
+
export interface Suggestion {
|
|
101
|
+
title: string;
|
|
102
|
+
label: string;
|
|
103
|
+
action: string;
|
|
104
|
+
}
|
|
105
|
+
export interface ModalConfig {
|
|
106
|
+
/**
|
|
107
|
+
* Whether to open the modal window by default.
|
|
108
|
+
*/
|
|
109
|
+
defaultOpen?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* The icon to use for the modal window.
|
|
112
|
+
* Receives the current state of the modal window.
|
|
113
|
+
*/
|
|
114
|
+
icon?: (state: 'open' | 'closed' | undefined) => ReactNode;
|
|
115
|
+
/**
|
|
116
|
+
* A class name to apply to the modal window.
|
|
117
|
+
*/
|
|
118
|
+
className?: string;
|
|
27
119
|
}
|
|
28
120
|
export interface ComposerConfig {
|
|
29
121
|
/**
|
package/package.json
CHANGED
|
@@ -2,13 +2,17 @@
|
|
|
2
2
|
"name": "@gram-ai/elements",
|
|
3
3
|
"description": "Gram Elements is a library of UI primitives for building chat-like experiences for MCP Servers.",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.7",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"import": "./dist/elements.js",
|
|
10
10
|
"types": "./dist/index.d.ts"
|
|
11
11
|
},
|
|
12
|
+
"./server": {
|
|
13
|
+
"import": "./dist/server.js",
|
|
14
|
+
"types": "./dist/server.d.ts"
|
|
15
|
+
},
|
|
12
16
|
"./elements.css": "./dist/elements.css"
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
@@ -23,7 +27,7 @@
|
|
|
23
27
|
},
|
|
24
28
|
"scripts": {
|
|
25
29
|
"build": "vite build",
|
|
26
|
-
"lint": "eslint
|
|
30
|
+
"lint": "eslint src",
|
|
27
31
|
"analyze": "pnpm dlx vite-bundle-visualizer",
|
|
28
32
|
"storybook": "storybook dev -p 6006",
|
|
29
33
|
"build-storybook": "storybook build",
|
|
@@ -55,7 +59,7 @@
|
|
|
55
59
|
"tailwind-merge": "^3.3.1"
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
58
|
-
"@ai-sdk/openai": "2.0.0-beta.5",
|
|
62
|
+
"@ai-sdk/openai": "^2.0.0-beta.5",
|
|
59
63
|
"@assistant-ui/react": "^0.11.37",
|
|
60
64
|
"@assistant-ui/react-markdown": "^0.11.4",
|
|
61
65
|
"@eslint/compat": "^2.0.0",
|
|
@@ -75,6 +79,7 @@
|
|
|
75
79
|
"eslint-plugin-storybook": "^10.1.4",
|
|
76
80
|
"eslint-plugin-unused-imports": "^4.3.0",
|
|
77
81
|
"motion": "^12.23.14",
|
|
82
|
+
"openai": "^6.9.1",
|
|
78
83
|
"prettier": "^3.7.4",
|
|
79
84
|
"prettier-plugin-tailwindcss": "^0.7.2",
|
|
80
85
|
"remark-gfm": "^4.0.1",
|