@lov3kaizen/agentsea-react 0.1.0
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 +85 -0
- package/dist/index.d.mts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +268 -0
- package/dist/index.mjs +228 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 lovekaizen
|
|
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,85 @@
|
|
|
1
|
+
# @agentsea/react
|
|
2
|
+
|
|
3
|
+
React components for rendering AgentSea agent responses with formatting support.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@agentsea/react)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Markdown rendering with GitHub Flavored Markdown support
|
|
12
|
+
- Code syntax highlighting
|
|
13
|
+
- Streaming response support
|
|
14
|
+
- Customizable styling
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @agentsea/react @agentsea/core react react-dom
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @agentsea/react @agentsea/core react react-dom
|
|
22
|
+
# or
|
|
23
|
+
yarn add @agentsea/react @agentsea/core react react-dom
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { AgentResponse } from '@agentsea/react';
|
|
30
|
+
|
|
31
|
+
function ChatMessage({ content }: { content: string }) {
|
|
32
|
+
return <AgentResponse content={content} />;
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Components
|
|
37
|
+
|
|
38
|
+
### AgentResponse
|
|
39
|
+
|
|
40
|
+
Renders agent response content with markdown formatting:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { AgentResponse } from '@agentsea/react';
|
|
44
|
+
|
|
45
|
+
<AgentResponse
|
|
46
|
+
content="# Hello World\n\nThis is **markdown** content."
|
|
47
|
+
className="my-response"
|
|
48
|
+
/>;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### StreamingResponse
|
|
52
|
+
|
|
53
|
+
For streaming responses:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { StreamingResponse } from '@agentsea/react';
|
|
57
|
+
|
|
58
|
+
<StreamingResponse content={streamingContent} isStreaming={true} />;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Styling
|
|
62
|
+
|
|
63
|
+
Components can be styled using CSS classes or inline styles:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
<AgentResponse
|
|
67
|
+
content={content}
|
|
68
|
+
className="custom-response"
|
|
69
|
+
style={{ maxWidth: '600px' }}
|
|
70
|
+
/>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Documentation
|
|
74
|
+
|
|
75
|
+
Full documentation available at [agentsea.dev](https://agentsea.dev)
|
|
76
|
+
|
|
77
|
+
## Related Packages
|
|
78
|
+
|
|
79
|
+
- [@agentsea/core](https://www.npmjs.com/package/@agentsea/core) - Core library
|
|
80
|
+
- [@agentsea/nestjs](https://www.npmjs.com/package/@agentsea/nestjs) - NestJS integration
|
|
81
|
+
- [@agentsea/cli](https://www.npmjs.com/package/@agentsea/cli) - Command-line interface
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT License - see [LICENSE](../../LICENSE) for details
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactMarkdown from 'react-markdown';
|
|
3
|
+
import { AgentResponse as AgentResponse$1, FormattedContent, StreamEvent } from '@lov3kaizen/agentsea-core';
|
|
4
|
+
|
|
5
|
+
interface AgentResponseProps {
|
|
6
|
+
response: AgentResponse$1;
|
|
7
|
+
className?: string;
|
|
8
|
+
showMetadata?: boolean;
|
|
9
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
10
|
+
components?: React.ComponentProps<typeof ReactMarkdown>['components'];
|
|
11
|
+
}
|
|
12
|
+
declare const AgentResponse: React.FC<AgentResponseProps>;
|
|
13
|
+
declare const useFormattedContent: (content: string, format?: "text" | "markdown" | "html" | "react") => FormattedContent | null;
|
|
14
|
+
|
|
15
|
+
interface StreamingResponseProps {
|
|
16
|
+
stream: AsyncIterable<StreamEvent>;
|
|
17
|
+
className?: string;
|
|
18
|
+
showMetadata?: boolean;
|
|
19
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
20
|
+
onComplete?: (content: string) => void;
|
|
21
|
+
}
|
|
22
|
+
declare const StreamingResponse: React.FC<StreamingResponseProps>;
|
|
23
|
+
declare const useStreamingContent: () => {
|
|
24
|
+
content: string;
|
|
25
|
+
isStreaming: boolean;
|
|
26
|
+
metadata: any;
|
|
27
|
+
consumeStream: (stream: AsyncIterable<StreamEvent>) => Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { AgentResponse, type AgentResponseProps, StreamingResponse, type StreamingResponseProps, useFormattedContent, useStreamingContent };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactMarkdown from 'react-markdown';
|
|
3
|
+
import { AgentResponse as AgentResponse$1, FormattedContent, StreamEvent } from '@lov3kaizen/agentsea-core';
|
|
4
|
+
|
|
5
|
+
interface AgentResponseProps {
|
|
6
|
+
response: AgentResponse$1;
|
|
7
|
+
className?: string;
|
|
8
|
+
showMetadata?: boolean;
|
|
9
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
10
|
+
components?: React.ComponentProps<typeof ReactMarkdown>['components'];
|
|
11
|
+
}
|
|
12
|
+
declare const AgentResponse: React.FC<AgentResponseProps>;
|
|
13
|
+
declare const useFormattedContent: (content: string, format?: "text" | "markdown" | "html" | "react") => FormattedContent | null;
|
|
14
|
+
|
|
15
|
+
interface StreamingResponseProps {
|
|
16
|
+
stream: AsyncIterable<StreamEvent>;
|
|
17
|
+
className?: string;
|
|
18
|
+
showMetadata?: boolean;
|
|
19
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
20
|
+
onComplete?: (content: string) => void;
|
|
21
|
+
}
|
|
22
|
+
declare const StreamingResponse: React.FC<StreamingResponseProps>;
|
|
23
|
+
declare const useStreamingContent: () => {
|
|
24
|
+
content: string;
|
|
25
|
+
isStreaming: boolean;
|
|
26
|
+
metadata: any;
|
|
27
|
+
consumeStream: (stream: AsyncIterable<StreamEvent>) => Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { AgentResponse, type AgentResponseProps, StreamingResponse, type StreamingResponseProps, useFormattedContent, useStreamingContent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AgentResponse: () => AgentResponse,
|
|
34
|
+
StreamingResponse: () => StreamingResponse,
|
|
35
|
+
useFormattedContent: () => useFormattedContent,
|
|
36
|
+
useStreamingContent: () => useStreamingContent
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/AgentResponse.tsx
|
|
41
|
+
var import_react = __toESM(require("react"));
|
|
42
|
+
var import_react_markdown = __toESM(require("react-markdown"));
|
|
43
|
+
var import_remark_gfm = __toESM(require("remark-gfm"));
|
|
44
|
+
var import_rehype_highlight = __toESM(require("rehype-highlight"));
|
|
45
|
+
var import_rehype_raw = __toESM(require("rehype-raw"));
|
|
46
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
47
|
+
var AgentResponse = ({
|
|
48
|
+
response,
|
|
49
|
+
className = "",
|
|
50
|
+
showMetadata = false,
|
|
51
|
+
theme = "auto",
|
|
52
|
+
components
|
|
53
|
+
}) => {
|
|
54
|
+
const formatted = response.formatted;
|
|
55
|
+
const content = formatted?.raw || response.content;
|
|
56
|
+
const shouldRenderMarkdown = !formatted || formatted.format === "markdown" || formatted.format === "react";
|
|
57
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `agentsea-response ${className}`, "data-theme": theme, children: [
|
|
58
|
+
shouldRenderMarkdown ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
59
|
+
import_react_markdown.default,
|
|
60
|
+
{
|
|
61
|
+
remarkPlugins: [import_remark_gfm.default],
|
|
62
|
+
rehypePlugins: [import_rehype_highlight.default, import_rehype_raw.default],
|
|
63
|
+
components,
|
|
64
|
+
children: content
|
|
65
|
+
}
|
|
66
|
+
) : formatted?.format === "html" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
67
|
+
"div",
|
|
68
|
+
{
|
|
69
|
+
dangerouslySetInnerHTML: { __html: formatted.rendered || content }
|
|
70
|
+
}
|
|
71
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "agentsea-text-content", children: content }),
|
|
72
|
+
showMetadata && response.metadata && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "agentsea-metadata", children: [
|
|
73
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "metadata-item", children: [
|
|
74
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "label", children: "Tokens:" }),
|
|
75
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "value", children: response.metadata.tokensUsed })
|
|
76
|
+
] }),
|
|
77
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "metadata-item", children: [
|
|
78
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "label", children: "Latency:" }),
|
|
79
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "value", children: [
|
|
80
|
+
response.metadata.latencyMs,
|
|
81
|
+
"ms"
|
|
82
|
+
] })
|
|
83
|
+
] }),
|
|
84
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "metadata-item", children: [
|
|
85
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "label", children: "Iterations:" }),
|
|
86
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "value", children: response.metadata.iterations })
|
|
87
|
+
] }),
|
|
88
|
+
response.metadata.cost && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "metadata-item", children: [
|
|
89
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "label", children: "Cost:" }),
|
|
90
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "value", children: [
|
|
91
|
+
"$",
|
|
92
|
+
response.metadata.cost.toFixed(4)
|
|
93
|
+
] })
|
|
94
|
+
] })
|
|
95
|
+
] }),
|
|
96
|
+
showMetadata && formatted?.metadata && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "agentsea-content-metadata", children: [
|
|
97
|
+
formatted.metadata.hasCodeBlocks && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "badge", children: "Code" }),
|
|
98
|
+
formatted.metadata.hasTables && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "badge", children: "Tables" }),
|
|
99
|
+
formatted.metadata.hasLists && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "badge", children: "Lists" }),
|
|
100
|
+
formatted.metadata.links && formatted.metadata.links.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "badge", children: [
|
|
101
|
+
formatted.metadata.links.length,
|
|
102
|
+
" Links"
|
|
103
|
+
] })
|
|
104
|
+
] })
|
|
105
|
+
] });
|
|
106
|
+
};
|
|
107
|
+
var useFormattedContent = (content, format) => {
|
|
108
|
+
const [formatted, setFormatted] = import_react.default.useState(
|
|
109
|
+
null
|
|
110
|
+
);
|
|
111
|
+
import_react.default.useEffect(() => {
|
|
112
|
+
setFormatted({
|
|
113
|
+
raw: content,
|
|
114
|
+
format: format || "markdown",
|
|
115
|
+
rendered: content
|
|
116
|
+
});
|
|
117
|
+
}, [content, format]);
|
|
118
|
+
return formatted;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/StreamingResponse.tsx
|
|
122
|
+
var import_react2 = __toESM(require("react"));
|
|
123
|
+
var import_react_markdown2 = __toESM(require("react-markdown"));
|
|
124
|
+
var import_remark_gfm2 = __toESM(require("remark-gfm"));
|
|
125
|
+
var import_rehype_highlight2 = __toESM(require("rehype-highlight"));
|
|
126
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
127
|
+
var StreamingResponse = ({
|
|
128
|
+
stream,
|
|
129
|
+
className = "",
|
|
130
|
+
showMetadata = false,
|
|
131
|
+
theme = "auto",
|
|
132
|
+
onComplete
|
|
133
|
+
}) => {
|
|
134
|
+
const [content, setContent] = import_react2.default.useState("");
|
|
135
|
+
const [metadata, setMetadata] = import_react2.default.useState(null);
|
|
136
|
+
const [isComplete, setIsComplete] = import_react2.default.useState(false);
|
|
137
|
+
const [currentIteration, setCurrentIteration] = import_react2.default.useState(0);
|
|
138
|
+
import_react2.default.useEffect(() => {
|
|
139
|
+
let isMounted = true;
|
|
140
|
+
const consumeStream = async () => {
|
|
141
|
+
try {
|
|
142
|
+
for await (const event of stream) {
|
|
143
|
+
if (!isMounted) break;
|
|
144
|
+
switch (event.type) {
|
|
145
|
+
case "iteration":
|
|
146
|
+
setCurrentIteration(event.iteration);
|
|
147
|
+
break;
|
|
148
|
+
case "content":
|
|
149
|
+
if (event.delta) {
|
|
150
|
+
setContent((prev) => prev + (event.content || ""));
|
|
151
|
+
} else {
|
|
152
|
+
setContent(event.content || "");
|
|
153
|
+
}
|
|
154
|
+
break;
|
|
155
|
+
case "done":
|
|
156
|
+
setMetadata(event.metadata);
|
|
157
|
+
setIsComplete(true);
|
|
158
|
+
if (onComplete) {
|
|
159
|
+
onComplete(content);
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
case "error":
|
|
163
|
+
console.error("Stream error:", event.error);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error("Error consuming stream:", error);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
consumeStream();
|
|
172
|
+
return () => {
|
|
173
|
+
isMounted = false;
|
|
174
|
+
};
|
|
175
|
+
}, [stream, onComplete, content]);
|
|
176
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
177
|
+
"div",
|
|
178
|
+
{
|
|
179
|
+
className: `agentsea-streaming-response ${className}`,
|
|
180
|
+
"data-theme": theme,
|
|
181
|
+
children: [
|
|
182
|
+
!isComplete && showMetadata && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "streaming-indicator", children: [
|
|
183
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "spinner" }),
|
|
184
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { children: [
|
|
185
|
+
"Iteration ",
|
|
186
|
+
currentIteration
|
|
187
|
+
] })
|
|
188
|
+
] }),
|
|
189
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
190
|
+
import_react_markdown2.default,
|
|
191
|
+
{
|
|
192
|
+
remarkPlugins: [import_remark_gfm2.default],
|
|
193
|
+
rehypePlugins: [import_rehype_highlight2.default],
|
|
194
|
+
children: content
|
|
195
|
+
}
|
|
196
|
+
),
|
|
197
|
+
!isComplete && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "cursor", children: "\u258A" }),
|
|
198
|
+
isComplete && showMetadata && metadata && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "agentsea-metadata", children: [
|
|
199
|
+
metadata.tokensUsed && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "metadata-item", children: [
|
|
200
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "label", children: "Tokens:" }),
|
|
201
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "value", children: metadata.tokensUsed })
|
|
202
|
+
] }),
|
|
203
|
+
metadata.latencyMs && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "metadata-item", children: [
|
|
204
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "label", children: "Latency:" }),
|
|
205
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "value", children: [
|
|
206
|
+
metadata.latencyMs,
|
|
207
|
+
"ms"
|
|
208
|
+
] })
|
|
209
|
+
] }),
|
|
210
|
+
metadata.iterations && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "metadata-item", children: [
|
|
211
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "label", children: "Iterations:" }),
|
|
212
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "value", children: metadata.iterations })
|
|
213
|
+
] })
|
|
214
|
+
] })
|
|
215
|
+
]
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
var useStreamingContent = () => {
|
|
220
|
+
const [content, setContent] = import_react2.default.useState("");
|
|
221
|
+
const [isStreaming, setIsStreaming] = import_react2.default.useState(false);
|
|
222
|
+
const [metadata, setMetadata] = import_react2.default.useState(null);
|
|
223
|
+
const consumeStream = import_react2.default.useCallback(
|
|
224
|
+
async (stream) => {
|
|
225
|
+
setIsStreaming(true);
|
|
226
|
+
setContent("");
|
|
227
|
+
setMetadata(null);
|
|
228
|
+
try {
|
|
229
|
+
for await (const event of stream) {
|
|
230
|
+
switch (event.type) {
|
|
231
|
+
case "content":
|
|
232
|
+
if (event.delta) {
|
|
233
|
+
setContent((prev) => prev + (event.content || ""));
|
|
234
|
+
} else {
|
|
235
|
+
setContent(event.content || "");
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
case "done":
|
|
239
|
+
setMetadata(event.metadata);
|
|
240
|
+
setIsStreaming(false);
|
|
241
|
+
break;
|
|
242
|
+
case "error":
|
|
243
|
+
console.error("Stream error:", event.error);
|
|
244
|
+
setIsStreaming(false);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.error("Error consuming stream:", error);
|
|
250
|
+
setIsStreaming(false);
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
[]
|
|
254
|
+
);
|
|
255
|
+
return {
|
|
256
|
+
content,
|
|
257
|
+
isStreaming,
|
|
258
|
+
metadata,
|
|
259
|
+
consumeStream
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
263
|
+
0 && (module.exports = {
|
|
264
|
+
AgentResponse,
|
|
265
|
+
StreamingResponse,
|
|
266
|
+
useFormattedContent,
|
|
267
|
+
useStreamingContent
|
|
268
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// src/AgentResponse.tsx
|
|
2
|
+
import React from "react";
|
|
3
|
+
import ReactMarkdown from "react-markdown";
|
|
4
|
+
import remarkGfm from "remark-gfm";
|
|
5
|
+
import rehypeHighlight from "rehype-highlight";
|
|
6
|
+
import rehypeRaw from "rehype-raw";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
var AgentResponse = ({
|
|
9
|
+
response,
|
|
10
|
+
className = "",
|
|
11
|
+
showMetadata = false,
|
|
12
|
+
theme = "auto",
|
|
13
|
+
components
|
|
14
|
+
}) => {
|
|
15
|
+
const formatted = response.formatted;
|
|
16
|
+
const content = formatted?.raw || response.content;
|
|
17
|
+
const shouldRenderMarkdown = !formatted || formatted.format === "markdown" || formatted.format === "react";
|
|
18
|
+
return /* @__PURE__ */ jsxs("div", { className: `agentsea-response ${className}`, "data-theme": theme, children: [
|
|
19
|
+
shouldRenderMarkdown ? /* @__PURE__ */ jsx(
|
|
20
|
+
ReactMarkdown,
|
|
21
|
+
{
|
|
22
|
+
remarkPlugins: [remarkGfm],
|
|
23
|
+
rehypePlugins: [rehypeHighlight, rehypeRaw],
|
|
24
|
+
components,
|
|
25
|
+
children: content
|
|
26
|
+
}
|
|
27
|
+
) : formatted?.format === "html" ? /* @__PURE__ */ jsx(
|
|
28
|
+
"div",
|
|
29
|
+
{
|
|
30
|
+
dangerouslySetInnerHTML: { __html: formatted.rendered || content }
|
|
31
|
+
}
|
|
32
|
+
) : /* @__PURE__ */ jsx("div", { className: "agentsea-text-content", children: content }),
|
|
33
|
+
showMetadata && response.metadata && /* @__PURE__ */ jsxs("div", { className: "agentsea-metadata", children: [
|
|
34
|
+
/* @__PURE__ */ jsxs("div", { className: "metadata-item", children: [
|
|
35
|
+
/* @__PURE__ */ jsx("span", { className: "label", children: "Tokens:" }),
|
|
36
|
+
/* @__PURE__ */ jsx("span", { className: "value", children: response.metadata.tokensUsed })
|
|
37
|
+
] }),
|
|
38
|
+
/* @__PURE__ */ jsxs("div", { className: "metadata-item", children: [
|
|
39
|
+
/* @__PURE__ */ jsx("span", { className: "label", children: "Latency:" }),
|
|
40
|
+
/* @__PURE__ */ jsxs("span", { className: "value", children: [
|
|
41
|
+
response.metadata.latencyMs,
|
|
42
|
+
"ms"
|
|
43
|
+
] })
|
|
44
|
+
] }),
|
|
45
|
+
/* @__PURE__ */ jsxs("div", { className: "metadata-item", children: [
|
|
46
|
+
/* @__PURE__ */ jsx("span", { className: "label", children: "Iterations:" }),
|
|
47
|
+
/* @__PURE__ */ jsx("span", { className: "value", children: response.metadata.iterations })
|
|
48
|
+
] }),
|
|
49
|
+
response.metadata.cost && /* @__PURE__ */ jsxs("div", { className: "metadata-item", children: [
|
|
50
|
+
/* @__PURE__ */ jsx("span", { className: "label", children: "Cost:" }),
|
|
51
|
+
/* @__PURE__ */ jsxs("span", { className: "value", children: [
|
|
52
|
+
"$",
|
|
53
|
+
response.metadata.cost.toFixed(4)
|
|
54
|
+
] })
|
|
55
|
+
] })
|
|
56
|
+
] }),
|
|
57
|
+
showMetadata && formatted?.metadata && /* @__PURE__ */ jsxs("div", { className: "agentsea-content-metadata", children: [
|
|
58
|
+
formatted.metadata.hasCodeBlocks && /* @__PURE__ */ jsx("span", { className: "badge", children: "Code" }),
|
|
59
|
+
formatted.metadata.hasTables && /* @__PURE__ */ jsx("span", { className: "badge", children: "Tables" }),
|
|
60
|
+
formatted.metadata.hasLists && /* @__PURE__ */ jsx("span", { className: "badge", children: "Lists" }),
|
|
61
|
+
formatted.metadata.links && formatted.metadata.links.length > 0 && /* @__PURE__ */ jsxs("span", { className: "badge", children: [
|
|
62
|
+
formatted.metadata.links.length,
|
|
63
|
+
" Links"
|
|
64
|
+
] })
|
|
65
|
+
] })
|
|
66
|
+
] });
|
|
67
|
+
};
|
|
68
|
+
var useFormattedContent = (content, format) => {
|
|
69
|
+
const [formatted, setFormatted] = React.useState(
|
|
70
|
+
null
|
|
71
|
+
);
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
setFormatted({
|
|
74
|
+
raw: content,
|
|
75
|
+
format: format || "markdown",
|
|
76
|
+
rendered: content
|
|
77
|
+
});
|
|
78
|
+
}, [content, format]);
|
|
79
|
+
return formatted;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/StreamingResponse.tsx
|
|
83
|
+
import React2 from "react";
|
|
84
|
+
import ReactMarkdown2 from "react-markdown";
|
|
85
|
+
import remarkGfm2 from "remark-gfm";
|
|
86
|
+
import rehypeHighlight2 from "rehype-highlight";
|
|
87
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
88
|
+
var StreamingResponse = ({
|
|
89
|
+
stream,
|
|
90
|
+
className = "",
|
|
91
|
+
showMetadata = false,
|
|
92
|
+
theme = "auto",
|
|
93
|
+
onComplete
|
|
94
|
+
}) => {
|
|
95
|
+
const [content, setContent] = React2.useState("");
|
|
96
|
+
const [metadata, setMetadata] = React2.useState(null);
|
|
97
|
+
const [isComplete, setIsComplete] = React2.useState(false);
|
|
98
|
+
const [currentIteration, setCurrentIteration] = React2.useState(0);
|
|
99
|
+
React2.useEffect(() => {
|
|
100
|
+
let isMounted = true;
|
|
101
|
+
const consumeStream = async () => {
|
|
102
|
+
try {
|
|
103
|
+
for await (const event of stream) {
|
|
104
|
+
if (!isMounted) break;
|
|
105
|
+
switch (event.type) {
|
|
106
|
+
case "iteration":
|
|
107
|
+
setCurrentIteration(event.iteration);
|
|
108
|
+
break;
|
|
109
|
+
case "content":
|
|
110
|
+
if (event.delta) {
|
|
111
|
+
setContent((prev) => prev + (event.content || ""));
|
|
112
|
+
} else {
|
|
113
|
+
setContent(event.content || "");
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
case "done":
|
|
117
|
+
setMetadata(event.metadata);
|
|
118
|
+
setIsComplete(true);
|
|
119
|
+
if (onComplete) {
|
|
120
|
+
onComplete(content);
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case "error":
|
|
124
|
+
console.error("Stream error:", event.error);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error("Error consuming stream:", error);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
consumeStream();
|
|
133
|
+
return () => {
|
|
134
|
+
isMounted = false;
|
|
135
|
+
};
|
|
136
|
+
}, [stream, onComplete, content]);
|
|
137
|
+
return /* @__PURE__ */ jsxs2(
|
|
138
|
+
"div",
|
|
139
|
+
{
|
|
140
|
+
className: `agentsea-streaming-response ${className}`,
|
|
141
|
+
"data-theme": theme,
|
|
142
|
+
children: [
|
|
143
|
+
!isComplete && showMetadata && /* @__PURE__ */ jsxs2("div", { className: "streaming-indicator", children: [
|
|
144
|
+
/* @__PURE__ */ jsx2("span", { className: "spinner" }),
|
|
145
|
+
/* @__PURE__ */ jsxs2("span", { children: [
|
|
146
|
+
"Iteration ",
|
|
147
|
+
currentIteration
|
|
148
|
+
] })
|
|
149
|
+
] }),
|
|
150
|
+
/* @__PURE__ */ jsx2(
|
|
151
|
+
ReactMarkdown2,
|
|
152
|
+
{
|
|
153
|
+
remarkPlugins: [remarkGfm2],
|
|
154
|
+
rehypePlugins: [rehypeHighlight2],
|
|
155
|
+
children: content
|
|
156
|
+
}
|
|
157
|
+
),
|
|
158
|
+
!isComplete && /* @__PURE__ */ jsx2("span", { className: "cursor", children: "\u258A" }),
|
|
159
|
+
isComplete && showMetadata && metadata && /* @__PURE__ */ jsxs2("div", { className: "agentsea-metadata", children: [
|
|
160
|
+
metadata.tokensUsed && /* @__PURE__ */ jsxs2("div", { className: "metadata-item", children: [
|
|
161
|
+
/* @__PURE__ */ jsx2("span", { className: "label", children: "Tokens:" }),
|
|
162
|
+
/* @__PURE__ */ jsx2("span", { className: "value", children: metadata.tokensUsed })
|
|
163
|
+
] }),
|
|
164
|
+
metadata.latencyMs && /* @__PURE__ */ jsxs2("div", { className: "metadata-item", children: [
|
|
165
|
+
/* @__PURE__ */ jsx2("span", { className: "label", children: "Latency:" }),
|
|
166
|
+
/* @__PURE__ */ jsxs2("span", { className: "value", children: [
|
|
167
|
+
metadata.latencyMs,
|
|
168
|
+
"ms"
|
|
169
|
+
] })
|
|
170
|
+
] }),
|
|
171
|
+
metadata.iterations && /* @__PURE__ */ jsxs2("div", { className: "metadata-item", children: [
|
|
172
|
+
/* @__PURE__ */ jsx2("span", { className: "label", children: "Iterations:" }),
|
|
173
|
+
/* @__PURE__ */ jsx2("span", { className: "value", children: metadata.iterations })
|
|
174
|
+
] })
|
|
175
|
+
] })
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
};
|
|
180
|
+
var useStreamingContent = () => {
|
|
181
|
+
const [content, setContent] = React2.useState("");
|
|
182
|
+
const [isStreaming, setIsStreaming] = React2.useState(false);
|
|
183
|
+
const [metadata, setMetadata] = React2.useState(null);
|
|
184
|
+
const consumeStream = React2.useCallback(
|
|
185
|
+
async (stream) => {
|
|
186
|
+
setIsStreaming(true);
|
|
187
|
+
setContent("");
|
|
188
|
+
setMetadata(null);
|
|
189
|
+
try {
|
|
190
|
+
for await (const event of stream) {
|
|
191
|
+
switch (event.type) {
|
|
192
|
+
case "content":
|
|
193
|
+
if (event.delta) {
|
|
194
|
+
setContent((prev) => prev + (event.content || ""));
|
|
195
|
+
} else {
|
|
196
|
+
setContent(event.content || "");
|
|
197
|
+
}
|
|
198
|
+
break;
|
|
199
|
+
case "done":
|
|
200
|
+
setMetadata(event.metadata);
|
|
201
|
+
setIsStreaming(false);
|
|
202
|
+
break;
|
|
203
|
+
case "error":
|
|
204
|
+
console.error("Stream error:", event.error);
|
|
205
|
+
setIsStreaming(false);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error("Error consuming stream:", error);
|
|
211
|
+
setIsStreaming(false);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
[]
|
|
215
|
+
);
|
|
216
|
+
return {
|
|
217
|
+
content,
|
|
218
|
+
isStreaming,
|
|
219
|
+
metadata,
|
|
220
|
+
consumeStream
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
export {
|
|
224
|
+
AgentResponse,
|
|
225
|
+
StreamingResponse,
|
|
226
|
+
useFormattedContent,
|
|
227
|
+
useStreamingContent
|
|
228
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lov3kaizen/agentsea-react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React components for rendering AgentSea agent responses with formatting support",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"react-markdown": "^9.0.1",
|
|
20
|
+
"remark-gfm": "^4.0.0",
|
|
21
|
+
"rehype-highlight": "^7.0.0",
|
|
22
|
+
"rehype-raw": "^7.0.0",
|
|
23
|
+
"@lov3kaizen/agentsea-core": "0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
27
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20.11.0",
|
|
31
|
+
"@types/react": "^18.2.48",
|
|
32
|
+
"@types/react-dom": "^18.2.18",
|
|
33
|
+
"@vitest/coverage-v8": "^1.2.0",
|
|
34
|
+
"react": "^18.2.0",
|
|
35
|
+
"react-dom": "^18.2.0",
|
|
36
|
+
"tsup": "^8.0.1",
|
|
37
|
+
"typescript": "^5.3.3",
|
|
38
|
+
"vitest": "^1.2.0"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"agentsea",
|
|
42
|
+
"react",
|
|
43
|
+
"components",
|
|
44
|
+
"markdown",
|
|
45
|
+
"rendering",
|
|
46
|
+
"formatting",
|
|
47
|
+
"ai",
|
|
48
|
+
"agents"
|
|
49
|
+
],
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=18.0.0"
|
|
52
|
+
},
|
|
53
|
+
"author": "lovekaizen",
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "https://github.com/lovekaizen/agentsea.git",
|
|
58
|
+
"directory": "packages/react"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://agentsea.dev",
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/lovekaizen/agentsea/issues"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --dts-resolve --clean --external react --external react-dom",
|
|
69
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --dts-resolve --watch --external react --external react-dom",
|
|
70
|
+
"test": "vitest run --passWithNoTests",
|
|
71
|
+
"test:watch": "vitest",
|
|
72
|
+
"test:cov": "vitest run --coverage",
|
|
73
|
+
"type-check": "tsc --noEmit",
|
|
74
|
+
"clean": "rm -rf dist"
|
|
75
|
+
}
|
|
76
|
+
}
|