-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
187 lines (123 loc) · 5.82 KB
/
Copy pathREADME.Rmd
File metadata and controls
187 lines (123 loc) · 5.82 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# PacketLLM
An RStudio gadget that connects an AI assistant to your editor: context-aware, non-blocking, and with direct code insertion and replacement.
[](https://CRAN.R-project.org/package=PacketLLM)
[](https://github.com/AntoniCzolgowski/PacketLLM/actions)
PacketLLM runs as an RStudio gadget (Viewer pane or pop-out window). It automatically captures your active selection, open file, and package metadata, so you can ask questions about your actual code without copy-pasting anything.
---
## Key features
**Context-aware conversations**
The gadget polls the RStudio editor every second and feeds the current context into every message. Four modes control scope:
| Mode | What the model sees |
|------|---------------------|
| Auto | Selection if present, else active file, else project metadata |
| Focused | Active selection or file only |
| Project | Active file + `DESCRIPTION` + project file list |
| None | Only the typed message |
**Non-blocking API calls**
Requests run in a background thread via `future` and `promises`. The R console stays fully usable while the model is thinking.
**Editor actions**
Each code block in a response exposes three buttons:
- **Copy:** puts the code on the clipboard.
- **Insert:** places the code at the captured cursor position in the active source document.
- **Replace:** opens a before/after preview, then validates that the editor selection has not changed since it was captured before writing the replacement. If it has changed, the action is blocked until context refreshes.
**Persistent conversations**
History, settings, and attached file names are written to `tools::R_user_dir("PacketLLM", "data")` automatically. Closing the gadget and reopening it, even after restarting RStudio, restores every conversation exactly as it was.
**Model presets**
Three presets cover common trade-offs between quality and latency:
| Preset | Model | Best for |
|--------|-------|----------|
| Best | gpt-5.5 | Complex reasoning, architecture review |
| Balanced | gpt-5.4 | Day-to-day development |
| Fast | gpt-5.4-mini | Quick edits, short questions |
The model is selectable before the first message in a conversation and locked after that to keep the context consistent.
**Assistant behavior presets**
| Preset | Effect |
|--------|--------|
| Default | Practical, precise R-focused responses |
| Concise | Short explanations, compact code |
| Code-focused | Runnable code first, minimal prose |
| Reviewer | Leads with bugs, risks, and CRAN concerns |
| Custom | Your own system instruction |
**File attachments**
Attach `.R`, `.pdf`, `.docx`, `.txt`, or `.csv` files as additional context. Attached files are stored in the conversation and included in every subsequent message. CSV files are converted into a model-friendly form: a header with the dimensions, a column schema with inferred types, and the rows rendered as a Markdown table (the field delimiter is detected automatically and large files are truncated to the first 1000 rows).
**Safe rendering**
Assistant output is parsed into typed blocks (text, code, change cards) and rendered as structured HTML. User and model content is never injected as raw HTML. Markdown headings, lists, tables, inline formatting, and LaTeX math (`\(...\)` and `\[...\]`) are all supported.
---
## Installation
```r
install.packages("PacketLLM")
```
Development version from GitHub:
```r
# install.packages("remotes")
remotes::install_github("AntoniCzolgowski/PacketLLM")
```
---
## Configuration
PacketLLM reads the API key from the `OPENAI_API_KEY` environment variable. Add it to your user-level `.Renviron`:
```r
usethis::edit_r_environ()
```
Then add the line:
```sh
OPENAI_API_KEY='your_key_here'
```
Restart R. Never hardcode keys in scripts or commit them to a repository.
---
## Usage
Launch from the RStudio Addins menu under **PacketLLM**, or from the console:
```r
library(PacketLLM)
run_llm_chat_app()
```
The gadget opens in the Viewer pane. Select some code in the editor, switch back to the gadget, and the context status label confirms what was captured. Type a question and press **Send**.
### Typical workflow
1. Select a function in the editor.
2. Open PacketLLM (Addins menu or `run_llm_chat_app()`).
3. Ask a question. No need to paste the code; it is already in context.
4. Click **Replace** on a suggested code block to preview and apply the change directly to the editor.
### Multiple conversations
Use **New chat** to open a separate tab. Each tab has its own context mode, model, attached files, and history. Close tabs with the **x** on the tab; the last tab cannot be closed.
---
## Package API
The public API exposes the history manager and key helpers for scripting and testing:
```r
# Conversation lifecycle
initialize_history_manager(persist = TRUE)
create_new_conversation()
delete_conversation(id)
reset_history_manager()
# History
add_message_to_active_history(role, content)
get_active_chat_history()
get_conversation_history(id)
# Settings
set_conversation_model(id, model_name)
set_conversation_system_message(id, message)
# Context
capture_rstudio_context(mode = "auto")
# Files
read_file_content(file_path)
parse_pages(pages_str)
```
---
## Requirements
- R >= 4.1.0
- RStudio (for editor-aware features; the gadget runs outside RStudio with reduced functionality)
- An API key set as `OPENAI_API_KEY`
**Imports:** `future`, `httr`, `pdftools`, `promises`, `readtext`, `rstudioapi`, `shiny`, `shinyjs`
---
## License
MIT. See `LICENSE` for details.