@applica-software-guru/persona-sdk 0.1.63 → 0.1.65
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 +45 -29
- package/dist/bundle.cjs.js +3 -3
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +543 -364
- package/dist/bundle.es.js.map +1 -1
- package/dist/bundle.iife.js +3 -3
- package/dist/bundle.iife.js.map +1 -1
- package/dist/bundle.umd.js +3 -3
- package/dist/bundle.umd.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/protocol/rest.d.ts +2 -0
- package/dist/protocol/rest.d.ts.map +1 -1
- package/dist/protocol/transaction.d.ts +7 -3
- package/dist/protocol/transaction.d.ts.map +1 -1
- package/dist/protocol/webrtc.d.ts +4 -0
- package/dist/protocol/webrtc.d.ts.map +1 -1
- package/dist/protocol/websocket.d.ts.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/tools.d.ts +72 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +6 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/playground/src/chat.tsx +12 -16
- package/playground/src/tools.ts +127 -0
- package/src/index.ts +1 -0
- package/src/protocol/rest.ts +38 -14
- package/src/protocol/transaction.ts +27 -17
- package/src/protocol/webrtc.ts +46 -4
- package/src/protocol/websocket.ts +14 -6
- package/src/runtime.tsx +22 -12
- package/src/tools.ts +211 -0
- package/src/types.ts +6 -2
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"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B,uBAAuB,EAAE,CAAC,QAAQ,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAClE,iBAAiB,EAAE,CAAC,QAAQ,EAAE,uBAAuB,KAAK,IAAI,CAAC;CAChE;AAED,MAAM,MAAM,aAAa,GAAG,iBAAiB,GAC3C,yBAAyB,GAAG;IAC1B;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,SAAS,CAAC;IACpB,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,SAAS,EACL,eAAe,EAAE,GACjB;QACE,IAAI,CAAC,EAAE,yBAAyB,GAAG,OAAO,CAAC;QAC3C,MAAM,CAAC,EAAE,yBAAyB,GAAG,OAAO,CAAC;QAC7C,SAAS,CAAC,EAAE,yBAAyB,GAAG,OAAO,CAAC;KACjD,CAAC;IAEN;;OAEG;IACH,KAAK,CAAC,EAAE,YAAY,GAAG,YAAY,EAAE,CAAC;CACvC,CAAC"}
|
package/package.json
CHANGED
package/playground/src/chat.tsx
CHANGED
|
@@ -3,6 +3,8 @@ import { PersonaRuntimeProvider, PersonaConsoleLogger, Session, usePersonaRuntim
|
|
|
3
3
|
import { useMemo } from 'react';
|
|
4
4
|
import _ from 'lodash';
|
|
5
5
|
import { TooltipProvider } from './components/ui/tooltip';
|
|
6
|
+
import { tools } from '@/tools';
|
|
7
|
+
|
|
6
8
|
const logger = new PersonaConsoleLogger();
|
|
7
9
|
|
|
8
10
|
function generateSID() {
|
|
@@ -58,7 +60,8 @@ function MessageLogger() {
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
function Chat() {
|
|
61
|
-
const
|
|
63
|
+
const agentId = getQueryParam('agentId');
|
|
64
|
+
const apiKey = getQueryParam('apiKey');
|
|
62
65
|
const sid = getQueryParam('sid');
|
|
63
66
|
|
|
64
67
|
const session = useMemo((): Session => {
|
|
@@ -74,26 +77,19 @@ function Chat() {
|
|
|
74
77
|
logger={logger}
|
|
75
78
|
context={{
|
|
76
79
|
data: {
|
|
77
|
-
username: '
|
|
78
|
-
userAgent:
|
|
79
|
-
},
|
|
80
|
-
}}
|
|
81
|
-
tools={{
|
|
82
|
-
get_user_agent: () => {
|
|
83
|
-
const userAgent = 'Bruno Trimone Agent';
|
|
84
|
-
return { userAgent };
|
|
85
|
-
},
|
|
86
|
-
get_client_date: () => {
|
|
87
|
-
const date = new Date();
|
|
88
|
-
return { date: date.toISOString() };
|
|
80
|
+
username: 'Roberto',
|
|
81
|
+
userAgent: 'Google Tridroid 14',
|
|
89
82
|
},
|
|
90
83
|
}}
|
|
84
|
+
tools={tools}
|
|
91
85
|
protocols={{
|
|
92
|
-
rest:
|
|
86
|
+
rest: false,
|
|
87
|
+
websocket: true,
|
|
88
|
+
webrtc: true,
|
|
93
89
|
}}
|
|
94
90
|
session={session}
|
|
95
|
-
apiKey=
|
|
96
|
-
agentId={
|
|
91
|
+
apiKey={apiKey!}
|
|
92
|
+
agentId={agentId!}
|
|
97
93
|
>
|
|
98
94
|
<div className="h-dvh w-full">
|
|
99
95
|
<TooltipProvider>
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example Tool Library
|
|
3
|
+
*
|
|
4
|
+
* This file demonstrates how to define and export a library of example tools
|
|
5
|
+
* using the Persona SDK's createTool utility. These tools can be imported
|
|
6
|
+
* and used in PersonaRuntimeProvider or other parts of the application to
|
|
7
|
+
* showcase or test tool integration.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createTool, PersonaTools } from '@applica-software-guru/persona-sdk';
|
|
11
|
+
|
|
12
|
+
const toolLibrary = [
|
|
13
|
+
createTool({
|
|
14
|
+
name: 'sum',
|
|
15
|
+
title: 'Sum two numbers',
|
|
16
|
+
description: 'Sums two numbers and returns the result.',
|
|
17
|
+
parameters: {
|
|
18
|
+
a: { type: 'number', description: 'First number to sum', required: true },
|
|
19
|
+
b: { type: 'number', description: 'Second number to sum', required: true },
|
|
20
|
+
},
|
|
21
|
+
output: {
|
|
22
|
+
result: { type: 'number', description: 'Sum of the two numbers' },
|
|
23
|
+
},
|
|
24
|
+
implementation: async (a: number, b: number) => {
|
|
25
|
+
const result = a + b;
|
|
26
|
+
return { result };
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
createTool({
|
|
30
|
+
name: 'concat',
|
|
31
|
+
title: 'Concatenate two strings',
|
|
32
|
+
description: 'Concatenates two strings and returns the result.',
|
|
33
|
+
parameters: {
|
|
34
|
+
str1: { type: 'string', description: 'First string to concatenate', required: true },
|
|
35
|
+
str2: { type: 'string', description: 'Second string to concatenate', required: true },
|
|
36
|
+
},
|
|
37
|
+
output: {
|
|
38
|
+
result: { type: 'string', description: 'Concatenated result of the two strings' },
|
|
39
|
+
},
|
|
40
|
+
implementation: async (str1: string, str2: string) => {
|
|
41
|
+
const result = str1 + str2;
|
|
42
|
+
return { result };
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
// User agent tool:
|
|
46
|
+
createTool({
|
|
47
|
+
name: 'userAgent',
|
|
48
|
+
title: 'Get User Agent',
|
|
49
|
+
description: 'Retrieves the user agent string from the client.',
|
|
50
|
+
parameters: {},
|
|
51
|
+
output: {
|
|
52
|
+
result: { type: 'string', description: 'User agent string' },
|
|
53
|
+
},
|
|
54
|
+
implementation: async () => {
|
|
55
|
+
return { result: navigator.userAgent };
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
// Navigate to URL tool:
|
|
59
|
+
createTool({
|
|
60
|
+
name: 'navigateTo',
|
|
61
|
+
title: 'Navigate to URL',
|
|
62
|
+
description: 'Navigates the browser to a specified URL.',
|
|
63
|
+
parameters: {
|
|
64
|
+
url: { type: 'string', description: 'The URL to navigate to', required: true },
|
|
65
|
+
},
|
|
66
|
+
output: {
|
|
67
|
+
success: { type: 'boolean', description: 'Indicates if navigation was successful' },
|
|
68
|
+
},
|
|
69
|
+
implementation: async (url: string) => {
|
|
70
|
+
window.location.href = url;
|
|
71
|
+
return { success: true };
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
// Get current URL tool:
|
|
75
|
+
createTool({
|
|
76
|
+
name: 'getCurrentUrl',
|
|
77
|
+
title: 'Get Current URL',
|
|
78
|
+
description: 'Retrieves the current browser URL.',
|
|
79
|
+
parameters: {},
|
|
80
|
+
output: {
|
|
81
|
+
url: { type: 'string', description: 'Current browser URL' },
|
|
82
|
+
},
|
|
83
|
+
implementation: async () => {
|
|
84
|
+
return { url: window.location.href };
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
|
|
88
|
+
// Current client date and time tool:
|
|
89
|
+
createTool({
|
|
90
|
+
name: 'getCurrentDateTime',
|
|
91
|
+
title: 'Get Current Date and Time',
|
|
92
|
+
description: 'Retrieves the current date and time from the client.',
|
|
93
|
+
parameters: {},
|
|
94
|
+
output: {
|
|
95
|
+
dateTime: { type: 'string', description: 'Current date and time in ISO format' },
|
|
96
|
+
},
|
|
97
|
+
implementation: async () => {
|
|
98
|
+
return { dateTime: new Date().toISOString() };
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
const tools: PersonaTools = {
|
|
104
|
+
sum: function (a: number, b: number) {
|
|
105
|
+
const result = a + b;
|
|
106
|
+
return { result };
|
|
107
|
+
},
|
|
108
|
+
concat: function (str1: string, str2: string) {
|
|
109
|
+
const result = str1 + str2;
|
|
110
|
+
return { result };
|
|
111
|
+
},
|
|
112
|
+
navigateTo: function (url: string) {
|
|
113
|
+
window.location.href = url;
|
|
114
|
+
return { success: true };
|
|
115
|
+
},
|
|
116
|
+
getUserAgent: function () {
|
|
117
|
+
return { result: navigator.userAgent };
|
|
118
|
+
},
|
|
119
|
+
getCurrentUrl: function () {
|
|
120
|
+
return { url: window.location.href };
|
|
121
|
+
},
|
|
122
|
+
getCurrentDateTime: function () {
|
|
123
|
+
return { dateTime: new Date().toISOString() };
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export { toolLibrary, tools };
|
package/src/index.ts
CHANGED
package/src/protocol/rest.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
PersonaPacket,
|
|
9
9
|
PersonaCommand,
|
|
10
10
|
} from '../types';
|
|
11
|
+
import { ToolInstance } from '../tools';
|
|
11
12
|
|
|
12
13
|
type PersonaRESTProtocolConfig = PersonaProtocolBaseConfig & {
|
|
13
14
|
apiUrl: string;
|
|
@@ -20,6 +21,7 @@ class PersonaRESTProtocol extends PersonaProtocolBase {
|
|
|
20
21
|
config: PersonaRESTProtocolConfig;
|
|
21
22
|
notify: boolean = true;
|
|
22
23
|
context: Record<string, any> = {};
|
|
24
|
+
tools: ToolInstance[] = [];
|
|
23
25
|
|
|
24
26
|
constructor(config: PersonaRESTProtocolConfig) {
|
|
25
27
|
super();
|
|
@@ -56,23 +58,45 @@ class PersonaRESTProtocol extends PersonaProtocolBase {
|
|
|
56
58
|
if (packet.type === 'command' && (packet?.payload as PersonaCommand)?.command == 'set_initial_context') {
|
|
57
59
|
this.context = (packet?.payload as PersonaCommand)?.arguments;
|
|
58
60
|
return;
|
|
61
|
+
} else if (packet.type === 'command' && (packet?.payload as PersonaCommand)?.command == 'set_local_tools') {
|
|
62
|
+
this.notifyPacket({
|
|
63
|
+
type: 'message',
|
|
64
|
+
payload: {
|
|
65
|
+
type: 'text',
|
|
66
|
+
role: 'assistant',
|
|
67
|
+
text: 'Local tools with rest protocol are not supported.',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
59
71
|
}
|
|
60
72
|
const input = packet.payload as PersonaMessage;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
try {
|
|
74
|
+
const response = await fetch(`${apiUrl}/sessions/${sessionId}/messages`, {
|
|
75
|
+
body: JSON.stringify({ agentId, userMessage: input, initialContext: this.context, tools: this.tools }),
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: {
|
|
78
|
+
'Content-Type': 'application/json',
|
|
79
|
+
'x-persona-apikey': apiKey,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
const personaResponse = (await response.json()) as PersonaResponse;
|
|
83
|
+
this.notifyPackets(
|
|
84
|
+
personaResponse.response.messages.map((payload) => ({
|
|
85
|
+
type: 'message',
|
|
86
|
+
payload,
|
|
87
|
+
})),
|
|
88
|
+
);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
this.notifyPacket({
|
|
72
91
|
type: 'message',
|
|
73
|
-
payload
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
payload: {
|
|
93
|
+
role: 'assistant',
|
|
94
|
+
type: 'text',
|
|
95
|
+
text: 'An error occurred while processing your request. Please try again later.',
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
this.config.logger?.error('Error sending packet:', error);
|
|
99
|
+
}
|
|
76
100
|
}
|
|
77
101
|
}
|
|
78
102
|
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import { PersonaProtocolBase } from './base';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
ProtocolStatus,
|
|
5
|
-
PersonaProtocolBaseConfig,
|
|
6
|
-
PersonaTransaction,
|
|
7
|
-
FunctionCall,
|
|
8
|
-
ReadonlyJSONObject,
|
|
9
|
-
PersonaPacket,
|
|
10
|
-
} from '../types';
|
|
2
|
+
import { Session, ProtocolStatus, PersonaProtocolBaseConfig, PersonaTransaction, FunctionCall, PersonaPacket } from '../types';
|
|
3
|
+
import { ToolInstance } from '../tools';
|
|
11
4
|
|
|
12
5
|
type FinishTransactionRequest = {
|
|
13
6
|
success: boolean;
|
|
@@ -45,7 +38,7 @@ class PersonaTransactionsManager {
|
|
|
45
38
|
}
|
|
46
39
|
}
|
|
47
40
|
|
|
48
|
-
export type PersonaToolCallback = (args:
|
|
41
|
+
export type PersonaToolCallback = (...args: any[]) => void | Record<string, any> | Promise<void | Record<string, any>>;
|
|
49
42
|
export type PersonaTools = {
|
|
50
43
|
[key: string]: PersonaToolCallback;
|
|
51
44
|
};
|
|
@@ -77,7 +70,7 @@ class PersonaPersistableTransaction {
|
|
|
77
70
|
return;
|
|
78
71
|
}
|
|
79
72
|
try {
|
|
80
|
-
const result = await tool(functionArgs);
|
|
73
|
+
const result = await tool.apply(null, Object.values(functionArgs));
|
|
81
74
|
await this.complete(result);
|
|
82
75
|
} catch (error) {
|
|
83
76
|
await this.fail(`Error executing tool ${functionName}: ${error}`);
|
|
@@ -95,7 +88,8 @@ type PersonaTransactionCallback = (transaction: PersonaPersistableTransaction) =
|
|
|
95
88
|
|
|
96
89
|
type PersonaTransactionProtocolConfig = PersonaProtocolBaseConfig & {
|
|
97
90
|
apiUrl: string;
|
|
98
|
-
|
|
91
|
+
tools: PersonaTools | ToolInstance[];
|
|
92
|
+
onTransaction?: PersonaTransactionCallback;
|
|
99
93
|
};
|
|
100
94
|
|
|
101
95
|
class PersonaTransactionProtocol extends PersonaProtocolBase {
|
|
@@ -104,12 +98,23 @@ class PersonaTransactionProtocol extends PersonaProtocolBase {
|
|
|
104
98
|
session: Session;
|
|
105
99
|
config: PersonaTransactionProtocolConfig;
|
|
106
100
|
notify: boolean = true;
|
|
101
|
+
private _tools: PersonaTools;
|
|
107
102
|
|
|
108
103
|
constructor(config: PersonaTransactionProtocolConfig) {
|
|
109
104
|
super();
|
|
110
105
|
this.config = config;
|
|
111
106
|
this.status = 'disconnected';
|
|
112
107
|
this.autostart = true;
|
|
108
|
+
if (Array.isArray(config.tools)) {
|
|
109
|
+
this._tools = {};
|
|
110
|
+
for (const tool of config.tools as ToolInstance[]) {
|
|
111
|
+
if (tool.schema && tool.implementation) {
|
|
112
|
+
this._tools[tool.schema.name] = tool.implementation;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
this._tools = config.tools as PersonaTools;
|
|
117
|
+
}
|
|
113
118
|
}
|
|
114
119
|
|
|
115
120
|
public getName(): string {
|
|
@@ -137,13 +142,18 @@ class PersonaTransactionProtocol extends PersonaProtocolBase {
|
|
|
137
142
|
public async sendPacket(_: PersonaPacket): Promise<void> {}
|
|
138
143
|
|
|
139
144
|
public onTransaction(transaction: PersonaTransaction): void {
|
|
140
|
-
|
|
141
|
-
this.config.logger?.error('Transaction protocol config is not set');
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
145
|
+
console.log('transaction received:', transaction);
|
|
144
146
|
const manager = new PersonaTransactionsManager(this.config);
|
|
145
147
|
const persistable = new PersonaPersistableTransaction(transaction, manager);
|
|
146
|
-
this.config.onTransaction
|
|
148
|
+
if (this.config.onTransaction) {
|
|
149
|
+
this.config.onTransaction(persistable);
|
|
150
|
+
} else {
|
|
151
|
+
persistable.invoke(this._tools);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public getTools(): PersonaTools {
|
|
156
|
+
return this._tools;
|
|
147
157
|
}
|
|
148
158
|
}
|
|
149
159
|
|
package/src/protocol/webrtc.ts
CHANGED
|
@@ -10,6 +10,8 @@ type AudioVisualizerCallback = (data: AudioAnalysisData) => void;
|
|
|
10
10
|
|
|
11
11
|
type PersonaWebRTCMessageCallback = (data: MessageEvent) => void;
|
|
12
12
|
|
|
13
|
+
type PersonaWebRTCErrorCallback = (error: string) => void;
|
|
14
|
+
|
|
13
15
|
type PersonaWebRTCConfig = PersonaProtocolBaseConfig & {
|
|
14
16
|
webrtcUrl: string;
|
|
15
17
|
iceServers?: RTCIceServer[];
|
|
@@ -31,6 +33,8 @@ class PersonaWebRTCClient {
|
|
|
31
33
|
private isConnected: boolean = false;
|
|
32
34
|
private visualizerCallbacks: AudioVisualizerCallback[] = [];
|
|
33
35
|
private messageCallbacks: PersonaWebRTCMessageCallback[] = [];
|
|
36
|
+
private errorCallbacks: PersonaWebRTCErrorCallback[] = [];
|
|
37
|
+
private queuedMessages: PersonaPacket[] = [];
|
|
34
38
|
|
|
35
39
|
constructor(config: PersonaWebRTCConfig) {
|
|
36
40
|
this.config = config;
|
|
@@ -100,6 +104,15 @@ class PersonaWebRTCClient {
|
|
|
100
104
|
callback(msg);
|
|
101
105
|
});
|
|
102
106
|
};
|
|
107
|
+
channel.onopen = () => {
|
|
108
|
+
while (this.queuedMessages.length > 0) {
|
|
109
|
+
const packet = this.queuedMessages.shift();
|
|
110
|
+
if (packet) {
|
|
111
|
+
channel.send(JSON.stringify(packet));
|
|
112
|
+
this.config.logger?.info('Sent queued message:', packet);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
103
116
|
};
|
|
104
117
|
|
|
105
118
|
const url = this.config.webrtcUrl || 'wss://persona.applica.guru/api/webrtc';
|
|
@@ -144,7 +157,12 @@ class PersonaWebRTCClient {
|
|
|
144
157
|
}
|
|
145
158
|
};
|
|
146
159
|
|
|
147
|
-
this.ws.onclose = () => {
|
|
160
|
+
this.ws.onclose = (event: CloseEvent) => {
|
|
161
|
+
if (event.code !== 1000) {
|
|
162
|
+
this.errorCallbacks.forEach((callback) => {
|
|
163
|
+
callback('Oops! The connection to the server was lost. Please try again later.');
|
|
164
|
+
});
|
|
165
|
+
}
|
|
148
166
|
this._stopAnalyzers();
|
|
149
167
|
};
|
|
150
168
|
}
|
|
@@ -176,10 +194,23 @@ class PersonaWebRTCClient {
|
|
|
176
194
|
this.messageCallbacks.push(callback);
|
|
177
195
|
}
|
|
178
196
|
|
|
197
|
+
public addErrorCallback(callback: PersonaWebRTCErrorCallback): void {
|
|
198
|
+
this.errorCallbacks.push(callback);
|
|
199
|
+
}
|
|
200
|
+
|
|
179
201
|
public createDataChannel(label = 'messages'): void {
|
|
180
202
|
if (!this.pc) return;
|
|
181
203
|
this.dataChannel = this.pc.createDataChannel(label);
|
|
182
|
-
this.dataChannel.onopen = () =>
|
|
204
|
+
this.dataChannel.onopen = () => {
|
|
205
|
+
this.config.logger?.info('Data channel opened');
|
|
206
|
+
while (this.queuedMessages.length > 0) {
|
|
207
|
+
const packet = this.queuedMessages.shift();
|
|
208
|
+
if (packet) {
|
|
209
|
+
this.dataChannel!.send(JSON.stringify(packet));
|
|
210
|
+
this.config.logger?.info('Sent queued message:', packet);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
183
214
|
this.dataChannel.onmessage = (msg: MessageEvent) => {
|
|
184
215
|
this.messageCallbacks.forEach((callback) => {
|
|
185
216
|
callback(msg);
|
|
@@ -188,8 +219,8 @@ class PersonaWebRTCClient {
|
|
|
188
219
|
}
|
|
189
220
|
|
|
190
221
|
public sendPacket(packet: PersonaPacket): void {
|
|
191
|
-
if (!this.dataChannel) {
|
|
192
|
-
this.
|
|
222
|
+
if (!this.dataChannel || this.dataChannel.readyState !== 'open') {
|
|
223
|
+
this.queuedMessages.push(packet);
|
|
193
224
|
return;
|
|
194
225
|
}
|
|
195
226
|
|
|
@@ -277,6 +308,17 @@ class PersonaWebRTCProtocol extends PersonaProtocolBase {
|
|
|
277
308
|
const data = JSON.parse(msg.data) as PersonaPacket;
|
|
278
309
|
this.notifyPacket(data);
|
|
279
310
|
});
|
|
311
|
+
this.webRTCClient.addErrorCallback((error: string) => {
|
|
312
|
+
this.config.logger?.error('WebRTC error:', error);
|
|
313
|
+
this.notifyPacket({
|
|
314
|
+
type: 'message',
|
|
315
|
+
payload: {
|
|
316
|
+
type: 'text',
|
|
317
|
+
role: 'assistant',
|
|
318
|
+
text: error,
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
});
|
|
280
322
|
}
|
|
281
323
|
|
|
282
324
|
public getName(): string {
|
|
@@ -59,18 +59,26 @@ class PersonaWebSocketProtocol extends PersonaProtocolBase {
|
|
|
59
59
|
const data = JSON.parse(event.data) as PersonaPacket;
|
|
60
60
|
this.notifyPacket(data);
|
|
61
61
|
});
|
|
62
|
-
this.webSocket.addEventListener('close', () => {
|
|
62
|
+
this.webSocket.addEventListener('close', (event: CloseEvent) => {
|
|
63
63
|
this.setStatus('disconnected');
|
|
64
64
|
this.webSocket = null;
|
|
65
|
-
|
|
65
|
+
if (event.code !== 1000) {
|
|
66
|
+
this.notifyPacket({
|
|
67
|
+
type: 'message',
|
|
68
|
+
payload: {
|
|
69
|
+
role: 'assistant',
|
|
70
|
+
type: 'text',
|
|
71
|
+
text: 'Oops! The connection to the server was lost. Please try again later.',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
this.config.logger?.warn('WebSocket connection closed');
|
|
75
|
+
}
|
|
66
76
|
});
|
|
67
77
|
|
|
68
|
-
this.webSocket.addEventListener('error', (
|
|
78
|
+
this.webSocket.addEventListener('error', () => {
|
|
69
79
|
this.setStatus('disconnected');
|
|
70
80
|
this.webSocket = null;
|
|
71
|
-
this.config.logger?.error('WebSocket error'
|
|
72
|
-
|
|
73
|
-
// TODO: Implement reconnection logic
|
|
81
|
+
this.config.logger?.error('WebSocket connection error');
|
|
74
82
|
});
|
|
75
83
|
|
|
76
84
|
return Promise.resolve(sid);
|
package/src/runtime.tsx
CHANGED
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
} from './types';
|
|
21
21
|
import { parseMessages, convertMessage } from './messages';
|
|
22
22
|
import {
|
|
23
|
-
PersonaPersistableTransaction,
|
|
24
23
|
PersonaRESTProtocol,
|
|
25
24
|
PersonaRESTProtocolConfig,
|
|
26
25
|
PersonaTransactionProtocol,
|
|
@@ -137,9 +136,7 @@ function PersonaRuntimeProviderInner({
|
|
|
137
136
|
apiUrl: `${baseEndpointProtocol}://${baseEndpoint}`,
|
|
138
137
|
apiKey: config.apiKey,
|
|
139
138
|
agentId: config.agentId,
|
|
140
|
-
|
|
141
|
-
await transaction.invoke(config.tools!);
|
|
142
|
-
},
|
|
139
|
+
tools: config.tools, // Pass raw tools
|
|
143
140
|
logger,
|
|
144
141
|
}),
|
|
145
142
|
);
|
|
@@ -163,14 +160,27 @@ function PersonaRuntimeProviderInner({
|
|
|
163
160
|
protocol.addStatusChangeListener((status: ProtocolStatus) => {
|
|
164
161
|
logger?.debug(`${protocol.getName()} has notified new status: ${status}`);
|
|
165
162
|
protocolsStatus.set(protocol.getName(), status);
|
|
166
|
-
if (status === 'connected'
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
163
|
+
if (status === 'connected') {
|
|
164
|
+
if (config.context) {
|
|
165
|
+
protocol.sendPacket({
|
|
166
|
+
type: 'command',
|
|
167
|
+
payload: {
|
|
168
|
+
command: 'set_initial_context',
|
|
169
|
+
arguments: config.context,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (config.tools && Array.isArray(config.tools)) {
|
|
174
|
+
protocol.sendPacket({
|
|
175
|
+
type: 'command',
|
|
176
|
+
payload: {
|
|
177
|
+
command: 'set_local_tools',
|
|
178
|
+
arguments: {
|
|
179
|
+
tools: config.tools.map((tool) => tool.schema),
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}
|
|
174
184
|
}
|
|
175
185
|
setProtocolsStatus(new Map(protocolsStatus));
|
|
176
186
|
});
|