This repository was archived by the owner on Sep 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (95 loc) · 2.8 KB
/
script.js
File metadata and controls
112 lines (95 loc) · 2.8 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
// WebView
(function () {
var eventHandlers = {};
function postEvent(eventType, callback, eventData) {
if (!callback) {
callback = function () { };
}
if (eventData === undefined) {
eventData = '';
}
if (window.BluerageWebViewProxy !== undefined) {
BluerageWebViewProxy.postEvent(eventType, JSON.stringify(eventData));
} else {
callback({ notAvailable: true });
}
};
function receiveEvent(eventType, eventData) {
// Handle both string and object data
let parsedData = eventData;
if (typeof eventData === 'string') {
try {
parsedData = JSON.parse(eventData);
} catch (e) {
console.error('Failed to parse event data:', e);
// If parsing fails, create an object with the raw string
parsedData = { response: eventData };
}
}
callEventCallbacks(eventType, function (callback) {
callback(eventType, parsedData);
});
}
function callEventCallbacks(eventType, func) {
var curEventHandlers = eventHandlers[eventType];
if (curEventHandlers === undefined ||
!curEventHandlers.length) {
return;
}
for (var i = 0; i < curEventHandlers.length; i++) {
try {
func(curEventHandlers[i]);
} catch (e) {
console.error('Error in callback:', e);
}
}
}
function onEvent(eventType, callback) {
if (eventHandlers[eventType] === undefined) {
eventHandlers[eventType] = [];
}
var index = eventHandlers[eventType].indexOf(callback);
if (index === -1) {
eventHandlers[eventType].push(callback);
}
};
if (!window.Bluerage) {
window.Bluerage = {};
}
window.Bluerage.WebView = {
onEvent: onEvent,
postEvent: postEvent,
receiveEvent: receiveEvent,
callEventCallbacks: callEventCallbacks
};
})();
// WebApp
(function () {
var WebView = window.Bluerage.WebView;
var WebApp = {};
function receiveWebViewEvent(eventType) {
var args = Array.prototype.slice.call(arguments);
eventType = args.shift();
WebView.callEventCallbacks('webview:' + eventType, function (callback) {
callback.apply(WebApp, args);
});
}
function onChatCompletionsResponse(eventType, eventData) {
console.log('Received chat completion response:', eventData);
receiveWebViewEvent('chatCompletionResponse', {
error: eventData.error || null,
response: eventData.response || ''
});
}
if (!window.Bluerage) {
window.Bluerage = {};
}
WebApp.chatCompletions = function (params, callback) {
if (!params || !params.role || !params.message) {
throw Error('ChatCompletionsParamInvalid');
}
WebView.postEvent('chat_completions', false, params);
};
window.Bluerage.WebApp = WebApp;
WebView.onEvent('chat_completions_response', onChatCompletionsResponse);
})();