@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/projects.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { PersonaProjectSubscription } from './types';
|
|
2
|
+
|
|
3
|
+
type ProjectsApiConfig = {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
apiKey: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
class ProjectsApi {
|
|
9
|
+
private readonly baseUrl: string;
|
|
10
|
+
private readonly apiKey: string;
|
|
11
|
+
|
|
12
|
+
constructor(config: ProjectsApiConfig) {
|
|
13
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
14
|
+
this.apiKey = config.apiKey;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
18
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
19
|
+
...init,
|
|
20
|
+
headers: {
|
|
21
|
+
'Content-Type': 'application/json',
|
|
22
|
+
'x-persona-apikey': this.apiKey,
|
|
23
|
+
...(init?.headers ?? {}),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(await response.text());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (await response.json()) as T;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async getSubscription(projectId: string): Promise<PersonaProjectSubscription> {
|
|
35
|
+
return this.request<PersonaProjectSubscription>(`/projects/${projectId}/subscription`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async changeSubscriptionPlan(
|
|
39
|
+
projectId: string,
|
|
40
|
+
toPlanType: PersonaProjectSubscription['planType'],
|
|
41
|
+
): Promise<PersonaProjectSubscription> {
|
|
42
|
+
return this.request<PersonaProjectSubscription>(`/projects/${projectId}/subscription/actions/change-plan`, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
body: JSON.stringify({ toPlanType }),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async addSubscriptionCredits(projectId: string, credits: number): Promise<PersonaProjectSubscription> {
|
|
49
|
+
return this.request<PersonaProjectSubscription>(`/projects/${projectId}/subscription/actions/add-credits`, {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
body: JSON.stringify({ credits }),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { ProjectsApi };
|
|
57
|
+
export type { ProjectsApiConfig };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MessageListenerCallback,
|
|
3
|
+
PersonaAgentContext,
|
|
4
|
+
PersonaPacket,
|
|
5
|
+
PersonaProtocol,
|
|
6
|
+
PersonaTransaction,
|
|
7
|
+
ProtocolStatus,
|
|
8
|
+
Session,
|
|
9
|
+
StatusChangeCallback,
|
|
10
|
+
} from '../types';
|
|
11
|
+
|
|
12
|
+
abstract class PersonaProtocolBase implements PersonaProtocol {
|
|
13
|
+
abstract status: ProtocolStatus;
|
|
14
|
+
abstract session: Session;
|
|
15
|
+
abstract autostart: boolean;
|
|
16
|
+
|
|
17
|
+
private statusChangeCallbacks: StatusChangeCallback[] = [];
|
|
18
|
+
private messageCallbacks: MessageListenerCallback[] = [];
|
|
19
|
+
|
|
20
|
+
public addStatusChangeListener(callback: StatusChangeCallback) {
|
|
21
|
+
this.statusChangeCallbacks.push(callback);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public addPacketListener(callback: MessageListenerCallback) {
|
|
25
|
+
this.messageCallbacks.push(callback);
|
|
26
|
+
}
|
|
27
|
+
public async syncSession(session: Session): Promise<void> {
|
|
28
|
+
this.session = session;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async notifyPacket(message: PersonaPacket): Promise<void> {
|
|
32
|
+
this.messageCallbacks.forEach((callback) => callback(message));
|
|
33
|
+
}
|
|
34
|
+
public async notifyPackets(messages: PersonaPacket[]): Promise<void> {
|
|
35
|
+
messages.forEach((message) => {
|
|
36
|
+
this.messageCallbacks.forEach((callback) => callback(message));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async setSession(session: Session): Promise<void> {
|
|
41
|
+
this.session = session;
|
|
42
|
+
}
|
|
43
|
+
public async setStatus(status: ProtocolStatus): Promise<void> {
|
|
44
|
+
const notify = this.status !== status;
|
|
45
|
+
this.status = status;
|
|
46
|
+
if (!notify) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.statusChangeCallbacks.forEach((callback) => callback(status));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public clearListeners(): void {
|
|
53
|
+
this.statusChangeCallbacks = [];
|
|
54
|
+
this.messageCallbacks = [];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public setAgentContext(context: PersonaAgentContext): Promise<void> {
|
|
58
|
+
return this.sendPacket({
|
|
59
|
+
type: 'command',
|
|
60
|
+
payload: { command: 'set_initial_context', arguments: context },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
abstract getName(): string;
|
|
65
|
+
abstract getPriority(): number;
|
|
66
|
+
abstract connect(session?: Session): Promise<Session>;
|
|
67
|
+
abstract disconnect(): Promise<void>;
|
|
68
|
+
abstract sendPacket(packet: PersonaPacket): Promise<void>;
|
|
69
|
+
|
|
70
|
+
public onTransaction(_: PersonaTransaction) {}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { PersonaProtocolBase };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { PersonaProtocolBase } from './base';
|
|
2
|
+
import {
|
|
3
|
+
PersonaResponse,
|
|
4
|
+
Session,
|
|
5
|
+
ProtocolStatus,
|
|
6
|
+
PersonaProtocolBaseConfig,
|
|
7
|
+
PersonaMessage,
|
|
8
|
+
PersonaPacket,
|
|
9
|
+
PersonaCommand,
|
|
10
|
+
} from '../types';
|
|
11
|
+
import { ToolInstance } from '../tools';
|
|
12
|
+
|
|
13
|
+
type PersonaRESTProtocolConfig = PersonaProtocolBaseConfig & {
|
|
14
|
+
apiUrl: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
class PersonaRESTProtocol extends PersonaProtocolBase {
|
|
18
|
+
status: ProtocolStatus;
|
|
19
|
+
autostart: boolean;
|
|
20
|
+
session: Session;
|
|
21
|
+
config: PersonaRESTProtocolConfig;
|
|
22
|
+
notify: boolean = true;
|
|
23
|
+
context: Record<string, any> = {};
|
|
24
|
+
tools: ToolInstance[] = [];
|
|
25
|
+
|
|
26
|
+
constructor(config: PersonaRESTProtocolConfig) {
|
|
27
|
+
super();
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.status = 'disconnected';
|
|
30
|
+
this.autostart = true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public getName(): string {
|
|
34
|
+
return 'rest';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public getPriority(): number {
|
|
38
|
+
return 100;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async connect(session: Session): Promise<Session> {
|
|
42
|
+
this.setStatus('connected');
|
|
43
|
+
return session;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async disconnect(): Promise<void> {
|
|
47
|
+
this.setStatus('disconnected');
|
|
48
|
+
this.session = null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async syncSession(session: Session): Promise<void> {
|
|
52
|
+
this.session = session;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async sendPacket(packet: PersonaPacket): Promise<void> {
|
|
56
|
+
const { apiUrl, apiKey, agentId } = this.config;
|
|
57
|
+
const sessionId = this.session ?? 'new';
|
|
58
|
+
if (packet.type === 'command' && (packet?.payload as PersonaCommand)?.command == 'set_initial_context') {
|
|
59
|
+
this.context = (packet?.payload as PersonaCommand)?.arguments;
|
|
60
|
+
return;
|
|
61
|
+
} else if (packet.type === 'command' && (packet?.payload as PersonaCommand)?.command == 'set_local_tools') {
|
|
62
|
+
const tools = (packet?.payload as PersonaCommand)?.arguments.tools as ToolInstance[];
|
|
63
|
+
if (tools?.length > 0) {
|
|
64
|
+
this.notifyPacket({
|
|
65
|
+
type: 'message',
|
|
66
|
+
payload: {
|
|
67
|
+
type: 'text',
|
|
68
|
+
role: 'assistant',
|
|
69
|
+
text: 'Local tools with rest protocol are not supported.',
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const input = packet.payload as PersonaMessage;
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(`${apiUrl}/sessions/${sessionId}/messages`, {
|
|
78
|
+
body: JSON.stringify({ agentId, userMessage: input, initialContext: this.context, tools: this.tools }),
|
|
79
|
+
method: 'POST',
|
|
80
|
+
headers: {
|
|
81
|
+
'Content-Type': 'application/json',
|
|
82
|
+
'x-persona-apikey': apiKey,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const personaResponse = (await response.json()) as PersonaResponse;
|
|
86
|
+
this.notifyPackets(
|
|
87
|
+
personaResponse.response.messages.map((payload) => ({
|
|
88
|
+
type: 'message',
|
|
89
|
+
payload,
|
|
90
|
+
})),
|
|
91
|
+
);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
this.notifyPacket({
|
|
94
|
+
type: 'message',
|
|
95
|
+
payload: {
|
|
96
|
+
role: 'assistant',
|
|
97
|
+
type: 'text',
|
|
98
|
+
text: 'An error occurred while processing your request. Please try again later.',
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
this.config.logger?.error('Error sending packet:', error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { PersonaRESTProtocol };
|
|
107
|
+
export type { PersonaRESTProtocolConfig };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { PersonaProtocolBase } from './base';
|
|
2
|
+
import { Session, ProtocolStatus, PersonaProtocolBaseConfig, PersonaTransaction, FunctionCall, PersonaPacket } from '../types';
|
|
3
|
+
import { ToolInstance } from '../tools';
|
|
4
|
+
import _ from 'lodash';
|
|
5
|
+
|
|
6
|
+
type FinishTransactionRequest = {
|
|
7
|
+
success: boolean;
|
|
8
|
+
output: any;
|
|
9
|
+
error: string | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
class PersonaTransactionsManager {
|
|
13
|
+
private config: PersonaTransactionProtocolConfig;
|
|
14
|
+
|
|
15
|
+
constructor(config: PersonaTransactionProtocolConfig) {
|
|
16
|
+
this.config = config;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async complete(transaction: PersonaTransaction, request: FinishTransactionRequest): Promise<void> {
|
|
20
|
+
await this.persist(transaction, { ...request, success: true });
|
|
21
|
+
this.config.logger?.debug('Transaction completed:', transaction);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async fail(transaction: PersonaTransaction, request: FinishTransactionRequest): Promise<void> {
|
|
25
|
+
await this.persist(transaction, { ...request, success: false });
|
|
26
|
+
this.config.logger?.debug('Transaction failed:', { ...transaction, ...request });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async persist(transaction: PersonaTransaction, request: FinishTransactionRequest): Promise<void> {
|
|
30
|
+
await fetch(`${this.config.apiUrl}/transactions/${transaction.id}`, {
|
|
31
|
+
body: JSON.stringify(request),
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: {
|
|
34
|
+
'Content-Type': 'application/json',
|
|
35
|
+
Accept: 'application/json',
|
|
36
|
+
'x-persona-apikey': this.config.apiKey,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type PersonaToolCallback = (...args: any[]) => void | Record<string, any> | Promise<void | Record<string, any>>;
|
|
43
|
+
export type PersonaTools = {
|
|
44
|
+
[key: string]: PersonaToolCallback;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class PersonaPersistableTransaction {
|
|
48
|
+
private transaction: PersonaTransaction;
|
|
49
|
+
private manager: PersonaTransactionsManager;
|
|
50
|
+
|
|
51
|
+
constructor(transaction: PersonaTransaction, manager: PersonaTransactionsManager) {
|
|
52
|
+
this.transaction = transaction;
|
|
53
|
+
this.manager = manager;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public getFunctionCall(): FunctionCall | null {
|
|
57
|
+
return this.transaction.functionCall;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async invoke(tools: PersonaTools, toolInstance?: ToolInstance): Promise<void> {
|
|
61
|
+
const functionCall = this.transaction.functionCall;
|
|
62
|
+
if (!functionCall) {
|
|
63
|
+
await this.fail('No function call found');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const functionName = functionCall.name;
|
|
67
|
+
const functionArgs = functionCall.args;
|
|
68
|
+
const tool = tools[functionName];
|
|
69
|
+
if (!tool) {
|
|
70
|
+
await this.fail(`Tool ${functionName} not found`);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const properties = _.get(toolInstance, 'schema.config.parameters.properties', {});
|
|
75
|
+
const paramNames =
|
|
76
|
+
properties && Object.keys(properties).length > 0
|
|
77
|
+
? Object.keys(properties)
|
|
78
|
+
: tool
|
|
79
|
+
.toString()
|
|
80
|
+
.replace(/\n/g, ' ')
|
|
81
|
+
.match(/^[^(]*\(([^)]*)\)/)?.[1]
|
|
82
|
+
.split(',')
|
|
83
|
+
.map((p: string) => p.trim())
|
|
84
|
+
.filter(Boolean) || [];
|
|
85
|
+
|
|
86
|
+
// Map arguments in order
|
|
87
|
+
const orderedArgs = paramNames.map((name: string) => functionArgs[name]);
|
|
88
|
+
const result = await tool.apply(null, orderedArgs);
|
|
89
|
+
await this.complete(result);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
await this.fail(`Error executing tool ${functionName}: ${error}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async complete(output: any): Promise<void> {
|
|
95
|
+
await this.manager.complete(this.transaction, { success: true, output, error: null });
|
|
96
|
+
}
|
|
97
|
+
async fail(error: string): Promise<void> {
|
|
98
|
+
await this.manager.fail(this.transaction, { success: false, output: null, error });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type PersonaTransactionCallback = (transaction: PersonaPersistableTransaction) => void;
|
|
103
|
+
|
|
104
|
+
type PersonaTransactionProtocolConfig = PersonaProtocolBaseConfig & {
|
|
105
|
+
apiUrl: string;
|
|
106
|
+
tools: PersonaTools | ToolInstance[];
|
|
107
|
+
onTransaction?: PersonaTransactionCallback;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
class PersonaTransactionProtocol extends PersonaProtocolBase {
|
|
111
|
+
status: ProtocolStatus;
|
|
112
|
+
autostart: boolean;
|
|
113
|
+
session: Session;
|
|
114
|
+
config: PersonaTransactionProtocolConfig;
|
|
115
|
+
notify: boolean = true;
|
|
116
|
+
private _tools: PersonaTools;
|
|
117
|
+
private _toolSchema: { [key: string]: ToolInstance } = {};
|
|
118
|
+
|
|
119
|
+
constructor(config: PersonaTransactionProtocolConfig) {
|
|
120
|
+
super();
|
|
121
|
+
this.config = config;
|
|
122
|
+
this.status = 'disconnected';
|
|
123
|
+
this.autostart = true;
|
|
124
|
+
if (Array.isArray(config.tools)) {
|
|
125
|
+
this._tools = {};
|
|
126
|
+
for (const tool of config.tools as ToolInstance[]) {
|
|
127
|
+
if (tool.schema && tool.implementation) {
|
|
128
|
+
this._tools[tool.schema.name] = tool.implementation;
|
|
129
|
+
this._toolSchema[tool.schema.name] = tool;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
this._tools = config.tools as PersonaTools;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public getName(): string {
|
|
138
|
+
return 'transaction';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public getPriority(): number {
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public getToolInstance(name: string): ToolInstance | undefined {
|
|
146
|
+
return this._toolSchema[name];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public async connect(session: Session): Promise<Session> {
|
|
150
|
+
this.setStatus('connected');
|
|
151
|
+
return session;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public async disconnect(): Promise<void> {
|
|
155
|
+
this.setStatus('disconnected');
|
|
156
|
+
this.session = null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public async syncSession(session: Session): Promise<void> {
|
|
160
|
+
this.session = session;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public async sendPacket(_: PersonaPacket): Promise<void> {}
|
|
164
|
+
|
|
165
|
+
public onTransaction(transaction: PersonaTransaction): void {
|
|
166
|
+
const manager = new PersonaTransactionsManager(this.config);
|
|
167
|
+
const persistable = new PersonaPersistableTransaction(transaction, manager);
|
|
168
|
+
if (this.config.onTransaction) {
|
|
169
|
+
this.config.onTransaction(persistable);
|
|
170
|
+
} else {
|
|
171
|
+
const toolInstance = transaction.functionCall ? this.getToolInstance(transaction.functionCall.name) : undefined;
|
|
172
|
+
persistable.invoke(this._tools, toolInstance);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public getTools(): PersonaTools {
|
|
177
|
+
return this._tools;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export { PersonaTransactionProtocol };
|
|
182
|
+
export type { PersonaTransactionProtocolConfig, FinishTransactionRequest, PersonaPersistableTransaction, PersonaTransactionCallback };
|