@carrieros/cli-staging 0.0.0-stage → 0.4.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 +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +26 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +160 -0
- package/dist/commands/update.js +285 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +229 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +364 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +132 -0
- package/dist/services/api.js +441 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +384 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +416 -0
- package/dist/services/device-auth.js +288 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +42 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +115 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +730 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1622 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +414 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { ApiClient, ApiError } from "./api.js";
|
|
3
|
+
import { DEFAULT_API_URL, sameApiOrigin } from "./config.js";
|
|
4
|
+
import { refreshCliSession } from "./device-auth.js";
|
|
5
|
+
import { assertCredentialOrigin } from "./auth-origin.js";
|
|
6
|
+
/**
|
|
7
|
+
* Thrown by requireAuth when no key is configured. Unlike a process.exit, this
|
|
8
|
+
* can be caught — so the interactive shell can keep running after a failed
|
|
9
|
+
* command, while standalone commands still turn it into a clean exit(1).
|
|
10
|
+
*/
|
|
11
|
+
export class AuthRequiredError extends Error {
|
|
12
|
+
constructor(message = "You are not logged in. Run `cos login` first.") {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "AuthRequiredError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* CommandContext is the single dependency every command receives. Commands
|
|
19
|
+
* depend on this abstraction (not on axios/fs directly), which keeps them thin
|
|
20
|
+
* and testable and satisfies the dependency-inversion principle.
|
|
21
|
+
*/
|
|
22
|
+
export class CommandContext {
|
|
23
|
+
config;
|
|
24
|
+
_sessionSelectionToken;
|
|
25
|
+
constructor(config) {
|
|
26
|
+
this.config = config;
|
|
27
|
+
this._sessionSelectionToken = config.get("sessionSelectionToken");
|
|
28
|
+
}
|
|
29
|
+
/** In-process generation for intentional changes to the shell's active chat. */
|
|
30
|
+
_sessionSelectionRevision = 0;
|
|
31
|
+
get sessionSelectionRevision() { return this._sessionSelectionRevision; }
|
|
32
|
+
get sessionSelectionToken() { return this._sessionSelectionToken; }
|
|
33
|
+
noteSessionSelectionChanged() {
|
|
34
|
+
this._sessionSelectionRevision += 1;
|
|
35
|
+
this._sessionSelectionToken = crypto.randomUUID();
|
|
36
|
+
this.config.set("sessionSelectionToken", this._sessionSelectionToken);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Record the ID returned by a turn only if the user has not selected or
|
|
40
|
+
* cleared another conversation while that turn was running. Reload first so
|
|
41
|
+
* a separate CLI process changing the shared active conversation is respected.
|
|
42
|
+
*/
|
|
43
|
+
rememberTurnSession(id, expectedId, expectedRevision, expectedToken) {
|
|
44
|
+
this.config.reload();
|
|
45
|
+
const storedToken = this.config.get("sessionSelectionToken");
|
|
46
|
+
if (storedToken !== this._sessionSelectionToken) {
|
|
47
|
+
this._sessionSelectionToken = storedToken;
|
|
48
|
+
this._sessionSelectionRevision += 1;
|
|
49
|
+
}
|
|
50
|
+
if (this._sessionSelectionRevision !== expectedRevision || this._sessionSelectionToken !== expectedToken ||
|
|
51
|
+
storedToken !== expectedToken || this.config.get("sessionId") !== expectedId)
|
|
52
|
+
return false;
|
|
53
|
+
this.config.set("sessionId", id);
|
|
54
|
+
this.noteSessionSelectionChanged();
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
/** In-process mode override. The interactive shell always OPENS in the Carrier
|
|
58
|
+
* OS mode (the core persona) regardless of the persisted default; setting the
|
|
59
|
+
* mode mid-session updates this. It is never written to disk, so standalone
|
|
60
|
+
* commands keep using the persisted `mode`. */
|
|
61
|
+
overrideMode;
|
|
62
|
+
/** True while running inside the interactive `cos` shell (vs a one-shot
|
|
63
|
+
* standalone command). Commands use this to decide between interactive
|
|
64
|
+
* prompts (shell) and finish-and-return "next steps" output (standalone). */
|
|
65
|
+
_inShell = false;
|
|
66
|
+
get inShell() {
|
|
67
|
+
return this._inShell;
|
|
68
|
+
}
|
|
69
|
+
setInShell(v) {
|
|
70
|
+
this._inShell = v;
|
|
71
|
+
}
|
|
72
|
+
/** File ids attached to chat messages, sent as `file_ids` on /v1/chat so the
|
|
73
|
+
* model can read the file. PERSISTED in config (not just in-process) so a file
|
|
74
|
+
* uploaded via a standalone `cos upload` is still attached when the shell opens
|
|
75
|
+
* in a separate process. Populated by upload (auto-attach) and `/attach`,
|
|
76
|
+
* cleared by `/detach` or logout. */
|
|
77
|
+
get attachedFileIds() {
|
|
78
|
+
return this.config.get("attachedFileIds") ?? [];
|
|
79
|
+
}
|
|
80
|
+
/** Attach a file id (deduped) so it rides along on subsequent messages. */
|
|
81
|
+
attachFile(fileId) {
|
|
82
|
+
if (!fileId)
|
|
83
|
+
return;
|
|
84
|
+
const list = this.attachedFileIds;
|
|
85
|
+
if (!list.includes(fileId))
|
|
86
|
+
this.config.set("attachedFileIds", [...list, fileId]);
|
|
87
|
+
}
|
|
88
|
+
/** Drop all attachments (an explicit /detach or logout). */
|
|
89
|
+
clearAttachments() {
|
|
90
|
+
this.config.set("attachedFileIds", []);
|
|
91
|
+
}
|
|
92
|
+
/** A client built from current config (anonymous if not logged in). */
|
|
93
|
+
client() {
|
|
94
|
+
const apiUrl = this.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
95
|
+
return new ApiClient(apiUrl, this.authProvider(apiUrl));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The one refresh currently on the wire, shared by every request that needs it.
|
|
99
|
+
*
|
|
100
|
+
* Refresh tokens are single-use. The shell opens by loading models and modes
|
|
101
|
+
* in parallel, so when the stored access token has expired both requests get
|
|
102
|
+
* a 401 in the same instant — and each used to run its own refresh with the
|
|
103
|
+
* same token. The server let exactly one rotate and answered the other 409;
|
|
104
|
+
* the CLI read that 409 as a dead session and deleted the credentials the
|
|
105
|
+
* winner had just saved. The first message still went through on the fresh
|
|
106
|
+
* token held in memory, and the second found nothing on disk: "You're not
|
|
107
|
+
* signed in".
|
|
108
|
+
*
|
|
109
|
+
* Lives on the context rather than the provider because `client()` builds a
|
|
110
|
+
* new provider for every client, and the coordination has to span all of them.
|
|
111
|
+
*/
|
|
112
|
+
refreshing = null;
|
|
113
|
+
/** Check configured and actual destinations before exposing saved credentials. */
|
|
114
|
+
validateCredentialOrigin(destination = this.config.get("apiUrl") ?? DEFAULT_API_URL) {
|
|
115
|
+
const auth = this.config.get("auth");
|
|
116
|
+
if (!auth?.accessToken && !auth?.refreshToken && !this.config.get("secretKey"))
|
|
117
|
+
return;
|
|
118
|
+
const issuer = auth?.accessToken || auth?.refreshToken ? auth.apiUrl : this.config.get("secretKeyApiUrl");
|
|
119
|
+
assertCredentialOrigin(issuer, this.config.get("apiUrl") ?? DEFAULT_API_URL);
|
|
120
|
+
assertCredentialOrigin(issuer, destination);
|
|
121
|
+
}
|
|
122
|
+
/** Credential + silent-refresh strategy, bound to the client's API base. */
|
|
123
|
+
authProvider(apiUrl = this.config.get("apiUrl") ?? DEFAULT_API_URL) {
|
|
124
|
+
return {
|
|
125
|
+
token: (destination = apiUrl) => {
|
|
126
|
+
this.validateCredentialOrigin(destination);
|
|
127
|
+
const auth = this.config.get("auth");
|
|
128
|
+
if (auth?.accessToken)
|
|
129
|
+
return auth.accessToken;
|
|
130
|
+
const key = this.config.get("secretKey");
|
|
131
|
+
if (key)
|
|
132
|
+
assertCredentialOrigin(this.config.get("secretKeyApiUrl"), destination);
|
|
133
|
+
return key;
|
|
134
|
+
},
|
|
135
|
+
refresh: async (failedToken) => {
|
|
136
|
+
this.validateCredentialOrigin(apiUrl);
|
|
137
|
+
this.refreshing ??= this.renewSession(apiUrl, failedToken).finally(() => {
|
|
138
|
+
this.refreshing = null;
|
|
139
|
+
});
|
|
140
|
+
return this.refreshing;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Renew the session, and clear local credentials ONLY when the server says it
|
|
146
|
+
* is finished.
|
|
147
|
+
*
|
|
148
|
+
* Before spending the refresh token this re-reads config from disk: another
|
|
149
|
+
* `cos` process (a second terminal, a standalone command alongside the shell)
|
|
150
|
+
* may already have rotated it, in which case the token held in memory is spent
|
|
151
|
+
* and presenting it trips the server's reuse detection — which revokes every
|
|
152
|
+
* device on the account. Adopting the rotated pair avoids asking at all.
|
|
153
|
+
*/
|
|
154
|
+
async renewSession(apiUrl, failedToken) {
|
|
155
|
+
this.config.reload();
|
|
156
|
+
const auth = this.config.get("auth");
|
|
157
|
+
if (!auth?.refreshToken)
|
|
158
|
+
return undefined;
|
|
159
|
+
this.validateCredentialOrigin(apiUrl);
|
|
160
|
+
// Someone already renewed since this request went out — just retry with it.
|
|
161
|
+
if (failedToken && auth.accessToken && auth.accessToken !== failedToken) {
|
|
162
|
+
return auth.accessToken;
|
|
163
|
+
}
|
|
164
|
+
const outcome = await refreshCliSession(apiUrl, auth.refreshToken);
|
|
165
|
+
switch (outcome.kind) {
|
|
166
|
+
case "ok": {
|
|
167
|
+
const renewed = outcome.data;
|
|
168
|
+
this.config.update({
|
|
169
|
+
auth: {
|
|
170
|
+
...auth,
|
|
171
|
+
accessToken: renewed.access_token,
|
|
172
|
+
refreshToken: renewed.refresh_token,
|
|
173
|
+
// Preserve the issuer that was validated before the exchange.
|
|
174
|
+
apiUrl: auth.apiUrl,
|
|
175
|
+
workspace: renewed.workspace ?? auth.workspace,
|
|
176
|
+
project: renewed.project ?? auth.project
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
return renewed.access_token;
|
|
180
|
+
}
|
|
181
|
+
case "dead":
|
|
182
|
+
// "This session is finished" is only true when it comes from the server
|
|
183
|
+
// that ISSUED the session. Pointing the CLI at a different API (a local
|
|
184
|
+
// dev server, a mistyped --url) gets the very same 401 from a server
|
|
185
|
+
// that has simply never heard of it — and acting on that is
|
|
186
|
+
// unrecoverable, because the refresh token exists nowhere else.
|
|
187
|
+
//
|
|
188
|
+
// Signing out is therefore something the CLI does only when it is sure,
|
|
189
|
+
// or something the user does explicitly with `cos logout`. A refresh
|
|
190
|
+
// token the server has already revoked grants nothing, so leaving one on
|
|
191
|
+
// disk costs nothing either.
|
|
192
|
+
// Another CLI process may have rotated the refresh token after our
|
|
193
|
+
// preflight reload but before this server answered. Re-read before
|
|
194
|
+
// treating 401 as logout; adopt a newer pair instead of deleting it.
|
|
195
|
+
this.config.reload();
|
|
196
|
+
const latest = this.config.get("auth");
|
|
197
|
+
if (latest?.refreshToken && latest.refreshToken !== auth.refreshToken) {
|
|
198
|
+
this.validateCredentialOrigin(apiUrl);
|
|
199
|
+
return latest.accessToken;
|
|
200
|
+
}
|
|
201
|
+
// An explicit logout may also have happened while refresh was in flight.
|
|
202
|
+
// Do not clear again or erase a subsequent sign-in.
|
|
203
|
+
if (!latest?.refreshToken)
|
|
204
|
+
return undefined;
|
|
205
|
+
if (sameApiOrigin(latest.apiUrl, apiUrl)) {
|
|
206
|
+
this.noteSessionSelectionChanged();
|
|
207
|
+
this.config.clearAuth();
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
throw new ApiError("SESSION_NOT_RENEWABLE", latest.apiUrl
|
|
211
|
+
? `This session was issued by ${latest.apiUrl}, but the CLI is pointed at ${apiUrl}. ` +
|
|
212
|
+
`Point it back with \`cos login --url ${latest.apiUrl}\`, or run \`cos logout\` to sign in here.`
|
|
213
|
+
: `${apiUrl} would not renew this session. If you signed out elsewhere or changed API URL, ` +
|
|
214
|
+
"run `cos logout` and sign in again.", 401);
|
|
215
|
+
case "busy": {
|
|
216
|
+
// A different process won the rotation; its tokens are about to land in
|
|
217
|
+
// the shared config. Give it a moment to write them rather than guessing.
|
|
218
|
+
const adopted = await this.awaitRotatedElsewhere(auth.refreshToken, apiUrl);
|
|
219
|
+
if (adopted)
|
|
220
|
+
return adopted;
|
|
221
|
+
throw new ApiError("SESSION_BUSY", "Your session is being renewed by another Carrier OS window. Try again in a moment.", 409);
|
|
222
|
+
}
|
|
223
|
+
case "unsupported":
|
|
224
|
+
// Retrying cannot help: this API build has no route for the session.
|
|
225
|
+
throw new ApiError("SESSION_ENDPOINT_MISSING", `${apiUrl} can't renew Carrier OS sign-in sessions — it may be running an older API version. ` +
|
|
226
|
+
"Point Carrier OS at the API that signed you in, or run `cos logout` and sign in again.", 404);
|
|
227
|
+
case "transient":
|
|
228
|
+
// The session may be perfectly healthy — never sign someone out because
|
|
229
|
+
// the API had a bad second.
|
|
230
|
+
throw new ApiError("REFRESH_UNAVAILABLE", "Couldn't renew your session right now. Try again in a moment.", outcome.status || 503);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Poll the shared config briefly for a refresh token rotated by another process. */
|
|
234
|
+
async awaitRotatedElsewhere(spentRefreshToken, apiUrl) {
|
|
235
|
+
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
236
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
237
|
+
this.config.reload();
|
|
238
|
+
const auth = this.config.get("auth");
|
|
239
|
+
this.validateCredentialOrigin(apiUrl);
|
|
240
|
+
if (auth?.refreshToken && auth.refreshToken !== spentRefreshToken && auth.accessToken) {
|
|
241
|
+
return auth.accessToken;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A client guaranteed to carry a key, or a clean exit with guidance. Use this
|
|
248
|
+
* in every command that hits an authenticated endpoint.
|
|
249
|
+
*/
|
|
250
|
+
requireAuth() {
|
|
251
|
+
if (!this.config.isAuthenticated) {
|
|
252
|
+
// Say WHY when we know: credentials that exist but cannot be opened by
|
|
253
|
+
// this Windows account (copied from another machine or user, or damaged)
|
|
254
|
+
// are a different situation from never having signed in.
|
|
255
|
+
throw new AuthRequiredError(this.config.credentialError
|
|
256
|
+
? `${this.config.credentialError} Run \`cos login\` to sign in again.`
|
|
257
|
+
: undefined);
|
|
258
|
+
}
|
|
259
|
+
return this.client();
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Active Carrier mode. Mode belongs to the CHAT TYPE, not the session:
|
|
263
|
+
*
|
|
264
|
+
* carrier → always "general". Carrier Chat runs one mode, so this is
|
|
265
|
+
* derived rather than stored — nothing chosen in Standard can
|
|
266
|
+
* leak in, and nothing chosen here can leak back out.
|
|
267
|
+
* standard → the session override, else the persisted default, else "auto"
|
|
268
|
+
* (Standard is the only place Auto routing applies).
|
|
269
|
+
*
|
|
270
|
+
* Deriving it is what keeps a Carrier→Standard switch from stranding you in
|
|
271
|
+
* General: there is no carrier mode to carry over in the first place.
|
|
272
|
+
*/
|
|
273
|
+
get mode() {
|
|
274
|
+
if (this.chatType === "carrier")
|
|
275
|
+
return "general";
|
|
276
|
+
return this.overrideMode ?? this.config.get("mode") ?? "auto";
|
|
277
|
+
}
|
|
278
|
+
/** True when the active chat type lets the user choose a mode at all. */
|
|
279
|
+
get modeIsSelectable() {
|
|
280
|
+
return this.chatType !== "carrier";
|
|
281
|
+
}
|
|
282
|
+
/** Set the mode for this process only (interactive shell), without persisting. */
|
|
283
|
+
setSessionMode(id) {
|
|
284
|
+
this.overrideMode = id;
|
|
285
|
+
}
|
|
286
|
+
/** Active chat experience: Standard by default. Persisted so an explicit
|
|
287
|
+
* choice survives across invocations; a fresh login starts in Standard. */
|
|
288
|
+
get chatType() {
|
|
289
|
+
return this.config.get("chatType") ?? "standard";
|
|
290
|
+
}
|
|
291
|
+
/** Switch chat type. Because a conversation's type is fixed at creation, this
|
|
292
|
+
* clears the active session so the NEXT message starts a fresh conversation of
|
|
293
|
+
* the new type. */
|
|
294
|
+
setChatType(type) {
|
|
295
|
+
this.config.set("chatType", type);
|
|
296
|
+
this.config.set("sessionId", undefined);
|
|
297
|
+
this.noteSessionSelectionChanged();
|
|
298
|
+
// Drop the session's mode override so the new chat type lands on its own
|
|
299
|
+
// default (carrier → General, standard → the saved mode or Auto) instead of
|
|
300
|
+
// inheriting a mode the user picked for the type they just left.
|
|
301
|
+
this.overrideMode = undefined;
|
|
302
|
+
}
|
|
303
|
+
/** Persisted web-search preference. `undefined` means "not chosen here" — the
|
|
304
|
+
* API then falls back to the workspace's admin toggle, which is what the web
|
|
305
|
+
* app does. `/websearch on|off` persists a choice that wins over that. */
|
|
306
|
+
get webSearch() {
|
|
307
|
+
return this.config.get("webSearch");
|
|
308
|
+
}
|
|
309
|
+
setWebSearch(on) {
|
|
310
|
+
this.config.set("webSearch", on);
|
|
311
|
+
}
|
|
312
|
+
/** One-shot arm from a bare `/websearch`: search the NEXT message, then go
|
|
313
|
+
* back to whatever the persisted preference was. Deliberately in-process —
|
|
314
|
+
* an armed turn that never got sent shouldn't survive into a new session. */
|
|
315
|
+
armed = false;
|
|
316
|
+
get webSearchArmed() {
|
|
317
|
+
return this.armed;
|
|
318
|
+
}
|
|
319
|
+
armWebSearch() {
|
|
320
|
+
this.armed = true;
|
|
321
|
+
}
|
|
322
|
+
disarmWebSearch() {
|
|
323
|
+
this.armed = false;
|
|
324
|
+
}
|
|
325
|
+
/** What to send as `web_search` on the next turn: forced on while armed,
|
|
326
|
+
* otherwise the persisted preference (undefined = follow the workspace). */
|
|
327
|
+
get effectiveWebSearch() {
|
|
328
|
+
return this.armed ? true : this.webSearch;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Fast mode — Llama via Groq instead of the selected model.
|
|
332
|
+
*
|
|
333
|
+
* STANDARD CHAT ONLY, mirroring the web's `isRestrictedType`: it forces
|
|
334
|
+
* fastMode off for `carrier`/`temp` conversations even when one has the flag
|
|
335
|
+
* stored. Deriving it here rather than clearing the setting means switching
|
|
336
|
+
* Carrier → Standard restores whatever the user last chose, instead of
|
|
337
|
+
* silently losing it on every round trip.
|
|
338
|
+
*/
|
|
339
|
+
get fast() {
|
|
340
|
+
if (this.chatType !== "standard")
|
|
341
|
+
return false;
|
|
342
|
+
return this.config.get("fast") === true;
|
|
343
|
+
}
|
|
344
|
+
setFast(on) {
|
|
345
|
+
this.config.set("fast", on);
|
|
346
|
+
}
|
|
347
|
+
/** High Power (HIPWR) — the 4-pass refinement pipeline. Persisted, but the
|
|
348
|
+
* server still treats it as opt-in per request and admin-gated. */
|
|
349
|
+
get highPower() {
|
|
350
|
+
return this.config.get("highPower") === true;
|
|
351
|
+
}
|
|
352
|
+
setHighPower(on) {
|
|
353
|
+
this.config.set("highPower", on);
|
|
354
|
+
}
|
|
355
|
+
/** SIA clarification — ask before sending, rather than guessing at an
|
|
356
|
+
* ambiguous prompt. Off by default, like the web composer's toggle. */
|
|
357
|
+
get siaMode() {
|
|
358
|
+
// STANDARD CHAT ONLY, derived the same way `fast` is: Carrier Chat runs its
|
|
359
|
+
// own behaviour layer and the web shows no SIA control there. Deriving
|
|
360
|
+
// rather than clearing means a Carrier -> Standard switch restores whatever
|
|
361
|
+
// the user last chose instead of silently losing it.
|
|
362
|
+
if (this.chatType !== "standard")
|
|
363
|
+
return "off";
|
|
364
|
+
const stored = this.config.get("sia");
|
|
365
|
+
// Legacy booleans from before the mode existed: true meant "consult the
|
|
366
|
+
// backend", which is exactly what auto does now.
|
|
367
|
+
if (stored === true)
|
|
368
|
+
return "auto";
|
|
369
|
+
if (stored === false)
|
|
370
|
+
return "off";
|
|
371
|
+
// Unset is OFF, as the web composer's toggle is. Defaulting to auto put
|
|
372
|
+
// clarifying questions in front of every coding task ("should the subtotal
|
|
373
|
+
// include discounts?") before the agent had even read the repository.
|
|
374
|
+
return stored === "auto" ? "auto" : "off";
|
|
375
|
+
}
|
|
376
|
+
/** True when a turn should ask the backend whether clarification is needed. */
|
|
377
|
+
get sia() {
|
|
378
|
+
return this.siaMode === "auto";
|
|
379
|
+
}
|
|
380
|
+
setSiaMode(mode) {
|
|
381
|
+
this.config.set("sia", mode);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* True while a main chat turn is rendering in the background.
|
|
385
|
+
*
|
|
386
|
+
* Runtime-only (never persisted): the shell sets it when it detaches a turn
|
|
387
|
+
* so commands that share the terminal — `/btw` — can tell who owns the
|
|
388
|
+
* transient region.
|
|
389
|
+
*/
|
|
390
|
+
mainTurnRunning = false;
|
|
391
|
+
/** Read-only Admin Panel Carrier Chat switch, refreshed by the shell. Null
|
|
392
|
+
* means it could not yet be verified; the send gate checks again. */
|
|
393
|
+
carrierMaintenance = null;
|
|
394
|
+
/**
|
|
395
|
+
* Credits this shell session's turns were actually debited, summed from the
|
|
396
|
+
* server's per-turn `credits_spent`. Runtime-only: "this session" means this
|
|
397
|
+
* process. Background context maintenance is billed separately on the server
|
|
398
|
+
* and is not a turn, so it is not in here — `/usage` reports it.
|
|
399
|
+
*/
|
|
400
|
+
sessionCreditsSpent = 0;
|
|
401
|
+
/** Whether any turn this session reported its spend (older APIs do not). */
|
|
402
|
+
sessionSpendKnown = false;
|
|
403
|
+
/** Citations from the most recent turn that returned any, so /sources and
|
|
404
|
+
* /open can act on them after the answer has scrolled away. */
|
|
405
|
+
lastSearch = null;
|
|
406
|
+
recordSearch(sources) {
|
|
407
|
+
if (sources.length > 0)
|
|
408
|
+
this.lastSearch = { sources, at: Date.now() };
|
|
409
|
+
}
|
|
410
|
+
get lastSources() {
|
|
411
|
+
return this.lastSearch?.sources ?? [];
|
|
412
|
+
}
|
|
413
|
+
get lastSearchAt() {
|
|
414
|
+
return this.lastSearch?.at ?? null;
|
|
415
|
+
}
|
|
416
|
+
}
|