forked from sourcewizard-ai/react-ai-agent-chat-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-client.ts
More file actions
145 lines (131 loc) · 4.07 KB
/
config-client.ts
File metadata and controls
145 lines (131 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { z } from 'zod';
import { ToolExecutionConfig } from './tool-execution';
export interface ToolCall {
toolCallId: string;
toolName: string;
input: any;
}
export interface ToolResult {
toolCallId: string;
toolName: string;
output?: any;
error?: string;
isError?: boolean;
}
export interface ChatMessage {
id: string;
role: 'user' | 'assistant' | 'system';
content: string;
timestamp: Date;
toolCalls?: ToolCall[];
toolResults?: ToolResult[];
uiMessageParts?: any[]; // Native AI SDK UIMessagePart format
}
export interface Conversation {
id: string;
messages: ChatMessage[];
createdAt: Date;
updatedAt: Date;
metadata?: Record<string, any>;
}
export interface Tool<TSchema extends z.ZodSchema = z.ZodSchema> {
description: string;
display_name: string;
inputSchema: TSchema;
execute: (input: z.infer<TSchema>) => Promise<any>;
render?: React.ComponentType<{ toolCall: ToolCall; toolResult?: ToolResult }>;
executionConfig?: Partial<ToolExecutionConfig>;
}
// Helper function to create a typed tool with automatic schema inference (client version with render support)
export function createTool<TSchema extends z.ZodSchema>(config: {
description: string;
display_name: string;
inputSchema: TSchema;
execute: (input: z.infer<TSchema>) => Promise<any>;
render?: React.ComponentType<{ toolCall: ToolCall; toolResult?: ToolResult }>;
executionConfig?: Partial<ToolExecutionConfig>;
}): Tool<TSchema> {
return config;
}
export interface ToolsObject {
[key: string]: Tool<any>;
}
// Helper to safely check if we're in development mode
function isDevMode(): boolean {
try {
// Check for environment variables in various ways
const nodeEnv = (globalThis as any).process?.env?.NODE_ENV ||
(globalThis as any).__NODE_ENV__ ||
(globalThis as any).__DEV__;
return nodeEnv === 'development' || nodeEnv === true;
} catch {
return false;
}
}
export interface AgentChatConfig {
tools: Record<string, { display_name: string; renderKey?: string }>;
route: string;
historyRoute?: string;
toolRenderers?: Record<string, React.ComponentType<{ toolCall: ToolCall; toolResult?: ToolResult }>>;
toolExecution?: ToolExecutionConfig;
showDebugPanel?: boolean; // Show debug panel (defaults to NODE_ENV === 'development')
}
const DEFAULT_TOOL_EXECUTION_CONFIG: ToolExecutionConfig = {
timeoutMs: 30000, // 30 seconds
retries: 3,
retryDelayMs: 1000, // 1 second initial delay
};
export function makeAgentChatClientConfig<TTools extends ToolsObject>({
route,
tools,
toolExecutionConfig,
historyRoute,
showDebugPanel
}: {
route: string;
tools: TTools;
toolExecutionConfig?: Partial<ToolExecutionConfig>;
historyRoute?: string;
showDebugPanel?: boolean;
}): AgentChatConfig {
const finalExecutionConfig = { ...DEFAULT_TOOL_EXECUTION_CONFIG, ...toolExecutionConfig };
const toolsMap = Object.keys(tools).reduce((acc: Record<string, any>, toolName: string) => ({
...acc, [toolName]: {
display_name: tools[toolName].display_name,
renderKey: tools[toolName].render ? toolName : undefined,
}
}), {} as Record<string, { display_name: string; renderKey?: string }>);
return {
tools: toolsMap,
route,
historyRoute: historyRoute || `${route}/history`,
toolExecution: finalExecutionConfig,
showDebugPanel: showDebugPanel ?? isDevMode(), // Default to development mode detection
// toolRenderers should be added separately on client-side
};
}
export function makeAgentChatConfig<TTools extends ToolsObject>({
system_prompt,
route,
tools,
auth_func,
toolExecutionConfig,
modelConfig,
storage,
historyRoute,
showDebugPanel
}: {
system_prompt: string;
route: string;
tools: TTools;
auth_func: () => Promise<boolean>;
toolExecutionConfig?: Partial<ToolExecutionConfig>;
modelConfig?: any;
storage?: any;
historyRoute?: string;
showDebugPanel?: boolean;
}): { agentChatConfig: AgentChatConfig } {
return {
agentChatConfig: makeAgentChatClientConfig({ route, tools, toolExecutionConfig, historyRoute, showDebugPanel })
};
}