@applica-software-guru/persona-chat-sdk 0.1.102
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/.eslintrc.cjs +11 -0
- package/.github/copilot-instructions.md +3 -0
- package/.nvmrc +1 -0
- package/.prettierignore +5 -0
- package/.prettierrc +8 -0
- package/CLAUDE.md +3 -0
- package/GEMINI.md +3 -0
- package/README.md +33 -0
- package/bitbucket-pipelines.yml +19 -0
- package/components.json +24 -0
- package/dist/bundle.cjs.js +28 -0
- package/dist/bundle.cjs.js.map +1 -0
- package/dist/bundle.es.js +5173 -0
- package/dist/bundle.es.js.map +1 -0
- package/dist/bundle.iife.js +28 -0
- package/dist/bundle.iife.js.map +1 -0
- package/dist/bundle.umd.js +28 -0
- package/dist/bundle.umd.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/logging.d.ts +18 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/messages.d.ts +9 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/projects.d.ts +17 -0
- package/dist/projects.d.ts.map +1 -0
- package/dist/protocol/base.d.ts +25 -0
- package/dist/protocol/base.d.ts.map +1 -0
- package/dist/protocol/index.d.ts +6 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/rest.d.ts +25 -0
- package/dist/protocol/rest.d.ts.map +1 -0
- package/dist/protocol/transaction.d.ts +56 -0
- package/dist/protocol/transaction.d.ts.map +1 -0
- package/dist/protocol/webrtc.d.ts +60 -0
- package/dist/protocol/webrtc.d.ts.map +1 -0
- package/dist/protocol/websocket.d.ts +22 -0
- package/dist/protocol/websocket.d.ts.map +1 -0
- package/dist/runtime/context.d.ts +34 -0
- package/dist/runtime/context.d.ts.map +1 -0
- package/dist/runtime/file-attachment-adapter.d.ts +15 -0
- package/dist/runtime/file-attachment-adapter.d.ts.map +1 -0
- package/dist/runtime/handlers.d.ts +21 -0
- package/dist/runtime/handlers.d.ts.map +1 -0
- package/dist/runtime/listeners.d.ts +6 -0
- package/dist/runtime/listeners.d.ts.map +1 -0
- package/dist/runtime/protocols.d.ts +17 -0
- package/dist/runtime/protocols.d.ts.map +1 -0
- package/dist/runtime/threads.d.ts +34 -0
- package/dist/runtime/threads.d.ts.map +1 -0
- package/dist/runtime/utils.d.ts +10 -0
- package/dist/runtime/utils.d.ts.map +1 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/storage/base.d.ts +9 -0
- package/dist/storage/base.d.ts.map +1 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/persona.d.ts +29 -0
- package/dist/storage/persona.d.ts.map +1 -0
- package/dist/tools.d.ts +72 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +237 -0
- package/dist/types.d.ts.map +1 -0
- package/docs/README.md +13 -0
- package/docs/api-reference.md +16 -0
- package/docs/contributing.md +21 -0
- package/docs/customization.md +74 -0
- package/docs/features.md +7 -0
- package/docs/installation.md +9 -0
- package/docs/messages.md +214 -0
- package/docs/protocols.md +39 -0
- package/docs/transactions.md +121 -0
- package/docs/usage.md +40 -0
- package/jsconfig.node.json +10 -0
- package/package.json +82 -0
- package/playground/index.html +14 -0
- package/playground/src/app.tsx +10 -0
- package/playground/src/chat.tsx +117 -0
- package/playground/src/components/assistant-ui/assistant-modal.tsx +57 -0
- package/playground/src/components/assistant-ui/attachment.tsx +197 -0
- package/playground/src/components/assistant-ui/markdown-text.tsx +228 -0
- package/playground/src/components/assistant-ui/thread-list.tsx +91 -0
- package/playground/src/components/assistant-ui/thread.tsx +302 -0
- package/playground/src/components/assistant-ui/threadlist-sidebar.tsx +59 -0
- package/playground/src/components/assistant-ui/tool-fallback.tsx +93 -0
- package/playground/src/components/assistant-ui/tooltip-icon-button.tsx +42 -0
- package/playground/src/components/chat/logging.tsx +53 -0
- package/playground/src/components/ui/avatar.tsx +51 -0
- package/playground/src/components/ui/button.tsx +60 -0
- package/playground/src/components/ui/dialog.tsx +141 -0
- package/playground/src/components/ui/input.tsx +21 -0
- package/playground/src/components/ui/separator.tsx +26 -0
- package/playground/src/components/ui/sheet.tsx +139 -0
- package/playground/src/components/ui/sidebar.tsx +619 -0
- package/playground/src/components/ui/skeleton.tsx +13 -0
- package/playground/src/components/ui/tooltip.tsx +59 -0
- package/playground/src/hooks/theme.ts +70 -0
- package/playground/src/hooks/use-mobile.ts +19 -0
- package/playground/src/lib/utils.ts +6 -0
- package/playground/src/main.tsx +10 -0
- package/playground/src/styles.css +120 -0
- package/playground/src/tools.ts +149 -0
- package/playground/src/vite-env.d.ts +1 -0
- package/preview-build.sh +23 -0
- package/src/index.ts +7 -0
- package/src/logging.ts +34 -0
- package/src/messages.ts +202 -0
- package/src/projects.ts +57 -0
- package/src/protocol/base.ts +73 -0
- package/src/protocol/index.ts +5 -0
- package/src/protocol/rest.ts +107 -0
- package/src/protocol/transaction.ts +182 -0
- package/src/protocol/webrtc.ts +379 -0
- package/src/protocol/websocket.ts +111 -0
- package/src/runtime/context.ts +88 -0
- package/src/runtime/file-attachment-adapter.ts +48 -0
- package/src/runtime/handlers.ts +322 -0
- package/src/runtime/index.ts +6 -0
- package/src/runtime/listeners.ts +79 -0
- package/src/runtime/protocols.ts +169 -0
- package/src/runtime/threads.ts +105 -0
- package/src/runtime/utils.ts +46 -0
- package/src/runtime.tsx +334 -0
- package/src/storage/base.ts +13 -0
- package/src/storage/index.ts +2 -0
- package/src/storage/persona.ts +138 -0
- package/src/tools.ts +211 -0
- package/src/types.ts +284 -0
- package/tsconfig.json +36 -0
- package/tsconfig.node.json +15 -0
- package/vite.config.ts +74 -0
package/src/tools.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
export type ToolParameterType = 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
2
|
+
|
|
3
|
+
export interface ToolParameter {
|
|
4
|
+
type: ToolParameterType;
|
|
5
|
+
description: string;
|
|
6
|
+
required?: boolean;
|
|
7
|
+
properties?: Record<string, ToolParameter>;
|
|
8
|
+
items?: ToolParameter;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ToolSchema {
|
|
12
|
+
type: 'local';
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
config: {
|
|
16
|
+
timeout: number;
|
|
17
|
+
parameters: {
|
|
18
|
+
type: 'object';
|
|
19
|
+
title: string;
|
|
20
|
+
required: string[];
|
|
21
|
+
properties: Record<string, ToolParameter>;
|
|
22
|
+
};
|
|
23
|
+
output: {
|
|
24
|
+
type: 'object';
|
|
25
|
+
title: string;
|
|
26
|
+
properties: Record<string, ToolParameter>;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ToolDefinition {
|
|
32
|
+
name: string;
|
|
33
|
+
description: string;
|
|
34
|
+
title?: string;
|
|
35
|
+
timeout?: number;
|
|
36
|
+
parameters: Record<string, ToolParameter>;
|
|
37
|
+
output: Record<string, ToolParameter>;
|
|
38
|
+
implementation: (...args: any[]) => any;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create a tool parameter definition
|
|
43
|
+
*/
|
|
44
|
+
export function createParameter(
|
|
45
|
+
type: ToolParameterType,
|
|
46
|
+
description: string,
|
|
47
|
+
options?: {
|
|
48
|
+
required?: boolean;
|
|
49
|
+
properties?: Record<string, ToolParameter>;
|
|
50
|
+
items?: ToolParameter;
|
|
51
|
+
},
|
|
52
|
+
): ToolParameter {
|
|
53
|
+
return {
|
|
54
|
+
type,
|
|
55
|
+
description,
|
|
56
|
+
...options,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Generate a tool schema from a tool definition
|
|
62
|
+
*/
|
|
63
|
+
export function generateToolSchema(definition: ToolDefinition): ToolSchema {
|
|
64
|
+
const requiredParams = Object.entries(definition.parameters)
|
|
65
|
+
.filter(([_, param]) => param.required)
|
|
66
|
+
.map(([name]) => name);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
type: 'local',
|
|
70
|
+
name: definition.name,
|
|
71
|
+
description: definition.description,
|
|
72
|
+
config: {
|
|
73
|
+
timeout: definition.timeout || 60,
|
|
74
|
+
parameters: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
title: definition.title || `${definition.name} parameters`,
|
|
77
|
+
required: requiredParams,
|
|
78
|
+
properties: definition.parameters,
|
|
79
|
+
},
|
|
80
|
+
output: {
|
|
81
|
+
type: 'object',
|
|
82
|
+
title: `${definition.name} output`,
|
|
83
|
+
properties: definition.output,
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type ToolInstance = { schema: ToolSchema; implementation: (...args: any[]) => any };
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Create a complete tool definition with schema and implementation
|
|
93
|
+
*/
|
|
94
|
+
export function createTool(definition: ToolDefinition): ToolInstance {
|
|
95
|
+
return {
|
|
96
|
+
schema: generateToolSchema(definition),
|
|
97
|
+
implementation: definition.implementation,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Extract function signature and generate schema from a JavaScript function
|
|
103
|
+
* This is a utility to help convert existing functions to tool schemas
|
|
104
|
+
*/
|
|
105
|
+
export function createToolFromFunction(
|
|
106
|
+
name: string,
|
|
107
|
+
description: string,
|
|
108
|
+
fn: (...args: any[]) => any,
|
|
109
|
+
parameterTypes: Record<string, ToolParameter>,
|
|
110
|
+
outputTypes: Record<string, ToolParameter>,
|
|
111
|
+
options?: {
|
|
112
|
+
title?: string;
|
|
113
|
+
timeout?: number;
|
|
114
|
+
},
|
|
115
|
+
): ToolInstance {
|
|
116
|
+
const definition: ToolDefinition = {
|
|
117
|
+
name,
|
|
118
|
+
description,
|
|
119
|
+
title: options?.title,
|
|
120
|
+
timeout: options?.timeout,
|
|
121
|
+
parameters: parameterTypes,
|
|
122
|
+
output: outputTypes,
|
|
123
|
+
implementation: fn,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return createTool(definition);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Example usage for the sum function you provided:
|
|
130
|
+
export const sumTool = createToolFromFunction(
|
|
131
|
+
'sum',
|
|
132
|
+
'Sum two numbers',
|
|
133
|
+
function sum(a: number, b: number) {
|
|
134
|
+
const result = a + b;
|
|
135
|
+
return { result };
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
a: createParameter('number', 'First number to sum', { required: true }),
|
|
139
|
+
b: createParameter('number', 'Second number to sum', { required: true }),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
result: createParameter('number', 'Sum of two numbers'),
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// Example for the navigate_to function as shown in the user's request:
|
|
147
|
+
export const navigateToToolExample = createTool({
|
|
148
|
+
name: 'navigate_to',
|
|
149
|
+
description: 'Allow agent to redirect user to specific sub page like /foo or #/foo or anything like that',
|
|
150
|
+
title: 'Sum two numbers', // As per the user's example
|
|
151
|
+
timeout: 60,
|
|
152
|
+
parameters: {
|
|
153
|
+
a: createParameter('number', 'First number to sum'),
|
|
154
|
+
b: createParameter('number', 'Seconth number to sum'), // Keeping the typo as in the original
|
|
155
|
+
},
|
|
156
|
+
output: {
|
|
157
|
+
result: createParameter('number', 'Sum of two numbers'),
|
|
158
|
+
},
|
|
159
|
+
implementation: function navigateTo(a: number, b: number) {
|
|
160
|
+
// This is just an example - you would implement actual navigation logic here
|
|
161
|
+
const result = a + b;
|
|
162
|
+
return { result };
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Helper function to create multiple tools at once
|
|
167
|
+
export function createToolRegistry(tools: ToolDefinition[]): {
|
|
168
|
+
schemas: ToolSchema[];
|
|
169
|
+
implementations: Record<string, (...args: any[]) => any>;
|
|
170
|
+
} {
|
|
171
|
+
const schemas: ToolSchema[] = [];
|
|
172
|
+
const implementations: Record<string, (...args: any[]) => any> = {};
|
|
173
|
+
|
|
174
|
+
tools.forEach((tool) => {
|
|
175
|
+
const { schema, implementation } = createTool(tool);
|
|
176
|
+
schemas.push(schema);
|
|
177
|
+
implementations[tool.name] = implementation;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
return { schemas, implementations };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Utility to validate tool parameters at runtime
|
|
184
|
+
export function validateToolParameters(parameters: Record<string, any>, schema: ToolSchema): boolean {
|
|
185
|
+
const { required, properties } = schema.config.parameters;
|
|
186
|
+
|
|
187
|
+
// Check required parameters
|
|
188
|
+
for (const requiredParam of required) {
|
|
189
|
+
if (!(requiredParam in parameters)) {
|
|
190
|
+
throw new Error(`Missing required parameter: ${requiredParam}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Type checking (basic)
|
|
195
|
+
for (const [paramName, paramValue] of Object.entries(parameters)) {
|
|
196
|
+
const paramSchema = properties[paramName];
|
|
197
|
+
if (paramSchema) {
|
|
198
|
+
if (paramSchema.type === 'number' && typeof paramValue !== 'number') {
|
|
199
|
+
throw new Error(`Parameter ${paramName} should be a number`);
|
|
200
|
+
}
|
|
201
|
+
if (paramSchema.type === 'string' && typeof paramValue !== 'string') {
|
|
202
|
+
throw new Error(`Parameter ${paramName} should be a string`);
|
|
203
|
+
}
|
|
204
|
+
if (paramSchema.type === 'boolean' && typeof paramValue !== 'boolean') {
|
|
205
|
+
throw new Error(`Parameter ${paramName} should be a boolean`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return true;
|
|
211
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { PersonaTools } from 'src/protocol';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import type { ToolInstance } from './tools';
|
|
4
|
+
import type { PersonaLogger } from './logging';
|
|
5
|
+
import type { SessionStorage } from './storage';
|
|
6
|
+
|
|
7
|
+
export type { PersonaLogger, SessionStorage };
|
|
8
|
+
|
|
9
|
+
export type ReadonlyJSONObject = {
|
|
10
|
+
readonly [key: string]: string | number | boolean | null | ReadonlyJSONObject | ReadonlyArray<ReadonlyJSONObject>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type FunctionCall = {
|
|
14
|
+
id?: string;
|
|
15
|
+
name: string;
|
|
16
|
+
args: ReadonlyJSONObject;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type FunctionResponse = {
|
|
20
|
+
name: string;
|
|
21
|
+
result: unknown;
|
|
22
|
+
function_call_id: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type PersonaFile = {
|
|
26
|
+
name: string;
|
|
27
|
+
url?: string;
|
|
28
|
+
base64?: string;
|
|
29
|
+
contentType: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type PersonaSession = {
|
|
33
|
+
id: string;
|
|
34
|
+
code: string;
|
|
35
|
+
projectId: string;
|
|
36
|
+
agentId: string;
|
|
37
|
+
currentAgentId: string;
|
|
38
|
+
userId: string;
|
|
39
|
+
status: 'active' | 'finished';
|
|
40
|
+
type: 'realtime' | 'async';
|
|
41
|
+
createdAt: string;
|
|
42
|
+
finishedAt: string | null;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type PersonaProjectSubscription = {
|
|
46
|
+
id: string;
|
|
47
|
+
projectId?: string | null;
|
|
48
|
+
owner?: string | null;
|
|
49
|
+
planType: 'free' | 'baby' | 'junior' | 'pro' | 'mentor';
|
|
50
|
+
status: 'creating' | 'active' | 'inactive' | 'cancelled';
|
|
51
|
+
startDate?: string | null;
|
|
52
|
+
planCredits: number;
|
|
53
|
+
extraCredits: number;
|
|
54
|
+
keepCreditsOnRenew: boolean;
|
|
55
|
+
endDate?: string | null;
|
|
56
|
+
createdAt?: string | null;
|
|
57
|
+
updatedAt?: string | null;
|
|
58
|
+
stripeCustomerId?: string | null;
|
|
59
|
+
stripeSubscriptionId?: string | null;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type PersonaAgentContext = {
|
|
63
|
+
data: Record<string, unknown>;
|
|
64
|
+
files?: PersonaFile[];
|
|
65
|
+
session?: PersonaSession;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type PersonaTransaction = {
|
|
69
|
+
id: string;
|
|
70
|
+
projectId: string;
|
|
71
|
+
sessionId: string;
|
|
72
|
+
status: 'pending' | 'completed' | 'failed';
|
|
73
|
+
error: string | null;
|
|
74
|
+
functionCall: FunctionCall | null;
|
|
75
|
+
context: PersonaAgentContext;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type PersonaReasoning = {
|
|
79
|
+
thought: string;
|
|
80
|
+
imageUrl?: string;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export type PersonaImage = {
|
|
84
|
+
content: string;
|
|
85
|
+
contentType: string;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export type PersonaSourceChunk = {
|
|
89
|
+
start: number;
|
|
90
|
+
end: number;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export type PersonaSource = {
|
|
94
|
+
type: 'web' | 'document';
|
|
95
|
+
url?: string;
|
|
96
|
+
documentId?: string;
|
|
97
|
+
pageId?: string;
|
|
98
|
+
pageNumber?: number;
|
|
99
|
+
chunks?: PersonaSourceChunk[];
|
|
100
|
+
mostRelevant?: boolean;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export type PersonaCommand = {
|
|
104
|
+
command: 'get_mcp_assets' | 'set_initial_context' | 'set_local_tools';
|
|
105
|
+
arguments: Record<string, unknown>;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type PersonaRequest = PersonaMessage | Array<PersonaMessage> | string;
|
|
109
|
+
|
|
110
|
+
export type PersonaPacketPayload = PersonaRequest | PersonaCommand | PersonaReasoning | PersonaTransaction;
|
|
111
|
+
|
|
112
|
+
export type PersonaPacket = {
|
|
113
|
+
type: 'request' | 'message' | 'command' | 'reasoning' | 'mcp_assets' | 'transaction';
|
|
114
|
+
payload?: PersonaPacketPayload;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export type PersonaMessage = {
|
|
118
|
+
id?: string | null;
|
|
119
|
+
protocol?: string;
|
|
120
|
+
thought?: string;
|
|
121
|
+
text: string;
|
|
122
|
+
image?: PersonaImage;
|
|
123
|
+
type: 'reasoning' | 'text' | 'transaction' | 'command';
|
|
124
|
+
role: 'user' | 'assistant' | 'function';
|
|
125
|
+
file?: PersonaFile;
|
|
126
|
+
sources?: PersonaSource[];
|
|
127
|
+
sessionId?: string;
|
|
128
|
+
finishReason?: 'stop' | 'function_call';
|
|
129
|
+
functionCalls?: FunctionCall[];
|
|
130
|
+
functionResponse?: FunctionResponse;
|
|
131
|
+
status?: { type: 'running' | 'complete' | 'incomplete' };
|
|
132
|
+
// ThreadMessageLike compatible fields
|
|
133
|
+
createdAt?: Date;
|
|
134
|
+
metadata?: {
|
|
135
|
+
custom?: Record<string, unknown>;
|
|
136
|
+
[key: string]: unknown;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export type ModelResponse = {
|
|
141
|
+
messages: PersonaMessage[];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export type PersonaResponse = {
|
|
145
|
+
sessionId: string;
|
|
146
|
+
response: ModelResponse;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export type PersonaBaseConfig = {
|
|
150
|
+
logger?: PersonaLogger;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export type PersonaWebRTCConfig = PersonaBaseConfig & {
|
|
154
|
+
webrtcUrl: string;
|
|
155
|
+
iceServers?: RTCIceServer[];
|
|
156
|
+
onDataMessage?: (msg: MessageEvent) => void;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export type Session = string | null | undefined;
|
|
160
|
+
|
|
161
|
+
export type TransformMessages = (messages: PersonaMessage[]) => PersonaMessage[];
|
|
162
|
+
|
|
163
|
+
export type ThreadData = {
|
|
164
|
+
threadId: string;
|
|
165
|
+
title?: string;
|
|
166
|
+
status: 'regular' | 'archived';
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export type MessageListenerCallback = (message: PersonaPacket) => void;
|
|
170
|
+
export type StatusChangeCallback = (status: ProtocolStatus) => void;
|
|
171
|
+
|
|
172
|
+
export type ProtocolStatus = 'disconnected' | 'connecting' | 'connected';
|
|
173
|
+
|
|
174
|
+
export type PersonaProtocolBaseConfig = {
|
|
175
|
+
logger?: PersonaLogger;
|
|
176
|
+
apiKey: string;
|
|
177
|
+
agentId: string;
|
|
178
|
+
userId?: string;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export interface PersonaProtocol {
|
|
182
|
+
status: ProtocolStatus;
|
|
183
|
+
autostart: boolean;
|
|
184
|
+
|
|
185
|
+
getName: () => string;
|
|
186
|
+
getPriority: () => number;
|
|
187
|
+
connect: (session?: Session) => Promise<Session>;
|
|
188
|
+
disconnect: () => Promise<void>;
|
|
189
|
+
|
|
190
|
+
onTransaction: (transaction: PersonaTransaction) => void;
|
|
191
|
+
|
|
192
|
+
setSession: (session: Session) => Promise<void>;
|
|
193
|
+
|
|
194
|
+
sendPacket: (packet: PersonaPacket) => Promise<void>;
|
|
195
|
+
setAgentContext: (context: PersonaAgentContext) => Promise<void>;
|
|
196
|
+
clearListeners: () => void;
|
|
197
|
+
addStatusChangeListener: (callback: StatusChangeCallback) => void;
|
|
198
|
+
addPacketListener: (callback: MessageListenerCallback) => void;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export type PersonaConfig = PersonaBaseConfig &
|
|
202
|
+
PersonaProtocolBaseConfig & {
|
|
203
|
+
/**
|
|
204
|
+
* In 'dev' mode the endpoint is set to localhost:8000,
|
|
205
|
+
* use it only for development purposes.
|
|
206
|
+
*/
|
|
207
|
+
dev?: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* The base URL of the API.
|
|
210
|
+
* @default 'persona.applica.guru/api'
|
|
211
|
+
*/
|
|
212
|
+
baseUrl?: string;
|
|
213
|
+
session?: Session;
|
|
214
|
+
children: ReactNode;
|
|
215
|
+
context?: PersonaAgentContext;
|
|
216
|
+
protocols:
|
|
217
|
+
| PersonaProtocol[]
|
|
218
|
+
| {
|
|
219
|
+
rest?: PersonaProtocolBaseConfig | boolean;
|
|
220
|
+
webrtc?: PersonaProtocolBaseConfig | boolean;
|
|
221
|
+
websocket?: PersonaProtocolBaseConfig | boolean;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Tools can be provided as an object (legacy) or as an array of { schema, implementation } (recommended)
|
|
226
|
+
*/
|
|
227
|
+
tools?: PersonaTools | ToolInstance[];
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Optional message transform function that processes messages before they are displayed
|
|
231
|
+
*/
|
|
232
|
+
transformMessages?: TransformMessages;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Enable thread management support
|
|
236
|
+
*/
|
|
237
|
+
threads?: boolean;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Session storage implementation
|
|
241
|
+
* If not provided, defaults to PersonaSessionStorage
|
|
242
|
+
*/
|
|
243
|
+
sessionStorage?: SessionStorage;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Initial thread list (if thread management is enabled)
|
|
247
|
+
*/
|
|
248
|
+
initialThreads?: ThreadData[];
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Callback when a new thread is created
|
|
252
|
+
*/
|
|
253
|
+
onThreadCreate?: (threadId: string) => void;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Callback when switching to a thread
|
|
257
|
+
*/
|
|
258
|
+
onThreadSwitch?: (threadId: string) => void;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Callback when a thread is renamed
|
|
262
|
+
*/
|
|
263
|
+
onThreadRename?: (threadId: string, newTitle: string) => void;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Callback when a thread is archived
|
|
267
|
+
*/
|
|
268
|
+
onThreadArchive?: (threadId: string) => void;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Callback when a thread is unarchived
|
|
272
|
+
*/
|
|
273
|
+
onThreadUnarchive?: (threadId: string) => void;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Callback when a thread is deleted
|
|
277
|
+
*/
|
|
278
|
+
onThreadDelete?: (threadId: string) => void;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Enable file attachments (images, PDFs, text files) in the composer
|
|
282
|
+
*/
|
|
283
|
+
enableAttachments?: boolean;
|
|
284
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"baseUrl": ".",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"isolatedModules": true,
|
|
11
|
+
"jsx": "react-jsx",
|
|
12
|
+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
13
|
+
"module": "ESNext",
|
|
14
|
+
"moduleResolution": "Node",
|
|
15
|
+
"noEmit": false,
|
|
16
|
+
"noUnusedLocals": true,
|
|
17
|
+
"noUnusedParameters": true,
|
|
18
|
+
"outDir": "dist",
|
|
19
|
+
"paths": {
|
|
20
|
+
"@/*": ["playground/src/*"],
|
|
21
|
+
"@applica-software-guru/persona-chat-sdk": ["src/index.ts"]
|
|
22
|
+
},
|
|
23
|
+
"resolveJsonModule": true,
|
|
24
|
+
"skipLibCheck": true,
|
|
25
|
+
"strict": true,
|
|
26
|
+
"target": "ESNext",
|
|
27
|
+
"typeRoots": ["node_modules/@types", "src/assets.d.ts", "src/index.d.ts", "src/types.d.ts"],
|
|
28
|
+
"useDefineForClassFields": true
|
|
29
|
+
},
|
|
30
|
+
"include": ["src", "playground/src"],
|
|
31
|
+
"references": [
|
|
32
|
+
{
|
|
33
|
+
"path": "./jsconfig.node.json"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"composite": true,
|
|
6
|
+
"jsx": "react-jsx",
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"target": "ES6"
|
|
13
|
+
},
|
|
14
|
+
"include": ["vite.config.ts", "package.json", "playground/src", "src/**/*"]
|
|
15
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import react from '@vitejs/plugin-react';
|
|
2
|
+
import * as packageJson from './package.json';
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
import { ConfigEnv, defineConfig, UserConfig } from 'vite';
|
|
5
|
+
import { default as dts } from 'vite-plugin-dts';
|
|
6
|
+
import tailwindcss from '@tailwindcss/vite';
|
|
7
|
+
|
|
8
|
+
export default defineConfig((configEnv: ConfigEnv) => {
|
|
9
|
+
const { mode } = configEnv;
|
|
10
|
+
const isDevelopment = mode === 'development';
|
|
11
|
+
const config: UserConfig = {
|
|
12
|
+
build: {
|
|
13
|
+
sourcemap: true,
|
|
14
|
+
lib: {
|
|
15
|
+
entry: resolve('src', 'index.ts'),
|
|
16
|
+
name: 'personaChatSDK',
|
|
17
|
+
formats: ['es', 'umd', 'cjs', 'iife'],
|
|
18
|
+
fileName: (format: string) => `bundle.${format}.js`,
|
|
19
|
+
},
|
|
20
|
+
rollupOptions: {
|
|
21
|
+
external: [
|
|
22
|
+
...Object.keys(packageJson.peerDependencies),
|
|
23
|
+
...Object.keys(packageJson.dependencies || {}),
|
|
24
|
+
/^react\//,
|
|
25
|
+
/^react-dom\//,
|
|
26
|
+
/^@radix-ui\//,
|
|
27
|
+
],
|
|
28
|
+
input: resolve(__dirname, 'src', 'index.ts'),
|
|
29
|
+
output: {
|
|
30
|
+
globals: {
|
|
31
|
+
'react-dom': 'ReactDOM',
|
|
32
|
+
react: 'React',
|
|
33
|
+
'react/jsx-runtime': 'React',
|
|
34
|
+
'react/jsx-dev-runtime': 'React',
|
|
35
|
+
'@assistant-ui/react': 'AssistantUI',
|
|
36
|
+
'@radix-ui/react-dialog': 'RadixDialog',
|
|
37
|
+
'@radix-ui/react-slot': 'RadixSlot',
|
|
38
|
+
'rehype-raw': 'rehypeRaw',
|
|
39
|
+
zustand: 'zustand',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
resolve: {
|
|
45
|
+
dedupe: Object.keys(packageJson.peerDependencies || {}),
|
|
46
|
+
alias: {
|
|
47
|
+
'@applica-software-guru/persona-chat-sdk': resolve(__dirname, 'src'),
|
|
48
|
+
'@': resolve(__dirname, 'playground/src'),
|
|
49
|
+
'@/*': resolve(__dirname, 'playground/src/*'),
|
|
50
|
+
'*': resolve(__dirname, 'src/*'),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
plugins: [
|
|
54
|
+
dts({
|
|
55
|
+
include: ['./src/**/*.{js,jsx,ts,tsx}'],
|
|
56
|
+
}),
|
|
57
|
+
react(),
|
|
58
|
+
],
|
|
59
|
+
define: {
|
|
60
|
+
'process.env': {
|
|
61
|
+
NODE_ENV: 'production',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
optimizeDeps: {
|
|
65
|
+
include: Object.keys(packageJson.peerDependencies || {}),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
if (isDevelopment) {
|
|
69
|
+
config.plugins = [react(), tailwindcss()];
|
|
70
|
+
config.root = resolve(__dirname, 'playground');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return config;
|
|
74
|
+
});
|