@wiro-ai/wiro-mcp 1.8.0 → 1.9.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 +54 -7
- package/dist/instructions.js +4 -4
- package/dist/instructions.js.map +1 -1
- package/dist/playbooks.d.ts +1 -1
- package/dist/playbooks.d.ts.map +1 -1
- package/dist/playbooks.js +176 -6
- package/dist/playbooks.js.map +1 -1
- package/dist/tools/check-model-access.d.ts +4 -0
- package/dist/tools/check-model-access.d.ts.map +1 -0
- package/dist/tools/check-model-access.js +281 -0
- package/dist/tools/check-model-access.js.map +1 -0
- package/dist/tools/get-account.d.ts +5 -1
- package/dist/tools/get-account.d.ts.map +1 -1
- package/dist/tools/get-account.js +39 -4
- package/dist/tools/get-account.js.map +1 -1
- package/dist/tools/get-agent-conversation.d.ts.map +1 -1
- package/dist/tools/get-agent-conversation.js +3 -2
- package/dist/tools/get-agent-conversation.js.map +1 -1
- package/dist/tools/get-controls.d.ts +4 -0
- package/dist/tools/get-controls.d.ts.map +1 -0
- package/dist/tools/get-controls.js +365 -0
- package/dist/tools/get-controls.js.map +1 -0
- package/dist/tools/index.d.ts +6 -2
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +22 -8
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/manage-model-access.d.ts +4 -0
- package/dist/tools/manage-model-access.d.ts.map +1 -0
- package/dist/tools/manage-model-access.js +816 -0
- package/dist/tools/manage-model-access.js.map +1 -0
- package/dist/tools/manage-spend-limit.d.ts +4 -0
- package/dist/tools/manage-spend-limit.d.ts.map +1 -0
- package/dist/tools/manage-spend-limit.js +535 -0
- package/dist/tools/manage-spend-limit.js.map +1 -0
- package/dist/tools/resume-workflow-run.d.ts +2 -1
- package/dist/tools/resume-workflow-run.d.ts.map +1 -1
- package/dist/tools/resume-workflow-run.js +4 -4
- package/dist/tools/resume-workflow-run.js.map +1 -1
- package/dist/tools/run-model.d.ts +2 -1
- package/dist/tools/run-model.d.ts.map +1 -1
- package/dist/tools/run-model.js +10 -9
- package/dist/tools/run-model.js.map +1 -1
- package/dist/tools/run-workflow.d.ts +2 -1
- package/dist/tools/run-workflow.d.ts.map +1 -1
- package/dist/tools/run-workflow.js +4 -4
- package/dist/tools/run-workflow.js.map +1 -1
- package/dist/tools/schemas.d.ts.map +1 -1
- package/dist/tools/schemas.js +18 -1
- package/dist/tools/schemas.js.map +1 -1
- package/dist/tools/send-agent-message.d.ts +2 -1
- package/dist/tools/send-agent-message.d.ts.map +1 -1
- package/dist/tools/send-agent-message.js +9 -4
- package/dist/tools/send-agent-message.js.map +1 -1
- package/dist/tools/update-agent.d.ts +2 -1
- package/dist/tools/update-agent.d.ts.map +1 -1
- package/dist/tools/update-agent.js +3 -3
- package/dist/tools/update-agent.js.map +1 -1
- package/dist/utils/agent-catalog.d.ts +1 -1
- package/dist/utils/agent-message.d.ts +6 -0
- package/dist/utils/agent-message.d.ts.map +1 -1
- package/dist/utils/agent-message.js +21 -1
- package/dist/utils/agent-message.js.map +1 -1
- package/dist/utils/agent-targets.d.ts +9 -3
- package/dist/utils/agent-targets.d.ts.map +1 -1
- package/dist/utils/agent-targets.js +41 -12
- package/dist/utils/agent-targets.js.map +1 -1
- package/dist/utils/agent-turns.d.ts +4 -0
- package/dist/utils/agent-turns.d.ts.map +1 -1
- package/dist/utils/agent-turns.js +5 -3
- package/dist/utils/agent-turns.js.map +1 -1
- package/dist/utils/api.d.ts +52 -5
- package/dist/utils/api.d.ts.map +1 -1
- package/dist/utils/api.js +98 -10
- package/dist/utils/api.js.map +1 -1
- package/dist/utils/controls.d.ts +427 -0
- package/dist/utils/controls.d.ts.map +1 -0
- package/dist/utils/controls.js +910 -0
- package/dist/utils/controls.js.map +1 -0
- package/dist/utils/wiro-project.d.ts +2 -0
- package/dist/utils/wiro-project.d.ts.map +1 -1
- package/dist/utils/wiro-project.js +1 -0
- package/dist/utils/wiro-project.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,816 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ApiResultError, asString, errorResult, errorText, isPlainObject, requireApiSuccess, } from '../utils/api.js';
|
|
3
|
+
import { agentVoice, blocksSomething, CATEGORY_KEYS, changeFailure, checkTargetArguments, confirmLead, confirmState, CONTROL_SCOPES, cannotChangeText, controlsPageUrl, entryName, GEMINI_VOICE_MODEL, isCategory, isThisConnection, MAX_RULE_MODELS, NOTHING_CHANGED, previewFailure, previewMissingText, readFailure, readRuleList, resolveTarget, ruleSummary, ruleView, ruleViewSchema, summaryId, summaryIdSchema, SURFACES, targetArgument, targetText, toRuleDecisions, toRuleRow, versionOf, wholeWallet, } from '../utils/controls.js';
|
|
4
|
+
import { jsonSpan, quotedSpan } from '../utils/quoted-text.js';
|
|
5
|
+
import { nextActionSchema, strictInput } from './schemas.js';
|
|
6
|
+
const MAX_MODELS_PER_CALL = 50;
|
|
7
|
+
const SET_UNKNOWN = 'The outcome is not known: the rule may or may not have changed. get_controls shows the rule as it '
|
|
8
|
+
+ 'is now, and repeating this same call is safe, since it asks for the same rule.';
|
|
9
|
+
const REMOVE_UNKNOWN = 'The outcome is not known: the rule may or may not be removed. get_controls shows whether it is '
|
|
10
|
+
+ 'still there, and repeating this same call is safe.';
|
|
11
|
+
const ASK_REASON = 'Ask the user to confirm this change first.';
|
|
12
|
+
const APPLIES_NOTE = 'It applies to runs that start from now on; work already running is not stopped.';
|
|
13
|
+
const AGENT_NOTE = 'The agent follows a change to its own rule within about a minute.';
|
|
14
|
+
const VOICE_STOPS = 'This rule also blocks Audio & Speech, which covers the agent\'s built-in voice: voice notes are not '
|
|
15
|
+
+ 'transcribed and voice replies go out as text. Allow Audio & Speech to keep them.';
|
|
16
|
+
const VOICE_REPLIES_STOP = 'This rule also blocks Audio & Speech, which covers the agent\'s voice replies, so they go out '
|
|
17
|
+
+ 'as text. Allow Audio & Speech to keep them.';
|
|
18
|
+
/** What stops the voice notes of an agent that chats on Gemini: the rule's entry for their model, or its categories. */
|
|
19
|
+
function geminiNotesWarning(byName) {
|
|
20
|
+
const opener = `The agent chats on Gemini, so ${quotedSpan(GEMINI_VOICE_MODEL)} transcribes its voice notes, and this rule blocks that model`;
|
|
21
|
+
return byName
|
|
22
|
+
? `${opener} by name: voice notes are not transcribed. Allow that model to keep them.`
|
|
23
|
+
: `${opener} as an Audio & Speech or an LLM & Chat model: voice notes are not transcribed. Allow that model by name to keep them.`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The warnings for what a change newly stops of an agent's built-in voice: its voice replies, its voice notes, or
|
|
27
|
+
* both. For an agent that chats on Gemini the rule decides its voice notes apart from its voice replies (agentVoice).
|
|
28
|
+
*/
|
|
29
|
+
function voiceWarnings(before, after, chatModel) {
|
|
30
|
+
const was = agentVoice(before, chatModel);
|
|
31
|
+
const now = agentVoice(after, chatModel);
|
|
32
|
+
const notesStop = now.notes === 'block' && was.notes === 'allow';
|
|
33
|
+
const repliesStop = now.replies === 'block' && was.replies === 'allow';
|
|
34
|
+
if (!now.gemini)
|
|
35
|
+
return notesStop || repliesStop ? [VOICE_STOPS] : [];
|
|
36
|
+
// One warning when allowing Audio & Speech would bring back both.
|
|
37
|
+
if (notesStop && repliesStop && !now.notesByName && (after.categories.llm ?? after.access) === 'allow')
|
|
38
|
+
return [VOICE_STOPS];
|
|
39
|
+
return [...(repliesStop ? [VOICE_REPLIES_STOP] : []), ...(notesStop ? [geminiNotesWarning(now.notesByName)] : [])];
|
|
40
|
+
}
|
|
41
|
+
const fromTo = z.enum(['allow', 'block', 'not named']);
|
|
42
|
+
const outputSchema = {
|
|
43
|
+
action: z.enum(['update', 'replace', 'remove']),
|
|
44
|
+
outcome: z.enum(['preview', 'saved', 'removed', 'unchanged', 'refused'])
|
|
45
|
+
.describe('preview: a change summary, nothing changed yet. saved or removed: done. unchanged: it was already so. refused: nothing was changed, and message says why.'),
|
|
46
|
+
workspace: z.object({
|
|
47
|
+
kind: z.enum(['personal', 'team', 'unknown']),
|
|
48
|
+
canManage: z.boolean(),
|
|
49
|
+
}),
|
|
50
|
+
target: z.object({
|
|
51
|
+
scope: z.enum(CONTROL_SCOPES),
|
|
52
|
+
target: z.string().optional(),
|
|
53
|
+
targetName: z.string().optional(),
|
|
54
|
+
thisConnection: z.boolean().optional(),
|
|
55
|
+
}).optional(),
|
|
56
|
+
before: ruleViewSchema.nullable().describe('The rule before the change; null when there was none.'),
|
|
57
|
+
after: ruleViewSchema.nullable().describe('The rule after the change; null for a removal.'),
|
|
58
|
+
changes: z.object({
|
|
59
|
+
everythingElse: z.object({ from: fromTo, to: fromTo }).optional(),
|
|
60
|
+
categories: z.array(z.object({ category: z.enum(CATEGORY_KEYS), label: z.string(), from: fromTo, to: fromTo })),
|
|
61
|
+
models: z.array(z.object({ id: z.string(), model: z.string().nullable(), title: z.string().optional(), from: fromTo, to: fromTo })),
|
|
62
|
+
}).optional(),
|
|
63
|
+
changedSinceSummary: z.boolean().optional().describe('True when the rule changed after the summary the user saw: this is a new summary, and nothing was saved.'),
|
|
64
|
+
warnings: z.array(z.string()),
|
|
65
|
+
notes: z.array(z.string()),
|
|
66
|
+
message: z.string(),
|
|
67
|
+
dashboardUrl: z.string(),
|
|
68
|
+
nextAction: nextActionSchema.optional(),
|
|
69
|
+
};
|
|
70
|
+
function inputError(text) {
|
|
71
|
+
return new Error(`${text} ${NOTHING_CHANGED}`);
|
|
72
|
+
}
|
|
73
|
+
function sameTarget(a, b) {
|
|
74
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
75
|
+
}
|
|
76
|
+
function result(structured, lines) {
|
|
77
|
+
return { content: [{ type: 'text', text: lines.join('\n') }], structuredContent: structured };
|
|
78
|
+
}
|
|
79
|
+
function targetView(target) {
|
|
80
|
+
return {
|
|
81
|
+
scope: target.scope,
|
|
82
|
+
...(target.scope !== 'account' ? { target: target.target } : {}),
|
|
83
|
+
...(target.targetName ? { targetName: target.targetName } : {}),
|
|
84
|
+
...(target.thisConnection ? { thisConnection: true } : {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function nextLine(nextAction) {
|
|
88
|
+
return nextAction ? ['', `**Next:** \`${nextAction.tool}\` with ${jsonSpan(nextAction.arguments)}. ${nextAction.reason}`] : [];
|
|
89
|
+
}
|
|
90
|
+
function bullets(warnings, notes) {
|
|
91
|
+
const all = [...warnings.map(warning => `Warning: ${warning}`), ...notes];
|
|
92
|
+
return all.length > 0 ? ['', ...all.map(line => `- ${line}`)] : [];
|
|
93
|
+
}
|
|
94
|
+
function word(access) {
|
|
95
|
+
return access === 'allow' ? 'allowed' : access === 'block' ? 'blocked' : 'not named';
|
|
96
|
+
}
|
|
97
|
+
function refusedResult(action, workspace) {
|
|
98
|
+
const message = cannotChangeText(workspace, 'Model Access rules', 'model-access');
|
|
99
|
+
return result({
|
|
100
|
+
action,
|
|
101
|
+
outcome: 'refused',
|
|
102
|
+
workspace: { kind: workspace.kind, canManage: false },
|
|
103
|
+
before: null,
|
|
104
|
+
after: null,
|
|
105
|
+
warnings: [],
|
|
106
|
+
notes: [],
|
|
107
|
+
message,
|
|
108
|
+
dashboardUrl: controlsPageUrl('model-access', workspace),
|
|
109
|
+
}, ['## Not changed', '', message]);
|
|
110
|
+
}
|
|
111
|
+
/** Refusals that need no call: arguments that do not fit together. */
|
|
112
|
+
function checkArguments(args) {
|
|
113
|
+
const hasEntries = args.everything_else !== undefined || (args.categories?.length ?? 0) > 0 || (args.models?.length ?? 0) > 0;
|
|
114
|
+
if (args.action === 'remove' && hasEntries) {
|
|
115
|
+
throw inputError('remove deletes the whole rule and takes no everything_else, categories or models. To take out one entry, use update with access unset.');
|
|
116
|
+
}
|
|
117
|
+
if (args.action === 'update' && !hasEntries) {
|
|
118
|
+
throw inputError('update needs something to change: everything_else, categories or models.');
|
|
119
|
+
}
|
|
120
|
+
if (args.action === 'replace') {
|
|
121
|
+
const unset = [...(args.categories ?? []), ...(args.models ?? [])].some(entry => entry.access === 'unset');
|
|
122
|
+
if (unset)
|
|
123
|
+
throw inputError('Access unset takes an entry out of a rule, so it goes with update. replace writes the whole rule: leave the entry out instead.');
|
|
124
|
+
}
|
|
125
|
+
const categories = args.categories ?? [];
|
|
126
|
+
const twice = categories.find((entry, index) => categories.findIndex(other => other.category === entry.category) !== index);
|
|
127
|
+
if (twice)
|
|
128
|
+
throw inputError(`The category ${twice.category} is named twice.`);
|
|
129
|
+
const models = args.models ?? [];
|
|
130
|
+
const twiceModel = models.find((entry, index) => models.findIndex(other => other.model.toLowerCase() === entry.model.toLowerCase()) !== index);
|
|
131
|
+
if (twiceModel)
|
|
132
|
+
throw inputError(`${quotedSpan(twiceModel.model)} is named twice.`);
|
|
133
|
+
if (!args.rule_guid && !args.scope) {
|
|
134
|
+
throw inputError('Name the rule: scope with its target (member, project, surface or agent), or rule_guid from get_controls.');
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
if (!args.rule_guid)
|
|
138
|
+
checkTargetArguments(args);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
throw inputError(errorText(error));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** The rule a call names: from rule_guid, or from the scope and its target. One rule per scope and target. */
|
|
145
|
+
async function locate(client, args, list, signal) {
|
|
146
|
+
if (args.rule_guid) {
|
|
147
|
+
const rule = list.rules.find(candidate => candidate.guid === args.rule_guid);
|
|
148
|
+
if (!rule)
|
|
149
|
+
return `No rule with rule_guid ${quotedSpan(args.rule_guid)} is in this workspace.`;
|
|
150
|
+
const fields = ['member', 'project', 'surface', 'agent'].filter(field => args[field] !== undefined);
|
|
151
|
+
const conflict = (args.scope !== undefined && args.scope !== rule.scope)
|
|
152
|
+
|| fields.some(field => field !== rule.scope
|
|
153
|
+
|| (!sameTarget(String(args[field]).trim(), rule.target) && !sameTarget(String(args[field]).trim(), rule.targetName ?? '')));
|
|
154
|
+
if (conflict) {
|
|
155
|
+
throw inputError(`rule_guid already names the rule for ${targetText(rule, list.workspace.kind)}: leave out scope and its target, or name the same ones.`);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
target: {
|
|
159
|
+
scope: rule.scope,
|
|
160
|
+
target: rule.target,
|
|
161
|
+
...(rule.targetName ? { targetName: rule.targetName } : {}),
|
|
162
|
+
thisConnection: isThisConnection(rule.scope, rule.target, list.caller),
|
|
163
|
+
},
|
|
164
|
+
existing: rule,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
let target;
|
|
168
|
+
try {
|
|
169
|
+
target = await resolveTarget(client, { ...args, scope: args.scope }, list, signal);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (signal.aborted)
|
|
173
|
+
throw error;
|
|
174
|
+
throw inputError(errorText(error));
|
|
175
|
+
}
|
|
176
|
+
const existing = list.rules.find(rule => rule.scope === target.scope && sameTarget(rule.target, target.target)) ?? null;
|
|
177
|
+
if (!target.targetName && existing?.targetName)
|
|
178
|
+
target.targetName = existing.targetName;
|
|
179
|
+
return { target, existing };
|
|
180
|
+
}
|
|
181
|
+
/** The rule asked for, and the entries an unset found nothing to take out of. */
|
|
182
|
+
function draftOf(args, existing) {
|
|
183
|
+
const notInRule = [];
|
|
184
|
+
if (args.action === 'replace') {
|
|
185
|
+
const categories = {};
|
|
186
|
+
for (const entry of args.categories ?? [])
|
|
187
|
+
if (entry.access !== 'unset')
|
|
188
|
+
categories[entry.category] = entry.access;
|
|
189
|
+
return {
|
|
190
|
+
draft: {
|
|
191
|
+
access: args.everything_else ?? 'allow',
|
|
192
|
+
categories,
|
|
193
|
+
models: (args.models ?? []).flatMap(entry => (entry.access === 'unset' ? [] : [{ ref: entry.model.trim(), access: entry.access }])),
|
|
194
|
+
},
|
|
195
|
+
notInRule,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const categories = { ...(existing?.categories ?? {}) };
|
|
199
|
+
for (const entry of args.categories ?? []) {
|
|
200
|
+
if (entry.access === 'unset') {
|
|
201
|
+
if (categories[entry.category] === undefined)
|
|
202
|
+
notInRule.push(entry.category);
|
|
203
|
+
delete categories[entry.category];
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
categories[entry.category] = entry.access;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Entries the rule holds go back by id: a name can have changed, and a model no longer on Wiro has none.
|
|
210
|
+
const models = (existing?.models ?? []).map(entry => ({ ref: entry.id, id: entry.id, access: entry.access }));
|
|
211
|
+
for (const entry of args.models ?? []) {
|
|
212
|
+
const ref = entry.model.trim();
|
|
213
|
+
const held = existing?.models.find(candidate => (/^\d+$/.test(ref)
|
|
214
|
+
? candidate.id === ref
|
|
215
|
+
: candidate.model !== null && candidate.model.toLowerCase() === ref.toLowerCase()));
|
|
216
|
+
const index = held ? models.findIndex(candidate => candidate.id === held.id) : -1;
|
|
217
|
+
if (entry.access === 'unset') {
|
|
218
|
+
if (index === -1)
|
|
219
|
+
notInRule.push(ref);
|
|
220
|
+
else
|
|
221
|
+
models.splice(index, 1);
|
|
222
|
+
}
|
|
223
|
+
else if (index !== -1) {
|
|
224
|
+
models[index] = { ...models[index], access: entry.access };
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
models.push({ ref, access: entry.access });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { draft: { access: args.everything_else ?? existing?.access ?? 'allow', categories, models }, notInRule };
|
|
231
|
+
}
|
|
232
|
+
function sameRule(a, b) {
|
|
233
|
+
if (a.access !== b.access)
|
|
234
|
+
return false;
|
|
235
|
+
if (CATEGORY_KEYS.some(key => a.categories[key] !== b.categories[key]))
|
|
236
|
+
return false;
|
|
237
|
+
if (a.models.length !== b.models.length)
|
|
238
|
+
return false;
|
|
239
|
+
const byId = new Map(a.models.map(entry => [entry.id, entry.access]));
|
|
240
|
+
return b.models.every(entry => byId.get(entry.id) === entry.access);
|
|
241
|
+
}
|
|
242
|
+
/** A draft whose entries all have ids is the same as a rule when every value matches. */
|
|
243
|
+
function draftMatches(draft, rule) {
|
|
244
|
+
if (!rule || draft.models.some(entry => entry.id === undefined))
|
|
245
|
+
return false;
|
|
246
|
+
return sameRule({ access: draft.access, categories: draft.categories, models: draft.models.map(entry => ({ id: entry.id, model: null, access: entry.access })) }, rule);
|
|
247
|
+
}
|
|
248
|
+
function ruleBody(target, draft) {
|
|
249
|
+
return {
|
|
250
|
+
scope: target.scope,
|
|
251
|
+
target: target.target,
|
|
252
|
+
access: draft.access,
|
|
253
|
+
categories: CATEGORY_KEYS.flatMap(category => (draft.categories[category] ? [{ category, access: draft.categories[category] }] : [])),
|
|
254
|
+
models: draft.models.map(entry => ({ model: entry.ref, access: entry.access })),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function changesOf(before, after, labels) {
|
|
258
|
+
const changes = { categories: [], models: [] };
|
|
259
|
+
const fromAccess = before?.access ?? 'allow';
|
|
260
|
+
if (!before || fromAccess !== after.access)
|
|
261
|
+
changes.everythingElse = { from: before ? fromAccess : 'not named', to: after.access };
|
|
262
|
+
for (const category of CATEGORY_KEYS) {
|
|
263
|
+
const from = before?.categories[category] ?? 'not named';
|
|
264
|
+
const to = after.categories[category] ?? 'not named';
|
|
265
|
+
if (from !== to)
|
|
266
|
+
changes.categories.push({ category, label: labels[category], from, to });
|
|
267
|
+
}
|
|
268
|
+
const ids = [...new Set([...(before?.models ?? []).map(entry => entry.id), ...after.models.map(entry => entry.id)])];
|
|
269
|
+
for (const id of ids) {
|
|
270
|
+
const was = before?.models.find(entry => entry.id === id);
|
|
271
|
+
const now = after.models.find(entry => entry.id === id);
|
|
272
|
+
const from = was?.access ?? 'not named';
|
|
273
|
+
const to = now?.access ?? 'not named';
|
|
274
|
+
if (from === to)
|
|
275
|
+
continue;
|
|
276
|
+
const model = now?.model ?? was?.model ?? null;
|
|
277
|
+
const title = now?.title ?? was?.title;
|
|
278
|
+
changes.models.push({ id, model, ...(title ? { title } : {}), from, to });
|
|
279
|
+
}
|
|
280
|
+
return changes;
|
|
281
|
+
}
|
|
282
|
+
function changeLines(changes) {
|
|
283
|
+
const lines = [];
|
|
284
|
+
if (changes.everythingElse) {
|
|
285
|
+
lines.push(changes.everythingElse.from === 'not named'
|
|
286
|
+
? `- Every model it does not name: ${word(changes.everythingElse.to)}`
|
|
287
|
+
: `- Every model it does not name: ${word(changes.everythingElse.from)} before, ${word(changes.everythingElse.to)} after`);
|
|
288
|
+
}
|
|
289
|
+
for (const entry of changes.categories)
|
|
290
|
+
lines.push(`- ${entry.label}: ${word(entry.from)} before, ${word(entry.to)} after`);
|
|
291
|
+
for (const entry of changes.models)
|
|
292
|
+
lines.push(`- ${entryName(entry, true)}: ${word(entry.from)} before, ${word(entry.to)} after`);
|
|
293
|
+
return lines;
|
|
294
|
+
}
|
|
295
|
+
/** Allowing a category here changes nothing while the whole wallet's rule blocks it. */
|
|
296
|
+
function categoryWarnings(target, before, after, list) {
|
|
297
|
+
if (target.scope === 'account')
|
|
298
|
+
return [];
|
|
299
|
+
const account = list.rules.find(rule => rule.scope === 'account');
|
|
300
|
+
if (!account)
|
|
301
|
+
return [];
|
|
302
|
+
const whole = wholeWallet(list.workspace.kind);
|
|
303
|
+
const warnings = [];
|
|
304
|
+
for (const category of CATEGORY_KEYS) {
|
|
305
|
+
if (after[category] !== 'allow' || before[category] === 'allow')
|
|
306
|
+
continue;
|
|
307
|
+
const label = list.labels[category];
|
|
308
|
+
if (account.categories[category] === 'block') {
|
|
309
|
+
warnings.push(`The rule for ${whole} blocks ${label}, so allowing it here changes nothing until that rule allows it too.`);
|
|
310
|
+
}
|
|
311
|
+
else if (account.access === 'block' && account.categories[category] !== 'allow') {
|
|
312
|
+
warnings.push(`The rule for ${whole} lets only what it names run, and it does not allow ${label}, so allowing it here changes nothing until that rule allows it too.`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return warnings;
|
|
316
|
+
}
|
|
317
|
+
/** When the wider rules could not be checked: the whole wallet's rule blocks this model by name. */
|
|
318
|
+
function accountModelWarning(target, id, name, list) {
|
|
319
|
+
if (target.scope === 'account' || !id)
|
|
320
|
+
return [];
|
|
321
|
+
const account = list.rules.find(rule => rule.scope === 'account');
|
|
322
|
+
if (!account?.models.some(held => held.id === id && held.access === 'block'))
|
|
323
|
+
return [];
|
|
324
|
+
return [`The rule for ${wholeWallet(list.workspace.kind)} blocks ${name}, so allowing it here changes nothing until that rule allows it too.`];
|
|
325
|
+
}
|
|
326
|
+
/** Besides a target's own rule, the rules that apply to every run of it: they must allow a model too. */
|
|
327
|
+
const ALWAYS_APPLY = {
|
|
328
|
+
account: [],
|
|
329
|
+
member: ['account'],
|
|
330
|
+
// A key acts as the person who created it, so in a team that member's rule covers every run with it.
|
|
331
|
+
project: ['account', 'member'],
|
|
332
|
+
surface: ['account'],
|
|
333
|
+
// An agent's runs come in on the agent surface with its own key, as that key's creator.
|
|
334
|
+
agent: ['account', 'member', 'project', 'surface'],
|
|
335
|
+
};
|
|
336
|
+
/** At most this many models of one change are checked against the wider rules. */
|
|
337
|
+
const MAX_WIDER_CHECKS = 10;
|
|
338
|
+
/** A ModelAccess/Check body for a run of the target. The agent surface needs an agent, so a plain run stands in for it. */
|
|
339
|
+
function targetRunBody(target) {
|
|
340
|
+
switch (target.scope) {
|
|
341
|
+
case 'member':
|
|
342
|
+
return { member: target.target };
|
|
343
|
+
case 'project':
|
|
344
|
+
return { project: target.target };
|
|
345
|
+
case 'surface':
|
|
346
|
+
return target.target === 'agent' ? {} : { surface: target.target };
|
|
347
|
+
case 'agent':
|
|
348
|
+
return { agent: target.target };
|
|
349
|
+
case 'account':
|
|
350
|
+
return {};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* For each model a change allows, the wider rules that still block it for the target, as ModelAccess/Check decides
|
|
355
|
+
* them today. null when that model's check failed, so nothing is known; a model past the first ten is not checked.
|
|
356
|
+
*/
|
|
357
|
+
async function widerBlocks(client, target, models, signal) {
|
|
358
|
+
const found = new Map();
|
|
359
|
+
if (target.scope === 'account')
|
|
360
|
+
return found;
|
|
361
|
+
const scopes = ALWAYS_APPLY[target.scope];
|
|
362
|
+
await Promise.all(models.slice(0, MAX_WIDER_CHECKS).map(async ({ key, ref }) => {
|
|
363
|
+
try {
|
|
364
|
+
const body = requireApiSuccess(await client.post('/ModelAccess/Check', { model: ref, ...targetRunBody(target) }, signal), 'The model could not be checked.');
|
|
365
|
+
const check = isPlainObject(body.check) ? body.check : {};
|
|
366
|
+
found.set(key, toRuleDecisions(check.rules).filter(decision => decision.decides === 'block'
|
|
367
|
+
&& scopes.includes(decision.rule.scope)
|
|
368
|
+
&& !(decision.rule.scope === target.scope && sameTarget(decision.rule.target, target.target))));
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
if (signal.aborted)
|
|
372
|
+
throw error;
|
|
373
|
+
found.set(key, null);
|
|
374
|
+
}
|
|
375
|
+
}));
|
|
376
|
+
return found;
|
|
377
|
+
}
|
|
378
|
+
/** A wider rule that still blocks a model this change allows. */
|
|
379
|
+
function widerBlockText(decision, model, kind, labels) {
|
|
380
|
+
const rule = `The rule for ${targetText(decision.rule, kind)}`;
|
|
381
|
+
const tail = 'so allowing it here changes nothing until that rule allows it too.';
|
|
382
|
+
if (decision.by === 'model')
|
|
383
|
+
return `${rule} blocks ${model} by name, ${tail}`;
|
|
384
|
+
if (decision.by === 'category') {
|
|
385
|
+
const label = decision.category && isCategory(decision.category) ? labels[decision.category] : 'one of its categories';
|
|
386
|
+
return `${rule} blocks ${label}, and ${model} is in it, ${tail}`;
|
|
387
|
+
}
|
|
388
|
+
return `${rule} lets only the models it names run, and ${model} is not one of them, ${tail}`;
|
|
389
|
+
}
|
|
390
|
+
/** What the wider rules say of the models a change allows: a warning for each one that still blocks one. */
|
|
391
|
+
async function modelWarnings(client, target, allowed, list, signal) {
|
|
392
|
+
const checks = await widerBlocks(client, target, allowed, signal);
|
|
393
|
+
return allowed.flatMap(entry => {
|
|
394
|
+
const decisions = checks.get(entry.key);
|
|
395
|
+
return decisions
|
|
396
|
+
? decisions.map(decision => widerBlockText(decision, entry.name, list.workspace.kind, list.labels))
|
|
397
|
+
: accountModelWarning(target, entry.id, entry.name, list);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/** Wiro's refusal of a model the rule holds that is no longer on Wiro, with how to take it out. */
|
|
401
|
+
function hiddenModelText(error, existing) {
|
|
402
|
+
if (!(error instanceof ApiResultError) || !existing)
|
|
403
|
+
return null;
|
|
404
|
+
const id = /^(\d+) is not a model on Wiro\.?$/i.exec(error.message.trim())?.[1];
|
|
405
|
+
if (!id || !existing.models.some(entry => entry.id === id))
|
|
406
|
+
return null;
|
|
407
|
+
return `Model ${id} in this rule is no longer on Wiro, so the rule cannot be saved with it. Add ${jsonSpan({ model: id, access: 'unset' })} `
|
|
408
|
+
+ `to models in this call to take it out. ${NOTHING_CHANGED}`;
|
|
409
|
+
}
|
|
410
|
+
async function previewRule(client, body, existing, workspace, signal) {
|
|
411
|
+
let answer;
|
|
412
|
+
try {
|
|
413
|
+
answer = requireApiSuccess(await client.post('/ModelAccess/Preview', body, signal), 'The change summary could not be made.');
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
if (signal.aborted)
|
|
417
|
+
throw error;
|
|
418
|
+
const hidden = hiddenModelText(error, existing);
|
|
419
|
+
if (hidden)
|
|
420
|
+
throw new Error(hidden);
|
|
421
|
+
throw previewFailure(error, 'model-access', workspace);
|
|
422
|
+
}
|
|
423
|
+
const rule = isPlainObject(answer.rule) ? toRuleRow(answer.rule) : null;
|
|
424
|
+
// Only an answer that says it is a preview proves nothing was written.
|
|
425
|
+
if (answer.preview !== true || !rule)
|
|
426
|
+
throw new Error(previewMissingText('model-access', workspace));
|
|
427
|
+
const previous = isPlainObject(answer.existing) ? toRuleRow(answer.existing) : answer.existing === null ? null : undefined;
|
|
428
|
+
const chat = isPlainObject(answer.agentchatmodel) ? answer.agentchatmodel : null;
|
|
429
|
+
const chatName = chat ? (asString(chat.model)?.trim() || asString(chat.ref)?.trim()) : undefined;
|
|
430
|
+
return {
|
|
431
|
+
rule,
|
|
432
|
+
existing: previous,
|
|
433
|
+
...(chat && chatName ? { chatModel: { name: chatName, blocked: chat.blocked === true } } : {}),
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function nextArguments(args, target, version, id) {
|
|
437
|
+
return {
|
|
438
|
+
action: args.action,
|
|
439
|
+
scope: target.scope,
|
|
440
|
+
...targetArgument(target.scope, target.target),
|
|
441
|
+
...(args.everything_else !== undefined ? { everything_else: args.everything_else } : {}),
|
|
442
|
+
...(args.categories && args.categories.length > 0 ? { categories: args.categories } : {}),
|
|
443
|
+
...(args.models && args.models.length > 0 ? { models: args.models.map(entry => ({ model: entry.model.trim(), access: entry.access })) } : {}),
|
|
444
|
+
confirm: true,
|
|
445
|
+
updated_at: version,
|
|
446
|
+
summary_id: id,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/** The change a rule summary shows, for its id: the rule Set would get, in an order that does not depend on the reads. */
|
|
450
|
+
function ruleChange(action, body, version) {
|
|
451
|
+
const models = body.models.map(entry => [entry.model, entry.access]).sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
452
|
+
return { tool: 'manage_model_access', action, ...body, models, version };
|
|
453
|
+
}
|
|
454
|
+
function removeNextAction(rule) {
|
|
455
|
+
return {
|
|
456
|
+
tool: 'manage_model_access',
|
|
457
|
+
arguments: { action: 'remove', rule_guid: rule.guid },
|
|
458
|
+
reason: 'Shows what removing the rule changes; nothing changes without the user\'s yes.',
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
async function changeRule(client, args, list, signal) {
|
|
462
|
+
const { workspace, caller, labels } = list;
|
|
463
|
+
const dashboardUrl = controlsPageUrl('model-access', workspace);
|
|
464
|
+
const located = await locate(client, args, list, signal);
|
|
465
|
+
if (typeof located === 'string')
|
|
466
|
+
throw inputError(`${located} get_controls lists the rules with their guids.`);
|
|
467
|
+
const { target, existing } = located;
|
|
468
|
+
const covers = () => targetText(target, workspace.kind);
|
|
469
|
+
const { draft, notInRule } = draftOf(args, existing);
|
|
470
|
+
const unsetNote = notInRule.length > 0
|
|
471
|
+
? [`${notInRule.map(name => quotedSpan(name)).join(', ')} ${notInRule.length === 1 ? 'is' : 'are'} not in the rule, so unset changes nothing there.`]
|
|
472
|
+
: [];
|
|
473
|
+
const unchanged = (rule) => {
|
|
474
|
+
const message = `The rule for ${covers()} already reads: ${ruleSummary(rule, labels, { quote: true, maxModels: 10 })}. ${NOTHING_CHANGED}`;
|
|
475
|
+
return result({
|
|
476
|
+
action: args.action,
|
|
477
|
+
outcome: 'unchanged',
|
|
478
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
479
|
+
target: targetView(target),
|
|
480
|
+
before: ruleView(rule, labels, caller),
|
|
481
|
+
after: ruleView(rule, labels, caller),
|
|
482
|
+
warnings: [],
|
|
483
|
+
notes: unsetNote,
|
|
484
|
+
message,
|
|
485
|
+
dashboardUrl,
|
|
486
|
+
}, ['## Nothing to change', '', message, ...bullets([], unsetNote)]);
|
|
487
|
+
};
|
|
488
|
+
if (draftMatches(draft, existing))
|
|
489
|
+
return unchanged(existing);
|
|
490
|
+
if (!blocksSomething(draft)) {
|
|
491
|
+
// Often the wish behind it is to open a model a wider rule blocks: say which rule that is.
|
|
492
|
+
const opened = draft.models.filter(entry => entry.access === 'allow'
|
|
493
|
+
&& !existing?.models.some(held => held.id !== undefined && held.id === entry.id && held.access === 'allow'));
|
|
494
|
+
const warnings = [...new Set([
|
|
495
|
+
...categoryWarnings(target, existing?.categories ?? {}, draft.categories, list),
|
|
496
|
+
...await modelWarnings(client, target, opened.map(entry => {
|
|
497
|
+
const held = entry.id ? existing?.models.find(candidate => candidate.id === entry.id) : undefined;
|
|
498
|
+
return { key: entry.id ?? entry.ref, ref: entry.ref, ...(entry.id ? { id: entry.id } : {}), name: held ? entryName(held, true) : quotedSpan(entry.ref) };
|
|
499
|
+
}), list, signal),
|
|
500
|
+
])];
|
|
501
|
+
const hint = warnings.length > 0
|
|
502
|
+
? 'To open a model that a wider rule blocks, that rule has to change: check_model_access shows every rule that blocks it.'
|
|
503
|
+
: 'To let only some models run, set everything_else to block.';
|
|
504
|
+
const message = `After this change the rule for ${covers()} would block nothing: with everything_else allow, allow entries `
|
|
505
|
+
+ `change nothing, since a narrower rule cannot open what a wider one blocks. ${hint} `
|
|
506
|
+
+ `${existing ? 'To lift the rule, remove it: nextAction shows that summary. ' : ''}${NOTHING_CHANGED}`;
|
|
507
|
+
const nextAction = existing?.guid ? removeNextAction(existing) : undefined;
|
|
508
|
+
return result({
|
|
509
|
+
action: args.action,
|
|
510
|
+
outcome: 'refused',
|
|
511
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
512
|
+
target: targetView(target),
|
|
513
|
+
before: existing ? ruleView(existing, labels, caller) : null,
|
|
514
|
+
after: null,
|
|
515
|
+
warnings,
|
|
516
|
+
notes: unsetNote,
|
|
517
|
+
message,
|
|
518
|
+
dashboardUrl,
|
|
519
|
+
...(nextAction ? { nextAction } : {}),
|
|
520
|
+
}, ['## Not changed', '', message, ...bullets(warnings, unsetNote), ...nextLine(nextAction)]);
|
|
521
|
+
}
|
|
522
|
+
if (draft.models.length > MAX_RULE_MODELS) {
|
|
523
|
+
throw inputError(`A rule can name at most ${MAX_RULE_MODELS} models, and this one would name ${draft.models.length}.`);
|
|
524
|
+
}
|
|
525
|
+
const body = ruleBody(target, draft);
|
|
526
|
+
const preview = await previewRule(client, body, existing, workspace, signal);
|
|
527
|
+
const before = preview.existing !== undefined ? preview.existing : existing;
|
|
528
|
+
const after = preview.rule;
|
|
529
|
+
if (!target.targetName && (after.targetName || before?.targetName))
|
|
530
|
+
target.targetName = after.targetName ?? before?.targetName;
|
|
531
|
+
if (before && sameRule(before, after))
|
|
532
|
+
return unchanged(before);
|
|
533
|
+
const changes = changesOf(before, after, labels);
|
|
534
|
+
const warnings = [];
|
|
535
|
+
if (target.scope === 'agent' && preview.chatModel?.blocked) {
|
|
536
|
+
warnings.push(`This rule also blocks ${quotedSpan(preview.chatModel.name)}, the chat model the agent answers with, so the agent `
|
|
537
|
+
+ 'would stop answering. Allow that model too, or allow LLM & Chat.');
|
|
538
|
+
}
|
|
539
|
+
if (target.scope === 'agent')
|
|
540
|
+
warnings.push(...voiceWarnings(before, after, preview.chatModel?.name));
|
|
541
|
+
if (!after.models.some(entry => entry.access === 'allow') && !Object.values(after.categories).includes('allow') && after.access === 'block') {
|
|
542
|
+
warnings.push(`This rule blocks every model for ${covers()}.`);
|
|
543
|
+
}
|
|
544
|
+
warnings.push(...categoryWarnings(target, before?.categories ?? {}, after.categories, list));
|
|
545
|
+
const allowedModels = changes.models.filter(entry => entry.to === 'allow' && entry.from !== 'allow');
|
|
546
|
+
warnings.push(...await modelWarnings(client, target, allowedModels.map(entry => ({
|
|
547
|
+
key: entry.id,
|
|
548
|
+
ref: entry.model ?? entry.id,
|
|
549
|
+
id: entry.id,
|
|
550
|
+
name: entryName(entry, true),
|
|
551
|
+
})), list, signal));
|
|
552
|
+
const notes = [APPLIES_NOTE];
|
|
553
|
+
if (target.scope === 'agent')
|
|
554
|
+
notes.push(AGENT_NOTE);
|
|
555
|
+
if (target.scope === 'account' || target.thisConnection)
|
|
556
|
+
notes.push('It covers runs through this connection too.');
|
|
557
|
+
const gone = after.models.filter(entry => entry.model === null).length;
|
|
558
|
+
if (gone > 0)
|
|
559
|
+
notes.push(`${gone === 1 ? 'One model' : `${gone} models`} in this rule ${gone === 1 ? 'is' : 'are'} no longer on Wiro and stay in it until taken out.`);
|
|
560
|
+
notes.push(...unsetNote);
|
|
561
|
+
const version = versionOf(before);
|
|
562
|
+
const id = summaryId(client, ruleChange(args.action, body, version));
|
|
563
|
+
const nextAction = { tool: 'manage_model_access', arguments: nextArguments(args, target, version, id), reason: ASK_REASON };
|
|
564
|
+
const state = confirmState(args, version, id);
|
|
565
|
+
if (state !== 'confirmed') {
|
|
566
|
+
const changedSinceSummary = state === 'changed';
|
|
567
|
+
const lead = confirmLead(state, 'rule');
|
|
568
|
+
const message = lead ?? `Change the rule for ${covers()} to: ${ruleSummary(after, labels)}? Nothing has changed yet.`;
|
|
569
|
+
return result({
|
|
570
|
+
action: args.action,
|
|
571
|
+
outcome: 'preview',
|
|
572
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
573
|
+
target: targetView(target),
|
|
574
|
+
before: before ? ruleView(before, labels, caller) : null,
|
|
575
|
+
after: ruleView(after, labels, caller),
|
|
576
|
+
changes,
|
|
577
|
+
...(changedSinceSummary ? { changedSinceSummary: true } : {}),
|
|
578
|
+
warnings,
|
|
579
|
+
notes,
|
|
580
|
+
message,
|
|
581
|
+
dashboardUrl,
|
|
582
|
+
nextAction,
|
|
583
|
+
}, [
|
|
584
|
+
changedSinceSummary ? '## The rule changed since the summary (nothing saved)' : '## Change a Model Access rule? (nothing changed yet)',
|
|
585
|
+
'',
|
|
586
|
+
...(lead ? [lead, ''] : []),
|
|
587
|
+
`**Rule for:** ${covers()}`,
|
|
588
|
+
`**Before:** ${before ? ruleSummary(before, labels, { quote: true, maxModels: 10 }) : 'no rule, so this rule allowed every model'}`,
|
|
589
|
+
`**After:** ${ruleSummary(after, labels, { quote: true, maxModels: 10 })}`,
|
|
590
|
+
'**Changes:**',
|
|
591
|
+
...changeLines(changes),
|
|
592
|
+
...bullets(warnings, notes),
|
|
593
|
+
'',
|
|
594
|
+
'Nothing has changed yet. Show this to the user and wait for a clear yes.',
|
|
595
|
+
...nextLine(nextAction),
|
|
596
|
+
]);
|
|
597
|
+
}
|
|
598
|
+
let saved;
|
|
599
|
+
try {
|
|
600
|
+
saved = requireApiSuccess(await client.post('/ModelAccess/Set', body, signal), 'The rule could not be saved.');
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
if (signal.aborted)
|
|
604
|
+
throw error;
|
|
605
|
+
const hidden = hiddenModelText(error, before);
|
|
606
|
+
if (hidden)
|
|
607
|
+
throw new Error(hidden);
|
|
608
|
+
throw changeFailure(error, 'model-access', SET_UNKNOWN, workspace);
|
|
609
|
+
}
|
|
610
|
+
const rule = (isPlainObject(saved.rule) ? toRuleRow(saved.rule) : null) ?? after;
|
|
611
|
+
const savedNotes = notes.filter(note => !unsetNote.includes(note));
|
|
612
|
+
const message = `Saved the rule for ${covers()}: ${ruleSummary(rule, labels)}.`;
|
|
613
|
+
return result({
|
|
614
|
+
action: args.action,
|
|
615
|
+
outcome: 'saved',
|
|
616
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
617
|
+
target: targetView(target),
|
|
618
|
+
before: before ? ruleView(before, labels, caller) : null,
|
|
619
|
+
after: ruleView(rule, labels, caller),
|
|
620
|
+
changes: changesOf(before, rule, labels),
|
|
621
|
+
warnings,
|
|
622
|
+
notes: savedNotes,
|
|
623
|
+
message,
|
|
624
|
+
dashboardUrl,
|
|
625
|
+
}, [
|
|
626
|
+
'## Model Access rule saved',
|
|
627
|
+
'',
|
|
628
|
+
`**Rule for:** ${covers()}`,
|
|
629
|
+
`**Now:** ${ruleSummary(rule, labels, { quote: true, maxModels: 10 })}`,
|
|
630
|
+
...bullets(warnings, savedNotes),
|
|
631
|
+
]);
|
|
632
|
+
}
|
|
633
|
+
/** The rule that still applies once one is removed: the whole wallet's. */
|
|
634
|
+
function stillApplyNote(rule, list) {
|
|
635
|
+
if (rule.scope === 'account')
|
|
636
|
+
return null;
|
|
637
|
+
const account = list.rules.find(candidate => candidate.scope === 'account');
|
|
638
|
+
return account
|
|
639
|
+
? `The rule for ${wholeWallet(list.workspace.kind)} still applies: ${ruleSummary(account, list.labels, { quote: true, maxModels: 10 })}.`
|
|
640
|
+
: null;
|
|
641
|
+
}
|
|
642
|
+
async function removeRule(client, args, list, signal) {
|
|
643
|
+
const { workspace, caller, labels } = list;
|
|
644
|
+
const dashboardUrl = controlsPageUrl('model-access', workspace);
|
|
645
|
+
const located = await locate(client, args, list, signal);
|
|
646
|
+
const nothing = (text) => result({
|
|
647
|
+
action: 'remove',
|
|
648
|
+
outcome: 'unchanged',
|
|
649
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
650
|
+
...(typeof located === 'string' ? {} : { target: targetView(located.target) }),
|
|
651
|
+
before: null,
|
|
652
|
+
after: null,
|
|
653
|
+
warnings: [],
|
|
654
|
+
notes: [],
|
|
655
|
+
message: text,
|
|
656
|
+
dashboardUrl,
|
|
657
|
+
}, ['## Nothing to remove', '', text]);
|
|
658
|
+
if (typeof located === 'string')
|
|
659
|
+
return nothing(`${located} It may be removed already; get_controls lists the rules. ${NOTHING_CHANGED}`);
|
|
660
|
+
const rule = located.existing;
|
|
661
|
+
const covers = targetText(located.target, workspace.kind);
|
|
662
|
+
if (!rule)
|
|
663
|
+
return nothing(`There is no rule for ${covers}, so there is nothing to remove. ${NOTHING_CHANGED}`);
|
|
664
|
+
if (!rule.guid)
|
|
665
|
+
throw inputError('Wiro did not give this rule\'s guid, so it cannot be removed here. It can be removed on the Model Access page of the Wiro dashboard.');
|
|
666
|
+
const stillApply = stillApplyNote(rule, list);
|
|
667
|
+
const notes = [
|
|
668
|
+
...(stillApply ? [stillApply] : []),
|
|
669
|
+
...(rule.scope === 'agent' ? [AGENT_NOTE] : []),
|
|
670
|
+
];
|
|
671
|
+
const version = versionOf(rule);
|
|
672
|
+
const id = summaryId(client, { tool: 'manage_model_access', action: 'remove', guid: rule.guid, version });
|
|
673
|
+
const state = confirmState(args, version, id);
|
|
674
|
+
if (state !== 'confirmed') {
|
|
675
|
+
const changedSinceSummary = state === 'changed';
|
|
676
|
+
const lead = confirmLead(state, 'rule');
|
|
677
|
+
const nextAction = {
|
|
678
|
+
tool: 'manage_model_access',
|
|
679
|
+
arguments: { action: 'remove', rule_guid: rule.guid, confirm: true, updated_at: version, summary_id: id },
|
|
680
|
+
reason: ASK_REASON,
|
|
681
|
+
};
|
|
682
|
+
const previewNotes = ['Once it is removed, models it blocked can run again, unless another rule blocks them.', ...notes];
|
|
683
|
+
const message = lead ?? `Remove the rule for ${covers}? Nothing has changed yet.`;
|
|
684
|
+
return result({
|
|
685
|
+
action: 'remove',
|
|
686
|
+
outcome: 'preview',
|
|
687
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
688
|
+
target: targetView(located.target),
|
|
689
|
+
before: ruleView(rule, labels, caller),
|
|
690
|
+
after: null,
|
|
691
|
+
...(changedSinceSummary ? { changedSinceSummary: true } : {}),
|
|
692
|
+
warnings: [],
|
|
693
|
+
notes: previewNotes,
|
|
694
|
+
message,
|
|
695
|
+
dashboardUrl,
|
|
696
|
+
nextAction,
|
|
697
|
+
}, [
|
|
698
|
+
changedSinceSummary ? '## The rule changed since the summary (nothing removed)' : '## Remove a Model Access rule? (nothing changed yet)',
|
|
699
|
+
'',
|
|
700
|
+
...(lead ? [lead, ''] : []),
|
|
701
|
+
`**Rule for:** ${covers}`,
|
|
702
|
+
`**It reads:** ${ruleSummary(rule, labels, { quote: true, maxModels: 10 })}`,
|
|
703
|
+
...bullets([], previewNotes),
|
|
704
|
+
'',
|
|
705
|
+
'Nothing has changed yet. Show this to the user and wait for a clear yes.',
|
|
706
|
+
...nextLine(nextAction),
|
|
707
|
+
]);
|
|
708
|
+
}
|
|
709
|
+
try {
|
|
710
|
+
requireApiSuccess(await client.post('/ModelAccess/Remove', { guid: rule.guid }, signal), 'The rule could not be removed.');
|
|
711
|
+
}
|
|
712
|
+
catch (error) {
|
|
713
|
+
if (signal.aborted)
|
|
714
|
+
throw error;
|
|
715
|
+
if (error instanceof ApiResultError && /rule not found in this workspace/i.test(error.message)) {
|
|
716
|
+
return nothing(`The rule for ${covers} is not there any more: it was removed already. ${NOTHING_CHANGED}`);
|
|
717
|
+
}
|
|
718
|
+
throw changeFailure(error, 'model-access', REMOVE_UNKNOWN, workspace);
|
|
719
|
+
}
|
|
720
|
+
const removedNotes = ['Models it blocked can run again from now on, unless another rule blocks them.', ...notes];
|
|
721
|
+
const message = `Removed the rule for ${covers}.`;
|
|
722
|
+
return result({
|
|
723
|
+
action: 'remove',
|
|
724
|
+
outcome: 'removed',
|
|
725
|
+
workspace: { kind: workspace.kind, canManage: true },
|
|
726
|
+
target: targetView(located.target),
|
|
727
|
+
before: ruleView(rule, labels, caller),
|
|
728
|
+
after: null,
|
|
729
|
+
warnings: [],
|
|
730
|
+
notes: removedNotes,
|
|
731
|
+
message,
|
|
732
|
+
dashboardUrl,
|
|
733
|
+
}, ['## Model Access rule removed', '', message, ...bullets([], removedNotes)]);
|
|
734
|
+
}
|
|
735
|
+
export function registerManageModelAccess(server, client) {
|
|
736
|
+
server.registerTool('manage_model_access', {
|
|
737
|
+
title: 'Change a Wiro Model Access rule',
|
|
738
|
+
description: 'Change one Model Access rule of the connected project\'s workspace: which models the whole wallet '
|
|
739
|
+
+ '(account), one team member, one API key (project), one way in (surface) or one agent may run. A rule names '
|
|
740
|
+
+ 'categories, single models (by owner/slug) and what every other model gets (everything_else). update changes '
|
|
741
|
+
+ 'only the entries it names and keeps the rest (access unset takes an entry out), replace writes the whole rule, '
|
|
742
|
+
+ 'remove deletes it. Every rule that applies to a run must allow its model, so a narrower rule cannot open what '
|
|
743
|
+
+ 'a wider one blocks. Only the account owner, a team admin or the organization\'s owner can change rules; for '
|
|
744
|
+
+ 'anyone else this changes nothing. Without `confirm: true` it returns a change summary: the rule before and '
|
|
745
|
+
+ 'after, each entry that changes, and warnings, such as a wider rule that still blocks a model it allows. Show it, '
|
|
746
|
+
+ 'wait for a clear yes, then repeat the call exactly as its nextAction shows. If the rule changed in between, or '
|
|
747
|
+
+ 'the call asks for anything else, nothing is saved and a new summary comes back.',
|
|
748
|
+
inputSchema: strictInput({
|
|
749
|
+
action: z.enum(['update', 'replace', 'remove'])
|
|
750
|
+
.describe('update merges the entries given into the rule, replace writes the whole rule from the entries given, remove deletes the rule.'),
|
|
751
|
+
scope: z.enum(CONTROL_SCOPES).optional()
|
|
752
|
+
.describe('Whose runs the rule covers: account (the whole wallet), member (one team member), project (one API key), surface (one way in) or agent (one deployed agent). Needed unless rule_guid names the rule.'),
|
|
753
|
+
member: z.string().trim().min(1).max(320).optional()
|
|
754
|
+
.describe('Scope member, teams only: the member, by e-mail or user id.'),
|
|
755
|
+
project: z.string().trim().min(1).max(200).optional()
|
|
756
|
+
.describe('Scope project: the API key\'s project, by id or exact name. Left out, this connection\'s own key.'),
|
|
757
|
+
surface: z.enum(SURFACES).optional()
|
|
758
|
+
.describe('Scope surface: web (the dashboard), api (an API key, a local install of this server included), mcp (the hosted MCP server), llm-gateway, extension or agent.'),
|
|
759
|
+
agent: z.string().trim().min(1).max(200).optional()
|
|
760
|
+
.describe('Scope agent: the deployed agent, by name or guid. Its rule also covers the chat model it answers with and its built-in voice (voice notes and voice replies, which count as Audio & Speech).'),
|
|
761
|
+
everything_else: z.enum(['allow', 'block']).optional()
|
|
762
|
+
.describe('What every model the rule names neither by category nor by name gets. allow: every model except what the rule blocks; block: only what the rule allows. update keeps it when left out; replace and a new rule default to allow.'),
|
|
763
|
+
categories: z.array(z.object({
|
|
764
|
+
category: z.enum(CATEGORY_KEYS),
|
|
765
|
+
access: z.enum(['allow', 'block', 'unset']),
|
|
766
|
+
})).max(CATEGORY_KEYS.length).optional()
|
|
767
|
+
.describe('Categories: image, video, audio, music, 3d, llm, utilities. A category also covers models added to it later; within a rule a model\'s own entry wins, then a block among its categories. unset, with update only, takes the entry out.'),
|
|
768
|
+
models: z.array(z.object({
|
|
769
|
+
model: z.string().trim().min(1).max(200).describe('owner/slug as search_models shows it, or the model\'s id.'),
|
|
770
|
+
access: z.enum(['allow', 'block', 'unset']),
|
|
771
|
+
})).max(MAX_MODELS_PER_CALL).optional()
|
|
772
|
+
.describe(`Single models, up to ${MAX_MODELS_PER_CALL} a call (a rule holds up to ${MAX_RULE_MODELS}). unset, with update only, takes the entry out.`),
|
|
773
|
+
rule_guid: z.string().trim().min(1).max(64).optional()
|
|
774
|
+
.describe('The rule\'s guid from get_controls, in place of scope and its target.'),
|
|
775
|
+
confirm: z.boolean().default(false)
|
|
776
|
+
.describe('Set true only by repeating the change summary\'s nextAction, after the user approved that summary.'),
|
|
777
|
+
updated_at: z.number().int().min(0).optional()
|
|
778
|
+
.describe('From the change summary\'s nextAction: the version of the rule the user saw. Never set it yourself.'),
|
|
779
|
+
summary_id: summaryIdSchema,
|
|
780
|
+
}),
|
|
781
|
+
outputSchema,
|
|
782
|
+
annotations: {
|
|
783
|
+
readOnlyHint: false,
|
|
784
|
+
destructiveHint: true,
|
|
785
|
+
// Repeating a change asks for the same rule again, which changes nothing.
|
|
786
|
+
idempotentHint: true,
|
|
787
|
+
openWorldHint: true,
|
|
788
|
+
},
|
|
789
|
+
}, async (args, extra) => {
|
|
790
|
+
try {
|
|
791
|
+
checkArguments(args);
|
|
792
|
+
let list;
|
|
793
|
+
try {
|
|
794
|
+
list = await readRuleList(client, extra.signal);
|
|
795
|
+
}
|
|
796
|
+
catch (error) {
|
|
797
|
+
if (extra.signal.aborted)
|
|
798
|
+
throw error;
|
|
799
|
+
throw readFailure('The Model Access rules', error);
|
|
800
|
+
}
|
|
801
|
+
// Wiro says whether it would take a change from this connection: a member gets the refusal here.
|
|
802
|
+
if (list.workspace.canManage === false)
|
|
803
|
+
return refusedResult(args.action, list.workspace);
|
|
804
|
+
// List's caller came in the same Wiro API release as Preview: without it there is no change summary, so no change.
|
|
805
|
+
if (!list.caller)
|
|
806
|
+
throw new Error(previewMissingText('model-access', list.workspace));
|
|
807
|
+
return args.action === 'remove'
|
|
808
|
+
? await removeRule(client, args, list, extra.signal)
|
|
809
|
+
: await changeRule(client, args, list, extra.signal);
|
|
810
|
+
}
|
|
811
|
+
catch (error) {
|
|
812
|
+
return errorResult(error);
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
//# sourceMappingURL=manage-model-access.js.map
|