firebase / genkit

An open source framework for building AI-powered apps with familiar code-centric patterns. Genkit makes it easy to develop, integrate, and test AI features with observability and evaluations. Genkit works with various models and platforms.
Apache License 2.0
782 stars 117 forks source link

make `flows` optional on `ai.startFlowServer` #1346

Open zerolethanh opened 1 week ago

zerolethanh commented 1 week ago

Is your feature request related to a problem? Please describe. I have to import many flows when run ai.startFlowServer

Describe the solution you'd like public this registeredFlows prop? or something like optional flows prop on ai.startFlowServer, e.g: ai.startFlowServer({flows?:[...ai.registeredFlows]})

Describe alternatives you've considered We have designed flows system by read flows.ts|.js in sub-folders like:

/src | /lib
  '- /flows/
       '- /users/
          '- flows.ts|.js <-- at this point `export {userListFlow} from './list/userListFlow'`
          '- /list/
              '- userListFlow.ts|.js
/// read flows code
const allFlowsDir = path.resolve(devEnv ? "src" : "lib", "flows");
fs.readdirSync(allFlowsDir).forEach((name) => {
    const flowDir = path.join(allFlowsDir, name);
    logInfo("loading flow:", flowDir);
    try {
        require(path.join(flowDir, "flows")); // require flows.ts|.js
        logSuccess(`loaded flow: ${flowDir}`);
    } catch (e:any) {
        console.log((e as Error).stack)
    }
});

Additional context image

zerolethanh commented 1 week ago

currently, we resolved this issue by this solution:

// read all flows in /flows dir
const allFlowsDir = path.resolve(devEnv ? "src" : "lib", "flows");
type IFlows = FlowServerOptions['flows']
const allFlows: IFlows = []

fs.readdirSync(allFlowsDir).forEach((name) => {
    const flowDir = path.join(allFlowsDir, name);
    // logInfo("loading flow:", flowDir);
    try {
        const flowList = require(path.join(flowDir, "flows")); // require flows.ts|.js
        if (flowList) {
            const flows = Object.values(flowList) as IFlows
            allFlows.push(...flows)
        }
        // logSuccess(`loaded flow: ${flowDir}`);
    } catch (e: any) {
        logError(e);
    }
});

ai.startFlowServer(
  {
    flows: allFlows
  }
);
apascal07 commented 5 days ago

@pavelgj Exposing Genkit.registeredFlows seems reasonable even if we were to deprecate/not recommend Genkit.startFlowServer anymore since users may still want to iterate through all of them to add to their own server.

Thanks for the suggestion, @zerolethanh!