Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions genkit-tools/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"configstore": "^5.0.1",
"express": "^4.18.2",
"get-port": "5.1.1",
"glob": "^10.3.12",
"inquirer": "^8.2.0",
"js-yaml": "^4.1.0",
"json-2-csv": "^5.5.1",
"open": "^6.3.0",
"tsx": "^4.9.3",
"uuid": "^9.0.1",
"winston": "^3.11.0",
"yaml": "^2.4.1",
Expand Down
6 changes: 6 additions & 0 deletions genkit-tools/common/src/plugin/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ const ToolsConfigSchema = z
})
.optional(),
evaluators: z.array(EvaluatorConfig).optional(),
runner: z
.object({
mode: z.enum(['default', 'harness']).default('default'),
files: z.array(z.string()).optional(),
})
.optional(),
})
.strict();

Expand Down
30 changes: 30 additions & 0 deletions genkit-tools/common/src/runner/harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { glob } from 'glob';
import * as path from 'path';
import { logger } from '../utils';

async function main() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work with loading/transpiling TS files? Does it automatically set GENKIT_ENV=dev?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested it with .ts, it worked. The envs are set automatically like in the default app loading.

logger.info(`Loading file paths: ${process.argv[2]}`);
const files = await glob(process.argv[2].split(','));
for (const file of files) {
logger.info(`Loading \`${file}\`...`);
await import(path.join(process.cwd(), file));
}
}

main();
24 changes: 21 additions & 3 deletions genkit-tools/common/src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ export class Runner {
* Starts the app code in a subprocess.
*/
private async startApp(): Promise<boolean> {
const config = await findToolsConfig();
const runtime = detectRuntime(process.cwd());
let command = '';
let args: string[] = [];
switch (runtime) {
case 'node':
command = 'node';
command =
config?.runner?.mode === 'harness'
? path.join(__dirname, '../../../node_modules/.bin/tsx')
: 'node';
break;
case 'go':
command = 'go';
Expand All @@ -172,7 +176,11 @@ export class Runner {
throw Error(`Unexpected runtime while starting app code: ${runtime}`);
}

const entryPoint = getEntryPoint(process.cwd());
const harnessEntryPoint = path.join(__dirname, '../runner/harness.js');
const entryPoint =
config?.runner?.mode === 'default'
? getEntryPoint(process.cwd())
: harnessEntryPoint;
if (!entryPoint) {
logger.error(
'Could not detect entry point for app. Make sure you are at the root of your project directory.'
Expand All @@ -184,7 +192,14 @@ export class Runner {
return false;
}

logger.info(`Starting app at \`${entryPoint}\`...`);
const files = config?.runner?.files;
if (config?.runner?.mode === 'harness') {
logger.info(
`Running harness with file paths:\n - ${files?.join('\n - ') || ' - None'}`
);
} else {
logger.info(`Starting app at \`${entryPoint}\`...`);
}

// Try the desired port first then fall back to default range.
let port = await getPort({ port: this.reflectionApiPort });
Expand All @@ -199,6 +214,9 @@ export class Runner {
this.reflectionApiPort = port;

args.push(entryPoint);
if (config?.runner?.mode === 'harness' && files) {
args.push(files.join(','));
}
this.appProcess = spawn(command, args, {
stdio: 'inherit',
env: {
Expand Down
Loading