Skip to content

Commit

Permalink
[Components] grain #14678
Browse files Browse the repository at this point in the history
Sources
 - New Highlight (Instant)
 - New Story (Instant)
 - New Recording (Instant)
 - Updated Highlight (Instant)
 - Updated Story (Instant)
 - Updated Recording (Instant)
 - Deleted Highlight (Instant)
 - Deleted Story (Instant)
 - Deleted Recording (Instant)

Actions
 - Get Recording
  • Loading branch information
luancazarine committed Nov 21, 2024
1 parent 7643f8a commit f5ce1b2
Show file tree
Hide file tree
Showing 24 changed files with 518 additions and 677 deletions.
46 changes: 21 additions & 25 deletions components/grain/actions/get-recording/get-recording.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {
INTELLIGENCE_NOTES_FORMAT_OPTIONS,
TRANSCRIPT_FORMAT_OPTIONS,
} from "../../common/constants.mjs";
import { parseObject } from "../../common/utils.mjs";
import grain from "../../grain.app.mjs";
import { axios } from "@pipedream/platform";

export default {
key: "grain-get-recording",
Expand All @@ -14,44 +18,36 @@ export default {
grain,
"recordId",
],
async options({ page }) {
const recordings = await this.grain.listRecordings({
page,
});
return recordings.map((recording) => ({
label: recording.title,
value: recording.id,
}));
},
},
transcriptFormat: {
propDefinition: [
grain,
"transcriptFormat",
],
type: "string",
label: "Transcript Format",
description: "Format for the transcript",
options: TRANSCRIPT_FORMAT_OPTIONS,
optional: true,
},
intelligenceNotesFormat: {
propDefinition: [
grain,
"intelligenceNotesFormat",
],
type: "string",
label: "Intelligence Notes Format",
description: "Format for the intelligence notes",
options: INTELLIGENCE_NOTES_FORMAT_OPTIONS,
optional: true,
},
allowedIntelligenceNotes: {
propDefinition: [
grain,
"allowedIntelligenceNotes",
],
type: "string[]",
label: "Allowed Intelligence Notes",
description: "Whitelist of intelligence notes section titles",
optional: true,
},
},
async run({ $ }) {
const response = await this.grain.fetchRecording({
recordId: this.recordId,
transcriptFormat: this.transcriptFormat,
intelligenceNotesFormat: this.intelligenceNotesFormat,
allowedIntelligenceNotes: this.allowedIntelligenceNotes,
params: {
transcript_format: this.transcriptFormat,
intelligence_notes_format: this.intelligenceNotesFormat,
allowed_intelligence_notes: parseObject(this.allowedIntelligenceNotes),
},
});

$.export("$summary", `Successfully fetched recording with ID ${this.recordId}`);
Expand Down
25 changes: 25 additions & 0 deletions components/grain/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const TRANSCRIPT_FORMAT_OPTIONS = [
{
label: "JSON",
value: "json",
},
{
label: "VTT",
value: "vtt",
},
];

export const INTELLIGENCE_NOTES_FORMAT_OPTIONS = [
{
label: "JSON",
value: "json",
},
{
label: "Markdown",
value: "md",
},
{
label: "Text",
value: "text",
},
];
24 changes: 24 additions & 0 deletions components/grain/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};
176 changes: 78 additions & 98 deletions components/grain/grain.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,126 +8,106 @@ export default {
type: "string",
label: "Record ID",
description: "The ID of the recording to fetch",
async options() {
const recordings = await this.listRecordings();
return recordings.map((recording) => ({
value: recording.id,
label: recording.title,
}));
async options({ prevContext: { nextPage } }) {
const {
recordings, cursor,
} = await this.listRecordings({
params: {
cursor: nextPage,
},
});
return {
options: recordings.map(({
id: value, title: label,
}) => ({
value,
label,
})),
context: {
nextPage: cursor,
},
};
},
},
transcriptFormat: {
viewId: {
type: "string",
label: "Transcript Format",
description: "Format for the transcript",
options: [
{
label: "JSON",
value: "json",
},
{
label: "VTT",
value: "vtt",
},
],
optional: true,
},
intelligenceNotesFormat: {
type: "string",
label: "Intelligence Notes Format",
description: "Format for the intelligence notes",
options: [
{
label: "JSON",
value: "json",
},
{
label: "Markdown",
value: "md",
},
{
label: "Text",
value: "text",
},
],
optional: true,
},
allowedIntelligenceNotes: {
type: "string[]",
label: "Allowed Intelligence Notes",
description: "Whitelist of intelligence notes section titles",
optional: true,
label: "View ID",
description: "The ID of the recording to fetch",
async options({
type, prevContext: { nextPage },
}) {
const {
views, cursor,
} = await this.listViews({
params: {
type_filter: type,
cursor: nextPage,
},
});
return {
options: views.map(({
id: value, name: label,
}) => ({
value,
label,
})),
context: {
nextPage: cursor,
},
};
},
},
},
methods: {
_baseUrl() {
return "https://grain.com";
return "https://grain.com/_/public-api";
},
_headers() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async _makeRequest(opts = {}) {
const {
$ = this, method = "GET", path = "/", headers, ...otherOpts
} = opts;
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
...otherOpts,
method,
url: this._baseUrl() + path,
headers: {
...headers,
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: this._headers(),
...opts,
});
},
async listRecordings(opts = {}) {
listRecordings(opts = {}) {
return this._makeRequest({
path: "/_/public-api/recordings",
path: "/recordings",
...opts,
});
},
async fetchRecording({
recordId, transcriptFormat, intelligenceNotesFormat, allowedIntelligenceNotes, ...opts
}) {
listViews(opts = {}) {
return this._makeRequest({
path: `/_/public-api/recordings/${recordId}`,
params: {
transcript_format: transcriptFormat,
intelligence_notes_format: intelligenceNotesFormat,
allowed_intelligence_notes: allowedIntelligenceNotes,
},
path: "/views",
...opts,
});
},
async emitNewEvent(eventType, entityType) {
// Logic to emit event - placeholder implementation
console.log(`Emit ${eventType} event for ${entityType}`);
},
},
hooks: {
async addedHighlight() {
await this.emitNewEvent("added", "highlight");
},
async addedStory() {
await this.emitNewEvent("added", "story");
},
async addedRecording() {
await this.emitNewEvent("added", "recording");
},
async updatedHighlight() {
await this.emitNewEvent("updated", "highlight");
},
async updatedStory() {
await this.emitNewEvent("updated", "story");
},
async updatedRecording() {
await this.emitNewEvent("updated", "recording");
},
async removedHighlight() {
await this.emitNewEvent("removed", "highlight");
fetchRecording({
recordId, ...opts
}) {
return this._makeRequest({
path: `/recordings/${recordId}`,
...opts,
});
},
async removedStory() {
await this.emitNewEvent("removed", "story");
createWebhook(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/hooks",
...opts,
});
},
async removedRecording() {
await this.emitNewEvent("removed", "recording");
deleteWebhook(hookId) {
return this._makeRequest({
method: "DELETE",
path: `/hooks/${hookId}`,
});
},
},
};
5 changes: 4 additions & 1 deletion components/grain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/grain",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Grain Components",
"main": "grain.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
47 changes: 47 additions & 0 deletions components/grain/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import grain from "../../grain.app.mjs";

export default {
props: {
grain,
http: "$.interface.http",
db: "$.service.db",
},
methods: {
_getHookId() {
return this.db.get("hookId");
},
_setHookId(hookId) {
this.db.set("hookId", hookId);
},
getExtraData() {
return {};
},
},
hooks: {
async activate() {
const response = await this.grain.createWebhook({
data: {
version: 2,
hook_url: this.http.endpoint,
view_id: this.viewId,
actions: this.getAction(),
},
});
this._setHookId(response.id);
},
async deactivate() {
const webhookId = this._getHookId();
await this.grain.deleteWebhook(webhookId);
},
},
async run({ body }) {
if (!body.data) return;

const ts = Date.parse(new Date());
this.$emit(body, {
id: `${body.data.id}-${ts}`,
summary: this.getSummary(body),
ts: ts,
});
},
};
Loading

0 comments on commit f5ce1b2

Please sign in to comment.