-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.php
108 lines (79 loc) · 3.12 KB
/
cli.php
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
<?php
require_once('vendor/autoload.php');
require_once('config.php');
$cli = new League\CLImate\CLImate;
// Define argument to specify a cron job
$cli->arguments->add([
'cron' => [
'prefix' => 'c',
'longPrefix' => 'cron',
'description' => 'Use in cron, not interactive.',
'defaultValue' => true,
]
]);
if ( !file_exists(__DIR__.'/config.php') ) {
$cli->red('Create your config.php file first.');
die();
}
$remarkable_ai = new reMarkableAI\reMarkable($config);
// Parse arguments
$cli->arguments->parse();
// This is a cron job
if ( $cli->arguments->defined('cron') ) {
$prompts = $remarkable_ai->getPrompts();
// Found prompt emails
if ( count($prompts) > 0 ) {
$cli->green('Found '.count($prompts).' prompts.');
$i = 1;
foreach($prompts as $prompt) {
$cli->out('<green>Prompt '.$i.':</green> '.$prompt);
// Get AI Respose for prompt
$cli->magenta('Getting response from OpenAI...');
$response = $remarkable_ai->getAIResponse($prompt);
if ( $response['headline'] && $response['body'] ) {
$cli->out('<green>Response received:</green> "'.$response['headline'].'"');
} else {
$cli->red('Unable get response.');
die();
}
// Create a PDF document from the response.
$cli->out('<magenta>Creating PDF Document...</magenta>');
$pdf_file = $remarkable_ai->createResponsePdf($response['headline'], $response['body']);
if ( file_exists(__DIR__.'/documents/'.$pdf_file) ) {
$cli->out('<green>Document created:</green> '.$pdf_file);
} else {
$cli->red('Unable to create PDF document.');
die();
}
// Send to reMarkable Cloud
$cli->out('<magenta>Sending to reMarkable cloud:</magenta> '.$pdf_file);
$remarkable_ai->sendToRMAPI($pdf_file);
$i++;
}
}
// This is an interactive prompt
} else {
$input = $cli->input('<green>Enter a prompt:</green> ');
$prompt = $input->prompt();
// Get AI Respose for prompt
$cli->magenta('Getting response from OpenAI...');
$response = $remarkable_ai->getAIResponse($prompt);
if ( $response['headline'] && $response['body'] ) {
$cli->out('<green>Response received:</green> "'.$response['headline'].'"');
} else {
$cli->red('Unable get response.');
die();
}
// Create a PDF document from the response.
$cli->out('<magenta>Creating PDF Document...</magenta>');
$pdf_file = $remarkable_ai->createResponsePdf($response['headline'], $response['body']);
if ( file_exists(__DIR__.'/documents/'.$pdf_file) ) {
$cli->out('<green>Document created:</green> '.$pdf_file);
} else {
$cli->red('Unable to create PDF document.');
die();
}
// Send to reMarkable Cloud
$cli->out('<magenta>Sending to reMarkable cloud:</magenta> '.$pdf_file);
$remarkable_ai->sendToRMAPI($pdf_file);
}