@genkit-ai/flow
Version:
Genkit AI framework workflow APIs.
1 lines • 4.01 kB
Source Map (JSON)
{"version":3,"sources":["../src/firestoreStateStore.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 {\n FlowState,\n FlowStateQuery,\n FlowStateQueryResponse,\n FlowStateSchema,\n FlowStateStore,\n} from '@genkit-ai/core';\nimport { logger } from '@genkit-ai/core/logging';\nimport { Firestore } from '@google-cloud/firestore';\n\n/** Allow customers to set service account credentials via an environment variable. */\ninterface Credentials {\n client_email?: string;\n private_key?: string;\n}\n\n/**\n * Implementation of flow state store that persistes flow state in Firestore.\n */\nexport class FirestoreStateStore implements FlowStateStore {\n readonly db: Firestore;\n readonly collection: string;\n readonly databaseId: string;\n\n constructor(\n params: {\n collection?: string;\n databaseId?: string;\n projectId?: string;\n credentials?: Credentials;\n } = {}\n ) {\n this.collection = params.collection || 'genkit-flows';\n this.databaseId = params.databaseId || '(default)';\n this.db = new Firestore({\n databaseId: this.databaseId,\n ignoreUndefinedProperties: true,\n credentials: params.credentials,\n });\n }\n\n async load(id: string): Promise<FlowState | undefined> {\n const data = (\n await this.db.collection(this.collection).doc(id).get()\n ).data();\n if (!data) {\n return undefined;\n }\n return FlowStateSchema.parse(data);\n }\n\n async save(id: string, state: FlowState): Promise<void> {\n logger.debug(state, 'save state');\n await this.db.collection(this.collection).doc(id).set(state);\n }\n\n async list(query?: FlowStateQuery): Promise<FlowStateQueryResponse> {\n const limit = query?.limit || 10;\n let fsQuery = this.db\n .collection(this.collection)\n .orderBy('startTime', 'desc');\n if (query?.continuationToken) {\n fsQuery = fsQuery.startAfter(parseInt(query.continuationToken));\n }\n fsQuery = fsQuery.limit(limit);\n\n const data = await fsQuery.get();\n const lastVisible = data.docs[data.docs.length - 1];\n return {\n flowStates: data.docs.map((d) => d.data() as FlowState),\n continuationToken:\n data.docs.length === limit\n ? `${lastVisible.data().startTime}`\n : undefined,\n };\n }\n}\n"],"mappings":";;;AAgBA;AAAA,EAIE;AAAA,OAEK;AACP,SAAS,cAAc;AACvB,SAAS,iBAAiB;AAWnB,MAAM,oBAA8C;AAAA,EAKzD,YACE,SAKI,CAAC,GACL;AACA,SAAK,aAAa,OAAO,cAAc;AACvC,SAAK,aAAa,OAAO,cAAc;AACvC,SAAK,KAAK,IAAI,UAAU;AAAA,MACtB,YAAY,KAAK;AAAA,MACjB,2BAA2B;AAAA,MAC3B,aAAa,OAAO;AAAA,IACtB,CAAC;AAAA,EACH;AAAA,EAEM,KAAK,IAA4C;AAAA;AACrD,YAAM,QACJ,MAAM,KAAK,GAAG,WAAW,KAAK,UAAU,EAAE,IAAI,EAAE,EAAE,IAAI,GACtD,KAAK;AACP,UAAI,CAAC,MAAM;AACT,eAAO;AAAA,MACT;AACA,aAAO,gBAAgB,MAAM,IAAI;AAAA,IACnC;AAAA;AAAA,EAEM,KAAK,IAAY,OAAiC;AAAA;AACtD,aAAO,MAAM,OAAO,YAAY;AAChC,YAAM,KAAK,GAAG,WAAW,KAAK,UAAU,EAAE,IAAI,EAAE,EAAE,IAAI,KAAK;AAAA,IAC7D;AAAA;AAAA,EAEM,KAAK,OAAyD;AAAA;AAClE,YAAM,SAAQ,+BAAO,UAAS;AAC9B,UAAI,UAAU,KAAK,GAChB,WAAW,KAAK,UAAU,EAC1B,QAAQ,aAAa,MAAM;AAC9B,UAAI,+BAAO,mBAAmB;AAC5B,kBAAU,QAAQ,WAAW,SAAS,MAAM,iBAAiB,CAAC;AAAA,MAChE;AACA,gBAAU,QAAQ,MAAM,KAAK;AAE7B,YAAM,OAAO,MAAM,QAAQ,IAAI;AAC/B,YAAM,cAAc,KAAK,KAAK,KAAK,KAAK,SAAS,CAAC;AAClD,aAAO;AAAA,QACL,YAAY,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE,KAAK,CAAc;AAAA,QACtD,mBACE,KAAK,KAAK,WAAW,QACjB,GAAG,YAAY,KAAK,EAAE,SAAS,KAC/B;AAAA,MACR;AAAA,IACF;AAAA;AACF;","names":[]}