@cortexkit/opencode-antigravity-auth 3.0.40 → 3.0.42
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/dist/cli.js +1277 -1073
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +122 -112
- package/dist/index.js +7472 -4214
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +1494 -509
- package/dist/server.js +5990 -2678
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/dist/src/common-auth-embedded/commands/builtins.js +28 -12
- package/dist/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/dist/src/common-auth-embedded/commands/menu.js +1 -0
- package/dist/src/common-auth-embedded/fs/file-read-cache.js +9 -5
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +159 -11
- package/dist/src/common-auth-embedded/fs/with-lock.d.ts +2 -0
- package/dist/src/common-auth-embedded/fs/with-lock.js +1 -1
- package/dist/src/common-auth-embedded/routing-accessor/index.d.ts +33 -5
- package/dist/src/common-auth-embedded/routing-accessor/index.js +49 -7
- package/dist/src/common-auth-embedded/settings/define.d.ts +35 -0
- package/dist/src/common-auth-embedded/settings/define.js +52 -0
- package/dist/src/common-auth-embedded/settings/descriptors.d.ts +97 -0
- package/dist/src/common-auth-embedded/settings/descriptors.js +81 -0
- package/dist/src/common-auth-embedded/settings/edit.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/edit.js +229 -0
- package/dist/src/common-auth-embedded/settings/fs.js +19 -0
- package/dist/src/common-auth-embedded/settings/handle.d.ts +30 -0
- package/dist/src/common-auth-embedded/settings/handle.js +228 -0
- package/dist/src/common-auth-embedded/settings/index.d.ts +10 -0
- package/dist/src/common-auth-embedded/settings/index.js +7 -0
- package/dist/src/common-auth-embedded/settings/lock-errors.js +15 -0
- package/dist/src/common-auth-embedded/settings/move.d.ts +76 -0
- package/dist/src/common-auth-embedded/settings/move.js +351 -0
- package/dist/src/common-auth-embedded/settings/parse.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/parse.js +65 -0
- package/dist/src/common-auth-embedded/settings/paths.d.ts +12 -0
- package/dist/src/common-auth-embedded/settings/paths.js +48 -0
- package/dist/src/common-auth-embedded/settings/refusal.d.ts +7 -0
- package/dist/src/common-auth-embedded/settings/refusal.js +15 -0
- package/dist/src/common-auth-embedded/settings/resolve.d.ts +15 -0
- package/dist/src/common-auth-embedded/settings/resolve.js +51 -0
- package/dist/src/common-auth-embedded/settings/schema.d.ts +29 -0
- package/dist/src/common-auth-embedded/settings/schema.js +81 -0
- package/dist/src/common-auth-embedded/settings/snapshot.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/snapshot.js +150 -0
- package/dist/src/common-auth-embedded/settings/update.js +139 -0
- package/dist/src/common-auth-embedded/settings/write.js +88 -0
- package/dist/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/dist/src/common-auth-embedded/setup/command-error.js +3 -2
- package/dist/src/common-auth-embedded/setup/command.js +5 -1
- package/dist/src/common-auth-embedded/setup/enroll.js +3 -3
- package/dist/src/common-auth-embedded/setup/enrollment-access.js +13 -4
- package/dist/src/common-auth-embedded/setup/types.d.ts +4 -0
- package/dist/src/common-auth-embedded/source-output.json +297 -61
- package/dist/src/common-auth-embedded/tui-prefs/watcher.js +196 -0
- package/dist/src/ga/tui/host-ga.js +1 -1
- package/dist/src/ga/tui/host-ga.js.map +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
- package/dist/src/plugin/command-apply.d.ts +43 -3
- package/dist/src/plugin/command-apply.d.ts.map +1 -1
- package/dist/src/plugin/command-apply.js +92 -12
- package/dist/src/plugin/command-apply.js.map +1 -1
- package/dist/src/plugin/commands.d.ts +5 -1
- package/dist/src/plugin/commands.d.ts.map +1 -1
- package/dist/src/plugin/commands.js +4 -3
- package/dist/src/plugin/commands.js.map +1 -1
- package/dist/src/plugin/config/writer.d.ts +10 -3
- package/dist/src/plugin/config/writer.d.ts.map +1 -1
- package/dist/src/plugin/config/writer.js +30 -22
- package/dist/src/plugin/config/writer.js.map +1 -1
- package/dist/src/plugin/fetch-interceptor.d.ts +8 -3
- package/dist/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/src/plugin/fetch-interceptor.js +1 -0
- package/dist/src/plugin/fetch-interceptor.js.map +1 -1
- package/dist/src/plugin/ga-location-services.d.ts +3 -1
- package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/src/plugin/ga-location-services.js +35 -5
- package/dist/src/plugin/ga-location-services.js.map +1 -1
- package/dist/src/plugin/index.d.ts.map +1 -1
- package/dist/src/plugin/index.js +65 -15
- package/dist/src/plugin/index.js.map +1 -1
- package/dist/src/plugin/killswitch.d.ts +20 -0
- package/dist/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/src/plugin/killswitch.js +14 -4
- package/dist/src/plugin/killswitch.js.map +1 -1
- package/dist/src/plugin/operator-settings.d.ts +6 -0
- package/dist/src/plugin/operator-settings.d.ts.map +1 -1
- package/dist/src/plugin/operator-settings.js.map +1 -1
- package/dist/src/plugin/request-helpers.d.ts +15 -0
- package/dist/src/plugin/request-helpers.d.ts.map +1 -1
- package/dist/src/plugin/request-helpers.js +17 -2
- package/dist/src/plugin/request-helpers.js.map +1 -1
- package/dist/src/plugin/shared/request-services.d.ts +18 -2
- package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/src/plugin/shared/request-services.js +130 -13
- package/dist/src/plugin/shared/request-services.js.map +1 -1
- package/dist/src/plugin/shared/runtime.d.ts +15 -0
- package/dist/src/plugin/shared/runtime.d.ts.map +1 -1
- package/dist/src/plugin/shared/runtime.js +43 -11
- package/dist/src/plugin/shared/runtime.js.map +1 -1
- package/dist/src/plugin/shared/vault-menu.d.ts +11 -1
- package/dist/src/plugin/shared/vault-menu.d.ts.map +1 -1
- package/dist/src/plugin/shared/vault-menu.js +33 -7
- package/dist/src/plugin/shared/vault-menu.js.map +1 -1
- package/dist/src/settings/core-settings.d.ts +7 -0
- package/dist/src/settings/core-settings.d.ts.map +1 -0
- package/dist/src/settings/core-settings.js +7 -0
- package/dist/src/settings/core-settings.js.map +1 -0
- package/dist/src/settings/legacy.d.ts +89 -0
- package/dist/src/settings/legacy.d.ts.map +1 -0
- package/dist/src/settings/legacy.js +184 -0
- package/dist/src/settings/legacy.js.map +1 -0
- package/dist/src/settings/location.d.ts +22 -0
- package/dist/src/settings/location.d.ts.map +1 -0
- package/dist/src/settings/location.js +49 -0
- package/dist/src/settings/location.js.map +1 -0
- package/dist/src/settings/move.d.ts +38 -0
- package/dist/src/settings/move.d.ts.map +1 -0
- package/dist/src/settings/move.js +83 -0
- package/dist/src/settings/move.js.map +1 -0
- package/dist/src/settings/protection.d.ts +11 -0
- package/dist/src/settings/protection.d.ts.map +1 -0
- package/dist/src/settings/protection.js +17 -0
- package/dist/src/settings/protection.js.map +1 -0
- package/dist/src/settings/report.d.ts +33 -0
- package/dist/src/settings/report.d.ts.map +1 -0
- package/dist/src/settings/report.js +103 -0
- package/dist/src/settings/report.js.map +1 -0
- package/dist/src/settings/runtime.d.ts +145 -0
- package/dist/src/settings/runtime.d.ts.map +1 -0
- package/dist/src/settings/runtime.js +697 -0
- package/dist/src/settings/runtime.js.map +1 -0
- package/dist/tui-build-map.json +1 -1
- package/dist/tui-ga-build-map.json +5 -5
- package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/dist/types/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/dist/types/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/dist/types/src/common-auth-embedded/routing-accessor/index.d.ts +33 -5
- package/dist/types/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/setup/types.d.ts +4 -0
- package/dist/types/src/plugin/command-apply.d.ts +43 -3
- package/dist/types/src/plugin/command-apply.d.ts.map +1 -1
- package/dist/types/src/plugin/commands.d.ts +5 -1
- package/dist/types/src/plugin/commands.d.ts.map +1 -1
- package/dist/types/src/plugin/config/writer.d.ts +10 -3
- package/dist/types/src/plugin/config/writer.d.ts.map +1 -1
- package/dist/types/src/plugin/fetch-interceptor.d.ts +8 -3
- package/dist/types/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/types/src/plugin/ga-location-services.d.ts +3 -1
- package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/killswitch.d.ts +20 -0
- package/dist/types/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/types/src/plugin/operator-settings.d.ts +6 -0
- package/dist/types/src/plugin/operator-settings.d.ts.map +1 -1
- package/dist/types/src/plugin/request-helpers.d.ts +15 -0
- package/dist/types/src/plugin/request-helpers.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/request-services.d.ts +18 -2
- package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/runtime.d.ts +15 -0
- package/dist/types/src/plugin/shared/runtime.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/vault-menu.d.ts +11 -1
- package/dist/types/src/plugin/shared/vault-menu.d.ts.map +1 -1
- package/dist/types/src/settings/core-settings.d.ts +7 -0
- package/dist/types/src/settings/core-settings.d.ts.map +1 -0
- package/dist/types/src/settings/legacy.d.ts +89 -0
- package/dist/types/src/settings/legacy.d.ts.map +1 -0
- package/dist/types/src/settings/location.d.ts +22 -0
- package/dist/types/src/settings/location.d.ts.map +1 -0
- package/dist/types/src/settings/move.d.ts +38 -0
- package/dist/types/src/settings/move.d.ts.map +1 -0
- package/dist/types/src/settings/protection.d.ts +11 -0
- package/dist/types/src/settings/protection.d.ts.map +1 -0
- package/dist/types/src/settings/report.d.ts +33 -0
- package/dist/types/src/settings/report.d.ts.map +1 -0
- package/dist/types/src/settings/runtime.d.ts +145 -0
- package/dist/types/src/settings/runtime.d.ts.map +1 -0
- package/package.json +3 -2
- package/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/src/common-auth-embedded/commands/builtins.js +28 -12
- package/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/src/common-auth-embedded/commands/menu.js +1 -0
- package/src/common-auth-embedded/fs/file-read-cache.js +9 -5
- package/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/src/common-auth-embedded/fs/refresh-file-lock.js +159 -11
- package/src/common-auth-embedded/fs/with-lock.d.ts +2 -0
- package/src/common-auth-embedded/fs/with-lock.js +1 -1
- package/src/common-auth-embedded/routing-accessor/index.d.ts +33 -5
- package/src/common-auth-embedded/routing-accessor/index.js +49 -7
- package/src/common-auth-embedded/settings/define.d.ts +35 -0
- package/src/common-auth-embedded/settings/define.js +52 -0
- package/src/common-auth-embedded/settings/descriptors.d.ts +97 -0
- package/src/common-auth-embedded/settings/descriptors.js +81 -0
- package/src/common-auth-embedded/settings/edit.d.ts +24 -0
- package/src/common-auth-embedded/settings/edit.js +229 -0
- package/src/common-auth-embedded/settings/fs.js +19 -0
- package/src/common-auth-embedded/settings/handle.d.ts +30 -0
- package/src/common-auth-embedded/settings/handle.js +228 -0
- package/src/common-auth-embedded/settings/index.d.ts +10 -0
- package/src/common-auth-embedded/settings/index.js +7 -0
- package/src/common-auth-embedded/settings/lock-errors.js +15 -0
- package/src/common-auth-embedded/settings/move.d.ts +76 -0
- package/src/common-auth-embedded/settings/move.js +351 -0
- package/src/common-auth-embedded/settings/parse.d.ts +24 -0
- package/src/common-auth-embedded/settings/parse.js +65 -0
- package/src/common-auth-embedded/settings/paths.d.ts +12 -0
- package/src/common-auth-embedded/settings/paths.js +48 -0
- package/src/common-auth-embedded/settings/refusal.d.ts +7 -0
- package/src/common-auth-embedded/settings/refusal.js +15 -0
- package/src/common-auth-embedded/settings/resolve.d.ts +15 -0
- package/src/common-auth-embedded/settings/resolve.js +51 -0
- package/src/common-auth-embedded/settings/schema.d.ts +29 -0
- package/src/common-auth-embedded/settings/schema.js +81 -0
- package/src/common-auth-embedded/settings/snapshot.d.ts +24 -0
- package/src/common-auth-embedded/settings/snapshot.js +150 -0
- package/src/common-auth-embedded/settings/update.js +139 -0
- package/src/common-auth-embedded/settings/write.js +88 -0
- package/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/src/common-auth-embedded/setup/command-error.js +3 -2
- package/src/common-auth-embedded/setup/command.js +5 -1
- package/src/common-auth-embedded/setup/enroll.js +3 -3
- package/src/common-auth-embedded/setup/enrollment-access.js +13 -4
- package/src/common-auth-embedded/setup/types.d.ts +4 -0
- package/src/common-auth-embedded/source-output.json +297 -61
- package/src/common-auth-embedded/tui-prefs/watcher.js +196 -0
- package/src/tui-ga-compiled/host-ga.js +1 -1
- package/src/tui-ga-raw/host-ga.tsx +1 -1
|
@@ -12,14 +12,16 @@ export interface RoutingCandidate {
|
|
|
12
12
|
accountId: string;
|
|
13
13
|
credentialId: string;
|
|
14
14
|
}
|
|
15
|
-
export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate> {
|
|
15
|
+
export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate, Refusals extends boolean = false> {
|
|
16
16
|
sessionId: string;
|
|
17
17
|
provider: string;
|
|
18
18
|
modelId: string;
|
|
19
19
|
/** Only routes already eligible under the plugin's vault and killswitch rules. */
|
|
20
20
|
candidates: readonly Candidate[];
|
|
21
|
-
/** Defaults to
|
|
21
|
+
/** Defaults to 1500 ms, above routing's normal tail; clamped to 1–2000 ms. */
|
|
22
22
|
deadlineMs?: number;
|
|
23
|
+
/** Opt in to refusals, which stop the request instead of using a local choice. */
|
|
24
|
+
refusals?: Refusals;
|
|
23
25
|
/**
|
|
24
26
|
* Longest same-account hold, in seconds, that routing may advise. The
|
|
25
27
|
* holdWaitMaxS helper from `./limits` caps it by the setting and the current
|
|
@@ -38,6 +40,14 @@ export interface RoutingSelection<Candidate extends RoutingCandidate = RoutingCa
|
|
|
38
40
|
candidate: Candidate;
|
|
39
41
|
hold?: RoutingHold;
|
|
40
42
|
}
|
|
43
|
+
export type RoutingRefusal = {
|
|
44
|
+
refused: {
|
|
45
|
+
code: 'account_limit_exhausted' | 'account_selection_refused' | 'account_selection_final';
|
|
46
|
+
routingCode?: string;
|
|
47
|
+
retryAfterSecs?: number;
|
|
48
|
+
resetsAtMs?: number;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
41
51
|
interface RoutingReportIdentity {
|
|
42
52
|
sessionId: string;
|
|
43
53
|
accountId: string;
|
|
@@ -77,6 +87,23 @@ export type ReportRoutingInput = (RoutingReportIdentity & ({
|
|
|
77
87
|
pool?: string;
|
|
78
88
|
/** Optional reset time in epoch milliseconds. */
|
|
79
89
|
resetsAt?: number;
|
|
90
|
+
} | {
|
|
91
|
+
/**
|
|
92
|
+
* A provider's typed refusal that condemns the API key until it is
|
|
93
|
+
* topped up, such as an exhausted prepaid balance. Never use for
|
|
94
|
+
* 401, 403 or short rate limits.
|
|
95
|
+
*/
|
|
96
|
+
outcome: 'credential_refused';
|
|
97
|
+
credentialId: string;
|
|
98
|
+
/**
|
|
99
|
+
* What the provider actually sent: its own code from the structured
|
|
100
|
+
* error (OpenRouter's `openrouter_credits`). When the body carries
|
|
101
|
+
* no code, `http_<status>`, but only for a status the provider
|
|
102
|
+
* documents with a single balance meaning (DeepSeek's `http_402`).
|
|
103
|
+
* Never message text, and never a status with several meanings such
|
|
104
|
+
* as 403 or 429. Must match `^[a-z0-9._-]{1,64}$`.
|
|
105
|
+
*/
|
|
106
|
+
providerCode: string;
|
|
80
107
|
})) | {
|
|
81
108
|
sessionId: string;
|
|
82
109
|
kind: 'turn_notes_supported';
|
|
@@ -128,10 +155,11 @@ export declare function turnNotes(sessionId: string): readonly {
|
|
|
128
155
|
}[];
|
|
129
156
|
/**
|
|
130
157
|
* Ask routing for an eligible vault route. Any failure or unfamiliar answer
|
|
131
|
-
* yields undefined so the plugin can keep its own choice.
|
|
132
|
-
*
|
|
158
|
+
* yields undefined so the plugin can keep its own choice. Opted-in routing
|
|
159
|
+
* refusals instead stop the request. This does not admit routes: callers must
|
|
160
|
+
* exclude vault refusals and killswitch-blocked accounts.
|
|
133
161
|
*/
|
|
134
|
-
export declare function askRouting<Candidate extends RoutingCandidate>(input: AskRoutingInput<Candidate>): Promise<RoutingSelection<Candidate> | undefined>;
|
|
162
|
+
export declare function askRouting<Candidate extends RoutingCandidate, Refusals extends boolean = false>(input: AskRoutingInput<Candidate, Refusals>): Promise<RoutingSelection<Candidate> | (Refusals extends true ? RoutingRefusal : never) | undefined>;
|
|
135
163
|
/**
|
|
136
164
|
* Report a vault request, billing result or turn-note event without waiting for
|
|
137
165
|
* routing. Only contract fields are sent, and all accessor failures are ignored.
|
|
@@ -136,11 +136,35 @@ function selectionFor(answer, candidates) {
|
|
|
136
136
|
}
|
|
137
137
|
return { candidate };
|
|
138
138
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
function refusalFor(value) {
|
|
140
|
+
if (!isObject(value))
|
|
141
|
+
return undefined;
|
|
142
|
+
const code = value.code;
|
|
143
|
+
if (code !== 'account_limit_exhausted' &&
|
|
144
|
+
code !== 'account_selection_refused' &&
|
|
145
|
+
code !== 'account_selection_final') {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
const refused = { code };
|
|
149
|
+
const routingCode = value.routing_code;
|
|
150
|
+
if (typeof routingCode === 'string' &&
|
|
151
|
+
/^[a-z0-9._-]{1,64}$/.test(routingCode)) {
|
|
152
|
+
refused.routingCode = routingCode;
|
|
153
|
+
}
|
|
154
|
+
const retryAfterSecs = value.retry_after_secs;
|
|
155
|
+
if (typeof retryAfterSecs === 'number' &&
|
|
156
|
+
Number.isFinite(retryAfterSecs) &&
|
|
157
|
+
retryAfterSecs >= 0) {
|
|
158
|
+
refused.retryAfterSecs = retryAfterSecs;
|
|
159
|
+
}
|
|
160
|
+
const resetsAtMs = value.resets_at_ms;
|
|
161
|
+
if (typeof resetsAtMs === 'number' &&
|
|
162
|
+
Number.isSafeInteger(resetsAtMs) &&
|
|
163
|
+
resetsAtMs > 0) {
|
|
164
|
+
refused.resetsAtMs = resetsAtMs;
|
|
165
|
+
}
|
|
166
|
+
return { refused };
|
|
167
|
+
}
|
|
144
168
|
export async function askRouting(input) {
|
|
145
169
|
let timer;
|
|
146
170
|
try {
|
|
@@ -150,9 +174,9 @@ export async function askRouting(input) {
|
|
|
150
174
|
const accountFor = handle.accountFor;
|
|
151
175
|
if (typeof accountFor !== 'function')
|
|
152
176
|
return undefined;
|
|
153
|
-
const requested = input.deadlineMs ??
|
|
177
|
+
const requested = input.deadlineMs ?? 1500;
|
|
154
178
|
const deadlineMs = Number.isNaN(requested)
|
|
155
|
-
?
|
|
179
|
+
? 1500
|
|
156
180
|
: Math.min(2000, Math.max(1, requested));
|
|
157
181
|
const expired = new Promise((resolve) => {
|
|
158
182
|
timer = setTimeout(() => resolve(undefined), deadlineMs);
|
|
@@ -166,10 +190,15 @@ export async function askRouting(input) {
|
|
|
166
190
|
if (typeof cap === 'number' && Number.isFinite(cap)) {
|
|
167
191
|
request.hold_wait_max_s = Math.max(0, Math.floor(cap));
|
|
168
192
|
}
|
|
193
|
+
if (input.refusals === true)
|
|
194
|
+
request.refusals = true;
|
|
169
195
|
const pending = accountFor.call(handle, request, { deadlineMs });
|
|
170
196
|
if (!isObject(pending) || typeof pending.then !== 'function')
|
|
171
197
|
return undefined;
|
|
172
198
|
const answer = await Promise.race([pending, expired]);
|
|
199
|
+
if (input.refusals === true && isObject(answer) && 'refused' in answer) {
|
|
200
|
+
return refusalFor(answer.refused);
|
|
201
|
+
}
|
|
173
202
|
return selectionFor(answer, input.candidates);
|
|
174
203
|
}
|
|
175
204
|
catch {
|
|
@@ -334,6 +363,19 @@ export function reportRouting(input) {
|
|
|
334
363
|
};
|
|
335
364
|
break;
|
|
336
365
|
}
|
|
366
|
+
case 'credential_refused': {
|
|
367
|
+
const { credentialId, providerCode } = input;
|
|
368
|
+
if (!isNonEmptyString(credentialId) ||
|
|
369
|
+
typeof providerCode !== 'string' ||
|
|
370
|
+
!/^[a-z0-9._-]{1,64}$/.test(providerCode))
|
|
371
|
+
return;
|
|
372
|
+
payload = {
|
|
373
|
+
...identity,
|
|
374
|
+
outcome: 'credential_refused',
|
|
375
|
+
provider_code: providerCode,
|
|
376
|
+
};
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
337
379
|
default:
|
|
338
380
|
return;
|
|
339
381
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ValueDescriptor, type ValueOf, type ValueValidationOptions } from './descriptors.js';
|
|
2
|
+
export interface SettingDeclaration<D extends ValueDescriptor = ValueDescriptor> {
|
|
3
|
+
readonly value: D;
|
|
4
|
+
readonly default: ValueDescriptor extends D ? unknown : ValueOf<D>;
|
|
5
|
+
readonly hosts: 'both' | 'opencode' | 'pi' | string;
|
|
6
|
+
readonly overrides?: {
|
|
7
|
+
readonly host?: true;
|
|
8
|
+
readonly model?: true;
|
|
9
|
+
};
|
|
10
|
+
readonly project?: 'user-only' | 'allowed';
|
|
11
|
+
readonly legacy?: readonly {
|
|
12
|
+
readonly source: string;
|
|
13
|
+
readonly path: readonly string[];
|
|
14
|
+
}[];
|
|
15
|
+
}
|
|
16
|
+
type Declarations = Readonly<Record<string, SettingDeclaration>>;
|
|
17
|
+
export interface SettingsDefinition<S extends Declarations = Declarations> {
|
|
18
|
+
readonly plugin: string;
|
|
19
|
+
readonly settings: {
|
|
20
|
+
readonly [K in keyof S]: S[K] & {
|
|
21
|
+
readonly project: S[K] extends {
|
|
22
|
+
readonly project: infer P;
|
|
23
|
+
} ? P : 'project' extends keyof S[K] ? Exclude<S[K]['project'], undefined> | 'user-only' : 'user-only';
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export declare function defineSettings<const S extends Declarations>(definition: {
|
|
28
|
+
readonly plugin: string;
|
|
29
|
+
readonly settings: S;
|
|
30
|
+
}): SettingsDefinition<S>;
|
|
31
|
+
/** Internal helper: host names are reserved and never count as model ids. */
|
|
32
|
+
export declare function isOverrideKeyAllowed(overrides: NonNullable<SettingDeclaration['overrides']>, key: string): boolean;
|
|
33
|
+
/** Validate a file, update or map value, including any permitted override object. */
|
|
34
|
+
export declare function validateSettingValue(declaration: Pick<SettingDeclaration, 'value' | 'overrides'>, value: unknown, options?: ValueValidationOptions): boolean;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { deepFreeze, isJsonObject, validateValue, } from './descriptors.js';
|
|
2
|
+
export function defineSettings(definition) {
|
|
3
|
+
for (const [name, declaration] of Object.entries(definition.settings)) {
|
|
4
|
+
if (name === 'schema_version' || name === '$schema') {
|
|
5
|
+
throw new TypeError(`Reserved setting name: ${name}`);
|
|
6
|
+
}
|
|
7
|
+
if (!/^[a-z][a-z0-9_]*$/.test(name) || /\s/.test(name)) {
|
|
8
|
+
throw new TypeError(`Setting name must be snake_case: ${name}`);
|
|
9
|
+
}
|
|
10
|
+
if (name === 'sidebar' && declaration.value.kind !== 'object') {
|
|
11
|
+
throw new TypeError('The sidebar setting must use an object descriptor');
|
|
12
|
+
}
|
|
13
|
+
if (declaration.overrides !== undefined &&
|
|
14
|
+
(declaration.value.kind === 'object' ||
|
|
15
|
+
declaration.value.kind === 'record')) {
|
|
16
|
+
throw new TypeError(`Object and record settings cannot use overrides: ${name}`);
|
|
17
|
+
}
|
|
18
|
+
if (!Object.hasOwn(declaration, 'default') ||
|
|
19
|
+
!validateValue(declaration.value, declaration.default, {
|
|
20
|
+
completeObjects: true,
|
|
21
|
+
})) {
|
|
22
|
+
throw new TypeError(`Invalid default for setting: ${name}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Copy before freezing so the caller can still use its original default values.
|
|
26
|
+
return deepFreeze({
|
|
27
|
+
plugin: definition.plugin,
|
|
28
|
+
settings: Object.fromEntries(Object.entries(definition.settings).map(([name, declaration]) => [
|
|
29
|
+
name,
|
|
30
|
+
structuredClone({
|
|
31
|
+
...declaration,
|
|
32
|
+
project: declaration.project ?? 'user-only',
|
|
33
|
+
}),
|
|
34
|
+
])),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** Internal helper: host names are reserved and never count as model ids. */
|
|
38
|
+
export function isOverrideKeyAllowed(overrides, key) {
|
|
39
|
+
if (key === 'default')
|
|
40
|
+
return true;
|
|
41
|
+
if (key === 'opencode' || key === 'pi')
|
|
42
|
+
return overrides.host === true;
|
|
43
|
+
return overrides.model === true;
|
|
44
|
+
}
|
|
45
|
+
/** Validate a file, update or map value, including any permitted override object. */
|
|
46
|
+
export function validateSettingValue(declaration, value, options = {}) {
|
|
47
|
+
if (declaration.overrides !== undefined && isJsonObject(value)) {
|
|
48
|
+
return Object.entries(value).every(([key, entry]) => isOverrideKeyAllowed(declaration.overrides ?? {}, key) &&
|
|
49
|
+
validateValue(declaration.value, entry, options));
|
|
50
|
+
}
|
|
51
|
+
return validateValue(declaration.value, value, options);
|
|
52
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export type ValueDescriptor = {
|
|
2
|
+
readonly kind: 'boolean';
|
|
3
|
+
} | {
|
|
4
|
+
readonly kind: 'integer';
|
|
5
|
+
readonly min?: number;
|
|
6
|
+
readonly max?: number;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: 'number';
|
|
9
|
+
readonly min?: number;
|
|
10
|
+
readonly max?: number;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: 'string';
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: 'oneOf';
|
|
15
|
+
readonly values: readonly string[];
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: 'list';
|
|
18
|
+
readonly item: ValueDescriptor;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: 'object';
|
|
21
|
+
readonly fields: Readonly<Record<string, ValueDescriptor>>;
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: 'record';
|
|
24
|
+
readonly value: ValueDescriptor;
|
|
25
|
+
};
|
|
26
|
+
type SettingsValue = boolean | number | string | SettingsValue[] | {
|
|
27
|
+
[key: string]: SettingsValue;
|
|
28
|
+
};
|
|
29
|
+
export type ValueOf<D extends ValueDescriptor> = ValueDescriptor extends D ? SettingsValue : D extends {
|
|
30
|
+
kind: 'boolean';
|
|
31
|
+
} ? boolean : D extends {
|
|
32
|
+
kind: 'integer' | 'number';
|
|
33
|
+
} ? number : D extends {
|
|
34
|
+
kind: 'string';
|
|
35
|
+
} ? string : D extends {
|
|
36
|
+
kind: 'oneOf';
|
|
37
|
+
values: infer V extends readonly string[];
|
|
38
|
+
} ? V[number] : D extends {
|
|
39
|
+
kind: 'list';
|
|
40
|
+
item: infer I extends ValueDescriptor;
|
|
41
|
+
} ? ValueOf<I>[] : D extends {
|
|
42
|
+
kind: 'object';
|
|
43
|
+
fields: infer F extends Readonly<Record<string, ValueDescriptor>>;
|
|
44
|
+
} ? {
|
|
45
|
+
-readonly [K in keyof F]: ValueOf<F[K]>;
|
|
46
|
+
} : D extends {
|
|
47
|
+
kind: 'record';
|
|
48
|
+
value: infer V extends ValueDescriptor;
|
|
49
|
+
} ? Record<string, ValueOf<V>> : never;
|
|
50
|
+
type Bounds = {
|
|
51
|
+
readonly min?: number;
|
|
52
|
+
readonly max?: number;
|
|
53
|
+
};
|
|
54
|
+
export declare function boolean(): Readonly<{
|
|
55
|
+
readonly kind: 'boolean';
|
|
56
|
+
}>;
|
|
57
|
+
export declare function integer({ min, max }?: Bounds): Readonly<{
|
|
58
|
+
readonly kind: 'integer';
|
|
59
|
+
readonly min: number | undefined;
|
|
60
|
+
readonly max: number | undefined;
|
|
61
|
+
}>;
|
|
62
|
+
export declare function number({ min, max }?: Bounds): Readonly<{
|
|
63
|
+
readonly kind: 'number';
|
|
64
|
+
readonly min: number | undefined;
|
|
65
|
+
readonly max: number | undefined;
|
|
66
|
+
}>;
|
|
67
|
+
export declare function string(): Readonly<{
|
|
68
|
+
readonly kind: 'string';
|
|
69
|
+
}>;
|
|
70
|
+
export declare function oneOf<const V extends readonly string[]>(values: V): Readonly<{
|
|
71
|
+
readonly kind: 'oneOf';
|
|
72
|
+
readonly values: Readonly<V>;
|
|
73
|
+
}>;
|
|
74
|
+
export declare function list<const D extends ValueDescriptor>(item: D): {
|
|
75
|
+
readonly kind: 'list';
|
|
76
|
+
readonly item: D;
|
|
77
|
+
};
|
|
78
|
+
export declare function object<const F extends Readonly<Record<string, ValueDescriptor>>>(fields: F): {
|
|
79
|
+
readonly kind: 'object';
|
|
80
|
+
readonly fields: F;
|
|
81
|
+
};
|
|
82
|
+
export declare function record<const D extends ValueDescriptor>(value: D): {
|
|
83
|
+
readonly kind: 'record';
|
|
84
|
+
readonly value: D;
|
|
85
|
+
};
|
|
86
|
+
/** Internal validation policy shared by file readers, updates and migration maps. */
|
|
87
|
+
export interface ValueValidationOptions {
|
|
88
|
+
readonly completeObjects?: boolean;
|
|
89
|
+
readonly allowUnknownFields?: boolean;
|
|
90
|
+
}
|
|
91
|
+
/** JSON objects exclude arrays and objects such as Date that serialize differently. */
|
|
92
|
+
export declare function isJsonObject(value: unknown): value is Record<string, unknown>;
|
|
93
|
+
/** Missing fields can be filled from lower levels, but list and record objects must be complete. */
|
|
94
|
+
export declare function validateValue(descriptor: ValueDescriptor, value: unknown, options?: ValueValidationOptions): boolean;
|
|
95
|
+
/** Internal helper; definitions and descriptor payloads are immutable at runtime. */
|
|
96
|
+
export declare function deepFreeze<T>(value: T): T;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export function boolean() {
|
|
2
|
+
return Object.freeze({ kind: 'boolean' });
|
|
3
|
+
}
|
|
4
|
+
export function integer({ min, max } = {}) {
|
|
5
|
+
return Object.freeze({ kind: 'integer', min, max });
|
|
6
|
+
}
|
|
7
|
+
export function number({ min, max } = {}) {
|
|
8
|
+
return Object.freeze({ kind: 'number', min, max });
|
|
9
|
+
}
|
|
10
|
+
export function string() {
|
|
11
|
+
return Object.freeze({ kind: 'string' });
|
|
12
|
+
}
|
|
13
|
+
export function oneOf(values) {
|
|
14
|
+
return Object.freeze({
|
|
15
|
+
kind: 'oneOf',
|
|
16
|
+
values: Object.freeze([...values]),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function list(item) {
|
|
20
|
+
return deepFreeze({ kind: 'list', item });
|
|
21
|
+
}
|
|
22
|
+
export function object(fields) {
|
|
23
|
+
return deepFreeze({ kind: 'object', fields: { ...fields } });
|
|
24
|
+
}
|
|
25
|
+
export function record(value) {
|
|
26
|
+
return deepFreeze({ kind: 'record', value });
|
|
27
|
+
}
|
|
28
|
+
/** JSON objects exclude arrays and objects such as Date that serialize differently. */
|
|
29
|
+
export function isJsonObject(value) {
|
|
30
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const prototype = Object.getPrototypeOf(value);
|
|
34
|
+
return prototype === Object.prototype || prototype === null;
|
|
35
|
+
}
|
|
36
|
+
/** Missing fields can be filled from lower levels, but list and record objects must be complete. */
|
|
37
|
+
export function validateValue(descriptor, value, options = {}) {
|
|
38
|
+
switch (descriptor.kind) {
|
|
39
|
+
case 'boolean':
|
|
40
|
+
return typeof value === 'boolean';
|
|
41
|
+
case 'integer':
|
|
42
|
+
case 'number':
|
|
43
|
+
return (typeof value === 'number' &&
|
|
44
|
+
Number.isFinite(value) &&
|
|
45
|
+
(descriptor.kind !== 'integer' || Number.isInteger(value)) &&
|
|
46
|
+
(descriptor.min === undefined || value >= descriptor.min) &&
|
|
47
|
+
(descriptor.max === undefined || value <= descriptor.max));
|
|
48
|
+
case 'string':
|
|
49
|
+
return typeof value === 'string';
|
|
50
|
+
case 'oneOf':
|
|
51
|
+
return typeof value === 'string' && descriptor.values.includes(value);
|
|
52
|
+
case 'list':
|
|
53
|
+
return (Array.isArray(value) &&
|
|
54
|
+
Array.from(value).every((item) => validateValue(descriptor.item, item, {
|
|
55
|
+
...options,
|
|
56
|
+
completeObjects: true,
|
|
57
|
+
})));
|
|
58
|
+
case 'object':
|
|
59
|
+
return (isJsonObject(value) &&
|
|
60
|
+
Object.entries(value).every(([key, field]) => Object.hasOwn(descriptor.fields, key)
|
|
61
|
+
? validateValue(descriptor.fields[key], field, options)
|
|
62
|
+
: options.allowUnknownFields === true) &&
|
|
63
|
+
(!options.completeObjects ||
|
|
64
|
+
Object.keys(descriptor.fields).every((key) => Object.hasOwn(value, key))));
|
|
65
|
+
case 'record':
|
|
66
|
+
return (isJsonObject(value) &&
|
|
67
|
+
Object.values(value).every((item) => validateValue(descriptor.value, item, {
|
|
68
|
+
...options,
|
|
69
|
+
completeObjects: true,
|
|
70
|
+
})));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Internal helper; definitions and descriptor payloads are immutable at runtime. */
|
|
74
|
+
export function deepFreeze(value) {
|
|
75
|
+
if (value && typeof value === 'object') {
|
|
76
|
+
for (const child of Object.values(value))
|
|
77
|
+
deepFreeze(child);
|
|
78
|
+
Object.freeze(value);
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { SettingsDefinition } from './define.js';
|
|
2
|
+
export type SettingsEdit = {
|
|
3
|
+
setting: string;
|
|
4
|
+
path?: readonly string[];
|
|
5
|
+
} & ({
|
|
6
|
+
value: unknown;
|
|
7
|
+
remove?: never;
|
|
8
|
+
} | {
|
|
9
|
+
remove: true;
|
|
10
|
+
value?: never;
|
|
11
|
+
});
|
|
12
|
+
export interface UpdateResult {
|
|
13
|
+
outcome: 'updated' | 'unchanged';
|
|
14
|
+
}
|
|
15
|
+
interface EditContext {
|
|
16
|
+
host: string;
|
|
17
|
+
model?: string;
|
|
18
|
+
}
|
|
19
|
+
/** Pure JSONC edit engine; the writer restores the BOM and owns durable I/O. */
|
|
20
|
+
export declare function applySettingsEdits(definition: SettingsDefinition, source: string, edits: readonly SettingsEdit[], context: EditContext): UpdateResult & {
|
|
21
|
+
text: string;
|
|
22
|
+
hasBom: boolean;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { applyEdits, createScanner, findNodeAtLocation, modify, } from 'jsonc-parser';
|
|
3
|
+
import { isJsonObject, validateValue, } from './descriptors.js';
|
|
4
|
+
import { parseSettings } from './parse.js';
|
|
5
|
+
import { SettingsRefusal } from './refusal.js';
|
|
6
|
+
// jsonc-parser declares these scanner tokens as an ambient const enum, which
|
|
7
|
+
// cannot be imported as a value with verbatimModuleSyntax enabled.
|
|
8
|
+
const SyntaxKind = {
|
|
9
|
+
CommaToken: 5,
|
|
10
|
+
LineCommentTrivia: 12,
|
|
11
|
+
BlockCommentTrivia: 13,
|
|
12
|
+
Trivia: 15,
|
|
13
|
+
EOF: 17,
|
|
14
|
+
};
|
|
15
|
+
function invalid() {
|
|
16
|
+
throw new SettingsRefusal('settings-invalid-value', 'The settings edit does not match its declared value');
|
|
17
|
+
}
|
|
18
|
+
function parseForEdit(text) {
|
|
19
|
+
const parsed = parseSettings(text);
|
|
20
|
+
if (!parsed.parseable || parsed.hasDuplicates) {
|
|
21
|
+
throw new SettingsRefusal('settings-unparseable', 'Settings must be an object without duplicate keys');
|
|
22
|
+
}
|
|
23
|
+
return parsed;
|
|
24
|
+
}
|
|
25
|
+
function descriptorAtPath(descriptor, path) {
|
|
26
|
+
let current = descriptor;
|
|
27
|
+
for (const key of path) {
|
|
28
|
+
if (current.kind === 'object' && Object.hasOwn(current.fields, key)) {
|
|
29
|
+
current = current.fields[key];
|
|
30
|
+
}
|
|
31
|
+
else if (current.kind === 'record') {
|
|
32
|
+
current = current.value;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
invalid();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return current;
|
|
39
|
+
}
|
|
40
|
+
function destination(declaration, setting, value, { host, model }) {
|
|
41
|
+
if (declaration.overrides !== undefined && isJsonObject(value)) {
|
|
42
|
+
if (declaration.overrides.model &&
|
|
43
|
+
model !== undefined &&
|
|
44
|
+
model !== 'default' &&
|
|
45
|
+
model !== 'opencode' &&
|
|
46
|
+
model !== 'pi' &&
|
|
47
|
+
Object.hasOwn(value, model)) {
|
|
48
|
+
return [setting, model];
|
|
49
|
+
}
|
|
50
|
+
if (declaration.overrides.host &&
|
|
51
|
+
(host === 'opencode' || host === 'pi') &&
|
|
52
|
+
Object.hasOwn(value, host)) {
|
|
53
|
+
return [setting, host];
|
|
54
|
+
}
|
|
55
|
+
return [setting, 'default'];
|
|
56
|
+
}
|
|
57
|
+
return [setting];
|
|
58
|
+
}
|
|
59
|
+
function resolutionUnit(descriptor, destination, path) {
|
|
60
|
+
let current = descriptor;
|
|
61
|
+
const unitPath = [...destination];
|
|
62
|
+
for (const key of path) {
|
|
63
|
+
// Records and lists resolve whole, even when the edit changes one leaf.
|
|
64
|
+
if (current.kind === 'record' || current.kind === 'list')
|
|
65
|
+
break;
|
|
66
|
+
current = descriptorAtPath(current, [key]);
|
|
67
|
+
unitPath.push(key);
|
|
68
|
+
}
|
|
69
|
+
return { path: unitPath, descriptor: current };
|
|
70
|
+
}
|
|
71
|
+
function checkPath(parsed, path) {
|
|
72
|
+
for (let length = 1; length < path.length; length++) {
|
|
73
|
+
const node = parsed.tree && findNodeAtLocation(parsed.tree, path.slice(0, length));
|
|
74
|
+
if (node && node.type !== 'object')
|
|
75
|
+
invalid();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Remove only the member's own tokens; intervening comment lines belong to neither neighbor. */
|
|
79
|
+
function removeMember(text, tree, path) {
|
|
80
|
+
const property = tree && findNodeAtLocation(tree, path)?.parent;
|
|
81
|
+
const parent = property?.parent;
|
|
82
|
+
if (property?.type !== 'property' || !parent?.children)
|
|
83
|
+
return text;
|
|
84
|
+
const scanner = createScanner(text, false);
|
|
85
|
+
const end = property.offset + property.length;
|
|
86
|
+
const index = parent.children.indexOf(property);
|
|
87
|
+
const next = parent.children[index + 1];
|
|
88
|
+
const followingEnd = next?.offset ?? parent.offset + parent.length - 1;
|
|
89
|
+
let comma;
|
|
90
|
+
scanner.setPosition(end);
|
|
91
|
+
while (scanner.getPosition() < followingEnd) {
|
|
92
|
+
const token = scanner.scan();
|
|
93
|
+
if (token === SyntaxKind.CommaToken) {
|
|
94
|
+
comma = { start: scanner.getTokenOffset(), end: scanner.getPosition() };
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (token === SyntaxKind.EOF)
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (!comma && index > 0) {
|
|
101
|
+
const previous = parent.children[index - 1];
|
|
102
|
+
scanner.setPosition(previous.offset + previous.length);
|
|
103
|
+
while (scanner.getPosition() < property.offset) {
|
|
104
|
+
const token = scanner.scan();
|
|
105
|
+
if (token === SyntaxKind.CommaToken) {
|
|
106
|
+
comma = { start: scanner.getTokenOffset(), end: scanner.getPosition() };
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if (token === SyntaxKind.EOF)
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let removeEnd = end;
|
|
114
|
+
scanner.setPosition(end);
|
|
115
|
+
while (scanner.getPosition() < followingEnd) {
|
|
116
|
+
const token = scanner.scan();
|
|
117
|
+
if (token === SyntaxKind.Trivia || token === SyntaxKind.CommaToken)
|
|
118
|
+
continue;
|
|
119
|
+
if (token !== SyntaxKind.LineCommentTrivia &&
|
|
120
|
+
token !== SyntaxKind.BlockCommentTrivia)
|
|
121
|
+
break;
|
|
122
|
+
removeEnd = scanner.getPosition();
|
|
123
|
+
// A multiline trailing comment belongs to the member, but comments starting
|
|
124
|
+
// on subsequent lines do not.
|
|
125
|
+
if (/[\r\n]/.test(scanner.getTokenValue()))
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
if (comma &&
|
|
129
|
+
comma.start >= end &&
|
|
130
|
+
!/[\r\n]/.test(text.slice(end, comma.start))) {
|
|
131
|
+
removeEnd = Math.max(removeEnd, comma.end);
|
|
132
|
+
}
|
|
133
|
+
const lineStart = Math.max(text.lastIndexOf('\n', property.offset - 1), text.lastIndexOf('\r', property.offset - 1)) + 1;
|
|
134
|
+
let removeStart = property.offset;
|
|
135
|
+
const lineBreak = /^[\t ]*(\r\n|\r|\n)/.exec(text.slice(removeEnd));
|
|
136
|
+
if (/^[\t ]*$/.test(text.slice(lineStart, property.offset)) && lineBreak) {
|
|
137
|
+
removeStart = lineStart;
|
|
138
|
+
removeEnd += lineBreak[0].length;
|
|
139
|
+
}
|
|
140
|
+
const edits = [
|
|
141
|
+
{ offset: removeStart, length: removeEnd - removeStart, content: '' },
|
|
142
|
+
];
|
|
143
|
+
// The last member's comma can be separated from it by preserved comment lines.
|
|
144
|
+
if (comma && (comma.start < removeStart || comma.start >= removeEnd)) {
|
|
145
|
+
edits.push({
|
|
146
|
+
offset: comma.start,
|
|
147
|
+
length: comma.end - comma.start,
|
|
148
|
+
content: '',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return applyEdits(text, edits);
|
|
152
|
+
}
|
|
153
|
+
/** Pure JSONC edit engine; the writer restores the BOM and owns durable I/O. */
|
|
154
|
+
export function applySettingsEdits(definition, source, edits, context) {
|
|
155
|
+
// Validate the entire request before changing even the private working text.
|
|
156
|
+
for (const edit of edits) {
|
|
157
|
+
if (!Object.hasOwn(definition.settings, edit.setting))
|
|
158
|
+
invalid();
|
|
159
|
+
const declaration = definition.settings[edit.setting];
|
|
160
|
+
if (edit.path !== undefined &&
|
|
161
|
+
declaration.value.kind !== 'object' &&
|
|
162
|
+
declaration.value.kind !== 'record')
|
|
163
|
+
invalid();
|
|
164
|
+
const descriptor = descriptorAtPath(declaration.value, edit.path ?? []);
|
|
165
|
+
if (edit.remove !== true && !validateValue(descriptor, edit.value))
|
|
166
|
+
invalid();
|
|
167
|
+
}
|
|
168
|
+
const start = parseForEdit(source);
|
|
169
|
+
let working = start;
|
|
170
|
+
const units = [];
|
|
171
|
+
for (const edit of edits) {
|
|
172
|
+
const declaration = definition.settings[edit.setting];
|
|
173
|
+
const base = edit.path !== undefined
|
|
174
|
+
? [edit.setting]
|
|
175
|
+
: destination(declaration, edit.setting, working.value[edit.setting], context);
|
|
176
|
+
const path = [...base, ...(edit.path ?? [])];
|
|
177
|
+
checkPath(working, path);
|
|
178
|
+
units.push(resolutionUnit(declaration.value, base, edit.path ?? []));
|
|
179
|
+
const node = working.tree && findNodeAtLocation(working.tree, path);
|
|
180
|
+
let text;
|
|
181
|
+
if (edit.remove === true) {
|
|
182
|
+
if (!node)
|
|
183
|
+
continue;
|
|
184
|
+
text = removeMember(working.text, working.tree, path);
|
|
185
|
+
let removed = parseForEdit(text);
|
|
186
|
+
const remaining = removed.value[edit.setting];
|
|
187
|
+
if (base.length === 2 &&
|
|
188
|
+
isJsonObject(remaining) &&
|
|
189
|
+
Object.keys(remaining).length === 0) {
|
|
190
|
+
text = removeMember(text, removed.tree, [edit.setting]);
|
|
191
|
+
removed = parseForEdit(text);
|
|
192
|
+
}
|
|
193
|
+
working = removed;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
if (node && isDeepStrictEqual(valueAt(working.value, path), edit.value))
|
|
197
|
+
continue;
|
|
198
|
+
text = applyEdits(working.text, modify(working.text, path, edit.value, {
|
|
199
|
+
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
|
200
|
+
}));
|
|
201
|
+
working = parseForEdit(text);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
for (const unit of units) {
|
|
205
|
+
const node = working.tree && findNodeAtLocation(working.tree, unit.path);
|
|
206
|
+
if (node &&
|
|
207
|
+
!validateValue(unit.descriptor, valueAt(working.value, unit.path), {
|
|
208
|
+
allowUnknownFields: true,
|
|
209
|
+
}))
|
|
210
|
+
invalid();
|
|
211
|
+
}
|
|
212
|
+
if (isDeepStrictEqual(start.value, working.value)) {
|
|
213
|
+
return { outcome: 'unchanged', text: start.text, hasBom: start.hasBom };
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
outcome: 'updated',
|
|
217
|
+
text: working.text,
|
|
218
|
+
hasBom: start.hasBom,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function valueAt(value, path) {
|
|
222
|
+
let current = value;
|
|
223
|
+
for (const key of path) {
|
|
224
|
+
if (!isJsonObject(current) || !Object.hasOwn(current, key))
|
|
225
|
+
return undefined;
|
|
226
|
+
current = current[key];
|
|
227
|
+
}
|
|
228
|
+
return current;
|
|
229
|
+
}
|