@wiro-ai/wiro-mcp 1.9.1 → 2.1.0
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 +95 -42
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/instructions.js +5 -5
- package/dist/instructions.js.map +1 -1
- package/dist/playbooks.d.ts.map +1 -1
- package/dist/playbooks.js +111 -94
- package/dist/playbooks.js.map +1 -1
- package/dist/prompts.js +7 -7
- package/dist/prompts.js.map +1 -1
- package/dist/tools/answer-agent-approval.d.ts +4 -0
- package/dist/tools/answer-agent-approval.d.ts.map +1 -0
- package/dist/tools/answer-agent-approval.js +354 -0
- package/dist/tools/answer-agent-approval.js.map +1 -0
- package/dist/tools/apply-agent-skills.d.ts.map +1 -1
- package/dist/tools/apply-agent-skills.js +81 -30
- package/dist/tools/apply-agent-skills.js.map +1 -1
- package/dist/tools/bind-agent-project.d.ts.map +1 -1
- package/dist/tools/bind-agent-project.js +29 -28
- package/dist/tools/bind-agent-project.js.map +1 -1
- package/dist/tools/cancel-agent-message.js +1 -1
- package/dist/tools/cancel-agent-message.js.map +1 -1
- package/dist/tools/check-model-access.d.ts.map +1 -1
- package/dist/tools/check-model-access.js +14 -11
- package/dist/tools/check-model-access.js.map +1 -1
- package/dist/tools/connect-agent-connector.d.ts +4 -0
- package/dist/tools/connect-agent-connector.d.ts.map +1 -0
- package/dist/tools/connect-agent-connector.js +634 -0
- package/dist/tools/connect-agent-connector.js.map +1 -0
- package/dist/tools/control-agent.d.ts.map +1 -1
- package/dist/tools/control-agent.js +30 -31
- package/dist/tools/control-agent.js.map +1 -1
- package/dist/tools/delete-agent.d.ts.map +1 -1
- package/dist/tools/delete-agent.js +12 -5
- package/dist/tools/delete-agent.js.map +1 -1
- package/dist/tools/deploy-agent.d.ts.map +1 -1
- package/dist/tools/deploy-agent.js +96 -44
- package/dist/tools/deploy-agent.js.map +1 -1
- package/dist/tools/disconnect-agent-connector.d.ts +4 -0
- package/dist/tools/disconnect-agent-connector.d.ts.map +1 -0
- package/dist/tools/disconnect-agent-connector.js +184 -0
- package/dist/tools/disconnect-agent-connector.js.map +1 -0
- package/dist/tools/get-account.d.ts.map +1 -1
- package/dist/tools/get-account.js +24 -8
- package/dist/tools/get-account.js.map +1 -1
- package/dist/tools/get-agent-activity.d.ts.map +1 -1
- package/dist/tools/get-agent-activity.js +285 -138
- package/dist/tools/get-agent-activity.js.map +1 -1
- package/dist/tools/get-agent-connector-permissions.d.ts +145 -0
- package/dist/tools/get-agent-connector-permissions.d.ts.map +1 -0
- package/dist/tools/get-agent-connector-permissions.js +194 -0
- package/dist/tools/get-agent-connector-permissions.js.map +1 -0
- package/dist/tools/get-agent-conversation.d.ts.map +1 -1
- package/dist/tools/get-agent-conversation.js +39 -10
- package/dist/tools/get-agent-conversation.js.map +1 -1
- package/dist/tools/get-agent-message.d.ts.map +1 -1
- package/dist/tools/get-agent-message.js +13 -5
- package/dist/tools/get-agent-message.js.map +1 -1
- package/dist/tools/get-agent-template.d.ts.map +1 -1
- package/dist/tools/get-agent-template.js +13 -15
- package/dist/tools/get-agent-template.js.map +1 -1
- package/dist/tools/get-my-agent.d.ts.map +1 -1
- package/dist/tools/get-my-agent.js +38 -5
- package/dist/tools/get-my-agent.js.map +1 -1
- package/dist/tools/get-task-price.d.ts.map +1 -1
- package/dist/tools/get-task-price.js +4 -4
- package/dist/tools/get-task-price.js.map +1 -1
- package/dist/tools/get-task.d.ts.map +1 -1
- package/dist/tools/get-task.js +4 -4
- package/dist/tools/get-task.js.map +1 -1
- package/dist/tools/index.d.ts +8 -3
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +17 -6
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/kill-task.js +1 -1
- package/dist/tools/kill-task.js.map +1 -1
- package/dist/tools/list-agent-connectors.d.ts +4 -0
- package/dist/tools/list-agent-connectors.d.ts.map +1 -0
- package/dist/tools/list-agent-connectors.js +325 -0
- package/dist/tools/list-agent-connectors.js.map +1 -0
- package/dist/tools/list-agent-skills.d.ts.map +1 -1
- package/dist/tools/list-agent-skills.js +59 -40
- package/dist/tools/list-agent-skills.js.map +1 -1
- package/dist/tools/list-agent-templates.d.ts.map +1 -1
- package/dist/tools/list-agent-templates.js +7 -5
- package/dist/tools/list-agent-templates.js.map +1 -1
- package/dist/tools/list-my-agents.d.ts.map +1 -1
- package/dist/tools/list-my-agents.js +7 -3
- package/dist/tools/list-my-agents.js.map +1 -1
- package/dist/tools/list-projects.d.ts.map +1 -1
- package/dist/tools/list-projects.js +7 -4
- package/dist/tools/list-projects.js.map +1 -1
- package/dist/tools/list-studio-results.d.ts.map +1 -1
- package/dist/tools/list-studio-results.js +3 -2
- package/dist/tools/list-studio-results.js.map +1 -1
- package/dist/tools/list-tasks.d.ts.map +1 -1
- package/dist/tools/list-tasks.js +6 -3
- package/dist/tools/list-tasks.js.map +1 -1
- package/dist/tools/manage-agent-billing.d.ts.map +1 -1
- package/dist/tools/manage-agent-billing.js +216 -359
- package/dist/tools/manage-agent-billing.js.map +1 -1
- package/dist/tools/manage-agent-custom-skill.d.ts.map +1 -1
- package/dist/tools/manage-agent-custom-skill.js +77 -30
- package/dist/tools/manage-agent-custom-skill.js.map +1 -1
- package/dist/tools/manage-model-access.js +1 -1
- package/dist/tools/manage-model-access.js.map +1 -1
- package/dist/tools/manage-spend-limit.js +1 -1
- package/dist/tools/manage-spend-limit.js.map +1 -1
- package/dist/tools/run-model.d.ts.map +1 -1
- package/dist/tools/run-model.js +4 -4
- package/dist/tools/run-model.js.map +1 -1
- package/dist/tools/schemas.d.ts +11 -0
- package/dist/tools/schemas.d.ts.map +1 -1
- package/dist/tools/schemas.js +19 -1
- package/dist/tools/schemas.js.map +1 -1
- package/dist/tools/send-agent-message.d.ts.map +1 -1
- package/dist/tools/send-agent-message.js +45 -20
- package/dist/tools/send-agent-message.js.map +1 -1
- package/dist/tools/set-agent-connector-accounts.d.ts +4 -0
- package/dist/tools/set-agent-connector-accounts.d.ts.map +1 -0
- package/dist/tools/set-agent-connector-accounts.js +263 -0
- package/dist/tools/set-agent-connector-accounts.js.map +1 -0
- package/dist/tools/set-agent-connector-permissions.d.ts +4 -0
- package/dist/tools/set-agent-connector-permissions.d.ts.map +1 -0
- package/dist/tools/set-agent-connector-permissions.js +280 -0
- package/dist/tools/set-agent-connector-permissions.js.map +1 -0
- package/dist/tools/set-agent-credentials.d.ts.map +1 -1
- package/dist/tools/set-agent-credentials.js +52 -169
- package/dist/tools/set-agent-credentials.js.map +1 -1
- package/dist/tools/update-agent.d.ts.map +1 -1
- package/dist/tools/update-agent.js +39 -18
- package/dist/tools/update-agent.js.map +1 -1
- package/dist/tools/wait-for-task.d.ts.map +1 -1
- package/dist/tools/wait-for-task.js +4 -4
- package/dist/tools/wait-for-task.js.map +1 -1
- package/dist/tools/wait-for-workflow-run.d.ts.map +1 -1
- package/dist/tools/wait-for-workflow-run.js +6 -3
- package/dist/tools/wait-for-workflow-run.js.map +1 -1
- package/dist/utils/agent-approvals.d.ts +328 -0
- package/dist/utils/agent-approvals.d.ts.map +1 -0
- package/dist/utils/agent-approvals.js +620 -0
- package/dist/utils/agent-approvals.js.map +1 -0
- package/dist/utils/agent-catalog.d.ts +15 -25
- package/dist/utils/agent-catalog.d.ts.map +1 -1
- package/dist/utils/agent-catalog.js +27 -36
- package/dist/utils/agent-catalog.js.map +1 -1
- package/dist/utils/agent-config.d.ts +87 -292
- package/dist/utils/agent-config.d.ts.map +1 -1
- package/dist/utils/agent-config.js +150 -787
- package/dist/utils/agent-config.js.map +1 -1
- package/dist/utils/agent-connectors.d.ts +197 -0
- package/dist/utils/agent-connectors.d.ts.map +1 -0
- package/dist/utils/agent-connectors.js +415 -0
- package/dist/utils/agent-connectors.js.map +1 -0
- package/dist/utils/agent-message.d.ts +106 -4
- package/dist/utils/agent-message.d.ts.map +1 -1
- package/dist/utils/agent-message.js +84 -14
- package/dist/utils/agent-message.js.map +1 -1
- package/dist/utils/agent-setup.d.ts +19 -14
- package/dist/utils/agent-setup.d.ts.map +1 -1
- package/dist/utils/agent-setup.js +171 -106
- package/dist/utils/agent-setup.js.map +1 -1
- package/dist/utils/agent-targets.d.ts.map +1 -1
- package/dist/utils/agent-targets.js +40 -14
- package/dist/utils/agent-targets.js.map +1 -1
- package/dist/utils/agent-turns.d.ts +256 -7
- package/dist/utils/agent-turns.d.ts.map +1 -1
- package/dist/utils/agent-turns.js +260 -71
- package/dist/utils/agent-turns.js.map +1 -1
- package/dist/utils/connector-permissions.d.ts +184 -0
- package/dist/utils/connector-permissions.d.ts.map +1 -0
- package/dist/utils/connector-permissions.js +439 -0
- package/dist/utils/connector-permissions.js.map +1 -0
- package/dist/utils/controls.d.ts +6 -2
- package/dist/utils/controls.d.ts.map +1 -1
- package/dist/utils/controls.js +20 -9
- package/dist/utils/controls.js.map +1 -1
- package/dist/utils/format.d.ts +10 -0
- package/dist/utils/format.d.ts.map +1 -1
- package/dist/utils/format.js +117 -38
- package/dist/utils/format.js.map +1 -1
- package/dist/utils/quoted-text.d.ts +2 -0
- package/dist/utils/quoted-text.d.ts.map +1 -1
- package/dist/utils/quoted-text.js +5 -0
- package/dist/utils/quoted-text.js.map +1 -1
- package/dist/utils/structured.d.ts +3 -0
- package/dist/utils/structured.d.ts.map +1 -1
- package/dist/utils/structured.js +7 -2
- package/dist/utils/structured.js.map +1 -1
- package/dist/utils/studio-runs.d.ts.map +1 -1
- package/dist/utils/studio-runs.js +6 -4
- package/dist/utils/studio-runs.js.map +1 -1
- package/dist/utils/user-agent.d.ts +271 -100
- package/dist/utils/user-agent.d.ts.map +1 -1
- package/dist/utils/user-agent.js +434 -159
- package/dist/utils/user-agent.js.map +1 -1
- package/dist/utils/wiro-project.d.ts +25 -0
- package/dist/utils/wiro-project.d.ts.map +1 -1
- package/dist/utils/wiro-project.js +76 -2
- package/dist/utils/wiro-project.js.map +1 -1
- package/dist/utils/workflow.d.ts.map +1 -1
- package/dist/utils/workflow.js +4 -2
- package/dist/utils/workflow.js.map +1 -1
- package/dist/utils/workspace.d.ts +3 -1
- package/dist/utils/workspace.d.ts.map +1 -1
- package/dist/utils/workspace.js +2 -1
- package/dist/utils/workspace.js.map +1 -1
- package/package.json +1 -1
- package/dist/tools/connect-agent-oauth.d.ts +0 -4
- package/dist/tools/connect-agent-oauth.d.ts.map +0 -1
- package/dist/tools/connect-agent-oauth.js +0 -778
- package/dist/tools/connect-agent-oauth.js.map +0 -1
- package/dist/tools/disconnect-agent-oauth.d.ts +0 -4
- package/dist/tools/disconnect-agent-oauth.d.ts.map +0 -1
- package/dist/tools/disconnect-agent-oauth.js +0 -138
- package/dist/tools/disconnect-agent-oauth.js.map +0 -1
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { asString, errorResult, errorText, isPlainObject, requireApiSuccess, toInteger, } from '../utils/api.js';
|
|
3
|
+
import { agentStatusOf, collectStrings, customSkillEntries, holdsSecretMask, isAgentRunning, LIVE_ADDED_NOTE, LIVE_OR_RESTART_NOTE, loadCredentialMeta, pausedSkillTitles, readUserAgent, redactSecrets, SECRET_MASK_TEXT, } from '../utils/agent-config.js';
|
|
4
|
+
import { CONNECT_MODES, SIGN_IN_LINK_TTL_SECONDS, connectionOf, connectionSchema, connectorFieldSchema, fieldsForMode, hangsOnConnector, isAllowedRedirectUrl, listConnections, readConnector, signsIn, toAgentConnections, waysText, withCatalogWay, } from '../utils/agent-connectors.js';
|
|
5
|
+
import { DEFAULT_WAIT_SECONDS, MAX_WAIT_SECONDS, pollUntil, reportProgress } from '../utils/poll.js';
|
|
6
|
+
import { jsonSpan } from '../utils/quoted-text.js';
|
|
7
|
+
import { AGENT_AI_USAGE, taskLabel, taskNamedByWiro } from '../utils/user-agent.js';
|
|
8
|
+
import { agentProjectState, CANNOT_BIND_TEXT, entriesTurnedOnByProject, isOtherWorkspace, OTHER_WORKSPACE_TEXT, OUTSIDE_LIST_TEXT, projectLabel, projectTurnsOnNote, readProjects, } from '../utils/wiro-project.js';
|
|
9
|
+
import { nextActionSchema, strictInput } from './schemas.js';
|
|
10
|
+
// What the route takes as a project's id.
|
|
11
|
+
const PROJECT_ID_PATTERN = /^[1-9][0-9]{0,17}$/;
|
|
12
|
+
const STATES = [
|
|
13
|
+
'connected',
|
|
14
|
+
'authorize_in_browser',
|
|
15
|
+
'waiting_for_user',
|
|
16
|
+
'setup_needed',
|
|
17
|
+
'confirm_needed',
|
|
18
|
+
'not_connected',
|
|
19
|
+
'failed',
|
|
20
|
+
'reauth_needed',
|
|
21
|
+
];
|
|
22
|
+
const outputSchema = {
|
|
23
|
+
action: z.enum(['connect', 'status']),
|
|
24
|
+
agentGuid: z.string(),
|
|
25
|
+
connector: z.string(),
|
|
26
|
+
title: z.string(),
|
|
27
|
+
state: z.enum(STATES),
|
|
28
|
+
mode: z.enum(CONNECT_MODES).optional(),
|
|
29
|
+
authorizeUrl: z.string().optional()
|
|
30
|
+
.describe('The sign-in link for the user to open in their own browser. Never open it for them.'),
|
|
31
|
+
expiresInSeconds: z.number().optional(),
|
|
32
|
+
fields: z.array(connectorFieldSchema).optional()
|
|
33
|
+
.describe('setup_needed: the values to ask the user for, with where each one comes from.'),
|
|
34
|
+
missing: z.array(z.string()).optional()
|
|
35
|
+
.describe('setup_needed: the names of the required values that are missing.'),
|
|
36
|
+
project: z.object({ id: z.string().optional(), name: z.string().optional() }).optional()
|
|
37
|
+
.describe('A connector that runs with a Wiro project (the wiro connector): the project connecting it binds to the '
|
|
38
|
+
+ 'agent as its own Wiro project, which the agent\'s video and voice skills use too.'),
|
|
39
|
+
replacesProject: z.object({
|
|
40
|
+
id: z.string().optional(),
|
|
41
|
+
name: z.string().optional(),
|
|
42
|
+
outsideList: z.boolean().optional().describe('The agent holds a project outside this connection\'s list or a key saved earlier.'),
|
|
43
|
+
}).optional()
|
|
44
|
+
.describe('The agent\'s Wiro project now, which the connect replaces. Left out when it has none, or when it is the same project.'),
|
|
45
|
+
connection: connectionSchema.optional(),
|
|
46
|
+
applied: z.number().int().nonnegative().optional()
|
|
47
|
+
.describe('1 when the saved connection changed, 0 when it already held this.'),
|
|
48
|
+
restart: z.enum(['live', 'scheduled', 'none', 'failed']).optional()
|
|
49
|
+
.describe('live: the running agent got the connector with no restart. scheduled: a running agent restarts to load the connector. none: nothing to load, or the agent is not running. failed: it is saved, but a running agent needs control_agent restart.'),
|
|
50
|
+
message: z.string(),
|
|
51
|
+
nextAction: nextActionSchema.optional(),
|
|
52
|
+
};
|
|
53
|
+
function result(action, context, state, message, extra = {}, textExtra = []) {
|
|
54
|
+
const lines = [`## ${context.connector.title}: ${state.replace(/_/g, ' ')}`, '', message, ...textExtra];
|
|
55
|
+
if (isPlainObject(extra.nextAction)) {
|
|
56
|
+
lines.push('', `Next: call ${String(extra.nextAction.tool)} with ${jsonSpan(extra.nextAction.arguments)}.`);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
content: [{ type: 'text', text: lines.join('\n') }],
|
|
60
|
+
structuredContent: {
|
|
61
|
+
action,
|
|
62
|
+
agentGuid: context.agentGuid,
|
|
63
|
+
connector: context.connector.slug,
|
|
64
|
+
title: context.connector.title,
|
|
65
|
+
state,
|
|
66
|
+
message,
|
|
67
|
+
...extra,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** Whether a value fills a field: an empty text or null clears it instead. */
|
|
72
|
+
function fills(value) {
|
|
73
|
+
if (value === null || value === undefined)
|
|
74
|
+
return false;
|
|
75
|
+
if (typeof value === 'string')
|
|
76
|
+
return value.trim() !== '';
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/** A JSON object or list sent as its text, such as a service account file, or undefined for any other text. */
|
|
80
|
+
function jsonInText(text) {
|
|
81
|
+
const trimmed = text.trim();
|
|
82
|
+
if (!/^[[{]/.test(trimmed))
|
|
83
|
+
return undefined;
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(trimmed);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Every value sent, to hide from an error text that quotes one back: each string of 8 or more characters at any
|
|
93
|
+
* depth (short values such as select options and flags are not secrets and would garble messages), as it is and
|
|
94
|
+
* as JSON writes it, and the whole JSON text of a JSON value. A JSON field sent as its text counts at every depth
|
|
95
|
+
* too, so a service's refusal that quotes one key of a service account file shows none of it.
|
|
96
|
+
*/
|
|
97
|
+
function secretsIn(values) {
|
|
98
|
+
const strings = [];
|
|
99
|
+
const secrets = [];
|
|
100
|
+
for (const value of Object.values(values)) {
|
|
101
|
+
collectStrings(value, strings);
|
|
102
|
+
if (isPlainObject(value) || Array.isArray(value))
|
|
103
|
+
secrets.push(JSON.stringify(value));
|
|
104
|
+
else if (typeof value === 'string')
|
|
105
|
+
collectStrings(jsonInText(value), strings);
|
|
106
|
+
}
|
|
107
|
+
for (const text of strings)
|
|
108
|
+
secrets.push(text, JSON.stringify(text).slice(1, -1));
|
|
109
|
+
return [...new Set(secrets.map(secret => secret.trim()).filter(secret => secret.length >= 8))];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Why accounts sent with a connect are refused, or null. A connector with account choices takes only their keys, and
|
|
113
|
+
* a single choice one id. Another connector's account keys are its setup steps' to name, so Wiro checks them.
|
|
114
|
+
*/
|
|
115
|
+
function accountsRefusal(connector, accounts) {
|
|
116
|
+
if (connector.accountPickers.length === 0)
|
|
117
|
+
return null;
|
|
118
|
+
const unknown = Object.keys(accounts).filter(key => !connector.accountPickers.some(picker => picker.key === key));
|
|
119
|
+
if (unknown.length > 0) {
|
|
120
|
+
return `${connector.title} has no account choice ${unknown.map(key => `"${key}"`).join(', ')}. Its choices: `
|
|
121
|
+
+ `${connector.accountPickers.map(picker => `"${picker.key}" (${picker.label})`).join(', ')}. Nothing was sent.`;
|
|
122
|
+
}
|
|
123
|
+
const single = connector.accountPickers.find(picker => !picker.multiple && (accounts[picker.key]?.length ?? 0) > 1);
|
|
124
|
+
return single ? `${single.label} takes one account. Nothing was sent.` : null;
|
|
125
|
+
}
|
|
126
|
+
/** Account choices by label: "Advertisers", "Facebook Pages and Instagram accounts". */
|
|
127
|
+
function pickerLabels(pickers) {
|
|
128
|
+
return pickers.map(picker => picker.label).join(' and ');
|
|
129
|
+
}
|
|
130
|
+
function fieldNeedLines(fields) {
|
|
131
|
+
return fields.flatMap((field) => {
|
|
132
|
+
const lines = [`- **${field.label}** (\`${field.name}\`${field.secret ? ', secret' : ''}${field.appKey ? ', a key of the user\'s own app' : ''})`];
|
|
133
|
+
if (field.description)
|
|
134
|
+
lines.push(...field.description.split(/\r?\n/).map(line => ` ${line}`));
|
|
135
|
+
return lines;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* What the agent says about a new connection: the scheduled tasks that are on and start running with this
|
|
140
|
+
* connector, as people see them, and whether it runs. A task runs while any one of its connectors is connected, so
|
|
141
|
+
* one that can use another connector of `connections` that is connected already runs and is left out. `read` is the
|
|
142
|
+
* agent as read just before the connect, when it was; otherwise it is read now. Nothing when the agent cannot be read.
|
|
143
|
+
*/
|
|
144
|
+
async function agentAfterConnect(context, connections, read) {
|
|
145
|
+
try {
|
|
146
|
+
const row = read ?? await readUserAgent(context.client, context.agentGuid, context.signal);
|
|
147
|
+
return {
|
|
148
|
+
tasks: customSkillEntries(row)
|
|
149
|
+
.filter(entry => entry.kind === 'scheduled_task' && entry.enabled && !entry.notWritten && !entry.skillOff
|
|
150
|
+
&& hangsOnConnector(entry.requiresConnectors, context.connector.slug, connections))
|
|
151
|
+
.map(entry => taskLabel(entry.key, taskNamedByWiro(entry))),
|
|
152
|
+
running: isAgentRunning(agentStatusOf(row)),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return { tasks: [], running: false };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function restartOf(value) {
|
|
160
|
+
const restart = asString(value);
|
|
161
|
+
return restart === 'live' || restart === 'scheduled' || restart === 'none' || restart === 'failed' ? restart : undefined;
|
|
162
|
+
}
|
|
163
|
+
function restartText(restart, applied) {
|
|
164
|
+
if (applied === 0)
|
|
165
|
+
return 'It already held this, so the agent does not restart.';
|
|
166
|
+
switch (restart) {
|
|
167
|
+
case 'live':
|
|
168
|
+
return LIVE_ADDED_NOTE;
|
|
169
|
+
case 'scheduled':
|
|
170
|
+
return 'A running agent restarts to load it, which takes a minute or two.';
|
|
171
|
+
case 'failed':
|
|
172
|
+
return 'It is saved, but the running agent could not be restarted: control_agent with action restart loads it.';
|
|
173
|
+
default:
|
|
174
|
+
return 'The agent loads it the next time it starts.';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function viewOf(project) {
|
|
178
|
+
return { id: project.view.id, name: project.view.name };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The project a connect would bind, the one it replaces and what the bind turns on, read afresh for the preview and
|
|
182
|
+
* for the confirm. A project Wiro would refuse to bind is refused here first, with bind_agent_project's sentences: an
|
|
183
|
+
* agent in another workspace, or a listed project this connection cannot bind. Reads that fail only leave out what
|
|
184
|
+
* they would show; Wiro still checks the connect.
|
|
185
|
+
*/
|
|
186
|
+
async function projectPlan(context, projectId) {
|
|
187
|
+
let projects;
|
|
188
|
+
try {
|
|
189
|
+
projects = await readProjects(context.client, context.signal);
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (context.signal.aborted)
|
|
193
|
+
throw error;
|
|
194
|
+
return { project: projectId !== undefined ? { id: projectId } : {}, already: false, turnsOn: [], titles: new Map() };
|
|
195
|
+
}
|
|
196
|
+
let row;
|
|
197
|
+
try {
|
|
198
|
+
row = await readUserAgent(context.client, context.agentGuid, context.signal);
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (context.signal.aborted)
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
if (row && isOtherWorkspace(row, projects))
|
|
205
|
+
throw new Error(OTHER_WORKSPACE_TEXT);
|
|
206
|
+
const target = projectId !== undefined
|
|
207
|
+
? projects.find(item => item.view.id === projectId)
|
|
208
|
+
: projects.find(item => item.view.connected);
|
|
209
|
+
if (projectId !== undefined && !target) {
|
|
210
|
+
throw new Error(`No project with the id ${projectId} is in this connection's workspace, and nothing was changed. list_projects shows the projects you can choose.`);
|
|
211
|
+
}
|
|
212
|
+
if (target && !target.view.canBind)
|
|
213
|
+
throw new Error(CANNOT_BIND_TEXT);
|
|
214
|
+
const project = target ? viewOf(target) : {};
|
|
215
|
+
if (!row)
|
|
216
|
+
return { project, already: false, turnsOn: [], titles: new Map() };
|
|
217
|
+
const { current, outsideList, holdsConnectionKey } = agentProjectState(row, projects, context.client);
|
|
218
|
+
const already = target ? target === current : holdsConnectionKey;
|
|
219
|
+
if (already)
|
|
220
|
+
return { project, already: true, row, turnsOn: [], titles: new Map() };
|
|
221
|
+
// Binding the project writes the agent's Wiro key, which turns on what requires the project, as a bind does.
|
|
222
|
+
const turnsOn = entriesTurnedOnByProject(row, current !== null || outsideList || holdsConnectionKey);
|
|
223
|
+
const titles = await pausedSkillTitles(context.client, turnsOn, context.signal);
|
|
224
|
+
const replaces = current ? viewOf(current) : outsideList ? { outsideList: true } : undefined;
|
|
225
|
+
return { project, ...(replaces ? { replaces } : {}), already: false, row, turnsOn, titles };
|
|
226
|
+
}
|
|
227
|
+
/** What a connect with a Wiro project does, said before the user's yes. */
|
|
228
|
+
function projectPreviewText(title, plan) {
|
|
229
|
+
const label = projectLabel(plan.project);
|
|
230
|
+
const binds = plan.already
|
|
231
|
+
? `${title} runs with the agent's own Wiro project, ${label}, which stays as it is.`
|
|
232
|
+
: `${title} runs with the agent's own Wiro project: connecting it makes ${label} that project, as bind_agent_project `
|
|
233
|
+
+ 'does, so the agent\'s video and voice skills use it too.';
|
|
234
|
+
const replaces = plan.replaces
|
|
235
|
+
? ` It replaces ${plan.replaces.outsideList ? OUTSIDE_LIST_TEXT : projectLabel(plan.replaces)}, the agent's project now.`
|
|
236
|
+
: '';
|
|
237
|
+
const turnsOn = projectTurnsOnNote(plan.turnsOn, 'once', plan.titles);
|
|
238
|
+
return `${binds}${replaces} Model runs through it are billed to the wallet of the workspace that owns that project, `
|
|
239
|
+
+ `and the agent's spend limits and Model Access rules apply to them.${turnsOn ? ` ${turnsOn}` : ''}`;
|
|
240
|
+
}
|
|
241
|
+
async function connect(context, input) {
|
|
242
|
+
const { client, agentGuid, connector, signal } = context;
|
|
243
|
+
const hybrid = connector.ways.includes('sign_in') && connector.ways.includes('key');
|
|
244
|
+
if (input.mode && !hybrid) {
|
|
245
|
+
return errorResult(new Error(`${connector.title} connects only with ${waysText(connector.ways)}, so it takes no mode. Leave mode out.`));
|
|
246
|
+
}
|
|
247
|
+
const takesProject = connector.fields.some(field => field.project);
|
|
248
|
+
if (input.projectId !== undefined && !takesProject) {
|
|
249
|
+
return errorResult(new Error(`${connector.title} takes no Wiro project. Leave project_id out.`));
|
|
250
|
+
}
|
|
251
|
+
if (input.redirectUrl !== undefined && !isAllowedRedirectUrl(input.redirectUrl.trim())) {
|
|
252
|
+
return errorResult(new Error('redirect_url must be an https address, or http://localhost or http://127.0.0.1 on the user\'s own machine. Leave it out to use Wiro\'s own page. Nothing was sent.'));
|
|
253
|
+
}
|
|
254
|
+
const fields = fieldsForMode(connector, input.mode);
|
|
255
|
+
const values = {};
|
|
256
|
+
const unknown = [];
|
|
257
|
+
for (const [rawName, value] of Object.entries(input.values ?? {})) {
|
|
258
|
+
const name = rawName.trim();
|
|
259
|
+
const field = connector.fields.find(candidate => candidate.name === name);
|
|
260
|
+
if (!field || field.project) {
|
|
261
|
+
unknown.push(name);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (!fields.includes(field)) {
|
|
265
|
+
return errorResult(new Error(`${field.label} (${name}) belongs to mode ${field.mode}, not mode ${input.mode ?? 'sign_in'}. Nothing was sent.`));
|
|
266
|
+
}
|
|
267
|
+
values[name] = value;
|
|
268
|
+
}
|
|
269
|
+
if (unknown.length > 0) {
|
|
270
|
+
const names = fields.filter(field => !field.project).map(field => field.name);
|
|
271
|
+
return errorResult(new Error(`Unknown value${unknown.length > 1 ? 's' : ''} ${unknown.map(name => `"${name}"`).join(', ')}. `
|
|
272
|
+
+ `${connector.title} takes ${names.length > 0 ? names.map(name => `"${name}"`).join(', ') : 'no values'}`
|
|
273
|
+
+ `${takesProject ? ', and project_id for its Wiro project' : ''}. Nothing was sent.`));
|
|
274
|
+
}
|
|
275
|
+
const secrets = secretsIn(values);
|
|
276
|
+
const masked = Object.entries(values).filter(([, value]) => holdsSecretMask(value)).map(([name]) => name);
|
|
277
|
+
if (masked.length > 0) {
|
|
278
|
+
return errorResult(new Error(`${masked.join(', ')} ${masked.length > 1 ? 'hold' : 'holds'} ${SECRET_MASK_TEXT}. Send the value itself. Nothing was sent.`));
|
|
279
|
+
}
|
|
280
|
+
const accounts = input.accounts && Object.keys(input.accounts).length > 0 ? input.accounts : undefined;
|
|
281
|
+
const accountRefusal = accounts ? accountsRefusal(connector, accounts) : null;
|
|
282
|
+
if (accountRefusal)
|
|
283
|
+
return errorResult(new Error(accountRefusal));
|
|
284
|
+
const connections = await listConnections(client, agentGuid, signal);
|
|
285
|
+
const listed = connectionOf(connections, connector.slug);
|
|
286
|
+
const existing = listed ? withCatalogWay(listed, connector) : undefined;
|
|
287
|
+
const saved = new Set(existing?.savedFields ?? []);
|
|
288
|
+
const missing = fields.filter(field => field.required && !field.project
|
|
289
|
+
&& (Object.hasOwn(values, field.name) ? !fills(values[field.name]) : !saved.has(field.name)));
|
|
290
|
+
const mode = hybrid ? input.mode ?? 'sign_in' : undefined;
|
|
291
|
+
const signIn = signsIn(connector, input.mode);
|
|
292
|
+
if (missing.length > 0) {
|
|
293
|
+
const appKeys = missing.some(field => field.appKey);
|
|
294
|
+
return result('connect', context, 'setup_needed', `${connector.title} needs ${missing.map(field => field.label).join(', ')} first.`
|
|
295
|
+
+ `${appKeys ? ' The sign-in uses the user\'s own app at the service, so its keys come with the connect call.' : ''}`
|
|
296
|
+
+ `${appKeys && connector.wiroApp ? ' Without an app of their own, the user can instead sign in with Wiro\'s own app, with nothing to type, on the agent\'s Connectors tab in the Wiro web app.' : ''}`
|
|
297
|
+
+ ' Ask the user for them, then call connect_agent_connector again with them in values. Never put them in a message to an agent. Nothing was sent.', {
|
|
298
|
+
...(mode ? { mode } : {}),
|
|
299
|
+
fields: fields.filter(field => !field.project),
|
|
300
|
+
missing: missing.map(field => field.name),
|
|
301
|
+
...(existing ? { connection: existing } : {}),
|
|
302
|
+
nextAction: {
|
|
303
|
+
tool: 'connect_agent_connector',
|
|
304
|
+
arguments: {
|
|
305
|
+
action: 'connect',
|
|
306
|
+
agent_guid: agentGuid,
|
|
307
|
+
connector: connector.slug,
|
|
308
|
+
...(input.mode ? { mode: input.mode } : {}),
|
|
309
|
+
...(accounts ? { accounts } : {}),
|
|
310
|
+
},
|
|
311
|
+
reason: `Call it again with values for ${missing.map(field => field.name).join(', ')} once the user gives them.`,
|
|
312
|
+
},
|
|
313
|
+
}, ['', ...fieldNeedLines(missing)]);
|
|
314
|
+
}
|
|
315
|
+
const connectedNow = existing?.status === 'connected';
|
|
316
|
+
const previewing = !input.confirm && (takesProject || connectedNow);
|
|
317
|
+
const plan = takesProject ? await projectPlan(context, input.projectId) : undefined;
|
|
318
|
+
const project = plan?.project;
|
|
319
|
+
if (previewing) {
|
|
320
|
+
// A connection that reads connected may still be one the service revoked, so nothing says it keeps working.
|
|
321
|
+
const typedValues = connector.fields.some(field => !field.project);
|
|
322
|
+
const replace = connectedNow
|
|
323
|
+
? `${connector.title} is already connected${existing?.connectedAt ? ` (since ${existing.connectedAt})` : ''}. Connecting again replaces that connection${signIn ? ' once the new sign-in is approved; until then the current one stays as it is' : typedValues ? ' and its saved values' : ''}.`
|
|
324
|
+
: '';
|
|
325
|
+
const pays = plan ? projectPreviewText(connector.title, plan) : '';
|
|
326
|
+
const sendAgain = Object.keys(values).length > 0 ? ' Repeat the call with confirm: true and the same values.' : '';
|
|
327
|
+
return result('connect', context, 'confirm_needed', `${[replace, pays].filter(Boolean).join(' ')} Nothing has changed yet. Ask the user to confirm.${sendAgain}`, {
|
|
328
|
+
...(mode ? { mode } : {}),
|
|
329
|
+
...(project ? { project } : {}),
|
|
330
|
+
...(plan?.replaces ? { replacesProject: plan.replaces } : {}),
|
|
331
|
+
...(existing ? { connection: existing } : {}),
|
|
332
|
+
nextAction: {
|
|
333
|
+
tool: 'connect_agent_connector',
|
|
334
|
+
arguments: {
|
|
335
|
+
action: 'connect',
|
|
336
|
+
agent_guid: agentGuid,
|
|
337
|
+
connector: connector.slug,
|
|
338
|
+
...(input.mode ? { mode: input.mode } : {}),
|
|
339
|
+
...(input.projectId !== undefined ? { project_id: input.projectId } : {}),
|
|
340
|
+
...(input.redirectUrl ? { redirect_url: input.redirectUrl } : {}),
|
|
341
|
+
...(accounts ? { accounts } : {}),
|
|
342
|
+
confirm: true,
|
|
343
|
+
},
|
|
344
|
+
reason: `Ask the user to confirm this first.${sendAgain ? ' Add the same values again: they are never repeated here.' : ''}`,
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
let body;
|
|
349
|
+
try {
|
|
350
|
+
body = requireApiSuccess(await client.post('/UserAgent/ConnectorConnect', {
|
|
351
|
+
useragentguid: agentGuid,
|
|
352
|
+
slug: connector.slug,
|
|
353
|
+
...(Object.keys(values).length > 0 ? { values } : {}),
|
|
354
|
+
...(hybrid && input.mode ? { mode: input.mode === 'key' ? 'key' : 'oauth' } : {}),
|
|
355
|
+
...(input.projectId !== undefined ? { wiroprojectguid: input.projectId } : {}),
|
|
356
|
+
...(input.redirectUrl ? { redirecturl: input.redirectUrl.trim() } : {}),
|
|
357
|
+
...(accounts ? { accounts } : {}),
|
|
358
|
+
}, signal), `${connector.title} could not be connected.`);
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
return errorResult(new Error(redactSecrets(`${errorText(error)} Nothing was saved.`, secrets)));
|
|
362
|
+
}
|
|
363
|
+
const authorizeUrl = asString(body.authorizeUrl);
|
|
364
|
+
if (authorizeUrl) {
|
|
365
|
+
const ownApp = connector.fields.some(field => field.appKey);
|
|
366
|
+
const consent = ownApp
|
|
367
|
+
? 'Its consent page shows the user\'s own app.'
|
|
368
|
+
: 'Its consent page names the agent\'s owner: its organization, team or account.';
|
|
369
|
+
const after = input.redirectUrl
|
|
370
|
+
? `After approving, the browser goes to ${input.redirectUrl.trim()}.`
|
|
371
|
+
: 'After approving, the browser lands on a page that says how it went.';
|
|
372
|
+
// A sign-in with account choices acts only on the accounts chosen, and they are listed once it finishes. Wiro keeps
|
|
373
|
+
// the accounts a connection holds through a new sign-in that sends none (a reconnect after it expired or was
|
|
374
|
+
// revoked, or a replace), so only a choice it lacks is asked for.
|
|
375
|
+
const toChoose = connector.accountPickers.filter(picker => !existing?.accounts[picker.key]?.length);
|
|
376
|
+
const kept = connector.accountPickers.filter(picker => !toChoose.includes(picker));
|
|
377
|
+
const accountsLater = accounts
|
|
378
|
+
? ' The accounts given are saved with the connection once the sign-in finishes.'
|
|
379
|
+
: `${kept.length > 0 ? ` The ${pickerLabels(kept)} chosen before stay through the new sign-in, and set_agent_connector_accounts changes them once it finishes.` : ''}`
|
|
380
|
+
+ `${toChoose.length > 0 ? ` Once it finishes, choose the ${pickerLabels(toChoose)} it may act on: set_agent_connector_accounts lists the ones the sign-in reaches.` : ''}`;
|
|
381
|
+
return result('connect', context, 'authorize_in_browser', `The user must open this link in their own browser and approve access within ${SIGN_IN_LINK_TTL_SECONDS / 60} minutes. Never open it for them. ${consent} `
|
|
382
|
+
+ `${after}${accountsLater} Give the link to the user and check the status once they say they are done.`, {
|
|
383
|
+
...(mode ? { mode } : {}),
|
|
384
|
+
authorizeUrl,
|
|
385
|
+
expiresInSeconds: SIGN_IN_LINK_TTL_SECONDS,
|
|
386
|
+
...(existing ? { connection: existing } : {}),
|
|
387
|
+
nextAction: {
|
|
388
|
+
tool: 'connect_agent_connector',
|
|
389
|
+
arguments: {
|
|
390
|
+
action: 'status',
|
|
391
|
+
agent_guid: agentGuid,
|
|
392
|
+
connector: connector.slug,
|
|
393
|
+
since_connected_at: connectedNow ? existing?.connectedAt ?? '' : '',
|
|
394
|
+
wait_seconds: DEFAULT_WAIT_SECONDS,
|
|
395
|
+
},
|
|
396
|
+
reason: 'After the user has approved access, check that the connection finished.',
|
|
397
|
+
},
|
|
398
|
+
}, ['', authorizeUrl]);
|
|
399
|
+
}
|
|
400
|
+
const answered = toAgentConnections(body.useragentconnectors)[0];
|
|
401
|
+
const connection = answered ? withCatalogWay(answered, connector) : undefined;
|
|
402
|
+
const applied = toInteger(body.applied, 1);
|
|
403
|
+
const restart = restartOf(body.restart);
|
|
404
|
+
const { tasks } = await agentAfterConnect(context, connections, plan?.row);
|
|
405
|
+
const unpicked = connector.accountPickers.filter(picker => !connection?.accounts[picker.key]?.length);
|
|
406
|
+
const message = [
|
|
407
|
+
`${connector.title} is connected${accounts ? ' with the accounts given' : ''}.`,
|
|
408
|
+
restartText(restart, applied),
|
|
409
|
+
tasks.length > 0 ? `Scheduled tasks that need it run once it is loaded: ${tasks.join(', ')}. Every run uses ${AGENT_AI_USAGE}.` : '',
|
|
410
|
+
unpicked.length > 0 ? `Choose the ${unpicked.map(picker => picker.label).join(' and ')} it may act on with set_agent_connector_accounts.` : '',
|
|
411
|
+
project
|
|
412
|
+
? `The agent's own Wiro project is now ${projectLabel(project)}: its model tools and its video and voice skills run `
|
|
413
|
+
+ 'models with it, billed to the wallet of the workspace that owns it.'
|
|
414
|
+
: '',
|
|
415
|
+
// What the project's key turned on, as the preview named it; a connect that changed nothing turned nothing on.
|
|
416
|
+
plan && applied !== 0 ? projectTurnsOnNote(plan.turnsOn, 'now that', plan.titles) ?? '' : '',
|
|
417
|
+
].filter(Boolean).join(' ');
|
|
418
|
+
return result('connect', context, 'connected', message, {
|
|
419
|
+
...(mode ? { mode } : {}),
|
|
420
|
+
...(project ? { project } : {}),
|
|
421
|
+
...(connection ? { connection } : {}),
|
|
422
|
+
applied,
|
|
423
|
+
...(restart ? { restart } : {}),
|
|
424
|
+
...(unpicked.length > 0
|
|
425
|
+
? {
|
|
426
|
+
nextAction: {
|
|
427
|
+
tool: 'set_agent_connector_accounts',
|
|
428
|
+
arguments: { agent_guid: agentGuid, connector: connector.slug },
|
|
429
|
+
reason: `List the ${unpicked.map(picker => picker.label).join(' and ')} to choose from.`,
|
|
430
|
+
},
|
|
431
|
+
}
|
|
432
|
+
: {}),
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/** Wiro's code for a sign-in that failed, in words. */
|
|
436
|
+
function failureText(code) {
|
|
437
|
+
switch (code) {
|
|
438
|
+
case 'authorization_denied':
|
|
439
|
+
return 'the user did not approve access at the service';
|
|
440
|
+
case 'token_exchange_failed':
|
|
441
|
+
return 'the service did not hand over access after the approval';
|
|
442
|
+
case 'oauth_app_missing':
|
|
443
|
+
case 'oauth_app_changed':
|
|
444
|
+
return 'the app keys the sign-in started with are missing or changed';
|
|
445
|
+
case 'connector_unavailable':
|
|
446
|
+
return 'the catalog no longer offers the connector';
|
|
447
|
+
default:
|
|
448
|
+
return code ? `the sign-in failed (${code})` : 'the sign-in failed';
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function isNewer(connection, since) {
|
|
452
|
+
if (!since)
|
|
453
|
+
return true;
|
|
454
|
+
const at = connection.connectedAt ? Date.parse(connection.connectedAt) : NaN;
|
|
455
|
+
const before = Date.parse(since);
|
|
456
|
+
return Number.isNaN(before) || (!Number.isNaN(at) && at > before);
|
|
457
|
+
}
|
|
458
|
+
function stateOf(connection, since) {
|
|
459
|
+
if (!connection)
|
|
460
|
+
return 'not_connected';
|
|
461
|
+
switch (connection.status) {
|
|
462
|
+
case 'connected':
|
|
463
|
+
return isNewer(connection, since) ? 'connected' : 'waiting_for_user';
|
|
464
|
+
case 'pending':
|
|
465
|
+
return 'waiting_for_user';
|
|
466
|
+
case 'reauth':
|
|
467
|
+
return 'reauth_needed';
|
|
468
|
+
default:
|
|
469
|
+
return 'failed';
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
async function status(context, input, extra) {
|
|
473
|
+
const { client, agentGuid, connector, signal } = context;
|
|
474
|
+
const polled = await pollUntil({
|
|
475
|
+
read: readSignal => listConnections(client, agentGuid, readSignal),
|
|
476
|
+
isDone: list => stateOf(connectionOf(list, connector.slug), input.since) !== 'waiting_for_user',
|
|
477
|
+
timeoutMs: input.waitSeconds * 1000,
|
|
478
|
+
signal,
|
|
479
|
+
interval: () => 5_000,
|
|
480
|
+
onPoll: (list, elapsedMs) => reportProgress(extra, elapsedMs, `${connector.title}: ${stateOf(connectionOf(list, connector.slug), input.since)}`),
|
|
481
|
+
});
|
|
482
|
+
if (!polled.value)
|
|
483
|
+
throw new Error(`Could not read the ${connector.title} connection.`);
|
|
484
|
+
const found = connectionOf(polled.value, connector.slug);
|
|
485
|
+
const connection = found ? withCatalogWay(found, connector) : undefined;
|
|
486
|
+
const state = stateOf(connection, input.since);
|
|
487
|
+
const connectAgain = {
|
|
488
|
+
tool: 'connect_agent_connector',
|
|
489
|
+
arguments: { action: 'connect', agent_guid: agentGuid, connector: connector.slug },
|
|
490
|
+
reason: `Connect ${connector.title} again.`,
|
|
491
|
+
};
|
|
492
|
+
const extraFields = connection ? { connection } : {};
|
|
493
|
+
switch (state) {
|
|
494
|
+
case 'connected': {
|
|
495
|
+
const unpicked = connector.accountPickers.filter(picker => !connection?.accounts[picker.key]?.length);
|
|
496
|
+
const { tasks, running } = await agentAfterConnect(context, polled.value);
|
|
497
|
+
return result('status', context, state, [
|
|
498
|
+
`${connector.title} is connected${connection?.connectedAt ? ` (since ${connection.connectedAt})` : ''}.`,
|
|
499
|
+
// The sign-in's own answer went to the browser, so whether it reached the agent live is not known here.
|
|
500
|
+
running ? LIVE_OR_RESTART_NOTE : '',
|
|
501
|
+
tasks.length > 0 ? `Scheduled tasks that need it run once it is loaded: ${tasks.join(', ')}.` : '',
|
|
502
|
+
unpicked.length > 0 ? `Choose the ${unpicked.map(picker => picker.label).join(' and ')} it may act on with set_agent_connector_accounts.` : '',
|
|
503
|
+
].filter(Boolean).join(' '), {
|
|
504
|
+
...extraFields,
|
|
505
|
+
...(unpicked.length > 0
|
|
506
|
+
? {
|
|
507
|
+
nextAction: {
|
|
508
|
+
tool: 'set_agent_connector_accounts',
|
|
509
|
+
arguments: { agent_guid: agentGuid, connector: connector.slug },
|
|
510
|
+
reason: `List the ${unpicked.map(picker => picker.label).join(' and ')} to choose from.`,
|
|
511
|
+
},
|
|
512
|
+
}
|
|
513
|
+
: {}),
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
case 'waiting_for_user':
|
|
517
|
+
return result('status', context, state, `The ${connector.title} sign-in has not finished yet. The link from connect works for ${SIGN_IN_LINK_TTL_SECONDS / 60} minutes; after that, connect again. If the user saw an error page, connect again.`, {
|
|
518
|
+
...extraFields,
|
|
519
|
+
nextAction: {
|
|
520
|
+
tool: 'connect_agent_connector',
|
|
521
|
+
arguments: {
|
|
522
|
+
action: 'status',
|
|
523
|
+
agent_guid: agentGuid,
|
|
524
|
+
connector: connector.slug,
|
|
525
|
+
...(input.since !== undefined ? { since_connected_at: input.since } : {}),
|
|
526
|
+
wait_seconds: DEFAULT_WAIT_SECONDS,
|
|
527
|
+
},
|
|
528
|
+
reason: 'Keep waiting for the user to finish signing in.',
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
case 'failed':
|
|
532
|
+
return result('status', context, state, `The latest ${connector.title} sign-in did not finish: ${failureText(connection?.lastError)}. Connect again with action connect.`, { ...extraFields, nextAction: connectAgain });
|
|
533
|
+
case 'reauth_needed':
|
|
534
|
+
return result('status', context, state, `The ${connector.title} sign-in expired or was revoked, so the agent cannot use it. Connect again with action connect.`, { ...extraFields, nextAction: connectAgain });
|
|
535
|
+
default:
|
|
536
|
+
return result('status', context, state, `${connector.title} is not connected to this agent. Start with action connect.`, { nextAction: connectAgain });
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
export function registerConnectAgentConnector(server, client) {
|
|
540
|
+
server.registerTool('connect_agent_connector', {
|
|
541
|
+
title: 'Connect an agent connector',
|
|
542
|
+
description: 'Connect a Connector (an outside service from list_agent_connectors) to a deployed agent, which '
|
|
543
|
+
+ 'then gets its tools. action connect: a connector that takes a key saves the values in values and connects '
|
|
544
|
+
+ 'at once; the result never shows them again. A sign-in connector returns a link that the user must open in '
|
|
545
|
+
+ 'their own browser and approve within 10 minutes; never open it for them. A sign-in with the service\'s own '
|
|
546
|
+
+ 'OAuth uses the user\'s own app there: its keys (such as a client id and secret) go in values with the same '
|
|
547
|
+
+ 'call. A connector that offers both takes mode sign_in or key. The wiro connector runs with a Wiro project and '
|
|
548
|
+
+ 'gives the agent only Wiro\'s model, task and file upload tools, plus read-only docs search and playbooks: '
|
|
549
|
+
+ 'connecting it takes project_id from list_projects (or the project of this connection) and makes that project '
|
|
550
|
+
+ 'the agent\'s own Wiro project, as bind_agent_project does, so its calls count as the agent\'s own (its spend '
|
|
551
|
+
+ 'limits and Model Access rules apply, and account, agent and billing calls are refused). It first shows the '
|
|
552
|
+
+ 'project, what it replaces, which wallet pays and the scheduled tasks and custom skills the project turns on. '
|
|
553
|
+
+ 'A call with required values missing sends nothing and lists what to ask for, with where each value comes '
|
|
554
|
+
+ 'from. action status: whether the sign-in finished (wait_seconds waits for the user). Connecting a connector '
|
|
555
|
+
+ 'that is connected now replaces that connection, and a connector that runs with a Wiro project always shows '
|
|
556
|
+
+ `its project first: both take confirm: true after the user agreed. ${LIVE_OR_RESTART_NOTE} `
|
|
557
|
+
+ 'Where a connector has account choices, such as the advertisers a TikTok Ads sign-in reaches, '
|
|
558
|
+
+ 'set_agent_connector_accounts lists and saves the accounts it may act on once it is connected; accounts '
|
|
559
|
+
+ 'saves ids the user already knows with this call. disconnect_agent_connector removes a connection. The person '
|
|
560
|
+
+ 'who deployed the agent or a team admin of its team can connect. Keys go only in values, never in a message '
|
|
561
|
+
+ 'to an agent.',
|
|
562
|
+
inputSchema: strictInput({
|
|
563
|
+
action: z.enum(['connect', 'status']).default('connect')
|
|
564
|
+
.describe('connect (default) connects or starts the sign-in, status checks a sign-in.'),
|
|
565
|
+
agent_guid: z.string().min(1).max(100).describe('The deployed agent\'s guid, from list_my_agents.'),
|
|
566
|
+
connector: z.string().min(1).max(100).describe('The connector\'s slug, from list_agent_connectors, such as "serper" or "google-ads".'),
|
|
567
|
+
values: z.record(z.string(), z.union([
|
|
568
|
+
z.string(),
|
|
569
|
+
z.number(),
|
|
570
|
+
z.boolean(),
|
|
571
|
+
z.null(),
|
|
572
|
+
z.array(z.unknown()),
|
|
573
|
+
z.record(z.string(), z.unknown()),
|
|
574
|
+
])).optional()
|
|
575
|
+
.describe('connect: the Connect card\'s values by field name, as list_agent_connectors shows them, for example {"apikey": "..."}. A json field takes the JSON value or its text. Values saved before are kept unless sent again; null or "" clears an optional one. Never echoed back.'),
|
|
576
|
+
mode: z.enum(CONNECT_MODES).optional()
|
|
577
|
+
.describe('connect, for a connector that connects either way: sign_in (the default) or key.'),
|
|
578
|
+
project_id: z.string()
|
|
579
|
+
.regex(PROJECT_ID_PATTERN, 'project_id must be the id of a project from list_projects, such as "12345". Nothing was sent.')
|
|
580
|
+
.optional()
|
|
581
|
+
.describe('connect, for a connector that runs with a Wiro project (wiro): the project\'s id from list_projects, which becomes the agent\'s own Wiro project. Leave it out for the project of this connection.'),
|
|
582
|
+
accounts: z.record(z.string().min(1).max(40), z.array(z.string().min(1).max(200)).max(500)).optional()
|
|
583
|
+
.describe('connect: the accounts the connection may act on, {account key: [ids]}, saved with it as given; a '
|
|
584
|
+
+ 'sign-in saves them once it finishes. Use it for ids the user already knows, such as TikTok Ads advertiser '
|
|
585
|
+
+ 'ids under "advertisers", or where the connector\'s setup steps name an account key. Otherwise choose them '
|
|
586
|
+
+ 'once connected with set_agent_connector_accounts, which lists the accounts the connection reaches and takes '
|
|
587
|
+
+ 'only those. A connector with account choices takes only their keys. Leave it out to keep the accounts '
|
|
588
|
+
+ 'saved before.'),
|
|
589
|
+
redirect_url: z.string().max(2_048).optional()
|
|
590
|
+
.describe('connect, for a sign-in: where the browser goes after approving, an https address or a local one on the user\'s own machine (http://localhost or http://127.0.0.1). Leave it out for Wiro\'s own page, which says how it went.'),
|
|
591
|
+
since_connected_at: z.string().max(64).optional()
|
|
592
|
+
.describe('status: the since_connected_at value from connect\'s nextAction, so the connection from before is not taken for the new one.'),
|
|
593
|
+
wait_seconds: z.number().int().min(0).max(MAX_WAIT_SECONDS).default(0)
|
|
594
|
+
.describe(`status: how long to wait for the sign-in to finish, 0 to ${MAX_WAIT_SECONDS} seconds (default 0, a single check).`),
|
|
595
|
+
confirm: z.boolean().default(false)
|
|
596
|
+
.describe('connect: true to replace a working connection, or to connect a connector that runs with a Wiro project (it binds the project to the agent), after the user agreed.'),
|
|
597
|
+
}),
|
|
598
|
+
outputSchema,
|
|
599
|
+
annotations: {
|
|
600
|
+
readOnlyHint: false,
|
|
601
|
+
destructiveHint: true,
|
|
602
|
+
idempotentHint: false,
|
|
603
|
+
openWorldHint: true,
|
|
604
|
+
},
|
|
605
|
+
}, async (args, extra) => {
|
|
606
|
+
try {
|
|
607
|
+
const slug = args.connector.trim().toLowerCase();
|
|
608
|
+
const agentGuid = args.agent_guid.trim();
|
|
609
|
+
const connector = await readConnector(client, slug, extra.signal);
|
|
610
|
+
if (!connector) {
|
|
611
|
+
const credential = await loadCredentialMeta(client, slug, extra.signal).catch(() => null);
|
|
612
|
+
return errorResult(new Error(credential && !slug.startsWith('sys-')
|
|
613
|
+
? `${credential.title} is ${credential.channel ? 'a chat channel' : 'a credential of the agent'}, not a connector. Save its fields with set_agent_credentials (call it with credential "${slug}" to see them).`
|
|
614
|
+
: `No connector is named "${slug}". list_agent_connectors lists them.`));
|
|
615
|
+
}
|
|
616
|
+
const context = { client, agentGuid, connector, signal: extra.signal };
|
|
617
|
+
if (args.action === 'status') {
|
|
618
|
+
return await status(context, { since: args.since_connected_at, waitSeconds: args.wait_seconds }, extra);
|
|
619
|
+
}
|
|
620
|
+
return await connect(context, {
|
|
621
|
+
values: args.values,
|
|
622
|
+
mode: args.mode,
|
|
623
|
+
projectId: args.project_id,
|
|
624
|
+
redirectUrl: args.redirect_url,
|
|
625
|
+
accounts: args.accounts,
|
|
626
|
+
confirm: args.confirm,
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
return errorResult(new Error(redactSecrets(errorText(error), secretsIn(args.values ?? {}))));
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
//# sourceMappingURL=connect-agent-connector.js.map
|