UNPKG

@genkit-ai/core

Version:

Genkit AI framework core libraries.

1 lines 4.37 kB
{"version":3,"sources":["../src/localFileFlowStateStore.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport crypto from 'crypto';\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport {\n FlowState,\n FlowStateQuery,\n FlowStateQueryResponse,\n FlowStateSchema,\n FlowStateStore,\n} from './flowTypes.js';\nimport { logger } from './logging.js';\n\n/**\n * Implementation of flow state store that persistes flow state on local disk.\n */\nexport class LocalFileFlowStateStore implements FlowStateStore {\n private readonly storeRoot: string;\n\n constructor() {\n const rootHash = crypto\n .createHash('md5')\n .update(require?.main?.filename || 'unknown')\n .digest('hex');\n this.storeRoot = path.resolve(os.tmpdir(), `.genkit/${rootHash}/flows`);\n fs.mkdirSync(this.storeRoot, { recursive: true });\n logger.info('Using DevFlowStateStore. Root: ' + this.storeRoot);\n }\n\n async load(id: string): Promise<FlowState | undefined> {\n const filePath = path.resolve(this.storeRoot, `${id}`);\n if (!fs.existsSync(filePath)) {\n return undefined;\n }\n const data = fs.readFileSync(filePath, 'utf8');\n return FlowStateSchema.parse(JSON.parse(data));\n }\n\n async save(id: string, state: FlowState): Promise<void> {\n logger.debug('save flow state ' + id);\n fs.writeFileSync(\n path.resolve(this.storeRoot, `${id}`),\n JSON.stringify(state)\n );\n }\n\n async list(query?: FlowStateQuery): Promise<FlowStateQueryResponse> {\n const files = fs.readdirSync(this.storeRoot);\n files.sort((a, b) => {\n return (\n fs.statSync(path.resolve(this.storeRoot, `${b}`)).mtime.getTime() -\n fs.statSync(path.resolve(this.storeRoot, `${a}`)).mtime.getTime()\n );\n });\n const startFrom = query?.continuationToken\n ? parseInt(query?.continuationToken)\n : 0;\n const stopAt = startFrom + (query?.limit || 10);\n const flowStates = files.slice(startFrom, stopAt).map((id) => {\n const filePath = path.resolve(this.storeRoot, `${id}`);\n const data = fs.readFileSync(filePath, 'utf8');\n return FlowStateSchema.parse(JSON.parse(data));\n });\n return {\n flowStates,\n continuationToken: files.length > stopAt ? stopAt.toString() : undefined,\n };\n }\n}\n"],"mappings":";;;AAgBA,OAAO,YAAY;AACnB,OAAO,QAAQ;AACf,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB;AAAA,EAIE;AAAA,OAEK;AACP,SAAS,cAAc;AAKhB,MAAM,wBAAkD;AAAA,EAG7D,cAAc;AAnChB;AAoCI,UAAM,WAAW,OACd,WAAW,KAAK,EAChB,SAAO,wCAAS,SAAT,mBAAe,aAAY,SAAS,EAC3C,OAAO,KAAK;AACf,SAAK,YAAY,KAAK,QAAQ,GAAG,OAAO,GAAG,WAAW,QAAQ,QAAQ;AACtE,OAAG,UAAU,KAAK,WAAW,EAAE,WAAW,KAAK,CAAC;AAChD,WAAO,KAAK,oCAAoC,KAAK,SAAS;AAAA,EAChE;AAAA,EAEM,KAAK,IAA4C;AAAA;AACrD,YAAM,WAAW,KAAK,QAAQ,KAAK,WAAW,GAAG,EAAE,EAAE;AACrD,UAAI,CAAC,GAAG,WAAW,QAAQ,GAAG;AAC5B,eAAO;AAAA,MACT;AACA,YAAM,OAAO,GAAG,aAAa,UAAU,MAAM;AAC7C,aAAO,gBAAgB,MAAM,KAAK,MAAM,IAAI,CAAC;AAAA,IAC/C;AAAA;AAAA,EAEM,KAAK,IAAY,OAAiC;AAAA;AACtD,aAAO,MAAM,qBAAqB,EAAE;AACpC,SAAG;AAAA,QACD,KAAK,QAAQ,KAAK,WAAW,GAAG,EAAE,EAAE;AAAA,QACpC,KAAK,UAAU,KAAK;AAAA,MACtB;A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