|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import utils from "./common/utils.mjs"; |
| 3 | +import constants from "./common/constants.mjs"; |
| 4 | + |
1 | 5 | export default { |
2 | 6 | type: "app", |
3 | 7 | app: "langfuse", |
4 | | - propDefinitions: {}, |
| 8 | + propDefinitions: { |
| 9 | + projectId: { |
| 10 | + type: "string", |
| 11 | + label: "Trace ID", |
| 12 | + description: "The ID of the trace to attach feedback to or to filter by for events.", |
| 13 | + async options() { |
| 14 | + const { data } = await this.listProjects(); |
| 15 | + return data.map(({ |
| 16 | + id: value, name: label, |
| 17 | + }) => ({ |
| 18 | + label, |
| 19 | + value, |
| 20 | + })); |
| 21 | + }, |
| 22 | + }, |
| 23 | + objectType: { |
| 24 | + type: "string", |
| 25 | + label: "Object Type", |
| 26 | + description: "The type of object to attach feedback to.", |
| 27 | + options: [ |
| 28 | + "TRACE", |
| 29 | + "OBSERVATION", |
| 30 | + "SESSION", |
| 31 | + "PROMPT", |
| 32 | + ], |
| 33 | + }, |
| 34 | + }, |
5 | 35 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 36 | + getUrl(path) { |
| 37 | + const baseUrl = constants.BASE_URL |
| 38 | + .replace(constants.REGION_PLACEHOLDER, this.$auth.region); |
| 39 | + return `${baseUrl}${constants.VERSION_PATH}${path}`; |
| 40 | + }, |
| 41 | + getAuth() { |
| 42 | + const { |
| 43 | + public_key: username, |
| 44 | + secret_key: password, |
| 45 | + } = this.$auth; |
| 46 | + return { |
| 47 | + username, |
| 48 | + password, |
| 49 | + }; |
| 50 | + }, |
| 51 | + _makeRequest({ |
| 52 | + $ = this, path, ...args |
| 53 | + } = {}) { |
| 54 | + return axios($, { |
| 55 | + ...args, |
| 56 | + debug: true, |
| 57 | + url: this.getUrl(path), |
| 58 | + auth: this.getAuth(), |
| 59 | + headers: { |
| 60 | + "Content-Type": "application/json", |
| 61 | + }, |
| 62 | + }); |
| 63 | + }, |
| 64 | + post(args = {}) { |
| 65 | + return this._makeRequest({ |
| 66 | + method: "POST", |
| 67 | + ...args, |
| 68 | + }); |
| 69 | + }, |
| 70 | + listProjects(args = {}) { |
| 71 | + return this._makeRequest({ |
| 72 | + path: "/projects", |
| 73 | + ...args, |
| 74 | + }); |
| 75 | + }, |
| 76 | + listTraces(args = {}) { |
| 77 | + return this._makeRequest({ |
| 78 | + path: "/traces", |
| 79 | + ...args, |
| 80 | + }); |
| 81 | + }, |
| 82 | + listScores(args = {}) { |
| 83 | + return this._makeRequest({ |
| 84 | + path: "/scores", |
| 85 | + ...args, |
| 86 | + }); |
| 87 | + }, |
| 88 | + async *getIterations({ |
| 89 | + resourcesFn, resourcesFnArgs, resourceName, |
| 90 | + lastDateAt, dateField, |
| 91 | + max = constants.DEFAULT_MAX, |
| 92 | + }) { |
| 93 | + let page = 1; |
| 94 | + let resourcesCount = 0; |
| 95 | + |
| 96 | + while (true) { |
| 97 | + const response = |
| 98 | + await resourcesFn({ |
| 99 | + ...resourcesFnArgs, |
| 100 | + params: { |
| 101 | + ...resourcesFnArgs?.params, |
| 102 | + page, |
| 103 | + limit: constants.DEFAULT_LIMIT, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + const nextResources = utils.getNestedProperty(response, resourceName); |
| 108 | + |
| 109 | + if (!nextResources?.length) { |
| 110 | + console.log("No more resources found"); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + for (const resource of nextResources) { |
| 115 | + const isDateGreater = |
| 116 | + lastDateAt |
| 117 | + && Date.parse(resource[dateField]) >= Date.parse(lastDateAt); |
| 118 | + |
| 119 | + if (!lastDateAt || isDateGreater) { |
| 120 | + yield resource; |
| 121 | + resourcesCount += 1; |
| 122 | + } |
| 123 | + |
| 124 | + if (resourcesCount >= max) { |
| 125 | + console.log("Reached max resources"); |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (nextResources.length < constants.DEFAULT_LIMIT) { |
| 131 | + console.log("No next page found"); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + page += 1; |
| 136 | + } |
| 137 | + }, |
| 138 | + paginate(args = {}) { |
| 139 | + return utils.iterate(this.getIterations(args)); |
9 | 140 | }, |
10 | 141 | }, |
11 | 142 | }; |
0 commit comments