Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#29 from gentlementlegen/fe…
Browse files Browse the repository at this point in the history
…at/build

feat: ncc build for faster boot
  • Loading branch information
gentlementlegen authored Nov 1, 2024
2 parents 399f8bc + 1f71562 commit 86303ce
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/knip.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { KnipConfig } from "knip";

const config: KnipConfig = {
entry: ["build/index.ts"],
entry: ["src/worker.ts", "src/workflow-entry.ts"],
project: ["src/**/*.ts"],
ignore: ["src/types/config.ts", "**/__mocks__/**", "**/__fixtures__/**"],
ignoreExportsUsedInFile: true,
// eslint can also be safely ignored as per the docs: https://knip.dev/guides/handling-issues#eslint--jest
ignoreDependencies: ["eslint-config-prettier", "eslint-plugin-prettier", "@mswjs/data", "smee-client"],
ignoreDependencies: ["eslint-config-prettier", "eslint-plugin-prettier", "smee-client"],
eslint: true,
};

Expand Down
13 changes: 2 additions & 11 deletions .github/workflows/compute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: setup node
uses: actions/setup-node@v4
with:
node-version: "20.10.0"

- name: install dependencies
run: yarn

- run: ${{ toJSON(inputs) }}
shell: cat {0}

- name: execute directive
run: npx tsx ./src/workflow-entry.ts
id: Kernel-Telegram
uses: ./
env:
TELEGRAM_BOT_ENV: ${{ secrets.TELEGRAM_BOT_ENV }}
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
20 changes: 20 additions & 0 deletions .github/workflows/update-configuration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Update Configuration and Build"

on:
workflow_dispatch:
push:

jobs:
update:
name: "Update Configuration & Build"
runs-on: ubuntu-latest
permissions: write-all

steps:
- uses: ubiquity-os/action-deploy-plugin@main
with:
pluginEntry: '${{ github.workspace }}/src/workflow-entry.ts'
schemaPath: '${{ github.workspace }}/src/types/plugin-inputs.ts'
env:
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
24 changes: 24 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Telegram Room"
description: "Handles the creation and deletion of a room."
inputs:
stateId:
description: "State Id to keep track on the Kernel side"
eventName:
description: "Event Name that triggered the run"
eventPayload:
description: "Event Payload for the plugin"
settings:
description: "Settings for the plugin"
authToken:
description: "Auth Token to auth as the Kernel"
ref:
description: "GitHub branch reference for the run"
signature:
description: "The kernel signature"
outputs:
result:
description: "Actions taken by the telegram bridge."
value: ${{ steps.main.outputs.result }}
runs:
using: "node20"
main: "dist/index.js"
21 changes: 21 additions & 0 deletions dist/index.js

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@
"collectCoverage": true,
"coverageReporters": ["json", "lcov", "text", "clover", "json-summary"],
"reporters": ["default", "jest-junit", "jest-md-dashboard"],
"coverageDirectory": "coverage"
"coverageDirectory": "coverage",
"extensionsToTreatAsEsm": [".ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"useESM": true
}
]
}
}
18 changes: 17 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,21 @@
"issue_comment.edited",
"issues.unassigned",
"pull_request.review_requested"
]
],
"configuration": {
"type": "object",
"properties": {
"botId": {
"default": 7543249164
},
"shouldUseGithubStorage": {
"default": false,
"type": "boolean"
},
"storageOwner": {
"default": "ubiquity-os-marketplace",
"type": "string"
}
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"knip": "knip --config .github/knip.ts",
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"prepare": "husky install",
"test": "jest --setupFiles dotenv/config --coverage",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --setupFiles dotenv/config --coverage",
"worker": "wrangler dev --env dev --port 3000",
"deploy": "wrangler deploy --env dev",
"sms-auth": "npx tsx src/bot/mtproto-api/bot/scripts/sms-auth/sms-auth.ts",
Expand All @@ -41,7 +41,7 @@
"@octokit/webhooks": "13.2.7",
"@sinclair/typebox": "0.32.33",
"@supabase/supabase-js": "^2.45.6",
"@ubiquity-dao/ubiquibot-logger": "^1.3.1",
"@ubiquity-os/ubiquity-os-logger": "^1.3.2",
"big-integer": "^1.6.52",
"dotenv": "16.4.5",
"grammy": "^1.29.0",
Expand Down
27 changes: 13 additions & 14 deletions src/adapters/github/storage-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*
*
*/
import { Context } from "../../types";
import { Chat, ChatAction, HandleChatParams, RetrievalHelper, StorageTypes, UserBaseStorage } from "../../types/storage";
import { Storage } from "../supabase/supabase";
import { Storage } from "../index";

export class GithubStorage implements Storage {
constructor() {}
Expand Down Expand Up @@ -60,7 +59,7 @@ export class GithubStorage implements Storage {
}

/**
*
*
import { logger } from "../../utils/logger";
import { Chat, ChatAction, ChatStorage, HandleChatParams, RetrievalHelper, StorageTypes, UserBaseStorage, SessionStorage, Withsha } from "../../types/storage";
import { PluginContext } from "../../types/plugin-context-single";
Expand All @@ -70,7 +69,7 @@ import { Storage } from "../supabase/supabase";
* Uses GitHub as a storage layer, in particular, a JSON
* based private repository.
export class GithubStorage implements Storage {
octokit: Context["octokit"];
logger = logger;
Expand Down Expand Up @@ -111,13 +110,13 @@ export class GithubStorage implements Storage {
*
* td - validate no GitHub webhook payloads are missing this info
* (current used webhooks are safe)
if (!this.payloadRepoOwner) {
/**
* @DEV - Forks should update the manifest pointing to their forked repository
*
* in this case "ubiquity-os"
this.payloadRepoOwner = getPluginManifestDetails().name.split("/")[0];
}
Expand All @@ -132,7 +131,7 @@ export class GithubStorage implements Storage {
* via the config or storage layer.
*
* this.payloadRepoOwner = getPartnerStorageLocation(this.payloadRepoOwner);
this.pluginRepo = getPluginManifestDetails().name.split("/")[1];
this.formatStoragePaths();
Expand All @@ -148,7 +147,7 @@ export class GithubStorage implements Storage {
* - ubiquibot-config/plugin-store/ubiquity-os/ubiquity-os-kernel-telegram/chat-storage.json
* - ubiquibot-config/plugin-store/ubiquity-os/ubiquity-os-kernel-telegram/user-base.json
* - ubiquibot-config/plugin-store/ubiquity-os/ubiquity-os-kernel-telegram/session-storage.json
formatStoragePaths() {
this.chatStoragePath = `plugin-store/${this.payloadRepoOwner}/${this.pluginRepo}/${this.chatStoragePath}`;
this.userStoragePath = `plugin-store/${this.payloadRepoOwner}/${this.pluginRepo}/${this.userStoragePath}`;
Expand All @@ -162,7 +161,7 @@ export class GithubStorage implements Storage {
*
* Storage is handled via a dedicated GitHub App with the
* necessary permissions to read/write to the repository.
async getStorageOctokit() {
if (this.isEnvSetup) {
// setup pushes secrets to ubiquity-os-kernel-telegram, doesn't need the app instance
Expand Down Expand Up @@ -283,7 +282,7 @@ export class GithubStorage implements Storage {
/**
* This will create | reopen | close a chat. It must be passed the full
* chat object.
async handleChat<TAction extends ChatAction>(params: HandleChatParams<TAction>) {
// we'll need this no matter what
const dbObject = await this.retrieveStorageDataObject("allChats");
Expand Down Expand Up @@ -322,7 +321,7 @@ export class GithubStorage implements Storage {
* and replace it with the new one.
*
* "delete" will remove the session, this will break things without a new session.
async handleSession<TAction extends "create" | "delete">(session: string, action: TAction) {
const dbObject = await this.retrieveStorageDataObject("session", true);
Expand All @@ -343,7 +342,7 @@ export class GithubStorage implements Storage {
* This way notifications can only be received to the account which is subscribing,
* them may choose to listen into other users, but cannot enable notifications for
* another user.
async handleUserBaseStorage<TType extends "create" | "delete" | "update">(user: UserBaseStorage, action: TType) {
const dbObject = await this.retrieveStorageDataObject("userBase");
Expand All @@ -368,7 +367,7 @@ export class GithubStorage implements Storage {
* updated properties, or mistakes will be made.
*
* Do we need a safety check to ensure we are not accidentally deleting data? Maybe, needs tested.
async storeData<TType extends StorageTypes>(data: RetrievalHelper<TType>) {
let path;
let type: StorageTypes;
Expand Down Expand Up @@ -436,7 +435,7 @@ export class GithubStorage implements Storage {
* and returns it.
*
* Fitted with a helper for returning the correct storage type depending on the param.
async retrieveStorageDataObject<TType extends StorageTypes = StorageTypes>(type: TType, withSha?: boolean): Promise<RetrievalHelper<TType>> {
const storagePaths = {
allChats: this.chatStoragePath,
Expand Down
5 changes: 3 additions & 2 deletions src/bot/helpers/grammy-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Update, UserFromGetMe } from "@grammyjs/types";
import { Octokit } from "@octokit/rest";
import { type Api, Context as DefaultContext, type SessionFlavor } from "grammy";
import type { AutoChatActionFlavor } from "@grammyjs/auto-chat-action";
import type { HydrateFlavor } from "@grammyjs/hydrate";
Expand All @@ -18,7 +19,7 @@ export interface SessionData {
interface Dependencies {
logger: Logger;
config: UbiquityOsContext["env"];
octokit: RestOctokitFromApp;
octokit: RestOctokitFromApp | Octokit;
}

interface ExtendedContextFlavor extends Dependencies {
Expand All @@ -42,7 +43,7 @@ export async function createContextConstructor({ logger, config, octokit }: Depe

return class extends DefaultContext implements ExtendedContextFlavor {
logger: Logger;
octokit: RestOctokitFromApp = octokit;
octokit: RestOctokitFromApp | Octokit = octokit;
config: UbiquityOsContext["env"];
adapters: ReturnType<typeof createAdapters>;

Expand Down
3 changes: 2 additions & 1 deletion src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { autoChatAction } from "@grammyjs/auto-chat-action";
import { hydrate } from "@grammyjs/hydrate";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode";
import { Octokit as RestOctokitFromApp } from "octokit";
import { Octokit } from "@octokit/rest";

import { Logger } from "../utils/logger";
import { createContextConstructor, SessionData } from "./helpers/grammy-context";
Expand All @@ -26,7 +27,7 @@ import { session } from "./middlewares/session";
interface Dependencies {
config: Context["env"];
logger: Logger;
octokit: RestOctokitFromApp;
octokit: RestOctokitFromApp | Octokit;
}

interface Options {
Expand Down
5 changes: 1 addition & 4 deletions src/bot/mtproto-api/bot/session/github-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export class GitHubSession extends StringSession implements SessionManager {

constructor(context: Context, session?: string) {
super(session);
const {
config: { storageOwner },
} = context;
this.storage = new GithubStorage(context, { storageOwner, isEnvSetup: false });
this.storage = new GithubStorage();
this.context = context;
this.session = session;
}
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/telegram-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function initializeBotInstance(env: Env, failures: unknown[]) {
stack: er instanceof Error ? er.stack : undefined,
};
failures.push(errorInfo);
logger.error(errorInfo.message, { error: er });
logger.error(errorInfo.message, { error: er as Error });
return null;
}
}
Expand All @@ -61,7 +61,7 @@ function getServerFromBot(botInstance: TelegramBotSingleton | null, failures: un
stack: er instanceof Error ? er.stack : undefined,
};
failures.push(errorInfo);
logger.error(errorInfo.message, { error: er });
logger.error(errorInfo.message, { error: er as Error });
return { server: null, bot: null };
}
}
Expand All @@ -87,7 +87,7 @@ async function makeServerRequest(
stack: er instanceof Error ? er.stack : undefined,
};
failures.push(errorInfo);
logger.error(errorInfo.message, { error: er });
logger.error(errorInfo.message, { error: er as Error });
return new Response("Internal Server Error", { status: 500 });
}
}
Expand All @@ -110,7 +110,7 @@ async function readResponseBody(res: Response, failures: unknown[]): Promise<str
stack: er instanceof Error ? er.stack : undefined,
};
failures.push(errorInfo);
logger.error(errorInfo.message, { error: er });
logger.error(errorInfo.message, { error: er as Error });
return "";
}
}
Expand All @@ -130,7 +130,7 @@ function createResponse(res: Response, body: string, failures: unknown[]): Respo
stack: er instanceof Error ? er.stack : undefined,
};
failures.push(errorInfo);
logger.error(errorInfo.message, { error: er });
logger.error(errorInfo.message, { error: er as Error });
return new Response("Internal Server Error", { status: 500 });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Octokit } from "@octokit/rest";
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks";
import { Env } from "./env";
import { PluginSettings } from "./plugin-inputs";
import { Logs } from "@ubiquity-dao/ubiquibot-logger";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import { createAdapters } from "../adapters";
import { Octokit as RestOctokitFromApp } from "octokit";

Expand Down
3 changes: 2 additions & 1 deletion src/types/telegram-bot-single.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Octokit as OctokitRest } from "@octokit/rest";
import { Octokit } from "octokit";
import { Env } from ".";
import { Bot, createBot } from "../bot";
Expand All @@ -22,7 +23,7 @@ export class TelegramBotSingleton {
},
} = env;

let octokit: Octokit | null = null;
let octokit: Octokit | OctokitRest | null = null;

try {
octokit = await PluginContext.getInstance().getTelegramEventOctokit();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogReturn } from "@ubiquity-dao/ubiquibot-logger";
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { Context } from "../types";
import { logger } from "./logger";
import { addCommentToIssue } from "./add-comment-to-issues";
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logs, LogReturn, Metadata, LogLevel } from "@ubiquity-dao/ubiquibot-logger";
import { Logs, LogReturn, Metadata, LogLevel } from "@ubiquity-os/ubiquity-os-logger";

function createLogReturn(method: (log: string, ...args: unknown[]) => void, ...args: Parameters<typeof method>): LogReturn {
method(...args);
Expand Down
Loading

0 comments on commit 86303ce

Please sign in to comment.