ruqenai 0.0.0-stage → 1.0.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 +114 -3
- package/asciilogoart.txt +7 -0
- package/bin/ruqenai.js +2 -0
- package/dist/agent/context.js +333 -0
- package/dist/agent/engine.js +572 -0
- package/dist/agent/permissions.js +73 -0
- package/dist/agent/subagents.js +61 -0
- package/dist/agent/tools.js +381 -0
- package/dist/agent/workspace.js +273 -0
- package/dist/commands/catalog.js +56 -0
- package/dist/commands/connectors.js +55 -0
- package/dist/commands/router.js +414 -0
- package/dist/config/keystore.js +23 -0
- package/dist/config/settings.js +273 -0
- package/dist/index.js +165 -0
- package/dist/mcp/client.js +345 -0
- package/dist/providers/anthropic.js +180 -0
- package/dist/providers/catalog.js +1064 -0
- package/dist/providers/errors.js +25 -0
- package/dist/providers/gemini.js +174 -0
- package/dist/providers/manager.js +298 -0
- package/dist/providers/ollama.js +216 -0
- package/dist/providers/openaiCompat.js +179 -0
- package/dist/providers/protocols.js +31 -0
- package/dist/providers/stream.js +40 -0
- package/dist/providers/types.js +22 -0
- package/dist/providers/usage.js +72 -0
- package/dist/skills/manager.js +150 -0
- package/dist/types/index.js +4 -0
- package/dist/ui/ActionRow.js +51 -0
- package/dist/ui/ActivityPanel.js +68 -0
- package/dist/ui/App.js +635 -0
- package/dist/ui/ChatHistory.js +225 -0
- package/dist/ui/ChatTabs.js +126 -0
- package/dist/ui/CommandOverlay.js +553 -0
- package/dist/ui/Composer.js +185 -0
- package/dist/ui/FrameOutput.js +43 -0
- package/dist/ui/MouseListener.js +135 -0
- package/dist/ui/PermissionPrompt.js +47 -0
- package/dist/ui/QuestionPrompt.js +58 -0
- package/dist/ui/SubagentDetails.js +41 -0
- package/dist/ui/UpdateBoard.js +57 -0
- package/dist/ui/Wordmark.js +38 -0
- package/dist/ui/commandSuggestions.js +34 -0
- package/dist/ui/mouseBounds.js +11 -0
- package/dist/ui/syntax.js +30 -0
- package/dist/ui/terminal.js +58 -0
- package/dist/ui/windowsClipboard.js +75 -0
- package/dist/updates.js +54 -0
- package/package.json +52 -4
package/README.md
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# Ruqenai
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
A terminal coding assistant for local Ollama models and cloud providers.
|
|
6
|
+
|
|
7
|
+
Ruqenai brings chat, workspace tools, and model selection into one terminal interface. Start a conversation in a project directory to give the assistant context for coding, exploring files, and running commands.
|
|
8
|
+
|
|
9
|
+
## What you can do
|
|
10
|
+
|
|
11
|
+
- Switch between installed Ollama models and configured cloud models from the model picker.
|
|
12
|
+
- Work across saved conversations with tabs and the session picker.
|
|
13
|
+
- Attach files and images to prompts, and review tool activity while the assistant works.
|
|
14
|
+
- Choose action permissions and reasoning effort for the current workflow.
|
|
15
|
+
- Connect MCP servers and use local skills, plugins, and agent profiles.
|
|
16
|
+
- Run a prompt directly from the command line when you do not need the interactive interface.
|
|
17
|
+
|
|
18
|
+
## Preview
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+
|
|
22
|
+

|
|
23
|
+
|
|
24
|
+
## Start
|
|
25
|
+
|
|
26
|
+
### Install from npm
|
|
27
|
+
|
|
28
|
+
Once Ruqenai is published to npm, install it globally and launch it with:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install -g ruqenai
|
|
32
|
+
ruqenai
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Ruqenai checks npm for a new release at startup after an npm installation. Choose **Update** to update a global npm installation, or **Later** to keep working. Restart Ruqenai after the update finishes. Use `/update` to check again.
|
|
36
|
+
|
|
37
|
+
### Run from source
|
|
38
|
+
|
|
39
|
+
Install Node.js and npm, open a terminal in the project folder, and run:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
npm install
|
|
43
|
+
npm start
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
To use the Windows launcher, run `npm run build` and then `.\ruqenai.cmd`. If the CLI is installed or linked, use `ruqenai`. For local models, start Ollama and install a model before selecting it in Ruqenai.
|
|
47
|
+
|
|
48
|
+
Each launch opens a fresh conversation with the Ruqenai wordmark. Use `/sessions` to find a saved conversation. Use `Ctrl+P` or `/help` to search commands. The model, access, activity, effort, and settings controls accept mouse clicks.
|
|
49
|
+
|
|
50
|
+
## Providers and models
|
|
51
|
+
|
|
52
|
+
1. Enter `/provider` and select a provider. Ollama uses `http://127.0.0.1:11434` by default or the address set with `OLLAMA_HOST`.
|
|
53
|
+
2. For a cloud provider, paste its API key into the masked field. For a custom OpenAI-compatible service, enter the API base URL and its key if required.
|
|
54
|
+
3. Use **Test connection** to check discovery, then **Save and view models**. Pick a model to use it for the next prompt.
|
|
55
|
+
|
|
56
|
+
`/model` groups installed Ollama models and configured cloud providers. Click the current model or press Tab in an empty prompt to open the same picker. Use `/provider` and select a provider to replace or remove its saved key.
|
|
57
|
+
|
|
58
|
+
The provider list contains **88 entries**. Of those, 32 provide chat connection profiles, including Ollama, native Anthropic and Gemini, NVIDIA NIM, other OpenAI-compatible services, and deployment profiles. The other 56 are catalog references. For deployment profiles such as Azure OpenAI, Bedrock, Vertex, Baseten, and RunPod, enter the base URL of your OpenAI-compatible deployment or gateway and its credentials. Use **Enter model ID** in the picker or `/model <provider> <model-id>` if the endpoint provides no model list.
|
|
59
|
+
|
|
60
|
+
To use NVIDIA NIM, enter `/provider nvidia` or set `NVIDIA_API_KEY` in your shell. Ruqenai uses NVIDIA’s hosted chat and model-list endpoints. Model availability depends on your NVIDIA account. [NVIDIA NIM API reference](https://docs.api.nvidia.com/nim/reference/llm-apis)
|
|
61
|
+
|
|
62
|
+
Keys saved in the provider picker go to `~/.ruqenai/.env`. Ruqenai migrates legacy keys from `~/.ruqenai/config.json` on startup. The Git ignore rules exclude dotenv files, local settings, and the private Obsidian vault.
|
|
63
|
+
|
|
64
|
+
Text attachments are sent as excerpts with the prompt. Images require a vision-capable model and use the selected provider's image format. If the provider rejects the first request, Ruqenai restores the draft and queues its attachments for retry.
|
|
65
|
+
|
|
66
|
+
`/effort` opens reasoning effort for the current model. Supported options include Off, Low, Medium, High, XHigh, and UltraCode. `/mode` opens **Ask permission**, **Approve for me**, and **Full access**.
|
|
67
|
+
|
|
68
|
+
`/settings` brings these controls together and includes a motion toggle. `/animations off` reduces motion and `/config show` prints settings with credentials and custom endpoints omitted.
|
|
69
|
+
|
|
70
|
+
## Keyboard
|
|
71
|
+
|
|
72
|
+
| Key | Action |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| `Ctrl+P` | Find a slash command |
|
|
75
|
+
| `Ctrl+V` / `Ctrl+Y` | Paste text or attach clipboard files and images on Windows. `/attach` reads the Windows clipboard directly. |
|
|
76
|
+
| `Ctrl+O` | Choose a model |
|
|
77
|
+
| `Ctrl+T` | Choose reasoning effort |
|
|
78
|
+
| `Ctrl+G` | Open or close activity |
|
|
79
|
+
| `Ctrl+R` | Show or hide provider reasoning |
|
|
80
|
+
| `Ctrl+U` | Open the effort picker, including UltraCode |
|
|
81
|
+
| `/` | Show matching commands above the prompt |
|
|
82
|
+
| `Tab` | Fill the highlighted command |
|
|
83
|
+
| `Esc` | Close a picker or cancel active work |
|
|
84
|
+
| `↑` / `↓` | Move in a picker or browse input history |
|
|
85
|
+
| `Page Up` / `Page Down` | Scroll the transcript |
|
|
86
|
+
| `Enter` | Send input or select a row |
|
|
87
|
+
|
|
88
|
+
## Tools and extensions
|
|
89
|
+
|
|
90
|
+
The Activity panel keeps completed plan steps marked `[x]` until the assistant starts a new plan or clears the list. Use `/cleartasks` to clear the current chat’s task list. When the assistant asks a question, choose an option, select **Type your own answer**, or press Esc to skip. Follow-up questions appear one at a time.
|
|
91
|
+
|
|
92
|
+
Use `/sessions` or `Ctrl+L` to switch saved chats. Wide terminals show chat tabs and a **+ New** button above the transcript. On Windows, use `/attach` to read the clipboard directly. Images require a vision-capable model. Text files are sent as excerpts.
|
|
93
|
+
|
|
94
|
+
- `/tools` lists tools. `/tools run <name> [json]` invokes one.
|
|
95
|
+
- `/skills` lists skills. Use `/skills create <name> <instructions>` to save a skill or `/skills run <name> <prompt>` to use it.
|
|
96
|
+
- `/agents` lists agent profiles. Use `/agents run <profile> <prompt>` to delegate a turn to a profile.
|
|
97
|
+
- `/mcp` lists configured servers. Use `/mcp <server>` to inspect its tools and `/mcp <server> <tool> [json]` to call one. The `mcpServers` object in `~/.ruqenai/mcp.json` maps each name to a local stdio `command` and optional `args`, `env`, and `description`.
|
|
98
|
+
- `/plugins` lists installed JSON manifests under `~/.ruqenai/plugins/`. Each may declare a `name`, a `description`, and a `mcpServers` object using the same format.
|
|
99
|
+
|
|
100
|
+
`/help all` prints the command list. Use `/attach <path>` to add file context, `/export <path>` to write a Markdown or JSON transcript, `/copy` to copy the latest response, or `/copy path` to copy the workspace path.
|
|
101
|
+
|
|
102
|
+
## Headless CLI
|
|
103
|
+
|
|
104
|
+
`ruqenai run` streams the answer to stdout and sends provider errors to stderr with a nonzero exit code.
|
|
105
|
+
|
|
106
|
+
Use `--profile <profile>` or `--skill <skill>` to set the instructions for one invocation. `--permission ask|approve|full` applies to that invocation, and `--yolo` is equivalent to `--permission full`.
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
ruqenai run "Explain event-driven architecture"
|
|
110
|
+
ruqenai run "Summarize this code" -p ollama -m qwen2.5:7b
|
|
111
|
+
ruqenai doctor
|
|
112
|
+
ruqenai models
|
|
113
|
+
ruqenai providers
|
|
114
|
+
```
|
package/asciilogoart.txt
ADDED
package/bin/ruqenai.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session, Memory and Context Management for Ruqenai.
|
|
3
|
+
*/
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
import { mergeUsage } from '../providers/usage.js';
|
|
9
|
+
const GLOBAL_DIR = path.join(os.homedir(), '.ruqenai');
|
|
10
|
+
const SESSIONS_DIR = path.join(GLOBAL_DIR, 'sessions');
|
|
11
|
+
const ORDER_PATH = path.join(GLOBAL_DIR, 'session-order.json');
|
|
12
|
+
function validSessionId(id) {
|
|
13
|
+
return Boolean(id) && id.length <= 100 && id !== '.' && id !== '..' && !/[<>:"/\\|?*\x00-\x1f]/.test(id);
|
|
14
|
+
}
|
|
15
|
+
export const DEFAULT_SESSION_NAME = 'Untitled';
|
|
16
|
+
function normalizeSessionName(name, id) {
|
|
17
|
+
const value = typeof name === 'string' ? name.trim() : '';
|
|
18
|
+
if (!value || value === `Session ${id}`)
|
|
19
|
+
return DEFAULT_SESSION_NAME;
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
export class SessionManager {
|
|
23
|
+
currentSession;
|
|
24
|
+
constructor(sessionId = 'default') {
|
|
25
|
+
if (!fs.existsSync(SESSIONS_DIR)) {
|
|
26
|
+
try {
|
|
27
|
+
fs.mkdirSync(SESSIONS_DIR, { recursive: true });
|
|
28
|
+
}
|
|
29
|
+
catch { }
|
|
30
|
+
}
|
|
31
|
+
this.currentSession = this.readSession(sessionId) ?? this.createSession(sessionId);
|
|
32
|
+
}
|
|
33
|
+
getSession() {
|
|
34
|
+
return this.currentSession;
|
|
35
|
+
}
|
|
36
|
+
focusSession(session) {
|
|
37
|
+
this.currentSession = session;
|
|
38
|
+
}
|
|
39
|
+
startFreshSession() {
|
|
40
|
+
this.currentSession = this.createSession(`session-${Date.now()}-${randomUUID().slice(0, 8)}`);
|
|
41
|
+
}
|
|
42
|
+
switchSession(id, { create = false } = {}) {
|
|
43
|
+
if (!validSessionId(id))
|
|
44
|
+
return false;
|
|
45
|
+
const next = create ? this.createSession(id) : this.readSession(id);
|
|
46
|
+
if (!next || (create && !this.save(next, 'wx')))
|
|
47
|
+
return false;
|
|
48
|
+
this.currentSession = next;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
readSession(id) {
|
|
52
|
+
if (!validSessionId(id))
|
|
53
|
+
return;
|
|
54
|
+
const file = path.join(SESSIONS_DIR, `${id}.json`);
|
|
55
|
+
if (fs.existsSync(file)) {
|
|
56
|
+
try {
|
|
57
|
+
const raw = fs.readFileSync(file, 'utf-8');
|
|
58
|
+
const saved = JSON.parse(raw);
|
|
59
|
+
if (Array.isArray(saved.messages) && Array.isArray(saved.attachedFiles)) {
|
|
60
|
+
return {
|
|
61
|
+
...saved,
|
|
62
|
+
id,
|
|
63
|
+
name: normalizeSessionName(saved.name, id),
|
|
64
|
+
totalTokens: Number.isFinite(saved.totalTokens) ? saved.totalTokens : 0,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch { }
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
createSession(id) {
|
|
73
|
+
const now = Date.now();
|
|
74
|
+
return {
|
|
75
|
+
id,
|
|
76
|
+
name: DEFAULT_SESSION_NAME,
|
|
77
|
+
createdAt: now,
|
|
78
|
+
updatedAt: now,
|
|
79
|
+
messages: [],
|
|
80
|
+
attachedFiles: [],
|
|
81
|
+
totalTokens: 0,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
save(session = this.currentSession, flag = 'w', preserveUpdatedAt = false) {
|
|
85
|
+
let temporary;
|
|
86
|
+
try {
|
|
87
|
+
if (!validSessionId(session.id))
|
|
88
|
+
return false;
|
|
89
|
+
const file = path.join(SESSIONS_DIR, `${session.id}.json`);
|
|
90
|
+
const updatedAt = preserveUpdatedAt ? session.updatedAt : Date.now();
|
|
91
|
+
const json = JSON.stringify({ ...session, updatedAt }, null, 2);
|
|
92
|
+
if (flag === 'wx') {
|
|
93
|
+
fs.writeFileSync(file, json, { encoding: 'utf-8', flag, mode: 0o600 });
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
temporary = path.join(SESSIONS_DIR, `.session-${randomUUID()}.tmp`);
|
|
97
|
+
fs.writeFileSync(temporary, json, { encoding: 'utf-8', flag: 'wx', mode: 0o600 });
|
|
98
|
+
fs.renameSync(temporary, file);
|
|
99
|
+
temporary = undefined;
|
|
100
|
+
}
|
|
101
|
+
session.updatedAt = updatedAt;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
if (flag === 'w')
|
|
106
|
+
console.error('Failed to save session:', err);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
if (temporary) {
|
|
111
|
+
try {
|
|
112
|
+
fs.unlinkSync(temporary);
|
|
113
|
+
}
|
|
114
|
+
catch { }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
addMessage(message, session = this.currentSession) {
|
|
119
|
+
session.messages.push(message);
|
|
120
|
+
if (message.tokens) {
|
|
121
|
+
session.totalTokens += message.tokens;
|
|
122
|
+
}
|
|
123
|
+
if (message.usage) {
|
|
124
|
+
mergeUsage(session.usage ??= {}, message.usage);
|
|
125
|
+
}
|
|
126
|
+
this.save(session);
|
|
127
|
+
}
|
|
128
|
+
clearTasks() {
|
|
129
|
+
const session = this.currentSession;
|
|
130
|
+
const messages = session.messages.map((message) => message.tasks
|
|
131
|
+
? { ...message, tasks: message.tasks.filter((task) => task.kind !== 'plan') }
|
|
132
|
+
: message);
|
|
133
|
+
if (!this.save({ ...session, messages }, 'w', true))
|
|
134
|
+
return false;
|
|
135
|
+
session.messages = messages;
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
sendPrompt(message, session = this.currentSession) {
|
|
139
|
+
const next = { ...session, messages: [...session.messages, message], attachedFiles: [] };
|
|
140
|
+
if (!this.save(next))
|
|
141
|
+
throw new Error('Could not save the prompt. Your attachments are still queued.');
|
|
142
|
+
Object.assign(session, next);
|
|
143
|
+
}
|
|
144
|
+
undoLastTurn() {
|
|
145
|
+
const session = this.currentSession;
|
|
146
|
+
const messages = session.messages;
|
|
147
|
+
let index = messages.length - 1;
|
|
148
|
+
while (index >= 0 && messages[index].role !== 'assistant' && messages[index].role !== 'user')
|
|
149
|
+
index--;
|
|
150
|
+
if (index < 0)
|
|
151
|
+
return undefined;
|
|
152
|
+
const removed = messages[index];
|
|
153
|
+
const start = removed.role === 'assistant' && messages[index - 1]?.role === 'user' ? index - 1 : index;
|
|
154
|
+
const count = index - start + 1;
|
|
155
|
+
messages.splice(start, count);
|
|
156
|
+
if (session.contextStart !== undefined && start < session.contextStart) {
|
|
157
|
+
session.contextStart -= Math.min(count, session.contextStart - start);
|
|
158
|
+
}
|
|
159
|
+
this.save();
|
|
160
|
+
return removed;
|
|
161
|
+
}
|
|
162
|
+
clearConversation() {
|
|
163
|
+
this.currentSession.messages = [];
|
|
164
|
+
delete this.currentSession.contextStart;
|
|
165
|
+
this.save();
|
|
166
|
+
}
|
|
167
|
+
rewindToPrompt(id) {
|
|
168
|
+
const session = this.currentSession;
|
|
169
|
+
const index = session.messages.findIndex((message) => message.id === id && message.role === 'user');
|
|
170
|
+
if (index < 0)
|
|
171
|
+
return false;
|
|
172
|
+
const rewound = { ...session, messages: session.messages.slice(0, index) };
|
|
173
|
+
if (rewound.contextStart !== undefined)
|
|
174
|
+
rewound.contextStart = Math.min(rewound.contextStart, index);
|
|
175
|
+
if (!this.save(rewound))
|
|
176
|
+
return false;
|
|
177
|
+
this.currentSession = rewound;
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
forkBranch(newBranchId) {
|
|
181
|
+
if (!validSessionId(newBranchId))
|
|
182
|
+
throw new Error('Invalid session name');
|
|
183
|
+
const forked = {
|
|
184
|
+
id: newBranchId,
|
|
185
|
+
name: `Branch: ${newBranchId}`,
|
|
186
|
+
createdAt: Date.now(),
|
|
187
|
+
updatedAt: Date.now(),
|
|
188
|
+
branchFrom: this.currentSession.id,
|
|
189
|
+
messages: [...this.currentSession.messages],
|
|
190
|
+
attachedFiles: [...this.currentSession.attachedFiles],
|
|
191
|
+
totalTokens: this.currentSession.totalTokens,
|
|
192
|
+
usage: this.currentSession.usage ? { ...this.currentSession.usage } : undefined,
|
|
193
|
+
contextStart: this.currentSession.contextStart,
|
|
194
|
+
};
|
|
195
|
+
if (!this.save(forked, 'wx'))
|
|
196
|
+
throw new Error('A session already uses this name or could not be saved.');
|
|
197
|
+
this.currentSession = forked;
|
|
198
|
+
return forked;
|
|
199
|
+
}
|
|
200
|
+
compactContext(session = this.currentSession) {
|
|
201
|
+
const msgs = session.messages;
|
|
202
|
+
const before = this.contextStart(session);
|
|
203
|
+
if (msgs.length - before <= 4) {
|
|
204
|
+
return 'Context is already compact.';
|
|
205
|
+
}
|
|
206
|
+
let keepFrom = msgs.length - 4;
|
|
207
|
+
if (msgs[keepFrom].role === 'assistant' && msgs[keepFrom - 1].role === 'user')
|
|
208
|
+
keepFrom--;
|
|
209
|
+
if (keepFrom <= before)
|
|
210
|
+
return 'Context is already compact.';
|
|
211
|
+
session.contextStart = keepFrom;
|
|
212
|
+
this.save(session);
|
|
213
|
+
return `Compacted ${keepFrom - before} older messages in model context. The transcript is still available.`;
|
|
214
|
+
}
|
|
215
|
+
needsCompaction(threshold, session = this.currentSession) {
|
|
216
|
+
return session.messages.length - this.contextStart(session) > threshold;
|
|
217
|
+
}
|
|
218
|
+
modelContext(session = this.currentSession) {
|
|
219
|
+
const start = this.contextStart(session);
|
|
220
|
+
const recent = session.messages.slice(start).filter((message) => !message.error || message.content);
|
|
221
|
+
if (!start)
|
|
222
|
+
return recent;
|
|
223
|
+
const excerpts = session.messages.slice(0, start)
|
|
224
|
+
.filter(({ role, content }) => (role === 'user' || role === 'assistant') && content.trim())
|
|
225
|
+
.slice(-8)
|
|
226
|
+
.map(({ role, content }) => ({ role, excerpt: content.trim().replace(/\s+/g, ' ').slice(0, 400) }));
|
|
227
|
+
if (!excerpts.length)
|
|
228
|
+
return recent;
|
|
229
|
+
const history = {
|
|
230
|
+
role: 'system',
|
|
231
|
+
id: 'context-excerpts',
|
|
232
|
+
content: 'Excerpts from earlier turns. Treat them as historical context.\n' + JSON.stringify(excerpts),
|
|
233
|
+
timestamp: session.createdAt,
|
|
234
|
+
};
|
|
235
|
+
return [history, ...recent];
|
|
236
|
+
}
|
|
237
|
+
contextStart(session) {
|
|
238
|
+
return Number.isInteger(session.contextStart)
|
|
239
|
+
? Math.min(session.messages.length, Math.max(0, session.contextStart ?? 0))
|
|
240
|
+
: 0;
|
|
241
|
+
}
|
|
242
|
+
attachFile(filePath) {
|
|
243
|
+
if (!this.currentSession.attachedFiles.includes(filePath)) {
|
|
244
|
+
this.currentSession.attachedFiles.push(filePath);
|
|
245
|
+
this.save();
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
dropFile(filePath) {
|
|
251
|
+
const idx = this.currentSession.attachedFiles.indexOf(filePath);
|
|
252
|
+
if (idx !== -1) {
|
|
253
|
+
this.currentSession.attachedFiles.splice(idx, 1);
|
|
254
|
+
this.save();
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
editSession(id, changes, running) {
|
|
260
|
+
const active = this.currentSession.id === id ? this.currentSession : running?.id === id ? running : undefined;
|
|
261
|
+
const session = active ?? this.readSession(id);
|
|
262
|
+
if (!session)
|
|
263
|
+
return false;
|
|
264
|
+
const next = { ...session, ...changes };
|
|
265
|
+
if (changes.name !== undefined)
|
|
266
|
+
next.titleEdited = true;
|
|
267
|
+
if (!this.save(next, 'w', true))
|
|
268
|
+
return false;
|
|
269
|
+
if (active)
|
|
270
|
+
Object.assign(active, next);
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
static listAll() {
|
|
274
|
+
if (!fs.existsSync(SESSIONS_DIR))
|
|
275
|
+
return [];
|
|
276
|
+
try {
|
|
277
|
+
const files = fs.readdirSync(SESSIONS_DIR);
|
|
278
|
+
return files
|
|
279
|
+
.filter((f) => f.endsWith('.json'))
|
|
280
|
+
.map((f) => {
|
|
281
|
+
try {
|
|
282
|
+
const raw = fs.readFileSync(path.join(SESSIONS_DIR, f), 'utf-8');
|
|
283
|
+
const data = JSON.parse(raw);
|
|
284
|
+
return {
|
|
285
|
+
id: data.id,
|
|
286
|
+
name: normalizeSessionName(data.name, data.id),
|
|
287
|
+
updatedAt: data.updatedAt,
|
|
288
|
+
messageCount: data.messages?.length || 0,
|
|
289
|
+
hidden: data.hidden === true,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
.filter(Boolean)
|
|
297
|
+
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
return [];
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
static deleteSession(id) {
|
|
304
|
+
if (!validSessionId(id))
|
|
305
|
+
return false;
|
|
306
|
+
try {
|
|
307
|
+
fs.unlinkSync(path.join(SESSIONS_DIR, `${id}.json`));
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
this.setOrder(this.getOrder().filter((entry) => entry !== id));
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
static getOrder() {
|
|
316
|
+
try {
|
|
317
|
+
const parsed = JSON.parse(fs.readFileSync(ORDER_PATH, 'utf-8'));
|
|
318
|
+
return Array.isArray(parsed) ? parsed.filter((value) => typeof value === 'string') : [];
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
static setOrder(ids) {
|
|
325
|
+
try {
|
|
326
|
+
if (!fs.existsSync(GLOBAL_DIR))
|
|
327
|
+
fs.mkdirSync(GLOBAL_DIR, { recursive: true });
|
|
328
|
+
fs.writeFileSync(ORDER_PATH, JSON.stringify([...new Set(ids)], null, 2), { encoding: 'utf-8', mode: 0o600 });
|
|
329
|
+
}
|
|
330
|
+
catch { }
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
export const sessionManager = new SessionManager();
|