@jfvilas/kwirth-plugin-chapete 0.0.0-stage → 0.1.1
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 +51 -2
- package/back.js +315 -0
- package/front.js +446 -0
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Chapete — chat with an LLM from Kwirth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Chapete** is a channel plugin for [Kwirth](https://kwirthmagnify.dev) that gives you a plain chat with a
|
|
4
|
+
large language model: a text box, the conversation and the answers, like ChatGPT or Claude, right inside
|
|
5
|
+
Kwirth.
|
|
6
|
+
|
|
7
|
+
It does **not** bring its own AI configuration. It uses the LLMs your Kwirth administrator already set up
|
|
8
|
+
in Kwirth's AI settings, so:
|
|
9
|
+
|
|
10
|
+
- the API keys stay in the Kwirth back end and never reach the browser;
|
|
11
|
+
- every question goes through Kwirth's AI usage limits, like any other AI call in Kwirth.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Pick the LLM and the temperature when you start the channel.
|
|
16
|
+
- Multi-turn conversation: the model keeps the context of the whole chat.
|
|
17
|
+
- Answers rendered as Markdown (code blocks, tables, lists), with a button to copy them.
|
|
18
|
+
- A *Thinking...* indicator while the model works.
|
|
19
|
+
- **Enter** sends, **Shift+Enter** adds a new line.
|
|
20
|
+
- One **system prompt** for the whole channel, editable from the tab (⚙ button).
|
|
21
|
+
- Errors and usage limits are shown inside the conversation.
|
|
22
|
+
|
|
23
|
+
What it does **not** do, on purpose: it has no tools and does not read your cluster. For a chat that
|
|
24
|
+
knows your Kubernetes, look at Kwirth's other AI channels.
|
|
25
|
+
|
|
26
|
+
The conversation lives only in your browser tab: closing the tab, or stopping and starting the channel,
|
|
27
|
+
starts a new one.
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
- Kwirth with at least one **AI provider** and one **LLM** configured (Kwirth's AI settings).
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
1. In Kwirth, add the **jfvilas marketplace** if it is not there yet:
|
|
36
|
+
`https://raw.githubusercontent.com/jfvilas/kwirth/master/manifest.json`
|
|
37
|
+
2. Open the **plugin manager** and install **Chapete** from that marketplace.
|
|
38
|
+
You can also install it from its npm package, `@jfvilas/kwirth-plugin-chapete`.
|
|
39
|
+
3. Open a new tab, choose the **Chapete** channel and, in its setup, pick an LLM and a temperature.
|
|
40
|
+
4. Start the channel and type your question.
|
|
41
|
+
|
|
42
|
+
## Instance setup
|
|
43
|
+
|
|
44
|
+
| field | meaning |
|
|
45
|
+
|---|---|
|
|
46
|
+
| **LLM** | one of the LLMs configured in Kwirth. Fixed for the whole session of the channel |
|
|
47
|
+
| **Temperature** | 0 to 2. It starts at the LLM's own temperature; lower is more precise, higher is more creative |
|
|
48
|
+
|
|
49
|
+
## Links
|
|
50
|
+
|
|
51
|
+
- Kwirth: https://kwirthmagnify.dev — source at https://github.com/kwirthmagnify/kwirth
|
|
52
|
+
- Chapete source code: https://github.com/jfvilas/kwirth (folder `plugins/chapete`)
|
package/back.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// kwirth-back-globals:@kwirthmagnify/kwirth-common
|
|
34
|
+
var require_kwirth_common = __commonJS({
|
|
35
|
+
"kwirth-back-globals:@kwirthmagnify/kwirth-common"(exports2, module2) {
|
|
36
|
+
module2.exports = global.__kwirth_back__.kwirthCommon;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// kwirth-back-globals:@kwirthmagnify/kwirth-common-ai
|
|
41
|
+
var require_kwirth_common_ai = __commonJS({
|
|
42
|
+
"kwirth-back-globals:@kwirthmagnify/kwirth-common-ai"(exports2, module2) {
|
|
43
|
+
module2.exports = global.__kwirth_back__.kwirthCommonAi;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// kwirth-back-globals:@kwirthmagnify/kwirth-common-ai/back
|
|
48
|
+
var require_back = __commonJS({
|
|
49
|
+
"kwirth-back-globals:@kwirthmagnify/kwirth-common-ai/back"(exports2, module2) {
|
|
50
|
+
module2.exports = global.__kwirth_back__.kwirthCommonAiBack;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// src/back/index.ts
|
|
55
|
+
var index_exports = {};
|
|
56
|
+
__export(index_exports, {
|
|
57
|
+
ChapeteChannel: () => ChapeteChannel
|
|
58
|
+
});
|
|
59
|
+
module.exports = __toCommonJS(index_exports);
|
|
60
|
+
var import_kwirth_common = __toESM(require_kwirth_common(), 1);
|
|
61
|
+
var import_kwirth_common_ai = __toESM(require_kwirth_common_ai(), 1);
|
|
62
|
+
|
|
63
|
+
// src/common/ChapeteTypes.ts
|
|
64
|
+
var DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant. Answer in the same language the user writes in, and use Markdown when it makes the answer clearer.";
|
|
65
|
+
|
|
66
|
+
// src/back/ChapeteChat.ts
|
|
67
|
+
var import_back = __toESM(require_back(), 1);
|
|
68
|
+
var defaultChatDeps = {
|
|
69
|
+
buildModel: import_back.buildModel,
|
|
70
|
+
generateText: (options) => (0, import_back.generateText)(options)
|
|
71
|
+
};
|
|
72
|
+
var invalidConversation = (messages) => {
|
|
73
|
+
if (!Array.isArray(messages) || messages.length === 0) return "The conversation is empty";
|
|
74
|
+
for (const message of messages) {
|
|
75
|
+
if (!message || typeof message.content !== "string") return "A message of the conversation has no text";
|
|
76
|
+
if (message.role !== "user" /* USER */ && message.role !== "assistant" /* ASSISTANT */) return `Unknown role '${String(message?.role)}'`;
|
|
77
|
+
}
|
|
78
|
+
if (messages[messages.length - 1].role !== "user" /* USER */) return "The last message of the conversation must be the user's";
|
|
79
|
+
return void 0;
|
|
80
|
+
};
|
|
81
|
+
var toModelMessage = (message) => message.role === "user" /* USER */ ? { role: "user", content: message.content } : { role: "assistant", content: message.content };
|
|
82
|
+
var runTurn = async (turn, deps = defaultChatDeps) => {
|
|
83
|
+
const started = Date.now();
|
|
84
|
+
const id = turn.request?.id ?? "";
|
|
85
|
+
const fail = (error, usageLimit = false) => ({
|
|
86
|
+
id,
|
|
87
|
+
error,
|
|
88
|
+
...usageLimit ? { usageLimit: true } : {},
|
|
89
|
+
elapsed: Date.now() - started
|
|
90
|
+
});
|
|
91
|
+
const invalid = invalidConversation(turn.request?.messages);
|
|
92
|
+
if (invalid) return fail(invalid);
|
|
93
|
+
if (!turn.config?.llmId) return fail("No LLM selected for this channel. Stop it and pick one in its setup");
|
|
94
|
+
const llm = turn.llms.find((l) => l.id === turn.config.llmId);
|
|
95
|
+
if (!llm) return fail(`LLM '${turn.config.llmId}' is no longer configured in Kwirth. Stop the channel and pick another one in its setup`);
|
|
96
|
+
const model = deps.buildModel(llm, turn.providers);
|
|
97
|
+
if (!model) return fail(`LLM '${llm.id}' could not be built: check its provider '${llm.provider}' and its key in Kwirth's AI settings`);
|
|
98
|
+
try {
|
|
99
|
+
const result = await deps.generateText({
|
|
100
|
+
model,
|
|
101
|
+
system: turn.system,
|
|
102
|
+
messages: turn.request.messages.map(toModelMessage),
|
|
103
|
+
temperature: turn.config.temperature
|
|
104
|
+
});
|
|
105
|
+
return { id, text: result.text, elapsed: Date.now() - started };
|
|
106
|
+
} catch (err) {
|
|
107
|
+
if (err instanceof import_back.UsageLimitError) return fail(err.message, true);
|
|
108
|
+
return fail(err instanceof Error ? err.message : String(err));
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/back/index.ts
|
|
113
|
+
var STORAGE_KEY_SYSTEM = "chapete-system";
|
|
114
|
+
var ChapeteChannel = class {
|
|
115
|
+
constructor(clusterInfo, backChannelObject) {
|
|
116
|
+
this.channelId = "chapete";
|
|
117
|
+
// storage: the system prompt lives in the channel's, and the LLMs are read from the common one
|
|
118
|
+
this.requirements = { storage: true, providers: [] };
|
|
119
|
+
this.webSockets = [];
|
|
120
|
+
this.getChannelData = () => ({
|
|
121
|
+
id: "chapete",
|
|
122
|
+
routable: false,
|
|
123
|
+
pauseable: false,
|
|
124
|
+
// nothing flows on its own: it only answers when asked
|
|
125
|
+
modifiable: false,
|
|
126
|
+
// the LLM and the temperature are fixed for the session
|
|
127
|
+
reconnectable: true,
|
|
128
|
+
metrics: false,
|
|
129
|
+
sources: [import_kwirth_common.EClusterType.KUBERNETES],
|
|
130
|
+
endpoints: [],
|
|
131
|
+
websocket: false,
|
|
132
|
+
cluster: true,
|
|
133
|
+
// a chat does not hang from a pod
|
|
134
|
+
resourced: false
|
|
135
|
+
});
|
|
136
|
+
this.getChannelScopeLevel = (scope) => ["", "none", "cluster"].indexOf(scope);
|
|
137
|
+
this.startChannel = async () => {
|
|
138
|
+
};
|
|
139
|
+
this.processProviderEvent = (_providerId, _obj) => {
|
|
140
|
+
};
|
|
141
|
+
this.endpointRequest = async (_endpoint, _req, _res) => {
|
|
142
|
+
};
|
|
143
|
+
this.websocketRequest = async (_ws) => {
|
|
144
|
+
};
|
|
145
|
+
// ---- registration: cluster channels arrive here through addObject('*all') ----
|
|
146
|
+
this.addObject = async (webSocket, instanceConfig, _ns, _pod, _ctr) => {
|
|
147
|
+
let socket = this.webSockets.find((s) => s.ws === webSocket);
|
|
148
|
+
if (!socket) {
|
|
149
|
+
const len = this.webSockets.push({ ws: webSocket, lastRefresh: Date.now(), instances: [] });
|
|
150
|
+
socket = this.webSockets[len - 1];
|
|
151
|
+
}
|
|
152
|
+
if (socket.instances.find((i) => i.instanceId === instanceConfig.instance)) return true;
|
|
153
|
+
const instance = {
|
|
154
|
+
instanceId: instanceConfig.instance,
|
|
155
|
+
accessKey: (0, import_kwirth_common.accessKeyDeserialize)(instanceConfig.accessKey),
|
|
156
|
+
config: instanceConfig.data
|
|
157
|
+
};
|
|
158
|
+
socket.instances.push(instance);
|
|
159
|
+
await this.sendSystem(socket, instance);
|
|
160
|
+
return true;
|
|
161
|
+
};
|
|
162
|
+
this.deleteObject = async (webSocket, instanceConfig, _ns, _pod, _ctr) => {
|
|
163
|
+
this.removeInstance(webSocket, instanceConfig.instance);
|
|
164
|
+
return true;
|
|
165
|
+
};
|
|
166
|
+
this.pauseContinueInstance = (_webSocket, _instanceConfig, _action) => {
|
|
167
|
+
};
|
|
168
|
+
this.modifyInstance = (_ws, _cfg) => {
|
|
169
|
+
};
|
|
170
|
+
this.stopInstance = (webSocket, instanceConfig) => {
|
|
171
|
+
if (this.getInstance(webSocket, instanceConfig.instance)) {
|
|
172
|
+
this.removeInstance(webSocket, instanceConfig.instance);
|
|
173
|
+
this.sendSignal(webSocket, import_kwirth_common.EInstanceMessageAction.STOP, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.INFO, instanceConfig.instance, "Chapete stopped");
|
|
174
|
+
} else {
|
|
175
|
+
this.sendSignal(webSocket, import_kwirth_common.EInstanceMessageAction.STOP, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instanceConfig.instance, "Chapete instance not found");
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
this.removeInstance = (webSocket, instanceId) => {
|
|
179
|
+
const socket = this.webSockets.find((s) => s.ws === webSocket);
|
|
180
|
+
if (socket) socket.instances = socket.instances.filter((i) => i.instanceId !== instanceId);
|
|
181
|
+
};
|
|
182
|
+
this.processCommand = async (webSocket, instanceMessage) => {
|
|
183
|
+
if (instanceMessage.flow === import_kwirth_common.EInstanceMessageFlow.IMMEDIATE) return false;
|
|
184
|
+
if (instanceMessage.action !== import_kwirth_common.EInstanceMessageAction.COMMAND) return false;
|
|
185
|
+
const socket = this.webSockets.find((s) => s.ws === webSocket);
|
|
186
|
+
const instance = this.getInstance(webSocket, instanceMessage.instance);
|
|
187
|
+
if (!socket || !instance) {
|
|
188
|
+
this.sendSignal(webSocket, instanceMessage.action, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instanceMessage.instance, "Chapete instance not found");
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
const msg = instanceMessage;
|
|
192
|
+
switch (msg.command) {
|
|
193
|
+
case "ask" /* ASK */:
|
|
194
|
+
await this.ask(socket, instance, msg.ask);
|
|
195
|
+
return true;
|
|
196
|
+
case "getsystem" /* GETSYSTEM */:
|
|
197
|
+
await this.sendSystem(socket, instance);
|
|
198
|
+
return true;
|
|
199
|
+
case "setsystem" /* SETSYSTEM */:
|
|
200
|
+
await this.setSystem(socket, instance, msg.system);
|
|
201
|
+
return true;
|
|
202
|
+
default:
|
|
203
|
+
this.sendSignal(webSocket, instanceMessage.action, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instanceMessage.instance, `Unknown command '${String(msg.command)}'`);
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
this.containsAsset = (_ws, _ns, _pod, _ctr) => false;
|
|
208
|
+
this.containsInstance = (instanceId) => this.webSockets.some((s) => s.instances.some((i) => i.instanceId === instanceId));
|
|
209
|
+
// What this channel has running, for the Status channel's Plugins tab. A connection counts while it
|
|
210
|
+
// carries an instance: one left with none is on its way out.
|
|
211
|
+
this.getInstances = () => {
|
|
212
|
+
const carrying = this.webSockets.filter((s) => s.instances.length > 0);
|
|
213
|
+
return { instances: carrying.reduce((n, s) => n + s.instances.length, 0), connections: carrying.length };
|
|
214
|
+
};
|
|
215
|
+
this.containsConnection = (webSocket) => Boolean(this.webSockets.find((s) => s.ws === webSocket));
|
|
216
|
+
this.removeConnection = (webSocket) => {
|
|
217
|
+
this.webSockets = this.webSockets.filter((s) => s.ws !== webSocket);
|
|
218
|
+
};
|
|
219
|
+
this.refreshConnection = (webSocket) => {
|
|
220
|
+
const socket = this.webSockets.find((s) => s.ws === webSocket);
|
|
221
|
+
if (socket) {
|
|
222
|
+
socket.lastRefresh = Date.now();
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
};
|
|
227
|
+
this.updateConnection = (newWebSocket, instanceId) => {
|
|
228
|
+
for (const entry of this.webSockets) {
|
|
229
|
+
if (entry.instances.some((i) => i.instanceId === instanceId)) {
|
|
230
|
+
entry.ws = newWebSocket;
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
};
|
|
236
|
+
// ---- chat ------------------------------------------------------------------
|
|
237
|
+
/*
|
|
238
|
+
The LLMs and the providers are read on EVERY turn, not once on start: they are the core's, and an
|
|
239
|
+
administrator who fixes a key in the AI settings expects the next question to use it.
|
|
240
|
+
*/
|
|
241
|
+
this.ask = async (socket, instance, request) => {
|
|
242
|
+
if (!request) {
|
|
243
|
+
this.sendSignal(socket.ws, import_kwirth_common.EInstanceMessageAction.COMMAND, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instance.instanceId, "Ask command received with no payload");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
let answer;
|
|
247
|
+
try {
|
|
248
|
+
const providers = await this.backChannelObject.readStorageCommon(import_kwirth_common_ai.STORAGE_KEY_PROVIDERS, true) ?? [];
|
|
249
|
+
const llms = await this.backChannelObject.readStorageCommon(import_kwirth_common_ai.STORAGE_KEY_LLMS, false) ?? [];
|
|
250
|
+
const system = await this.readSystem();
|
|
251
|
+
answer = await runTurn({ config: instance.config, llms, providers, system, request });
|
|
252
|
+
} catch (err) {
|
|
253
|
+
answer = { id: request.id, error: `Could not read Kwirth's AI configuration: ${err instanceof Error ? err.message : String(err)}`, elapsed: 0 };
|
|
254
|
+
}
|
|
255
|
+
if (answer.error) this.backChannelObject.logWarning?.(`Chapete could not answer with LLM '${instance.config?.llmId}': ${answer.error}`);
|
|
256
|
+
this.send(socket, instance, { payloadType: "answer" /* ANSWER */, answer }, import_kwirth_common.EInstanceMessageAction.COMMAND, import_kwirth_common.EInstanceMessageFlow.RESPONSE);
|
|
257
|
+
};
|
|
258
|
+
this.readSystem = async () => {
|
|
259
|
+
const stored = await this.backChannelObject.readStorage(STORAGE_KEY_SYSTEM, false);
|
|
260
|
+
return typeof stored === "string" && stored.trim() !== "" ? stored : DEFAULT_SYSTEM_PROMPT;
|
|
261
|
+
};
|
|
262
|
+
this.sendSystem = async (socket, instance) => {
|
|
263
|
+
let system = DEFAULT_SYSTEM_PROMPT;
|
|
264
|
+
try {
|
|
265
|
+
system = await this.readSystem();
|
|
266
|
+
} catch (err) {
|
|
267
|
+
this.backChannelObject.logWarning?.(`Chapete could not read its system prompt, using the default one: ${String(err)}`);
|
|
268
|
+
}
|
|
269
|
+
this.send(socket, instance, { payloadType: "system" /* SYSTEM */, system }, import_kwirth_common.EInstanceMessageAction.NONE, import_kwirth_common.EInstanceMessageFlow.UNSOLICITED);
|
|
270
|
+
};
|
|
271
|
+
/*
|
|
272
|
+
An empty text is stored as such and means 'the default one': there is no separate reset, so clearing
|
|
273
|
+
the field is how the default comes back.
|
|
274
|
+
*/
|
|
275
|
+
this.setSystem = async (socket, instance, system) => {
|
|
276
|
+
if (typeof system !== "string") {
|
|
277
|
+
this.sendSignal(socket.ws, import_kwirth_common.EInstanceMessageAction.COMMAND, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instance.instanceId, "Set system command received with no text");
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
await this.backChannelObject.writeStorage(STORAGE_KEY_SYSTEM, false, system);
|
|
282
|
+
} catch (err) {
|
|
283
|
+
this.sendSignal(socket.ws, import_kwirth_common.EInstanceMessageAction.COMMAND, import_kwirth_common.EInstanceMessageFlow.RESPONSE, import_kwirth_common.ESignalMessageLevel.ERROR, instance.instanceId, `Could not store the system prompt: ${String(err)}`);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
await this.sendSystem(socket, instance);
|
|
287
|
+
};
|
|
288
|
+
this.send = (socket, instance, payload, action, flow) => {
|
|
289
|
+
const msg = {
|
|
290
|
+
msgtype: "chapetemessageresponse",
|
|
291
|
+
channel: this.channelId,
|
|
292
|
+
action,
|
|
293
|
+
flow,
|
|
294
|
+
type: import_kwirth_common.EInstanceMessageType.DATA,
|
|
295
|
+
instance: instance.instanceId,
|
|
296
|
+
...payload
|
|
297
|
+
};
|
|
298
|
+
socket.ws.send(JSON.stringify(msg));
|
|
299
|
+
};
|
|
300
|
+
this.sendSignal = (ws, action, flow, level, instanceId, text) => {
|
|
301
|
+
const msg = { action, flow, channel: "chapete", instance: instanceId, type: import_kwirth_common.EInstanceMessageType.SIGNAL, text, level };
|
|
302
|
+
ws.send(JSON.stringify(msg));
|
|
303
|
+
};
|
|
304
|
+
this.getInstance = (webSocket, instanceId) => {
|
|
305
|
+
const socket = this.webSockets.find((s) => s.ws === webSocket);
|
|
306
|
+
return socket?.instances.find((i) => i.instanceId === instanceId);
|
|
307
|
+
};
|
|
308
|
+
this.clusterInfo = clusterInfo;
|
|
309
|
+
this.backChannelObject = backChannelObject;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
313
|
+
0 && (module.exports = {
|
|
314
|
+
ChapeteChannel
|
|
315
|
+
});
|
package/front.js
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
|
|
29
|
+
// kwirth-globals:@kwirthmagnify/kwirth-common-front
|
|
30
|
+
var require_kwirth_common_front = __commonJS({
|
|
31
|
+
"kwirth-globals:@kwirthmagnify/kwirth-common-front"(exports, module) {
|
|
32
|
+
var _m = window.__kwirth__.kwirthCommonFront;
|
|
33
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
34
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
35
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// kwirth-globals:@kwirthmagnify/kwirth-common
|
|
40
|
+
var require_kwirth_common = __commonJS({
|
|
41
|
+
"kwirth-globals:@kwirthmagnify/kwirth-common"(exports, module) {
|
|
42
|
+
var _m = window.__kwirth__.kwirthCommon;
|
|
43
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
44
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
45
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// kwirth-globals:react
|
|
50
|
+
var require_react = __commonJS({
|
|
51
|
+
"kwirth-globals:react"(exports, module) {
|
|
52
|
+
var _m = window.__kwirth__.React;
|
|
53
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
54
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
55
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// kwirth-globals:@mui/material
|
|
60
|
+
var require_material = __commonJS({
|
|
61
|
+
"kwirth-globals:@mui/material"(exports, module) {
|
|
62
|
+
var _m = window.__kwirth__.MUI.material;
|
|
63
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
64
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
65
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// kwirth-globals:@kwirthmagnify/kwirth-common-front/icons
|
|
70
|
+
var require_icons = __commonJS({
|
|
71
|
+
"kwirth-globals:@kwirthmagnify/kwirth-common-front/icons"(exports, module) {
|
|
72
|
+
var _m = window.__kwirth__.MUI.icons;
|
|
73
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
74
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
75
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// kwirth-globals:@kwirthmagnify/kwirth-common-ai/front
|
|
80
|
+
var require_front = __commonJS({
|
|
81
|
+
"kwirth-globals:@kwirthmagnify/kwirth-common-ai/front"(exports, module) {
|
|
82
|
+
var _m = window.__kwirth__.kwirthCommonAiFront;
|
|
83
|
+
var _d = _m != null && "default" in Object(_m) ? _m.default : _m;
|
|
84
|
+
if (typeof _d !== "function" && _d != null && typeof _d.default !== "undefined") _d = _d.default;
|
|
85
|
+
module.exports = Object.assign({}, typeof _m === "object" && _m !== null ? _m : {}, { default: _d, __esModule: true });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// src/front/ChapeteChannel.tsx
|
|
90
|
+
var import_kwirth_common_front2 = __toESM(require_kwirth_common_front(), 1);
|
|
91
|
+
var import_kwirth_common2 = __toESM(require_kwirth_common(), 1);
|
|
92
|
+
|
|
93
|
+
// src/front/ChapeteSetup.tsx
|
|
94
|
+
var import_react = __toESM(require_react(), 1);
|
|
95
|
+
var import_material = __toESM(require_material(), 1);
|
|
96
|
+
var import_icons = __toESM(require_icons(), 1);
|
|
97
|
+
var import_front = __toESM(require_front(), 1);
|
|
98
|
+
|
|
99
|
+
// src/front/ChapeteConfig.ts
|
|
100
|
+
var ChapeteConfig = class {
|
|
101
|
+
};
|
|
102
|
+
var ChapeteInstanceConfig = class {
|
|
103
|
+
constructor() {
|
|
104
|
+
this.llmId = "";
|
|
105
|
+
this.temperature = 0.7;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/front/ChapeteSetup.tsx
|
|
110
|
+
var ChapeteIcon = /* @__PURE__ */ import_react.default.createElement(import_icons.Forum, null);
|
|
111
|
+
var ChapeteSetup = (props) => {
|
|
112
|
+
const instanceConfig = props.setupConfig?.channelInstanceConfig || new ChapeteInstanceConfig();
|
|
113
|
+
const config = props.setupConfig?.channelConfig || new ChapeteConfig();
|
|
114
|
+
const [llms, setLlms] = (0, import_react.useState)([]);
|
|
115
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
116
|
+
const [loadError, setLoadError] = (0, import_react.useState)("");
|
|
117
|
+
const [llmId, setLlmId] = (0, import_react.useState)(instanceConfig.llmId);
|
|
118
|
+
const [temperature, setTemperature] = (0, import_react.useState)(instanceConfig.temperature);
|
|
119
|
+
const defaultRef = (0, import_react.useRef)(null);
|
|
120
|
+
(0, import_react.useEffect)(() => {
|
|
121
|
+
const load = async () => {
|
|
122
|
+
try {
|
|
123
|
+
const response = await fetch(`${props.channelObject.clusterUrl}/core/aiconfig/llms`, {
|
|
124
|
+
headers: { "Authorization": `Bearer ${props.channelObject.accessString}`, "X-Kwirth-App": "true" }
|
|
125
|
+
});
|
|
126
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
127
|
+
const list = await response.json();
|
|
128
|
+
setLlms(list);
|
|
129
|
+
if (!list.some((l) => l.id === llmId)) {
|
|
130
|
+
const only = list.length === 1 ? list[0] : void 0;
|
|
131
|
+
setLlmId(only?.id ?? "");
|
|
132
|
+
if (only) setTemperature(only.temperature);
|
|
133
|
+
}
|
|
134
|
+
} catch (err) {
|
|
135
|
+
setLoadError(`Could not read the LLMs configured in Kwirth: ${err instanceof Error ? err.message : String(err)}`);
|
|
136
|
+
}
|
|
137
|
+
setLoading(false);
|
|
138
|
+
};
|
|
139
|
+
load();
|
|
140
|
+
}, []);
|
|
141
|
+
const selectLlm = (id) => {
|
|
142
|
+
setLlmId(id);
|
|
143
|
+
const llm = llms.find((l) => l.id === id);
|
|
144
|
+
if (llm) setTemperature(llm.temperature);
|
|
145
|
+
};
|
|
146
|
+
const invalidTemperature = () => !Number.isFinite(temperature) || temperature < 0 || temperature > 2;
|
|
147
|
+
const noLlms = !loading && llms.length === 0;
|
|
148
|
+
const statusText = () => {
|
|
149
|
+
if (loading) return "Reading the LLMs configured in Kwirth...";
|
|
150
|
+
if (loadError) return loadError;
|
|
151
|
+
if (noLlms) return "No LLMs configured. Configure them in Kwirth's AI settings, then open this setup again.";
|
|
152
|
+
return "The model and the temperature are fixed while the channel runs. To change them, stop the channel and start it again: the conversation starts over.";
|
|
153
|
+
};
|
|
154
|
+
const ok = () => {
|
|
155
|
+
const chosen = { llmId, temperature };
|
|
156
|
+
props.onChannelSetupClosed(props.channel, {
|
|
157
|
+
channelId: props.channel.channelId,
|
|
158
|
+
channelConfig: config,
|
|
159
|
+
channelInstanceConfig: chosen
|
|
160
|
+
}, true, defaultRef.current?.checked || false);
|
|
161
|
+
};
|
|
162
|
+
const cancel = () => {
|
|
163
|
+
props.onChannelSetupClosed(props.channel, {
|
|
164
|
+
channelId: props.channel.channelId,
|
|
165
|
+
channelConfig: void 0,
|
|
166
|
+
channelInstanceConfig: void 0
|
|
167
|
+
}, false, false);
|
|
168
|
+
};
|
|
169
|
+
return /* @__PURE__ */ import_react.default.createElement(import_material.Dialog, { open: true, maxWidth: false, sx: { "& .MuiDialog-paper": { width: "34vw", maxWidth: "34vw", height: "38vh", maxHeight: "38vh" } } }, /* @__PURE__ */ import_react.default.createElement(import_material.DialogTitle, null, "Configure Chapete channel"), /* @__PURE__ */ import_react.default.createElement(import_material.DialogContent, { sx: { display: "flex", flexDirection: "column", overflow: "hidden" } }, /* @__PURE__ */ import_react.default.createElement(import_material.Stack, { direction: "column", spacing: 2, sx: { m: 1, flex: 1, minHeight: 0 } }, /* @__PURE__ */ import_react.default.createElement(import_material.Typography, { variant: "body2", color: loadError || noLlms ? "warning.main" : "text.secondary" }, statusText()), /* @__PURE__ */ import_react.default.createElement(import_front.LlmSelector, { llms, value: llmId, onChange: selectLlm, label: "LLM" }), /* @__PURE__ */ import_react.default.createElement(
|
|
170
|
+
import_material.TextField,
|
|
171
|
+
{
|
|
172
|
+
value: temperature,
|
|
173
|
+
onChange: (e) => setTemperature(+e.target.value),
|
|
174
|
+
type: "number",
|
|
175
|
+
variant: "standard",
|
|
176
|
+
label: "Temperature",
|
|
177
|
+
error: invalidTemperature(),
|
|
178
|
+
disabled: llmId === "",
|
|
179
|
+
slotProps: { htmlInput: { step: 0.1, min: 0, max: 2 } },
|
|
180
|
+
helperText: invalidTemperature() ? "From 0 to 2" : "Lower is more precise, higher is more creative",
|
|
181
|
+
fullWidth: true
|
|
182
|
+
}
|
|
183
|
+
))), /* @__PURE__ */ import_react.default.createElement(import_material.DialogActions, null, /* @__PURE__ */ import_react.default.createElement(import_material.FormControlLabel, { control: /* @__PURE__ */ import_react.default.createElement(import_material.Checkbox, { slotProps: { input: { ref: defaultRef } } }), label: "Set as default", sx: { width: "100%", ml: "8px" } }), /* @__PURE__ */ import_react.default.createElement(import_material.Button, { variant: "outlined", onClick: ok, disabled: llmId === "" || invalidTemperature() }, "OK"), /* @__PURE__ */ import_react.default.createElement(import_material.Button, { variant: "outlined", onClick: cancel }, "CANCEL")));
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/front/ChapeteTabContent.tsx
|
|
187
|
+
var import_react3 = __toESM(require_react(), 1);
|
|
188
|
+
var import_material3 = __toESM(require_material(), 1);
|
|
189
|
+
var import_kwirth_common = __toESM(require_kwirth_common(), 1);
|
|
190
|
+
var import_kwirth_common_front = __toESM(require_kwirth_common_front(), 1);
|
|
191
|
+
var import_icons2 = __toESM(require_icons(), 1);
|
|
192
|
+
|
|
193
|
+
// src/common/ChapeteTypes.ts
|
|
194
|
+
var DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant. Answer in the same language the user writes in, and use Markdown when it makes the answer clearer.";
|
|
195
|
+
|
|
196
|
+
// src/front/ChapeteData.ts
|
|
197
|
+
var ChapeteData = class {
|
|
198
|
+
constructor() {
|
|
199
|
+
this.entries = [];
|
|
200
|
+
this.system = "";
|
|
201
|
+
this.signals = [];
|
|
202
|
+
this.draft = "";
|
|
203
|
+
this.started = false;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
var conversationOf = (entries) => entries.filter((e) => !e.error && !e.pendingId).map((e) => ({ role: e.role, content: e.content }));
|
|
207
|
+
var isThinking = (entries) => entries.some((e) => e.pendingId !== void 0);
|
|
208
|
+
var completeAnswer = (entries, answer) => {
|
|
209
|
+
const entry = entries.find((e) => e.pendingId === answer.id);
|
|
210
|
+
if (!entry) return;
|
|
211
|
+
delete entry.pendingId;
|
|
212
|
+
if (answer.error !== void 0) {
|
|
213
|
+
entry.content = answer.error;
|
|
214
|
+
entry.error = true;
|
|
215
|
+
if (answer.usageLimit) entry.usageLimit = true;
|
|
216
|
+
} else {
|
|
217
|
+
entry.content = answer.text ?? "";
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
var abandonPending = (entries) => {
|
|
221
|
+
for (const entry of entries) {
|
|
222
|
+
if (entry.pendingId !== void 0) {
|
|
223
|
+
delete entry.pendingId;
|
|
224
|
+
entry.content = "No answer: the channel was stopped before the model replied.";
|
|
225
|
+
entry.error = true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// src/front/ChapeteSystemDialog.tsx
|
|
231
|
+
var import_react2 = __toESM(require_react(), 1);
|
|
232
|
+
var import_material2 = __toESM(require_material(), 1);
|
|
233
|
+
var ChapeteSystemDialog = (props) => {
|
|
234
|
+
const [system, setSystem] = (0, import_react2.useState)(props.system);
|
|
235
|
+
const save = () => {
|
|
236
|
+
props.onSave(system);
|
|
237
|
+
props.onClose();
|
|
238
|
+
};
|
|
239
|
+
return /* @__PURE__ */ import_react2.default.createElement(import_material2.Dialog, { open: true, maxWidth: false, sx: { "& .MuiDialog-paper": { width: "44vw", maxWidth: "44vw", height: "52vh", maxHeight: "52vh" } } }, /* @__PURE__ */ import_react2.default.createElement(import_material2.DialogTitle, null, "Chapete system prompt"), /* @__PURE__ */ import_react2.default.createElement(import_material2.DialogContent, { sx: { display: "flex", flexDirection: "column", overflow: "hidden" } }, /* @__PURE__ */ import_react2.default.createElement(import_material2.Stack, { direction: "column", spacing: 2, sx: { m: 1, flex: 1, minHeight: 0 } }, /* @__PURE__ */ import_react2.default.createElement(import_material2.Typography, { variant: "body2", color: "text.secondary" }, "What the model is told before every conversation. It is shared by everyone using this channel, and it applies from the next question on. Leave it empty to use the default one."), /* @__PURE__ */ import_react2.default.createElement(
|
|
240
|
+
import_material2.TextField,
|
|
241
|
+
{
|
|
242
|
+
value: system,
|
|
243
|
+
onChange: (e) => setSystem(e.target.value),
|
|
244
|
+
variant: "outlined",
|
|
245
|
+
label: "System prompt",
|
|
246
|
+
placeholder: DEFAULT_SYSTEM_PROMPT,
|
|
247
|
+
multiline: true,
|
|
248
|
+
fullWidth: true,
|
|
249
|
+
sx: { flex: 1, minHeight: 0, "& .MuiInputBase-root": { height: "100%", alignItems: "flex-start", overflow: "auto" } }
|
|
250
|
+
}
|
|
251
|
+
))), /* @__PURE__ */ import_react2.default.createElement(import_material2.DialogActions, null, /* @__PURE__ */ import_react2.default.createElement(import_material2.Button, { variant: "outlined", onClick: save }, "SAVE"), /* @__PURE__ */ import_react2.default.createElement(import_material2.Button, { variant: "outlined", onClick: props.onClose }, "CANCEL")));
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/front/ChapeteTabContent.tsx
|
|
255
|
+
var ChapeteTabContent = (props) => {
|
|
256
|
+
const data = props.channelObject.data;
|
|
257
|
+
const instanceConfig = props.channelObject.instanceConfig;
|
|
258
|
+
const [, forceRender] = (0, import_react3.useState)(0);
|
|
259
|
+
const [systemOpen, setSystemOpen] = (0, import_react3.useState)(false);
|
|
260
|
+
const containerRef = (0, import_react3.useRef)(null);
|
|
261
|
+
const [containerHeight, setContainerHeight] = (0, import_react3.useState)(0);
|
|
262
|
+
const bottomRef = (0, import_react3.useRef)(null);
|
|
263
|
+
const refresh = () => forceRender((n) => n + 1);
|
|
264
|
+
(0, import_react3.useEffect)(() => {
|
|
265
|
+
const observer = new ResizeObserver(() => {
|
|
266
|
+
if (!containerRef.current) return;
|
|
267
|
+
setContainerHeight(window.innerHeight - containerRef.current.getBoundingClientRect().top);
|
|
268
|
+
});
|
|
269
|
+
observer.observe(document.body);
|
|
270
|
+
return () => observer.disconnect();
|
|
271
|
+
}, [containerRef.current]);
|
|
272
|
+
(0, import_react3.useEffect)(() => {
|
|
273
|
+
bottomRef.current?.scrollIntoView({ block: "end" });
|
|
274
|
+
}, [data.entries.length, data.entries[data.entries.length - 1]?.pendingId]);
|
|
275
|
+
const thinking = isThinking(data.entries);
|
|
276
|
+
const sendCommand = (command, extra) => {
|
|
277
|
+
if (!props.channelObject.instanceId) return;
|
|
278
|
+
const msg = {
|
|
279
|
+
msgtype: "chapetemessage",
|
|
280
|
+
channel: "chapete",
|
|
281
|
+
action: import_kwirth_common.EInstanceMessageAction.COMMAND,
|
|
282
|
+
flow: import_kwirth_common.EInstanceMessageFlow.REQUEST,
|
|
283
|
+
type: import_kwirth_common.EInstanceMessageType.DATA,
|
|
284
|
+
accessKey: props.channelObject.accessString,
|
|
285
|
+
instance: props.channelObject.instanceId,
|
|
286
|
+
command,
|
|
287
|
+
...extra
|
|
288
|
+
};
|
|
289
|
+
props.channelObject.webSocket?.send(JSON.stringify(msg));
|
|
290
|
+
};
|
|
291
|
+
const canSend = () => Boolean(props.channelObject.instanceId) && !thinking && data.draft.trim() !== "";
|
|
292
|
+
const send = () => {
|
|
293
|
+
if (!canSend()) return;
|
|
294
|
+
data.entries.push({ role: "user" /* USER */, content: data.draft });
|
|
295
|
+
const ask = {
|
|
296
|
+
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
297
|
+
messages: conversationOf(data.entries)
|
|
298
|
+
};
|
|
299
|
+
data.entries.push({ role: "assistant" /* ASSISTANT */, content: "", pendingId: ask.id });
|
|
300
|
+
data.draft = "";
|
|
301
|
+
sendCommand("ask" /* ASK */, { ask });
|
|
302
|
+
refresh();
|
|
303
|
+
};
|
|
304
|
+
const onKeyDown = (event) => {
|
|
305
|
+
if (event.key === "Enter" && !event.shiftKey) {
|
|
306
|
+
event.preventDefault();
|
|
307
|
+
send();
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const newChat = () => {
|
|
311
|
+
data.entries = [];
|
|
312
|
+
data.signals = [];
|
|
313
|
+
refresh();
|
|
314
|
+
};
|
|
315
|
+
const saveSystem = (system) => sendCommand("setsystem" /* SETSYSTEM */, { system });
|
|
316
|
+
const copy = (text) => {
|
|
317
|
+
navigator.clipboard?.writeText(text);
|
|
318
|
+
};
|
|
319
|
+
const bubble = (entry, index) => {
|
|
320
|
+
const user = entry.role === "user" /* USER */;
|
|
321
|
+
return /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { key: index, direction: "row", justifyContent: user ? "flex-end" : "flex-start", sx: { mb: 1.5 } }, /* @__PURE__ */ import_react3.default.createElement(
|
|
322
|
+
import_material3.Paper,
|
|
323
|
+
{
|
|
324
|
+
variant: "outlined",
|
|
325
|
+
"data-testid": user ? "chapete-user" : "chapete-assistant",
|
|
326
|
+
sx: { maxWidth: "80%", px: 1.5, py: 1, bgcolor: user ? "action.hover" : "background.paper", borderColor: entry.error ? "error.main" : void 0 }
|
|
327
|
+
},
|
|
328
|
+
user && /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "body2", sx: { whiteSpace: "pre-wrap", wordBreak: "break-word" } }, entry.content),
|
|
329
|
+
!user && entry.pendingId !== void 0 && /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { direction: "row", spacing: 1, alignItems: "center" }, /* @__PURE__ */ import_react3.default.createElement(import_material3.CircularProgress, { size: 14 }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "body2", color: "text.secondary", sx: { fontStyle: "italic" } }, "Thinking...")),
|
|
330
|
+
!user && entry.pendingId === void 0 && entry.error && /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { direction: "row", spacing: 1, alignItems: "flex-start" }, /* @__PURE__ */ import_react3.default.createElement(import_icons2.Warning, { fontSize: "small", color: "error" }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "body2", color: "error", sx: { whiteSpace: "pre-wrap", wordBreak: "break-word" } }, entry.usageLimit ? `Usage limit reached. ${entry.content}` : entry.content)),
|
|
331
|
+
!user && entry.pendingId === void 0 && !entry.error && /* @__PURE__ */ import_react3.default.createElement(import_material3.Box, null, /* @__PURE__ */ import_react3.default.createElement(import_kwirth_common_front.MarkdownViewer, { content: entry.content }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { direction: "row", justifyContent: "flex-end" }, /* @__PURE__ */ import_react3.default.createElement(import_material3.Tooltip, { title: "Copy answer" }, /* @__PURE__ */ import_react3.default.createElement(import_material3.IconButton, { size: "small", "aria-label": "Copy answer", onClick: () => copy(entry.content) }, /* @__PURE__ */ import_react3.default.createElement(import_icons2.ContentCopy, { fontSize: "small" })))))
|
|
332
|
+
));
|
|
333
|
+
};
|
|
334
|
+
if (!data.started) {
|
|
335
|
+
return /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { ref: containerRef, alignItems: "center", justifyContent: "center", spacing: 1, sx: { width: "100%", height: `${containerHeight}px`, px: 4, textAlign: "center" } }, /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "h6", color: "text.secondary" }, "Chapete not started"), /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "body2", color: "text.secondary", sx: { maxWidth: 480 } }, "Start the channel (tab settings \u2699 \u2192 Start), pick an LLM and start chatting."));
|
|
336
|
+
}
|
|
337
|
+
return /* @__PURE__ */ import_react3.default.createElement(import_material3.Box, { ref: containerRef, sx: { display: "flex", flexDirection: "column", width: "100%", height: `${containerHeight}px`, p: 1, boxSizing: "border-box" } }, /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { direction: "row", alignItems: "center", spacing: 1, sx: { px: 1, pb: 1 } }, /* @__PURE__ */ import_react3.default.createElement(import_material3.Chip, { label: `LLM: ${instanceConfig?.llmId || "(none)"}`, size: "small", variant: "outlined" }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Chip, { label: `Temperature: ${instanceConfig?.temperature ?? "-"}`, size: "small", variant: "outlined" }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Box, { sx: { flex: 1 } }), /* @__PURE__ */ import_react3.default.createElement(import_material3.Tooltip, { title: "Start a new conversation" }, /* @__PURE__ */ import_react3.default.createElement("span", null, /* @__PURE__ */ import_react3.default.createElement(import_material3.IconButton, { size: "small", "aria-label": "New chat", onClick: newChat, disabled: data.entries.length === 0 || thinking }, /* @__PURE__ */ import_react3.default.createElement(import_icons2.Add, { fontSize: "small" })))), /* @__PURE__ */ import_react3.default.createElement(import_material3.Tooltip, { title: "System prompt of the channel" }, /* @__PURE__ */ import_react3.default.createElement(import_material3.IconButton, { size: "small", "aria-label": "System prompt", onClick: () => setSystemOpen(true) }, /* @__PURE__ */ import_react3.default.createElement(import_icons2.Settings, { fontSize: "small" })))), /* @__PURE__ */ import_react3.default.createElement(import_material3.Box, { sx: { flex: 1, minHeight: 0, overflowY: "auto", px: 1 } }, data.entries.length === 0 && /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { alignItems: "center", justifyContent: "center", sx: { height: "100%" } }, /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { variant: "body2", color: "text.secondary" }, "Ask anything. Enter sends, Shift+Enter adds a new line.")), data.entries.map((entry, index) => bubble(entry, index)), data.signals.map((s, index) => /* @__PURE__ */ import_react3.default.createElement(import_material3.Typography, { key: `s${index}`, variant: "caption", color: "text.secondary", component: "div" }, "*** ", s, " ***")), /* @__PURE__ */ import_react3.default.createElement("div", { ref: bottomRef })), /* @__PURE__ */ import_react3.default.createElement(import_material3.Stack, { direction: "row", spacing: 1, alignItems: "flex-end", sx: { pt: 1 } }, /* @__PURE__ */ import_react3.default.createElement(
|
|
338
|
+
import_material3.TextField,
|
|
339
|
+
{
|
|
340
|
+
value: data.draft,
|
|
341
|
+
onChange: (e) => {
|
|
342
|
+
data.draft = e.target.value;
|
|
343
|
+
refresh();
|
|
344
|
+
},
|
|
345
|
+
onKeyDown,
|
|
346
|
+
placeholder: "Message Chapete...",
|
|
347
|
+
multiline: true,
|
|
348
|
+
maxRows: 8,
|
|
349
|
+
fullWidth: true,
|
|
350
|
+
size: "small",
|
|
351
|
+
disabled: thinking,
|
|
352
|
+
slotProps: { htmlInput: { "aria-label": "Message" } }
|
|
353
|
+
}
|
|
354
|
+
), /* @__PURE__ */ import_react3.default.createElement(import_material3.Button, { variant: "contained", startIcon: /* @__PURE__ */ import_react3.default.createElement(import_icons2.Send, null), onClick: send, disabled: !canSend() }, "SEND")), systemOpen && /* @__PURE__ */ import_react3.default.createElement(ChapeteSystemDialog, { system: data.system, onSave: saveSystem, onClose: () => setSystemOpen(false) }));
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// src/front/ChapeteChannel.tsx
|
|
358
|
+
var ChapeteChannel = class {
|
|
359
|
+
constructor() {
|
|
360
|
+
this.setupVisible = false;
|
|
361
|
+
this.SetupDialog = ChapeteSetup;
|
|
362
|
+
this.TabContent = ChapeteTabContent;
|
|
363
|
+
this.channelId = "chapete";
|
|
364
|
+
this.requirements = {
|
|
365
|
+
accessString: true,
|
|
366
|
+
// every COMMAND travels with its accessKey, and the setup reads the LLMs with it
|
|
367
|
+
clusterUrl: true,
|
|
368
|
+
// the setup asks the core for its LLMs
|
|
369
|
+
clusterInfo: false,
|
|
370
|
+
exit: false,
|
|
371
|
+
frontChannels: false,
|
|
372
|
+
backChannels: false,
|
|
373
|
+
metrics: false,
|
|
374
|
+
notifier: false,
|
|
375
|
+
notifications: false,
|
|
376
|
+
setup: true,
|
|
377
|
+
settings: false,
|
|
378
|
+
palette: false,
|
|
379
|
+
userSettings: false,
|
|
380
|
+
webSocket: true
|
|
381
|
+
// the tab sends its questions through the instance's websocket
|
|
382
|
+
};
|
|
383
|
+
this.getScope = () => import_kwirth_common2.EInstanceConfigScope.NONE;
|
|
384
|
+
this.getChannelIcon = () => ChapeteIcon;
|
|
385
|
+
this.getSetupVisibility = () => this.setupVisible;
|
|
386
|
+
this.setSetupVisibility = (v) => {
|
|
387
|
+
this.setupVisible = v;
|
|
388
|
+
};
|
|
389
|
+
this.processChannelMessage = (channelObject, wsEvent) => {
|
|
390
|
+
const msg = JSON.parse(wsEvent.data);
|
|
391
|
+
const data = channelObject.data;
|
|
392
|
+
switch (msg.type) {
|
|
393
|
+
case import_kwirth_common2.EInstanceMessageType.DATA:
|
|
394
|
+
switch (msg.payloadType) {
|
|
395
|
+
case "system" /* SYSTEM */:
|
|
396
|
+
data.system = msg.system ?? "";
|
|
397
|
+
break;
|
|
398
|
+
case "answer" /* ANSWER */:
|
|
399
|
+
if (msg.answer) completeAnswer(data.entries, msg.answer);
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
return { action: import_kwirth_common_front2.EChannelRefreshAction.REFRESH };
|
|
403
|
+
case import_kwirth_common2.EInstanceMessageType.SIGNAL: {
|
|
404
|
+
const signal = JSON.parse(wsEvent.data);
|
|
405
|
+
if (signal.flow === import_kwirth_common2.EInstanceMessageFlow.RESPONSE && signal.action === import_kwirth_common2.EInstanceMessageAction.START) channelObject.instanceId = signal.instance;
|
|
406
|
+
if (signal.text && signal.level !== void 0 && signal.level !== import_kwirth_common2.ESignalMessageLevel.INFO) data.signals.push(signal.text);
|
|
407
|
+
return { action: import_kwirth_common_front2.EChannelRefreshAction.REFRESH };
|
|
408
|
+
}
|
|
409
|
+
default:
|
|
410
|
+
return { action: import_kwirth_common_front2.EChannelRefreshAction.NONE };
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
this.initChannel = async (channelObject) => {
|
|
414
|
+
channelObject.data = new ChapeteData();
|
|
415
|
+
channelObject.instanceConfig = new ChapeteInstanceConfig();
|
|
416
|
+
channelObject.config = new ChapeteConfig();
|
|
417
|
+
return false;
|
|
418
|
+
};
|
|
419
|
+
/*
|
|
420
|
+
A start is a new session: the LLM or the temperature may have changed in the setup, so the
|
|
421
|
+
conversation starts over too, instead of continuing a chat begun with another model.
|
|
422
|
+
*/
|
|
423
|
+
this.startChannel = (channelObject) => {
|
|
424
|
+
const data = channelObject.data;
|
|
425
|
+
data.entries = [];
|
|
426
|
+
data.signals = [];
|
|
427
|
+
data.started = true;
|
|
428
|
+
return true;
|
|
429
|
+
};
|
|
430
|
+
this.stopChannel = (channelObject) => {
|
|
431
|
+
const data = channelObject.data;
|
|
432
|
+
abandonPending(data.entries);
|
|
433
|
+
data.started = false;
|
|
434
|
+
return true;
|
|
435
|
+
};
|
|
436
|
+
this.pauseChannel = (_channelObject) => false;
|
|
437
|
+
this.continueChannel = (_channelObject) => false;
|
|
438
|
+
this.socketDisconnected = (_channelObject) => false;
|
|
439
|
+
this.socketReconnect = (_channelObject) => false;
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// src/front/index.ts
|
|
444
|
+
if (!window.__kwirth_plugins__) window.__kwirth_plugins__ = {};
|
|
445
|
+
window.__kwirth_plugins__["chapete"] = ChapeteChannel;
|
|
446
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
+
"id": "chapete",
|
|
2
3
|
"name": "@jfvilas/kwirth-plugin-chapete",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"displayName": "Chapete",
|
|
5
|
+
"extensionType": "plugin",
|
|
6
|
+
"version": "0.1.1",
|
|
7
|
+
"description": "Chat with an LLM from Kwirth, using the LLMs configured in the core",
|
|
8
|
+
"icon": "Forum",
|
|
9
|
+
"website": "https://kwirthmagnify.dev",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/jfvilas/kwirth.git",
|
|
13
|
+
"directory": "plugins/chapete"
|
|
14
|
+
},
|
|
15
|
+
"requiresRestart": false,
|
|
16
|
+
"requiresExtension": []
|
|
6
17
|
}
|