@agent-platform/ui 0.0.12 → 0.0.13
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/README.md +27 -10
- package/dist/index.d.ts +35 -2
- package/dist/index.js +2462 -3
- package/dist/styles/agent-ui.css +1226 -0
- package/dist/styles/globals.css +18 -1188
- package/dist/styles/theme.css +34 -0
- package/package.json +14 -8
- package/dist/components/agent/agent-container-model.d.ts +0 -19
- package/dist/components/agent/agent-container-model.js +0 -17
- package/dist/components/agent/agent-container-state.d.ts +0 -2
- package/dist/components/agent/agent-container-state.js +0 -52
- package/dist/components/agent/agent-container-view.d.ts +0 -20
- package/dist/components/agent/agent-container-view.js +0 -19
- package/dist/components/agent/agent-container-view.test.d.ts +0 -1
- package/dist/components/agent/agent-container-view.test.js +0 -16
- package/dist/components/agent/agent-container.d.ts +0 -3
- package/dist/components/agent/agent-container.js +0 -24
- package/dist/components/agent/agent-greeting.d.ts +0 -3
- package/dist/components/agent/agent-greeting.js +0 -7
- package/dist/components/agent/agent-header.d.ts +0 -3
- package/dist/components/agent/agent-header.js +0 -9
- package/dist/components/agent/agent-home-card.d.ts +0 -3
- package/dist/components/agent/agent-home-card.js +0 -7
- package/dist/components/agent/agent-home-cards.d.ts +0 -9
- package/dist/components/agent/agent-home-cards.js +0 -13
- package/dist/components/agent/agent-input.d.ts +0 -3
- package/dist/components/agent/agent-input.js +0 -51
- package/dist/components/agent/agent-popup-widget.d.ts +0 -15
- package/dist/components/agent/agent-popup-widget.js +0 -49
- package/dist/components/agent/agent-screen.d.ts +0 -5
- package/dist/components/agent/agent-screen.js +0 -9
- package/dist/components/agent/approval-ui-model.d.ts +0 -18
- package/dist/components/agent/approval-ui-model.js +0 -27
- package/dist/components/agent/approval-ui-model.test.d.ts +0 -1
- package/dist/components/agent/approval-ui-model.test.js +0 -39
- package/dist/components/agent/defaults.d.ts +0 -5
- package/dist/components/agent/defaults.js +0 -12
- package/dist/components/agent/index.d.ts +0 -3
- package/dist/components/agent/index.js +0 -1
- package/dist/components/agent/input-mode.d.ts +0 -5
- package/dist/components/agent/input-mode.js +0 -9
- package/dist/components/agent/message/index.d.ts +0 -8
- package/dist/components/agent/message/index.js +0 -4
- package/dist/components/agent/message/markdown.d.ts +0 -6
- package/dist/components/agent/message/markdown.js +0 -66
- package/dist/components/agent/message/message-item.d.ts +0 -7
- package/dist/components/agent/message/message-item.js +0 -24
- package/dist/components/agent/message/message-list.d.ts +0 -8
- package/dist/components/agent/message/message-list.js +0 -20
- package/dist/components/agent/message/message-loading.d.ts +0 -4
- package/dist/components/agent/message/message-loading.js +0 -10
- package/dist/components/agent/message/tool-call-card.d.ts +0 -9
- package/dist/components/agent/message/tool-call-card.js +0 -26
- package/dist/components/agent/message/utils.d.ts +0 -8
- package/dist/components/agent/message/utils.js +0 -21
- package/dist/components/agent/provider/agent-context.d.ts +0 -4
- package/dist/components/agent/provider/agent-context.js +0 -13
- package/dist/components/agent/provider/agent-provider.d.ts +0 -10
- package/dist/components/agent/provider/agent-provider.js +0 -40
- package/dist/components/agent/provider/index.d.ts +0 -4
- package/dist/components/agent/provider/index.js +0 -2
- package/dist/components/agent/provider/parse-sse-buffer.d.ts +0 -9
- package/dist/components/agent/provider/parse-sse-buffer.js +0 -23
- package/dist/components/agent/provider/runtime-config.d.ts +0 -5
- package/dist/components/agent/provider/runtime-config.js +0 -24
- package/dist/components/agent/provider/types.d.ts +0 -66
- package/dist/components/agent/provider/types.js +0 -1
- package/dist/components/agent/provider/use-agent-chat.d.ts +0 -6
- package/dist/components/agent/provider/use-agent-chat.js +0 -493
- package/dist/components/agent/tool-approval-panel.d.ts +0 -9
- package/dist/components/agent/tool-approval-panel.js +0 -11
- package/dist/components/agent/types.d.ts +0 -44
- package/dist/components/agent/types.js +0 -1
- package/dist/components/index.d.ts +0 -7
- package/dist/components/index.js +0 -9
- package/dist/components/ui/badge.d.ts +0 -9
- package/dist/components/ui/badge.js +0 -24
- package/dist/components/ui/button.d.ts +0 -10
- package/dist/components/ui/button.js +0 -35
- package/dist/components/ui/card.d.ts +0 -9
- package/dist/components/ui/card.js +0 -24
- package/dist/components/ui/input.d.ts +0 -3
- package/dist/components/ui/input.js +0 -6
- package/dist/components/ui/label.d.ts +0 -4
- package/dist/components/ui/label.js +0 -7
- package/dist/components/ui/separator.d.ts +0 -4
- package/dist/components/ui/separator.js +0 -8
- package/dist/components/ui/textarea.d.ts +0 -3
- package/dist/components/ui/textarea.js +0 -6
- package/dist/lib/index.d.ts +0 -1
- package/dist/lib/index.js +0 -1
- package/dist/lib/utils.d.ts +0 -2
- package/dist/lib/utils.js +0 -5
- package/dist/modules/agent/agent.repository.d.ts +0 -58
- package/dist/modules/agent/agent.repository.js +0 -235
- package/dist/modules/agent/agent.repository.test.d.ts +0 -1
- package/dist/modules/agent/agent.repository.test.js +0 -64
- package/dist/modules/agent/domain/chat-state.d.ts +0 -64
- package/dist/modules/agent/domain/chat-state.js +0 -148
- package/dist/modules/agent/domain/chat-state.test.d.ts +0 -1
- package/dist/modules/agent/domain/chat-state.test.js +0 -72
- package/dist/modules/agent/use-agent-chat.d.ts +0 -6
- package/dist/modules/agent/use-agent-chat.js +0 -106
- package/dist/modules/agent/usecases/process-stream.d.ts +0 -26
- package/dist/modules/agent/usecases/process-stream.js +0 -112
- package/dist/modules/agent/usecases/process-stream.test.d.ts +0 -1
- package/dist/modules/agent/usecases/process-stream.test.js +0 -91
- package/dist/modules/agent/usecases/send-message.d.ts +0 -21
- package/dist/modules/agent/usecases/send-message.js +0 -298
- package/dist/modules/agent/usecases/send-message.test.d.ts +0 -1
- package/dist/modules/agent/usecases/send-message.test.js +0 -257
- package/dist/styles/ensure-styles.d.ts +0 -21
- package/dist/styles/ensure-styles.js +0 -22
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
declare function Card({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
3
|
-
declare function CardHeader({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
4
|
-
declare function CardTitle({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
5
|
-
declare function CardDescription({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
6
|
-
declare function CardAction({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
7
|
-
declare function CardContent({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
8
|
-
declare function CardFooter({ className, ...props }: React.ComponentProps<"div">): import("react/jsx-runtime").JSX.Element;
|
|
9
|
-
export { Card, CardHeader, CardFooter, CardTitle, CardAction, CardDescription, CardContent, };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { cn } from "../../lib/utils";
|
|
3
|
-
function Card({ className, ...props }) {
|
|
4
|
-
return (_jsx("div", { "data-slot": "card", className: cn("bg-card text-card-foreground flex flex-col gap-6 rounded-xl border py-6", className), ...props }));
|
|
5
|
-
}
|
|
6
|
-
function CardHeader({ className, ...props }) {
|
|
7
|
-
return (_jsx("div", { "data-slot": "card-header", className: cn("@container/card-header grid auto-rows-min grid-rows-[auto_auto] items-start gap-2 px-6 has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-6", className), ...props }));
|
|
8
|
-
}
|
|
9
|
-
function CardTitle({ className, ...props }) {
|
|
10
|
-
return (_jsx("div", { "data-slot": "card-title", className: cn("leading-none font-semibold", className), ...props }));
|
|
11
|
-
}
|
|
12
|
-
function CardDescription({ className, ...props }) {
|
|
13
|
-
return (_jsx("div", { "data-slot": "card-description", className: cn("text-muted-foreground text-sm", className), ...props }));
|
|
14
|
-
}
|
|
15
|
-
function CardAction({ className, ...props }) {
|
|
16
|
-
return (_jsx("div", { "data-slot": "card-action", className: cn("col-start-2 row-span-2 row-start-1 self-start justify-self-end", className), ...props }));
|
|
17
|
-
}
|
|
18
|
-
function CardContent({ className, ...props }) {
|
|
19
|
-
return (_jsx("div", { "data-slot": "card-content", className: cn("px-6", className), ...props }));
|
|
20
|
-
}
|
|
21
|
-
function CardFooter({ className, ...props }) {
|
|
22
|
-
return (_jsx("div", { "data-slot": "card-footer", className: cn("flex items-center px-6 [.border-t]:pt-6", className), ...props }));
|
|
23
|
-
}
|
|
24
|
-
export { Card, CardHeader, CardFooter, CardTitle, CardAction, CardDescription, CardContent, };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { cn } from "../../lib/utils";
|
|
3
|
-
function Input({ className, type, ...props }) {
|
|
4
|
-
return (_jsx("input", { type: type, "data-slot": "input", className: cn("file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]", "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive", className), ...props }));
|
|
5
|
-
}
|
|
6
|
-
export { Input };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Label as LabelPrimitive } from "radix-ui";
|
|
3
|
-
import { cn } from "../../lib/utils";
|
|
4
|
-
function Label({ className, ...props }) {
|
|
5
|
-
return (_jsx(LabelPrimitive.Root, { "data-slot": "label", className: cn("flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50", className), ...props }));
|
|
6
|
-
}
|
|
7
|
-
export { Label };
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { Separator as SeparatorPrimitive } from "radix-ui";
|
|
3
|
-
declare function Separator({ className, orientation, decorative, ...props }: React.ComponentProps<typeof SeparatorPrimitive.Root>): import("react/jsx-runtime").JSX.Element;
|
|
4
|
-
export { Separator };
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { Separator as SeparatorPrimitive } from "radix-ui";
|
|
4
|
-
import { cn } from "../../lib/utils";
|
|
5
|
-
function Separator({ className, orientation = "horizontal", decorative = true, ...props }) {
|
|
6
|
-
return (_jsx(SeparatorPrimitive.Root, { "data-slot": "separator", decorative: decorative, orientation: orientation, className: cn("bg-border shrink-0 data-[orientation=horizontal]:h-px data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-px", className), ...props }));
|
|
7
|
-
}
|
|
8
|
-
export { Separator };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { cn } from '../../lib/utils';
|
|
3
|
-
function Textarea({ className, ...props }) {
|
|
4
|
-
return (_jsx("textarea", { "data-slot": "textarea", className: cn('border-input placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:bg-input/30 flex field-sizing-content min-h-16 w-full rounded-md border bg-transparent px-3 py-2 text-base outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 md:text-sm', className), ...props }));
|
|
5
|
-
}
|
|
6
|
-
export { Textarea };
|
package/dist/lib/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './utils';
|
package/dist/lib/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './utils';
|
package/dist/lib/utils.d.ts
DELETED
package/dist/lib/utils.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { AgentStreamEvent, ToolCallData } from '../../components/agent/provider/types';
|
|
2
|
-
export interface ToolExecutionResult {
|
|
3
|
-
output: unknown;
|
|
4
|
-
isError?: boolean;
|
|
5
|
-
}
|
|
6
|
-
export interface ContinuePayload {
|
|
7
|
-
threadId: string | null;
|
|
8
|
-
agentId: string;
|
|
9
|
-
toolResults: {
|
|
10
|
-
toolCallId: string;
|
|
11
|
-
result: unknown;
|
|
12
|
-
isError?: boolean;
|
|
13
|
-
}[];
|
|
14
|
-
toolCalls: {
|
|
15
|
-
toolCallId: string;
|
|
16
|
-
toolName: string;
|
|
17
|
-
args: unknown;
|
|
18
|
-
}[];
|
|
19
|
-
}
|
|
20
|
-
export interface SendChatParams {
|
|
21
|
-
endpoint: string;
|
|
22
|
-
threadId: string | null;
|
|
23
|
-
message: string;
|
|
24
|
-
agentId: string;
|
|
25
|
-
headers: Record<string, string>;
|
|
26
|
-
signal?: AbortSignal;
|
|
27
|
-
}
|
|
28
|
-
export interface ContinueChatParams {
|
|
29
|
-
endpoint: string;
|
|
30
|
-
payload: ContinuePayload;
|
|
31
|
-
headers: Record<string, string>;
|
|
32
|
-
signal?: AbortSignal;
|
|
33
|
-
}
|
|
34
|
-
export interface ExecuteToolCallParams {
|
|
35
|
-
toolCall: ToolCallData;
|
|
36
|
-
apiBaseUrl: string;
|
|
37
|
-
authHeaders: Record<string, string>;
|
|
38
|
-
}
|
|
39
|
-
export interface AgentRepository {
|
|
40
|
-
buildAgentHeaders: (getAgentHeaders?: () => Record<string, string> | Promise<Record<string, string>>) => Promise<Record<string, string>>;
|
|
41
|
-
buildToolApiHeaders: () => Promise<Record<string, string>>;
|
|
42
|
-
sendChat: (params: SendChatParams) => Promise<Response>;
|
|
43
|
-
continueChat: (params: ContinueChatParams) => Promise<Response>;
|
|
44
|
-
collectStreamEvents: (response: Response, options?: CollectStreamEventsOptions) => Promise<AgentStreamEvent[]>;
|
|
45
|
-
executeToolCall: (params: ExecuteToolCallParams) => Promise<ToolExecutionResult>;
|
|
46
|
-
parseErrorResponse: (response: Response, fallback: string) => Promise<string>;
|
|
47
|
-
}
|
|
48
|
-
export interface CollectStreamEventsOptions {
|
|
49
|
-
onEvents?: (events: AgentStreamEvent[]) => void;
|
|
50
|
-
}
|
|
51
|
-
interface CreateAgentRepositoryOptions {
|
|
52
|
-
fetchFn?: typeof fetch;
|
|
53
|
-
authToken?: string;
|
|
54
|
-
getAuthToken?: () => string | Promise<string>;
|
|
55
|
-
disableToolApiAuthHeader?: boolean;
|
|
56
|
-
}
|
|
57
|
-
export declare function createAgentRepository(options: CreateAgentRepositoryOptions): AgentRepository;
|
|
58
|
-
export {};
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
function parseSSEBuffer(buffer) {
|
|
2
|
-
const events = [];
|
|
3
|
-
const lines = buffer.split('\n');
|
|
4
|
-
const remaining = lines.pop() || '';
|
|
5
|
-
for (const line of lines) {
|
|
6
|
-
if (!line.startsWith('data: ')) {
|
|
7
|
-
continue;
|
|
8
|
-
}
|
|
9
|
-
const jsonString = line.slice(6);
|
|
10
|
-
if (!jsonString) {
|
|
11
|
-
continue;
|
|
12
|
-
}
|
|
13
|
-
try {
|
|
14
|
-
const event = JSON.parse(jsonString);
|
|
15
|
-
events.push(event);
|
|
16
|
-
}
|
|
17
|
-
catch (error) {
|
|
18
|
-
console.warn('Failed to parse SSE event:', error);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return { events, remaining };
|
|
22
|
-
}
|
|
23
|
-
function isLikelyJwtToken(token) {
|
|
24
|
-
const normalized = token.startsWith('Bearer ') ? token.slice(7) : token;
|
|
25
|
-
return normalized.split('.').length === 3;
|
|
26
|
-
}
|
|
27
|
-
function buildToolEndpointRequest(toolCall, apiBaseUrl) {
|
|
28
|
-
if (!toolCall.apiRequest) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const { apiRequest, input } = toolCall;
|
|
32
|
-
const inputData = (input ?? {});
|
|
33
|
-
const method = apiRequest.method.toUpperCase();
|
|
34
|
-
let path = apiRequest.path;
|
|
35
|
-
const usedParams = new Set();
|
|
36
|
-
for (const paramName of apiRequest.pathParams) {
|
|
37
|
-
const value = inputData[paramName];
|
|
38
|
-
if (value === undefined) {
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
path = path.replace(`{${paramName}}`, encodeURIComponent(String(value)));
|
|
42
|
-
usedParams.add(paramName);
|
|
43
|
-
}
|
|
44
|
-
const queryParams = {};
|
|
45
|
-
const bodyData = {};
|
|
46
|
-
for (const key of apiRequest.queryParams) {
|
|
47
|
-
const value = inputData[key];
|
|
48
|
-
if (value === undefined) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
queryParams[key] = String(value);
|
|
52
|
-
usedParams.add(key);
|
|
53
|
-
}
|
|
54
|
-
for (const key of apiRequest.bodyFields) {
|
|
55
|
-
const value = inputData[key];
|
|
56
|
-
if (value === undefined) {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
bodyData[key] = value;
|
|
60
|
-
usedParams.add(key);
|
|
61
|
-
}
|
|
62
|
-
for (const [key, value] of Object.entries(inputData)) {
|
|
63
|
-
if (usedParams.has(key) || value === undefined) {
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (method === 'GET' || method === 'DELETE') {
|
|
67
|
-
queryParams[key] = String(value);
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
bodyData[key] = value;
|
|
71
|
-
}
|
|
72
|
-
const queryString = new URLSearchParams(queryParams).toString();
|
|
73
|
-
return {
|
|
74
|
-
method,
|
|
75
|
-
url: `${apiBaseUrl}${path}${queryString ? `?${queryString}` : ''}`,
|
|
76
|
-
bodyData,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
export function createAgentRepository(options) {
|
|
80
|
-
const { fetchFn = fetch, authToken, getAuthToken, disableToolApiAuthHeader = false, } = options;
|
|
81
|
-
const resolveToken = async () => {
|
|
82
|
-
return getAuthToken ? getAuthToken() : authToken;
|
|
83
|
-
};
|
|
84
|
-
return {
|
|
85
|
-
buildAgentHeaders: async (getAgentHeaders) => {
|
|
86
|
-
const headers = {
|
|
87
|
-
'Content-Type': 'application/json',
|
|
88
|
-
};
|
|
89
|
-
const token = await resolveToken();
|
|
90
|
-
if (token) {
|
|
91
|
-
headers.Authorization = token.startsWith('Bearer ') ? token : `Bearer ${token}`;
|
|
92
|
-
}
|
|
93
|
-
const customHeaders = getAgentHeaders ? await getAgentHeaders() : undefined;
|
|
94
|
-
if (!customHeaders) {
|
|
95
|
-
return headers;
|
|
96
|
-
}
|
|
97
|
-
for (const [key, value] of Object.entries(customHeaders)) {
|
|
98
|
-
if (value) {
|
|
99
|
-
headers[key] = value;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return headers;
|
|
103
|
-
},
|
|
104
|
-
buildToolApiHeaders: async () => {
|
|
105
|
-
const headers = {};
|
|
106
|
-
if (disableToolApiAuthHeader) {
|
|
107
|
-
return headers;
|
|
108
|
-
}
|
|
109
|
-
const token = await resolveToken();
|
|
110
|
-
if (!token) {
|
|
111
|
-
return headers;
|
|
112
|
-
}
|
|
113
|
-
const resolvedToken = token.startsWith('Bearer ') ? token : `Bearer ${token}`;
|
|
114
|
-
if (!isLikelyJwtToken(resolvedToken)) {
|
|
115
|
-
return headers;
|
|
116
|
-
}
|
|
117
|
-
headers.Authorization = resolvedToken;
|
|
118
|
-
return headers;
|
|
119
|
-
},
|
|
120
|
-
sendChat: async ({ endpoint, threadId, message, agentId, headers, signal }) => {
|
|
121
|
-
return fetchFn(`${endpoint}/chat`, {
|
|
122
|
-
method: 'POST',
|
|
123
|
-
headers,
|
|
124
|
-
credentials: 'include',
|
|
125
|
-
body: JSON.stringify({ threadId, message, agentId }),
|
|
126
|
-
signal,
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
continueChat: async ({ endpoint, payload, headers, signal }) => {
|
|
130
|
-
return fetchFn(`${endpoint}/chat/tool-result`, {
|
|
131
|
-
method: 'POST',
|
|
132
|
-
headers,
|
|
133
|
-
credentials: 'include',
|
|
134
|
-
body: JSON.stringify(payload),
|
|
135
|
-
signal,
|
|
136
|
-
});
|
|
137
|
-
},
|
|
138
|
-
collectStreamEvents: async (response, options) => {
|
|
139
|
-
const reader = response.body?.getReader();
|
|
140
|
-
if (!reader) {
|
|
141
|
-
return [];
|
|
142
|
-
}
|
|
143
|
-
const decoder = new TextDecoder();
|
|
144
|
-
const events = [];
|
|
145
|
-
let buffer = '';
|
|
146
|
-
try {
|
|
147
|
-
while (true) {
|
|
148
|
-
const { done, value } = await reader.read();
|
|
149
|
-
if (done) {
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
buffer += decoder.decode(value, { stream: true });
|
|
153
|
-
const parsed = parseSSEBuffer(buffer);
|
|
154
|
-
buffer = parsed.remaining;
|
|
155
|
-
events.push(...parsed.events);
|
|
156
|
-
if (parsed.events.length > 0) {
|
|
157
|
-
options?.onEvents?.(parsed.events);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (buffer.startsWith('data: ')) {
|
|
161
|
-
try {
|
|
162
|
-
const event = JSON.parse(buffer.slice(6));
|
|
163
|
-
events.push(event);
|
|
164
|
-
options?.onEvents?.([event]);
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
console.warn('Failed to parse remaining SSE event:', error);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return events;
|
|
171
|
-
}
|
|
172
|
-
finally {
|
|
173
|
-
reader.releaseLock();
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
executeToolCall: async ({ toolCall, apiBaseUrl, authHeaders }) => {
|
|
177
|
-
const request = buildToolEndpointRequest(toolCall, apiBaseUrl);
|
|
178
|
-
if (!request) {
|
|
179
|
-
return { output: 'No API endpoint defined for this tool', isError: true };
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
const fetchOptions = {
|
|
183
|
-
method: request.method,
|
|
184
|
-
headers: {
|
|
185
|
-
'Content-Type': 'application/json',
|
|
186
|
-
...authHeaders,
|
|
187
|
-
},
|
|
188
|
-
credentials: 'include',
|
|
189
|
-
};
|
|
190
|
-
if ((request.method === 'POST' || request.method === 'PUT' || request.method === 'PATCH') &&
|
|
191
|
-
Object.keys(request.bodyData).length > 0) {
|
|
192
|
-
fetchOptions.body = JSON.stringify(request.bodyData);
|
|
193
|
-
}
|
|
194
|
-
const response = await fetchFn(request.url, fetchOptions);
|
|
195
|
-
const text = await response.text();
|
|
196
|
-
let data;
|
|
197
|
-
try {
|
|
198
|
-
data = text ? JSON.parse(text) : null;
|
|
199
|
-
}
|
|
200
|
-
catch {
|
|
201
|
-
data = { raw: text, status: response.status };
|
|
202
|
-
}
|
|
203
|
-
if (!response.ok) {
|
|
204
|
-
return { output: data, isError: true };
|
|
205
|
-
}
|
|
206
|
-
return { output: data };
|
|
207
|
-
}
|
|
208
|
-
catch (error) {
|
|
209
|
-
return {
|
|
210
|
-
output: error instanceof Error ? error.message : 'Network error',
|
|
211
|
-
isError: true,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
},
|
|
215
|
-
parseErrorResponse: async (response, fallback) => {
|
|
216
|
-
let errorMessage = fallback;
|
|
217
|
-
try {
|
|
218
|
-
const contentType = response.headers.get('content-type');
|
|
219
|
-
if (contentType?.includes('application/json')) {
|
|
220
|
-
const errorData = await response.json();
|
|
221
|
-
if (typeof errorData?.error === 'string') {
|
|
222
|
-
return errorData.error;
|
|
223
|
-
}
|
|
224
|
-
if (typeof errorData?.message === 'string') {
|
|
225
|
-
return errorData.message;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
catch (error) {
|
|
230
|
-
console.warn('Failed to parse error response:', error);
|
|
231
|
-
}
|
|
232
|
-
return errorMessage;
|
|
233
|
-
},
|
|
234
|
-
};
|
|
235
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { createAgentRepository } from './agent.repository';
|
|
3
|
-
describe('createAgentRepository', () => {
|
|
4
|
-
it('parseErrorResponseはjsonのerror/messageを優先して返す', async () => {
|
|
5
|
-
const repository = createAgentRepository({
|
|
6
|
-
fetchFn: vi.fn(),
|
|
7
|
-
getAuthToken: undefined,
|
|
8
|
-
authToken: undefined,
|
|
9
|
-
disableToolApiAuthHeader: false,
|
|
10
|
-
});
|
|
11
|
-
const response = new Response(JSON.stringify({ error: 'failed' }), {
|
|
12
|
-
status: 400,
|
|
13
|
-
headers: { 'content-type': 'application/json' },
|
|
14
|
-
});
|
|
15
|
-
await expect(repository.parseErrorResponse(response, 'fallback')).resolves.toBe('failed');
|
|
16
|
-
});
|
|
17
|
-
it('collectStreamEventsはSSEのdata行をイベント配列に変換する', async () => {
|
|
18
|
-
const repository = createAgentRepository({
|
|
19
|
-
fetchFn: vi.fn(),
|
|
20
|
-
getAuthToken: undefined,
|
|
21
|
-
authToken: undefined,
|
|
22
|
-
disableToolApiAuthHeader: false,
|
|
23
|
-
});
|
|
24
|
-
const stream = new ReadableStream({
|
|
25
|
-
start(controller) {
|
|
26
|
-
controller.enqueue(new TextEncoder().encode('data: {"type":"text-delta","payload":{"text":"hi"}}\n\n'));
|
|
27
|
-
controller.close();
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
const response = new Response(stream);
|
|
31
|
-
const events = await repository.collectStreamEvents(response);
|
|
32
|
-
expect(events).toEqual([{ type: 'text-delta', payload: { text: 'hi' } }]);
|
|
33
|
-
});
|
|
34
|
-
it('collectStreamEventsはストリーム完了前にonEventsを呼び出す', async () => {
|
|
35
|
-
const repository = createAgentRepository({
|
|
36
|
-
fetchFn: vi.fn(),
|
|
37
|
-
getAuthToken: undefined,
|
|
38
|
-
authToken: undefined,
|
|
39
|
-
disableToolApiAuthHeader: false,
|
|
40
|
-
});
|
|
41
|
-
const encoder = new TextEncoder();
|
|
42
|
-
let closeStream = null;
|
|
43
|
-
const stream = new ReadableStream({
|
|
44
|
-
start(controller) {
|
|
45
|
-
controller.enqueue(encoder.encode('data: {"type":"text-delta","payload":{"text":"hel"}}\n\n'));
|
|
46
|
-
closeStream = () => {
|
|
47
|
-
controller.enqueue(encoder.encode('data: {"type":"text-delta","payload":{"text":"lo"}}\n\n'));
|
|
48
|
-
controller.close();
|
|
49
|
-
};
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
const response = new Response(stream);
|
|
53
|
-
const onEvents = vi.fn();
|
|
54
|
-
const collectPromise = repository.collectStreamEvents(response, { onEvents });
|
|
55
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
56
|
-
expect(onEvents).toHaveBeenCalledWith([{ type: 'text-delta', payload: { text: 'hel' } }]);
|
|
57
|
-
closeStream?.();
|
|
58
|
-
const events = await collectPromise;
|
|
59
|
-
expect(events).toEqual([
|
|
60
|
-
{ type: 'text-delta', payload: { text: 'hel' } },
|
|
61
|
-
{ type: 'text-delta', payload: { text: 'lo' } },
|
|
62
|
-
]);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { AgentMessage, ToolCallData, ToolCallState, ToolCallStatus } from '../../../components/agent/provider/types';
|
|
2
|
-
export interface AgentChatState {
|
|
3
|
-
messages: AgentMessage[];
|
|
4
|
-
threadId: string | null;
|
|
5
|
-
isLoading: boolean;
|
|
6
|
-
error: string | null;
|
|
7
|
-
pendingToolCalls: Map<string, ToolCallState>;
|
|
8
|
-
}
|
|
9
|
-
export interface StartChatPayload {
|
|
10
|
-
userMessageId: string;
|
|
11
|
-
text: string;
|
|
12
|
-
assistantMessageId: string;
|
|
13
|
-
}
|
|
14
|
-
export interface UpsertToolCallStatePayload {
|
|
15
|
-
toolCall: ToolCallData;
|
|
16
|
-
status: ToolCallStatus;
|
|
17
|
-
result?: unknown;
|
|
18
|
-
error?: string;
|
|
19
|
-
}
|
|
20
|
-
export type AgentChatAction = {
|
|
21
|
-
type: 'START_CHAT';
|
|
22
|
-
payload: StartChatPayload;
|
|
23
|
-
} | {
|
|
24
|
-
type: 'START_ASSISTANT_MESSAGE';
|
|
25
|
-
payload: {
|
|
26
|
-
assistantMessageId: string;
|
|
27
|
-
};
|
|
28
|
-
} | {
|
|
29
|
-
type: 'UPDATE_ASSISTANT_PROGRESS';
|
|
30
|
-
payload: {
|
|
31
|
-
assistantMessageId: string;
|
|
32
|
-
text: string;
|
|
33
|
-
toolCalls: ToolCallData[];
|
|
34
|
-
};
|
|
35
|
-
} | {
|
|
36
|
-
type: 'FINISH_ASSISTANT_MESSAGE';
|
|
37
|
-
payload: {
|
|
38
|
-
assistantMessageId: string;
|
|
39
|
-
};
|
|
40
|
-
} | {
|
|
41
|
-
type: 'UPSERT_TOOL_CALL_STATE';
|
|
42
|
-
payload: UpsertToolCallStatePayload;
|
|
43
|
-
} | {
|
|
44
|
-
type: 'SET_LOADING';
|
|
45
|
-
payload: {
|
|
46
|
-
isLoading: boolean;
|
|
47
|
-
};
|
|
48
|
-
} | {
|
|
49
|
-
type: 'SET_ERROR';
|
|
50
|
-
payload: {
|
|
51
|
-
error: string | null;
|
|
52
|
-
};
|
|
53
|
-
} | {
|
|
54
|
-
type: 'SET_THREAD_ID';
|
|
55
|
-
payload: {
|
|
56
|
-
threadId: string;
|
|
57
|
-
};
|
|
58
|
-
} | {
|
|
59
|
-
type: 'RESET_CHAT';
|
|
60
|
-
};
|
|
61
|
-
export declare function createInitialChatState(): AgentChatState;
|
|
62
|
-
export declare function appendIntentText(current: string, intentMessage: string): string;
|
|
63
|
-
export declare function selectPendingToolCalls(state: AgentChatState): ToolCallState[];
|
|
64
|
-
export declare function chatStateReducer(state: AgentChatState, action: AgentChatAction): AgentChatState;
|