Skip to content

Commit 7ef59bd

Browse files
committed
feat: move calling AI service to the client side
1 parent 94a2414 commit 7ef59bd

File tree

16 files changed

+305
-352
lines changed

16 files changed

+305
-352
lines changed

_build/gpm.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: modAI
2-
version: 0.0.8-alpha
2+
version: 0.0.9-alpha
33
lowCaseName: modai
44
namespace: modAI
55
author: 'John Peca'

assets/components/modai/js/mgr/autosummary.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,19 @@ Ext.onReady(function() {
195195
listeners: {
196196
success: {
197197
fn: (r) => {
198-
cache.store(fieldName, r.object.content);
199-
Ext.Msg.hide();
198+
modAI.serviceExecutor(r.object).then((result) => {
199+
cache.store(fieldName, result.content);
200+
Ext.Msg.hide();
201+
}).catch((err) => {
202+
Ext.Msg.hide();
203+
Ext.Msg.alert("Failed", `Failed to generated. Please try again. ${err.message}`);
204+
});
200205
}
201206
},
202207
failure: {
203208
fn: function() {
204-
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
205209
Ext.Msg.hide();
210+
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
206211
} ,
207212
scope: this
208213
}
@@ -268,14 +273,19 @@ Ext.onReady(function() {
268273
listeners: {
269274
success: {
270275
fn: (r) => {
271-
cache.store(fieldName, r.object.content);
272-
Ext.Msg.hide();
276+
modAI.serviceExecutor(r.object).then((result) => {
277+
cache.store(fieldName, result.content);
278+
Ext.Msg.hide();
279+
}).catch((err) => {
280+
Ext.Msg.hide();
281+
Ext.Msg.alert("Failed", `Failed to generated. Please try again. ${err.message}`);
282+
});
273283
}
274284
},
275285
failure: {
276286
fn: function() {
277-
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
278287
Ext.Msg.hide();
288+
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
279289
} ,
280290
scope: this
281291
}
@@ -332,16 +342,21 @@ Ext.onReady(function() {
332342
listeners: {
333343
success: {
334344
fn: (r) => {
335-
imagePlus.altTextField.items.items[0].setValue(r.object.content);
336-
imagePlus.image.altTag = r.object.content;
337-
imagePlus.updateValue();
338-
Ext.Msg.hide();
345+
modAI.serviceExecutor(r.object).then((result) => {
346+
imagePlus.altTextField.items.items[0].setValue(result.content);
347+
imagePlus.image.altTag = result.content;
348+
imagePlus.updateValue();
349+
Ext.Msg.hide();
350+
}).catch((err) => {
351+
Ext.Msg.hide();
352+
Ext.Msg.alert("Failed", `Failed to generated. Please try again. ${err.message}`);
353+
});
339354
}
340355
},
341356
failure: {
342357
fn: function() {
343-
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
344358
Ext.Msg.hide();
359+
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
345360
} ,
346361
scope: this
347362
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
modAI.serviceExecutor = async (details) => {
2+
const callService = async (details) => {
3+
const res = await fetch(details.url, {
4+
method: 'POST',
5+
body: details.body,
6+
headers: details.headers
7+
});
8+
9+
if (!res.ok) {
10+
const data = await res.json();
11+
if (data?.error) {
12+
throw new Error(data.error.message);
13+
}
14+
15+
throw new Error(`${res.status} ${res.statusText}`);
16+
}
17+
18+
const data = await res.json();
19+
20+
if (data.error) {
21+
throw new Error(data.error.message);
22+
}
23+
24+
return data;
25+
}
26+
27+
const services = {
28+
chatgpt: {
29+
content: (data) => {
30+
const content = data?.choices?.[0]?.message?.content;
31+
32+
if (!content) {
33+
throw new Error("Failed to process your request.");
34+
}
35+
36+
return {
37+
content
38+
}
39+
},
40+
image: (data) => {
41+
const url = data?.data?.[0]?.url;
42+
43+
if (!url) {
44+
throw new Error("Failed to process your request.");
45+
}
46+
47+
return {
48+
url
49+
}
50+
}
51+
},
52+
claude: {
53+
content: (data) => {
54+
const content = data?.content?.[0]?.text;
55+
56+
if (!content) {
57+
throw new Error("Failed to process your request.");
58+
}
59+
60+
return {
61+
content
62+
}
63+
}
64+
},
65+
gemini: {
66+
content: (data) => {
67+
const content = data?.candidates?.[0]?.content?.parts?.[0]?.text;
68+
69+
if (!content) {
70+
throw new Error("Failed to process your request.");
71+
}
72+
73+
return {
74+
content
75+
}
76+
},
77+
image: (data) => {
78+
const base64 = data?.predictions?.[0]?.bytesBase64Encoded;
79+
80+
if (!base64) {
81+
throw new Error("Failed to process your request.");
82+
}
83+
84+
return {
85+
base64: `data:image/png;base64,${base64}`
86+
}
87+
}
88+
}
89+
};
90+
91+
if (!details.service || !details.parser) {
92+
throw new Error("Service is required");
93+
}
94+
95+
if (!services[details.service]?.[details.parser]) {
96+
throw new Error("Unsupported Service/Parser");
97+
}
98+
99+
const data = await callService(details);
100+
101+
return services[details.service][details.parser](data);
102+
}

assets/components/modai/js/mgr/widgets/image_prompt.window.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Ext.extend(modAI.window.ImagePrompt,MODx.Window, {
136136
cls: 'primary-button',
137137
scope: this,
138138
handler: this.submit,
139-
disabled: false
139+
disabled: true
140140
});
141141

142142
this.pagination = {
@@ -194,14 +194,19 @@ Ext.extend(modAI.window.ImagePrompt,MODx.Window, {
194194
listeners: {
195195
success: {
196196
fn: (r) => {
197-
this.pagination.addItem({prompt: this.prompt.getValue(), ...r.object});
198-
Ext.Msg.hide();
197+
modAI.serviceExecutor(r.object).then((result) => {
198+
this.pagination.addItem({prompt: this.prompt.getValue(), ...result});
199+
Ext.Msg.hide();
200+
}).catch((err) => {
201+
Ext.Msg.hide();
202+
Ext.Msg.alert("Failed", `Failed to generated. Please try again. ${err.message}`);
203+
});
199204
}
200205
},
201206
failure: {
202207
fn: function() {
203-
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
204208
Ext.Msg.hide();
209+
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
205210
} ,
206211
scope: this
207212
}

assets/components/modai/js/mgr/widgets/text_prompt.window.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,19 @@ Ext.extend(modAI.window.TextPrompt,MODx.Window, {
182182
listeners: {
183183
success: {
184184
fn: (r) => {
185-
this.pagination.addItem({prompt: this.prompt.getValue(), content: r.object.content});
186-
Ext.Msg.hide();
185+
modAI.serviceExecutor(r.object).then((result) => {
186+
this.pagination.addItem({prompt: this.prompt.getValue(), content: result.content});
187+
Ext.Msg.hide();
188+
}).catch((err) => {
189+
Ext.Msg.hide();
190+
Ext.Msg.alert("Failed", `Failed to generated. Please try again. ${err.message}`);
191+
});
187192
}
188193
},
189194
failure: {
190195
fn: function() {
191-
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
192196
Ext.Msg.hide();
197+
Ext.Msg.alert("Failed", "Failed to generated. Please try again.");
193198
} ,
194199
scope: this
195200
}

core/components/modai/elements/plugins/modai.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
$modx->regClientCSS($assetsUrl . 'css/mgr.css');
4242
$modx->regClientStartupScript($assetsUrl . 'js/mgr/modai.js');
43+
$modx->regClientStartupScript($assetsUrl . 'js/mgr/serviceExecutor.js');
4344
$modx->regClientStartupScript($assetsUrl . 'js/mgr/autosummary.js');
4445
$modx->regClientStartupScript($assetsUrl . 'js/mgr/widgets/image_prompt.window.js');
4546
$modx->regClientStartupScript($assetsUrl . 'js/mgr/widgets/text_prompt.window.js');
46-
}
47+
}

core/components/modai/src/Processors/Prompt/FreeText.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function process()
4444
$aiService = AIServiceFactory::new($model, $this->modx);
4545
$result = $aiService->getCompletions([$prompt], CompletionsConfig::new($model)->maxTokens($maxTokens)->temperature($temperature)->systemInstructions($systemInstructions));
4646

47-
return $this->success('', ['content' => $result]);
47+
return $this->success('', $result->toArray());
4848
} catch (\Exception $e) {
4949
return $this->failure($e->getMessage());
5050
}

core/components/modai/src/Processors/Prompt/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function process()
3232
$aiService = AIServiceFactory::new($model, $this->modx);
3333
$result = $aiService->generateImage($prompt, ImageConfig::new($model)->size($size)->quality($quality));
3434

35-
return $this->success('', $result);
35+
return $this->success('', $result->toArray());
3636
} catch (\Exception $e) {
3737
return $this->failure($e->getMessage());
3838
}

core/components/modai/src/Processors/Prompt/Text.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use modAI\RequiredSettingException;
55
use modAI\Services\AIServiceFactory;
6+
use modAI\Services\ChatGPT;
67
use modAI\Services\Config\CompletionsConfig;
78
use modAI\Settings;
89
use MODX\Revolution\Processors\Processor;
@@ -78,7 +79,7 @@ public function process()
7879
$aiService = AIServiceFactory::new($model, $this->modx);
7980
$result = $aiService->getCompletions([$content], CompletionsConfig::new($model)->maxTokens($maxTokens)->temperature($temperature)->systemInstructions($systemInstructions));
8081

81-
return $this->success('', ['content' => $result]);
82+
return $this->success('', $result->toArray());
8283
} catch (\Exception $e) {
8384
return $this->failure($e->getMessage());
8485
}

core/components/modai/src/Processors/Prompt/Vision.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function process()
2626
$aiService = AIServiceFactory::new($model, $this->modx);
2727
$result = $aiService->getVision($prompt, $image, VisionConfig::new($model));
2828

29-
return $this->success('', ['content' => $result]);
29+
return $this->success('', $result->toArray());
3030
} catch (\Exception $e) {
3131
return $this->failure($e->getMessage());
3232
}

core/components/modai/src/Services/AIService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
use modAI\Services\Config\CompletionsConfig;
66
use modAI\Services\Config\ImageConfig;
77
use modAI\Services\Config\VisionConfig;
8+
use modAI\Services\Response\AIResponse;
89

910
interface AIService {
1011

11-
public function getCompletions(array $data, CompletionsConfig $config): string;
12-
public function getVision(string $prompt, string $image, VisionConfig $config): string;
13-
public function generateImage(string $prompt, ImageConfig $config): array;
12+
public function getCompletions(array $data, CompletionsConfig $config): AIResponse;
13+
public function getVision(string $prompt, string $image, VisionConfig $config): AIResponse;
14+
public function generateImage(string $prompt, ImageConfig $config): AIResponse;
1415
}

0 commit comments

Comments
 (0)