Below is an example to help you understand how you can create a regular action on any Tegon event.

Example Task

Bug Enricher

Whenever an issue is created in Tegon and labeled as bug then create an action which call OpenAI to search for possible solution and put the solution as a comment of that issue.

  1. Create a new folder for this action
mkdir bug-resolution
  1. Now run our cli command, this will create the basic folder structure for action.
npx @tegonhq/cli init
  1. Let’s edit the config.json as below to tell Tegon to trigger the function when issue is created.
bug-resolution/config.json
{
  "slug": "bug-resolver",
  "name": "Bug Resolver",
  "triggers": [
    {
      "type": "on_create",
      "entities": ["Issue"]
    }
  ],
  "integrations": []
}

  1. Then let’s edit the index.ts with the below content
bug-resolution/index.ts
import { ActionEventPayload } from '@tegonhq/sdk';
import {
  ActionEventPayload,
  createIssueComment,
  getLabels,
  Label,
  getIssueById,
  getAIRequest,
} from '@tegonhq/sdk';
import axios from 'axios';

export async function run(eventPayload: ActionEventPayload) {
  const { modelId: issueId, action } = actionPayload;
  const { data: actionInput } = action;

  const issue = await getIssueById({ issueId });

  // Get all labels for the workspace
  const labels = await getLabels({
    workspaceId: issue.team.workspaceId,
    teamId: issue.teamId,
  });

  // Map the labels of the issue with all labels
  const labelMap = Object.fromEntries(
    labels.map((label: Label) => [label.id, label.name]),
  );

  // Check if bug label is assigned to issue
  const hasBugLabel = issue.labelIds.some(
    (labelId: string) => labelMap[labelId].toLowerCase() === 'bug',
  );

  if (!hasBugLabel) {
    return null;
  }

  const requestData = {
    messages: [
      { role: 'system', content: PARTIAL_SOLUTION_PROMPT },
      {
        role: 'user',
        content: `[INPUT] bug_description: ${issue.description}`,
      },
    ],
    llmModel: LLMMappings.GPT35TURBO,
    model: 'BugSuggestion',
    workspaceId: issue.team.workspaceId,
  };

  const aiResponse = await getAIRequest(requestData);

  const pattern = /\[OUTPUT\]\s*([\s\S]*)/;
  const match = aiResponse.match(pattern);
  let response;
  [, response] = match;

  const tiptapJson = await convertMarkdownToTiptapJson(response);

  return await createIssueComment({
    issueId,
    body: tiptapJson,
  });
}

You can check the prompt here.

  1. Finally you can deploy the action to your workspace.
TOKEN=<your-pat> npx @tegonhq/cli deploy --workspace-id="<workspace-id>"
You can find the whole code for this example trigger here