farseer-cli 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/app.js +377 -0
- package/dist/commands/apps.js +91 -0
- package/dist/commands/checkout.js +164 -0
- package/dist/commands/config.js +141 -0
- package/dist/commands/diff.js +210 -0
- package/dist/commands/dimensions.js +130 -0
- package/dist/commands/files.js +88 -0
- package/dist/commands/install.js +82 -0
- package/dist/commands/list.js +77 -0
- package/dist/commands/login.js +109 -0
- package/dist/commands/logout.js +44 -0
- package/dist/commands/mcp-server.js +26 -0
- package/dist/commands/model.js +181 -0
- package/dist/commands/pull.js +286 -0
- package/dist/commands/push.js +235 -0
- package/dist/commands/run.js +336 -0
- package/dist/commands/status.js +178 -0
- package/dist/commands/variables.js +200 -0
- package/dist/commands/whoami.js +47 -0
- package/dist/index.js +163 -0
- package/dist/mcp/index.js +19 -0
- package/dist/mcp/prompts/workflows.js +403 -0
- package/dist/mcp/resources/documentation.js +158 -0
- package/dist/mcp/server.js +36 -0
- package/dist/mcp/tools/appTools.js +348 -0
- package/dist/mcp/tools/authTools.js +152 -0
- package/dist/mcp/tools/dataTools.js +358 -0
- package/dist/mcp/tools/modelTools.js +293 -0
- package/dist/mcp/tools/runTools.js +239 -0
- package/dist/mcp/tools/syncTools.js +383 -0
- package/dist/mcp/utils/helpers.js +142 -0
- package/dist/services/appSyncService.js +458 -0
- package/dist/services/configService.js +186 -0
- package/dist/services/farseerApi.js +426 -0
- package/dist/services/farseerFactory.js +297 -0
- package/dist/services/farseerService.js +647 -0
- package/dist/services/syncService.js +764 -0
- package/dist/utils/constants.js +21 -0
- package/dist/utils/helpers.js +446 -0
- package/dist/utils/logger.js +45 -0
- package/package.json +8 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* App Execution Tools
|
|
4
|
+
*
|
|
5
|
+
* Tools for running Farseer apps locally.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.registerRunTools = registerRunTools;
|
|
42
|
+
const zod_1 = require("zod");
|
|
43
|
+
const child_process_1 = require("child_process");
|
|
44
|
+
const fs = __importStar(require("fs"));
|
|
45
|
+
const path = __importStar(require("path"));
|
|
46
|
+
const farseerFactory_1 = require("../../services/farseerFactory");
|
|
47
|
+
const configService_1 = require("../../services/configService");
|
|
48
|
+
const farseerService_1 = require("../../services/farseerService");
|
|
49
|
+
const helpers_1 = require("../../utils/helpers");
|
|
50
|
+
const helpers_2 = require("../utils/helpers");
|
|
51
|
+
async function getRunCredentials(organisation, tenant) {
|
|
52
|
+
// First try API key config
|
|
53
|
+
const credential = (0, configService_1.getCredential)(tenant);
|
|
54
|
+
if (credential) {
|
|
55
|
+
return {
|
|
56
|
+
tenantId: credential.tenantId,
|
|
57
|
+
apiKey: credential.apiKey,
|
|
58
|
+
basePath: credential.basePath,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// Try user auth (browser login)
|
|
62
|
+
const auth = (0, configService_1.getUserAuth)();
|
|
63
|
+
if (!auth) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// Check if token needs refresh
|
|
67
|
+
if (!(0, configService_1.isUserAuthValid)()) {
|
|
68
|
+
const refreshed = await (0, farseerService_1.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
|
|
69
|
+
if (!refreshed) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
// Save refreshed token
|
|
73
|
+
(0, configService_1.setUserAuth)({
|
|
74
|
+
accessToken: refreshed.accessToken,
|
|
75
|
+
refreshToken: refreshed.refreshToken,
|
|
76
|
+
expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
|
|
77
|
+
realm: auth.realm,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const updatedAuth = (0, configService_1.getUserAuth)();
|
|
81
|
+
if (!updatedAuth)
|
|
82
|
+
return null;
|
|
83
|
+
return {
|
|
84
|
+
tenantId: tenant,
|
|
85
|
+
accessToken: updatedAuth.accessToken,
|
|
86
|
+
basePath: (0, configService_1.generateBasePath)(organisation),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// Define schemas outside of tool registration to avoid deep type instantiation
|
|
90
|
+
const runAppSchema = {
|
|
91
|
+
organisation: zod_1.z
|
|
92
|
+
.string()
|
|
93
|
+
.optional()
|
|
94
|
+
.describe('Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping.'),
|
|
95
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID. Uses checked-out tenant if omitted.'),
|
|
96
|
+
appName: zod_1.z.string().describe('Name of the app to run'),
|
|
97
|
+
argumentOverrides: zod_1.z
|
|
98
|
+
.record(zod_1.z.string(), zod_1.z.string())
|
|
99
|
+
.optional()
|
|
100
|
+
.describe('Override argument values (Name: Value pairs)'),
|
|
101
|
+
};
|
|
102
|
+
function registerRunTools(server) {
|
|
103
|
+
// farseer_run_app - Run an app locally
|
|
104
|
+
server.tool('farseer_run_app', `Run a Farseer app locally.
|
|
105
|
+
|
|
106
|
+
This executes the app's entrypoint script using npx tsx with credentials automatically injected.
|
|
107
|
+
The script's FarseerClient() constructor picks up the injected env vars - no hardcoded credentials needed.
|
|
108
|
+
|
|
109
|
+
How it works:
|
|
110
|
+
1. Looks up app configuration to find the entrypoint script
|
|
111
|
+
2. Injects credentials as environment variables:
|
|
112
|
+
- TENANT_ID: The tenant identifier
|
|
113
|
+
- FARSEER_URL: Base API URL (e.g., https://myorg.farseer.io/api/v3)
|
|
114
|
+
- FARSEER_API_KEY or FARSEER_ACCESS_TOKEN: Authentication credentials from login
|
|
115
|
+
3. Passes app arguments via command line in the order defined by app config
|
|
116
|
+
4. Executes the script using npx tsx in the tenant directory
|
|
117
|
+
|
|
118
|
+
Requirements:
|
|
119
|
+
- Scripts must be pulled locally first (farseer_pull)
|
|
120
|
+
- Dependencies must be installed (run "farseer install <tenant>" in terminal)
|
|
121
|
+
|
|
122
|
+
Arguments can be overridden using the argumentOverrides parameter.
|
|
123
|
+
Arguments not overridden use their default values from app config.
|
|
124
|
+
|
|
125
|
+
If no tenant is specified, uses the currently checked-out tenant.`, runAppSchema,
|
|
126
|
+
// @ts-expect-error TS2589 - MCP SDK deep type inference with zod schemas
|
|
127
|
+
async (params) => {
|
|
128
|
+
const resolved = (0, helpers_2.resolveOrgAndTenant)(params);
|
|
129
|
+
if (!resolved) {
|
|
130
|
+
return (0, helpers_2.tenantRequiredResponse)();
|
|
131
|
+
}
|
|
132
|
+
const { organisation, tenant } = resolved;
|
|
133
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
134
|
+
if (!clientResult) {
|
|
135
|
+
return (0, helpers_2.authRequiredResponse)(tenant);
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
// Get app details
|
|
139
|
+
const app = await clientResult.client.getAppByName(params.appName);
|
|
140
|
+
if (!app) {
|
|
141
|
+
return (0, helpers_2.errorResponse)(`App "${params.appName}" not found`, 'NOT_FOUND');
|
|
142
|
+
}
|
|
143
|
+
const runScripts = app.scriptFiles ?? [];
|
|
144
|
+
if (!app.mainScriptFileId || runScripts.length === 0) {
|
|
145
|
+
return (0, helpers_2.errorResponse)(`App "${params.appName}" has no scripts configured. Configure it with farseer_configure_app.`, 'VALIDATION_ERROR');
|
|
146
|
+
}
|
|
147
|
+
// Find entrypoint script
|
|
148
|
+
const mainScript = runScripts.find((s) => s.id === app.mainScriptFileId);
|
|
149
|
+
if (!mainScript) {
|
|
150
|
+
return (0, helpers_2.errorResponse)(`Entrypoint script not found for app "${params.appName}"`, 'NOT_FOUND');
|
|
151
|
+
}
|
|
152
|
+
// Get run credentials
|
|
153
|
+
const credentials = await getRunCredentials(organisation, tenant);
|
|
154
|
+
if (!credentials) {
|
|
155
|
+
return (0, helpers_2.authRequiredResponse)(tenant);
|
|
156
|
+
}
|
|
157
|
+
const tenantDir = (0, helpers_1.getTenantDir)(tenant);
|
|
158
|
+
const srcDir = (0, helpers_1.getTenantSrcDir)(tenant);
|
|
159
|
+
const scriptPath = path.resolve(srcDir, mainScript.name);
|
|
160
|
+
// Check if script exists locally
|
|
161
|
+
if (!fs.existsSync(scriptPath)) {
|
|
162
|
+
return (0, helpers_2.errorResponse)(`Script not found locally: ${mainScript.name}. Run farseer_pull first.`, 'NOT_FOUND');
|
|
163
|
+
}
|
|
164
|
+
// Check if node_modules exists
|
|
165
|
+
const nodeModulesPath = path.join(tenantDir, 'node_modules');
|
|
166
|
+
if (!fs.existsSync(nodeModulesPath)) {
|
|
167
|
+
return (0, helpers_2.errorResponse)(`Dependencies not installed. Run "farseer install ${tenant}" in terminal first.`, 'VALIDATION_ERROR');
|
|
168
|
+
}
|
|
169
|
+
// Build arguments
|
|
170
|
+
const appArgs = {};
|
|
171
|
+
// Start with defaults
|
|
172
|
+
for (const arg of app.expectedArguments) {
|
|
173
|
+
appArgs[arg.name] = arg.defaultValue;
|
|
174
|
+
}
|
|
175
|
+
// Apply overrides
|
|
176
|
+
if (params.argumentOverrides) {
|
|
177
|
+
for (const [name, value] of Object.entries(params.argumentOverrides)) {
|
|
178
|
+
appArgs[name] = value;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Build environment
|
|
182
|
+
const envVars = {
|
|
183
|
+
...process.env,
|
|
184
|
+
TENANT_ID: credentials.tenantId,
|
|
185
|
+
FARSEER_URL: credentials.basePath,
|
|
186
|
+
};
|
|
187
|
+
if (credentials.apiKey) {
|
|
188
|
+
envVars.FARSEER_API_KEY = credentials.apiKey;
|
|
189
|
+
}
|
|
190
|
+
if (credentials.accessToken) {
|
|
191
|
+
envVars.FARSEER_ACCESS_TOKEN = credentials.accessToken;
|
|
192
|
+
}
|
|
193
|
+
// Build command line arguments (in order)
|
|
194
|
+
const cmdArgs = [];
|
|
195
|
+
for (const arg of app.expectedArguments) {
|
|
196
|
+
cmdArgs.push(appArgs[arg.name] ?? arg.defaultValue);
|
|
197
|
+
}
|
|
198
|
+
// Run the script
|
|
199
|
+
return new Promise((resolve) => {
|
|
200
|
+
let stdout = '';
|
|
201
|
+
let stderr = '';
|
|
202
|
+
const child = (0, child_process_1.spawn)('npx', ['tsx', `files/Scripts/${mainScript.name}`, ...cmdArgs], {
|
|
203
|
+
cwd: tenantDir,
|
|
204
|
+
shell: true,
|
|
205
|
+
env: envVars,
|
|
206
|
+
});
|
|
207
|
+
child.stdout?.on('data', (data) => {
|
|
208
|
+
stdout += data.toString();
|
|
209
|
+
});
|
|
210
|
+
child.stderr?.on('data', (data) => {
|
|
211
|
+
stderr += data.toString();
|
|
212
|
+
});
|
|
213
|
+
child.on('close', (code) => {
|
|
214
|
+
resolve((0, helpers_2.successResponse)({
|
|
215
|
+
tenant,
|
|
216
|
+
app: params.appName,
|
|
217
|
+
script: mainScript.name,
|
|
218
|
+
arguments: appArgs,
|
|
219
|
+
exitCode: code ?? 0,
|
|
220
|
+
stdout: stdout.slice(0, 10000), // Limit output size
|
|
221
|
+
stderr: stderr.slice(0, 5000),
|
|
222
|
+
truncated: stdout.length > 10000 || stderr.length > 5000,
|
|
223
|
+
}));
|
|
224
|
+
});
|
|
225
|
+
child.on('error', (err) => {
|
|
226
|
+
resolve((0, helpers_2.errorResponse)(`Failed to run script: ${err.message}. Make sure tsx is installed: npm install -g tsx`, 'UNKNOWN'));
|
|
227
|
+
});
|
|
228
|
+
// Timeout after 5 minutes
|
|
229
|
+
setTimeout(() => {
|
|
230
|
+
child.kill();
|
|
231
|
+
resolve((0, helpers_2.errorResponse)('Script execution timed out after 5 minutes', 'UNKNOWN'));
|
|
232
|
+
}, 5 * 60 * 1000);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
return (0, helpers_2.errorResponse)(`Run app failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* File Synchronization Tools
|
|
4
|
+
*
|
|
5
|
+
* Tools for syncing files between local repository and Farseer instance.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.registerSyncTools = registerSyncTools;
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const diff_1 = require("diff");
|
|
11
|
+
const syncService_1 = require("../../services/syncService");
|
|
12
|
+
const appSyncService_1 = require("../../services/appSyncService");
|
|
13
|
+
const farseerFactory_1 = require("../../services/farseerFactory");
|
|
14
|
+
const helpers_1 = require("../utils/helpers");
|
|
15
|
+
// Define schemas outside of tool registration to avoid deep type instantiation
|
|
16
|
+
const pullSchema = {
|
|
17
|
+
organisation: zod_1.z
|
|
18
|
+
.string()
|
|
19
|
+
.optional()
|
|
20
|
+
.describe('Farseer organisation (subdomain, e.g. "salvus-2"). If omitted, uses checked-out org or saved mapping.'),
|
|
21
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID (e.g. "salvus-prodaja"). Uses checked-out tenant if omitted.'),
|
|
22
|
+
all: zod_1.z.boolean().optional().describe('Download all files, not just scripts (.ts, .js)'),
|
|
23
|
+
force: zod_1.z.boolean().optional().describe('Force pull, overwriting local changes on conflicts'),
|
|
24
|
+
};
|
|
25
|
+
const pushSchema = {
|
|
26
|
+
organisation: zod_1.z
|
|
27
|
+
.string()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe('Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping.'),
|
|
30
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID. Uses checked-out tenant if omitted.'),
|
|
31
|
+
force: zod_1.z.boolean().optional().describe('Skip remote changes check (may overwrite remote changes)'),
|
|
32
|
+
};
|
|
33
|
+
const statusSchema = {
|
|
34
|
+
organisation: zod_1.z
|
|
35
|
+
.string()
|
|
36
|
+
.optional()
|
|
37
|
+
.describe('Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping.'),
|
|
38
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID. Uses checked-out tenant if omitted.'),
|
|
39
|
+
all: zod_1.z.boolean().optional().describe('Show all files, not just scripts'),
|
|
40
|
+
};
|
|
41
|
+
const diffSchema = {
|
|
42
|
+
organisation: zod_1.z.string().optional().describe('Farseer organisation (subdomain).'),
|
|
43
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID'),
|
|
44
|
+
file: zod_1.z.string().optional().describe('Specific file path to diff. If omitted, shows all differences.'),
|
|
45
|
+
showRemote: zod_1.z.boolean().optional().describe('Show only the remote content instead of diff'),
|
|
46
|
+
};
|
|
47
|
+
const listFilesSchema = {
|
|
48
|
+
organisation: zod_1.z.string().optional().describe('Farseer organisation (subdomain).'),
|
|
49
|
+
tenant: zod_1.z.string().optional().describe('Tenant ID'),
|
|
50
|
+
all: zod_1.z.boolean().optional().describe('Show all files, not just scripts'),
|
|
51
|
+
};
|
|
52
|
+
function registerSyncTools(server) {
|
|
53
|
+
// farseer_pull - Download files from remote
|
|
54
|
+
server.tool('farseer_pull', `Pull (download) files and app configurations from a Farseer instance to the local repository.
|
|
55
|
+
|
|
56
|
+
What gets pulled:
|
|
57
|
+
- TypeScript/JavaScript scripts from Files/ folder (searched recursively)
|
|
58
|
+
- App configurations (saved as JSON in apps/ folder)
|
|
59
|
+
|
|
60
|
+
Files are saved to: farseer-apps/<tenant>/files/
|
|
61
|
+
Apps are saved to: farseer-apps/<tenant>/apps/
|
|
62
|
+
|
|
63
|
+
By default only script files (.ts, .js, .mjs, .cjs) are pulled.
|
|
64
|
+
Use all=true to pull all files including images, data files, etc.
|
|
65
|
+
|
|
66
|
+
Organisation and tenant:
|
|
67
|
+
- Organisation = subdomain (e.g. "salvus-2" for salvus-2.farseer.io)
|
|
68
|
+
- Tenant = workspace ID (e.g. "salvus-prodaja")
|
|
69
|
+
- Often they are the same. Only specify organisation when it differs from tenant.
|
|
70
|
+
- If checked out, both are resolved automatically.`, pullSchema, async (params) => {
|
|
71
|
+
const resolved = (0, helpers_1.resolveOrgAndTenant)(params);
|
|
72
|
+
if (!resolved) {
|
|
73
|
+
return (0, helpers_1.tenantRequiredResponse)();
|
|
74
|
+
}
|
|
75
|
+
const { organisation, tenant } = resolved;
|
|
76
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
77
|
+
if (!clientResult) {
|
|
78
|
+
return (0, helpers_1.authRequiredResponse)(tenant);
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
// Pull files (use tenant for folder path)
|
|
82
|
+
const syncService = new syncService_1.SyncService(tenant, clientResult.client);
|
|
83
|
+
const fileResult = await syncService.pull({ all: params.all, force: params.force });
|
|
84
|
+
// Pull apps (only with JWT auth)
|
|
85
|
+
let appResult = null;
|
|
86
|
+
if (clientResult.authType === 'jwt') {
|
|
87
|
+
const appSyncService = new appSyncService_1.AppSyncService(tenant, clientResult.client);
|
|
88
|
+
appResult = await appSyncService.pull();
|
|
89
|
+
}
|
|
90
|
+
const response = {
|
|
91
|
+
tenant,
|
|
92
|
+
organisation,
|
|
93
|
+
files: {
|
|
94
|
+
downloaded: fileResult.downloaded,
|
|
95
|
+
deleted: fileResult.deleted,
|
|
96
|
+
unchanged: fileResult.unchanged.length,
|
|
97
|
+
},
|
|
98
|
+
apps: appResult
|
|
99
|
+
? {
|
|
100
|
+
created: appResult.created,
|
|
101
|
+
updated: appResult.updated,
|
|
102
|
+
deleted: appResult.deleted,
|
|
103
|
+
unchanged: appResult.unchanged.length,
|
|
104
|
+
}
|
|
105
|
+
: { note: 'App sync requires browser login (JWT)' },
|
|
106
|
+
message: `Pulled ${fileResult.downloaded.length} files${appResult ? ` and ${appResult.created.length + appResult.updated.length} apps` : ''}`,
|
|
107
|
+
};
|
|
108
|
+
if (fileResult.conflicts.length > 0) {
|
|
109
|
+
response.conflicts = fileResult.conflicts.map((c) => ({
|
|
110
|
+
file: c.filePath,
|
|
111
|
+
type: c.type,
|
|
112
|
+
description: c.description,
|
|
113
|
+
}));
|
|
114
|
+
response.message = `Pulled ${fileResult.downloaded.length} files but ${fileResult.conflicts.length} conflict(s) need resolution`;
|
|
115
|
+
response.hint =
|
|
116
|
+
'Use force=true to overwrite local changes, or resolve conflicts via CLI: farseer pull <tenant>';
|
|
117
|
+
}
|
|
118
|
+
if (fileResult.conflictFilesWritten.length > 0) {
|
|
119
|
+
response.conflictFilesWritten = fileResult.conflictFilesWritten;
|
|
120
|
+
}
|
|
121
|
+
return (0, helpers_1.successResponse)(response);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return (0, helpers_1.errorResponse)(`Pull failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
// farseer_push - Upload files to remote
|
|
128
|
+
server.tool('farseer_push', `Push (upload) local file and app changes to a Farseer instance.
|
|
129
|
+
|
|
130
|
+
What gets pushed:
|
|
131
|
+
- New/modified scripts from farseer-apps/<tenant>/files/
|
|
132
|
+
- New/modified app configurations from farseer-apps/<tenant>/apps/
|
|
133
|
+
- Deleted files (if they were previously synced)
|
|
134
|
+
|
|
135
|
+
Important:
|
|
136
|
+
- Checks for unpulled remote changes first to prevent conflicts
|
|
137
|
+
- Use force=true to skip this check (may overwrite remote changes)
|
|
138
|
+
|
|
139
|
+
If no tenant is specified, uses the currently checked-out tenant.`, pushSchema, async (params) => {
|
|
140
|
+
const resolved = (0, helpers_1.resolveOrgAndTenant)(params);
|
|
141
|
+
if (!resolved) {
|
|
142
|
+
return (0, helpers_1.tenantRequiredResponse)();
|
|
143
|
+
}
|
|
144
|
+
const { organisation, tenant } = resolved;
|
|
145
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
146
|
+
if (!clientResult) {
|
|
147
|
+
return (0, helpers_1.authRequiredResponse)(tenant);
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const syncService = new syncService_1.SyncService(tenant, clientResult.client);
|
|
151
|
+
// Check for remote changes unless force is true
|
|
152
|
+
if (!params.force) {
|
|
153
|
+
const hasRemoteChanges = await syncService.checkRemoteChanges();
|
|
154
|
+
if (hasRemoteChanges) {
|
|
155
|
+
return (0, helpers_1.errorResponse)('Remote has unpulled changes. Pull first or use force=true to overwrite.', 'VALIDATION_ERROR');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Push files
|
|
159
|
+
const fileResult = await syncService.push({ force: params.force });
|
|
160
|
+
// Check for conflicts
|
|
161
|
+
if (fileResult.conflicts.length > 0) {
|
|
162
|
+
return (0, helpers_1.errorResponse)(`Push blocked by ${fileResult.conflicts.length} conflict(s):\n${fileResult.conflicts.map((c) => ` - ${c.filePath}: ${c.description}`).join('\n')}\n\nPull first or use force=true.`, 'VALIDATION_ERROR');
|
|
163
|
+
}
|
|
164
|
+
// Push apps (only with JWT auth)
|
|
165
|
+
let appResult = null;
|
|
166
|
+
if (clientResult.authType === 'jwt') {
|
|
167
|
+
const appSyncService = new appSyncService_1.AppSyncService(tenant, clientResult.client);
|
|
168
|
+
appResult = await appSyncService.push();
|
|
169
|
+
}
|
|
170
|
+
return (0, helpers_1.successResponse)({
|
|
171
|
+
tenant,
|
|
172
|
+
organisation,
|
|
173
|
+
files: {
|
|
174
|
+
uploaded: fileResult.uploaded,
|
|
175
|
+
updated: fileResult.updated,
|
|
176
|
+
deleted: fileResult.deleted,
|
|
177
|
+
unchanged: fileResult.unchanged.length,
|
|
178
|
+
},
|
|
179
|
+
apps: appResult
|
|
180
|
+
? {
|
|
181
|
+
created: appResult.created,
|
|
182
|
+
updated: appResult.updated,
|
|
183
|
+
unchanged: appResult.unchanged.length,
|
|
184
|
+
}
|
|
185
|
+
: { note: 'App sync requires browser login (JWT)' },
|
|
186
|
+
summary: `Pushed ${fileResult.uploaded.length + fileResult.updated.length} files${appResult ? ` and ${appResult.created.length + appResult.updated.length} apps` : ''}`,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
return (0, helpers_1.errorResponse)(`Push failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
// farseer_status - Check sync status
|
|
194
|
+
server.tool('farseer_status', `Check the synchronization status between local files and the remote Farseer instance.
|
|
195
|
+
|
|
196
|
+
Shows which files are:
|
|
197
|
+
- Modified locally (changed since last sync)
|
|
198
|
+
- Modified remotely (changed on server since last sync)
|
|
199
|
+
- Only local (new files not yet pushed)
|
|
200
|
+
- Only remote (files not yet pulled)
|
|
201
|
+
- In sync (no changes)
|
|
202
|
+
|
|
203
|
+
Also shows app sync status if using JWT authentication.
|
|
204
|
+
|
|
205
|
+
If no tenant is specified, uses the currently checked-out tenant.`, statusSchema, async (params) => {
|
|
206
|
+
const resolved = (0, helpers_1.resolveOrgAndTenant)(params);
|
|
207
|
+
if (!resolved) {
|
|
208
|
+
return (0, helpers_1.tenantRequiredResponse)();
|
|
209
|
+
}
|
|
210
|
+
const { organisation, tenant } = resolved;
|
|
211
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
212
|
+
if (!clientResult) {
|
|
213
|
+
return (0, helpers_1.authRequiredResponse)(tenant);
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const syncService = new syncService_1.SyncService(tenant, clientResult.client);
|
|
217
|
+
const fileStatus = await syncService.getStatus({ all: params.all });
|
|
218
|
+
// Get app status (only with JWT auth)
|
|
219
|
+
let appStatus = null;
|
|
220
|
+
if (clientResult.authType === 'jwt') {
|
|
221
|
+
const appSyncService = new appSyncService_1.AppSyncService(tenant, clientResult.client);
|
|
222
|
+
appStatus = await appSyncService.getStatus();
|
|
223
|
+
}
|
|
224
|
+
const hasChanges = fileStatus.modifiedLocally.length > 0 ||
|
|
225
|
+
fileStatus.modifiedRemotely.length > 0 ||
|
|
226
|
+
fileStatus.onlyLocal.length > 0 ||
|
|
227
|
+
fileStatus.onlyRemote.length > 0 ||
|
|
228
|
+
(fileStatus.conflicts && fileStatus.conflicts.length > 0);
|
|
229
|
+
return (0, helpers_1.successResponse)({
|
|
230
|
+
tenant,
|
|
231
|
+
organisation,
|
|
232
|
+
files: {
|
|
233
|
+
modifiedLocally: fileStatus.modifiedLocally,
|
|
234
|
+
modifiedRemotely: fileStatus.modifiedRemotely,
|
|
235
|
+
onlyLocal: fileStatus.onlyLocal,
|
|
236
|
+
onlyRemote: fileStatus.onlyRemote,
|
|
237
|
+
inSync: fileStatus.inSync.length,
|
|
238
|
+
conflicts: fileStatus.conflicts?.map((c) => ({
|
|
239
|
+
file: c.filePath,
|
|
240
|
+
type: c.type,
|
|
241
|
+
description: c.description,
|
|
242
|
+
})) ?? [],
|
|
243
|
+
},
|
|
244
|
+
apps: appStatus
|
|
245
|
+
? {
|
|
246
|
+
newLocal: appStatus.newLocal,
|
|
247
|
+
newRemote: appStatus.newRemote,
|
|
248
|
+
modified: appStatus.modified,
|
|
249
|
+
synced: appStatus.synced.length,
|
|
250
|
+
}
|
|
251
|
+
: { note: 'App sync requires browser login (JWT)' },
|
|
252
|
+
summary: fileStatus.conflicts?.length
|
|
253
|
+
? `${fileStatus.conflicts.length} conflict(s) detected`
|
|
254
|
+
: hasChanges
|
|
255
|
+
? 'Changes detected'
|
|
256
|
+
: 'Everything is in sync',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
return (0, helpers_1.errorResponse)(`Status check failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
// farseer_diff - Show file differences
|
|
264
|
+
server.tool('farseer_diff', `Show the differences between local and remote versions of files.
|
|
265
|
+
|
|
266
|
+
Can show:
|
|
267
|
+
- Unified diff format (default)
|
|
268
|
+
- Just the remote content (showRemote=true)
|
|
269
|
+
|
|
270
|
+
Useful for reviewing changes before pull or push.
|
|
271
|
+
|
|
272
|
+
If no file is specified, shows diffs for all changed files.`, diffSchema, async (params) => {
|
|
273
|
+
const resolved = (0, helpers_1.resolveOrgAndTenant)(params);
|
|
274
|
+
if (!resolved) {
|
|
275
|
+
return (0, helpers_1.tenantRequiredResponse)();
|
|
276
|
+
}
|
|
277
|
+
const { organisation, tenant } = resolved;
|
|
278
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
279
|
+
if (!clientResult) {
|
|
280
|
+
return (0, helpers_1.authRequiredResponse)(tenant);
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const syncService = new syncService_1.SyncService(tenant, clientResult.client);
|
|
284
|
+
if (params.file) {
|
|
285
|
+
// Single file diff
|
|
286
|
+
const { localContent, remoteContent } = await syncService.getFileDiff(params.file);
|
|
287
|
+
if (params.showRemote) {
|
|
288
|
+
return (0, helpers_1.successResponse)({
|
|
289
|
+
file: params.file,
|
|
290
|
+
remoteContent: remoteContent || '(file does not exist on remote)',
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
const diff = (0, diff_1.createPatch)(params.file, remoteContent || '', localContent || '', 'remote', 'local');
|
|
294
|
+
return (0, helpers_1.successResponse)({
|
|
295
|
+
file: params.file,
|
|
296
|
+
localExists: localContent !== null,
|
|
297
|
+
remoteExists: remoteContent !== null,
|
|
298
|
+
diff,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
// All changed files
|
|
303
|
+
const status = await syncService.getStatus();
|
|
304
|
+
const changedFiles = [
|
|
305
|
+
...status.modifiedLocally,
|
|
306
|
+
...status.modifiedRemotely,
|
|
307
|
+
...status.onlyLocal,
|
|
308
|
+
...status.onlyRemote,
|
|
309
|
+
];
|
|
310
|
+
const diffs = [];
|
|
311
|
+
for (const f of changedFiles.slice(0, 10)) {
|
|
312
|
+
// Limit to 10 files
|
|
313
|
+
const { localContent, remoteContent } = await syncService.getFileDiff(f);
|
|
314
|
+
const diff = (0, diff_1.createPatch)(f, remoteContent || '', localContent || '', 'remote', 'local');
|
|
315
|
+
diffs.push({
|
|
316
|
+
file: f,
|
|
317
|
+
localExists: localContent !== null,
|
|
318
|
+
remoteExists: remoteContent !== null,
|
|
319
|
+
diff,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
return (0, helpers_1.successResponse)({
|
|
323
|
+
totalChangedFiles: changedFiles.length,
|
|
324
|
+
diffs,
|
|
325
|
+
note: changedFiles.length > 10
|
|
326
|
+
? `Showing first 10 of ${changedFiles.length} changed files`
|
|
327
|
+
: undefined,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
return (0, helpers_1.errorResponse)(`Diff failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
// farseer_list_files - List remote files
|
|
336
|
+
server.tool('farseer_list_files', `List all files stored on the remote Farseer instance in the Files/ folder.
|
|
337
|
+
|
|
338
|
+
By default shows only script files (.ts, .js, .mjs, .cjs).
|
|
339
|
+
Use all=true to show all files including images, data files, etc.
|
|
340
|
+
|
|
341
|
+
Returns file paths relative to the Files/ folder.`, listFilesSchema, async (params) => {
|
|
342
|
+
const resolved = (0, helpers_1.resolveOrgAndTenant)(params);
|
|
343
|
+
if (!resolved) {
|
|
344
|
+
return (0, helpers_1.tenantRequiredResponse)();
|
|
345
|
+
}
|
|
346
|
+
const { organisation, tenant } = resolved;
|
|
347
|
+
const clientResult = await (0, farseerFactory_1.getFarseerClientWithFallback)(organisation, tenant);
|
|
348
|
+
if (!clientResult) {
|
|
349
|
+
return (0, helpers_1.authRequiredResponse)(tenant);
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
let files = await clientResult.client.listFiles();
|
|
353
|
+
// Filter to scripts unless --all
|
|
354
|
+
if (!params.all) {
|
|
355
|
+
const scriptExtensions = ['.ts', '.js', '.mjs', '.cjs'];
|
|
356
|
+
files = files.filter((f) => scriptExtensions.some((ext) => f.name.toLowerCase().endsWith(ext)));
|
|
357
|
+
}
|
|
358
|
+
// Group by directory
|
|
359
|
+
const byDirectory = {};
|
|
360
|
+
for (const file of files) {
|
|
361
|
+
const dir = file.path.includes('/') ? file.path.split('/').slice(0, -1).join('/') : '.';
|
|
362
|
+
if (!byDirectory[dir]) {
|
|
363
|
+
byDirectory[dir] = [];
|
|
364
|
+
}
|
|
365
|
+
byDirectory[dir].push(file.name);
|
|
366
|
+
}
|
|
367
|
+
return (0, helpers_1.successResponse)({
|
|
368
|
+
tenant,
|
|
369
|
+
organisation,
|
|
370
|
+
totalFiles: files.length,
|
|
371
|
+
files: files.map((f) => ({
|
|
372
|
+
name: f.name,
|
|
373
|
+
path: f.path,
|
|
374
|
+
reference: f.reference,
|
|
375
|
+
})),
|
|
376
|
+
byDirectory,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
return (0, helpers_1.errorResponse)(`List files failed: ${error instanceof Error ? error.message : 'Unknown error'}`, 'NETWORK_ERROR');
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|