Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server api for apps + Add external hooks for apps #101

Merged
merged 8 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/hooks/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": [["@babel/env"], "@babel/react", "@babel/preset-typescript"],
"plugins": ["@babel/plugin-transform-runtime"]
}
106 changes: 106 additions & 0 deletions packages/hooks/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:prettier/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"project": "../../tsconfig.json"
},
"plugins": ["prettier", "react", "import", "@atlaskit/design-system"],
"rules": {
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
// "@atlaskit/design-system/ensure-design-token-usage": "warn",
"@atlaskit/design-system/no-deprecated-design-token-usage": "warn",
"@atlaskit/design-system/no-unsafe-design-token-usage": "error",
"@atlaskit/design-system/use-visually-hidden": "error",
"@atlaskit/design-system/no-deprecated-imports": "error",
"@atlaskit/design-system/no-deprecated-apis": "error",
"react/no-unknown-property": ["error", { "ignore": ["css"] }],
"linebreak-style": "off",
"prettier/prettier": [
"error",
{ "singleQuote": true, "endOfLine": "auto" }
],
"semi": ["error", "always"],
"object-curly-spacing": ["error", "always"],
"camelcase": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": ["off"],
"@typescript-eslint/no-inferrable-types": [
"warn",
{
"ignoreParameters": true
}
],
"no-underscore-dangle": "off",
"no-shadow": "off",
"no-new": 0,
"@typescript-eslint/no-shadow": ["error"],
"@typescript-eslint/no-unused-vars": "warn",
"no-restricted-imports": "off",
"@typescript-eslint/no-restricted-imports": [
"warn",
{
"name": "react-redux",
"importNames": ["useSelector", "useDispatch"],
"message": "Use typed hooks `useAppDispatch` and `useAppSelector` instead."
}
],
"quotes": [2, "single", { "avoidEscape": true }],
"class-methods-use-this": "off",
"import/order": [
"error",
{
"groups": ["builtin", "external", "parent", "sibling", "index"],
"pathGroups": [
{
"pattern": "react?(-*)",
"group": "builtin",
"position": "before"
},
{
"group": "external",
"pattern": "internal/**/*",
"position": "after"
}
],
"pathGroupsExcludedImportTypes": ["react"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
],
"import/extensions": [
"error",
"ignorePackages",
{
"": "never",
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
},
"settings": {
"react": {
"version": "detect"
}
}
}
9 changes: 9 additions & 0 deletions packages/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules

build
es
lib
dist

.DS_Store
.env
9 changes: 9 additions & 0 deletions packages/hooks/.release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"git": {
"requireCleanWorkingDir": false,
"tag": false
},
"npm": {
"versionArgs": ["--workspaces-update=false"]
}
}
49 changes: 49 additions & 0 deletions packages/hooks/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const gulp = require("gulp");
const babel = require("gulp-babel");
const ts = require("gulp-typescript");
const sourcemaps = require("gulp-sourcemaps");
const del = require("del");

gulp.task("clean", async function () {
await del("lib/**");
await del("es/**");
await del("dist/**");
});

gulp.task("cjs", function () {
return gulp
.src(["./es/**/*.js"])
.pipe(
babel({
configFile: "./.babelrc",
})
)
.pipe(gulp.dest("lib/"));
});

gulp.task("es", function () {
const tsProject = ts.createProject("tsconfig.pro.json", {
module: "ESNext",
});
return tsProject
.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.pipe(babel())
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("es/"));
});

gulp.task("declaration", function () {
const tsProject = ts.createProject("tsconfig.pro.json", {
declaration: true,
emitDeclarationOnly: true,
});
return tsProject
.src()
.pipe(tsProject())
.pipe(gulp.dest("es/"))
.pipe(gulp.dest("lib/"));
});

exports.default = gulp.series("clean", "es", "cjs", "declaration");
85 changes: 85 additions & 0 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"name": "@encrejs/hooks",
"version": "0.0.0",
"engines": {
"node": ">=20.0.0"
},
"author": "Encre",
"license": "MIT",
"files": [
"dist",
"lib",
"es",
"package.json"
],
"main": "./lib/index.js",
"module": "./es/index.js",
"types": "./lib/index.d.ts",
"unpkg": "dist/hooks.js",
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/VictorS67/encre.git",
"directory": "packages/ui"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"packageManager": "[email protected]",
"devDependencies": {
"@babel/cli": "^7.10.1",
"@babel/core": "^7.25.2",
"@babel/plugin-transform-runtime": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/gulp": "^4.0.17",
"@types/gulp-babel": "^6.1.33",
"@types/jest": "^29.5.4",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"babel-loader": "^9.1.3",
"babel-plugin-import": "^1.12.0",
"cross-env": "^7.0.3",
"del": "^5.1.0",
"eslint": "^8.49.0",
"eslint-cjs-to-esm": "^2.2.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard-with-typescript": "^39.0.0",
"eslint-import-resolver-typescript": "^3.6.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.1.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"gulp": "^5.0.0",
"gulp-babel": "^8.0.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-typescript": "^6.0.0-alpha.1",
"jest": "^29.4.1",
"prettier": "^3.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.2.2",
"webpack": "^5.90.0",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"eventsource-parser": "1.0.0",
"lodash-es": "^4.17.21",
"lru-cache": "^11.0.0",
"webpack-cli": "^5.1.4"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"scripts": {
"clean": "rimraf '{lib,es,dist}'",
"build": "gulp && webpack-cli",
"lint": "eslint src/*.ts src/**/*.ts",
"lint-fix": "eslint --fix src/*.ts src/**/*.ts",
"test": "cross-env NODE_ENV=test jest"
}
}
45 changes: 45 additions & 0 deletions packages/hooks/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { LRUCache } from 'lru-cache';

import { EncreData } from './types';

export interface EncreCache {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this interface for specifically, I understand that it is storing a graph and a node, but just want to make sure I understand the software, what part fo the process does EncreCache speeds up specifically?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Question! EncreCache is used for storing incoming data from input handlers. You can image a scenario where there is a message box that connects to an input handler, so whenever user change the message, the input handler should store that changed value. But this changed value is not sent to the workflow immediately, since no action is triggered by the uset. So we need a cache to store this value.

get: (
graphId: string,
nodeId: string,
) => [string, Record<string, EncreData>] | undefined;

has: (graphId: string, nodeId: string) => boolean;

set: (
graphId: string,
nodeId: string,
data: [string, Record<string, EncreData>] | undefined,
) => void;
}

export const encreCache = (): EncreCache => {
const ref = new LRUCache<string, [string, Record<string, EncreData>]>({
maxSize: 500,
sizeCalculation: (value) => {
return JSON.stringify(value).length;
},
});

const set = (
graphId: string,
nodeId: string,
data: [string, Record<string, EncreData>] | undefined,
) => {
ref.set(`${graphId}-${nodeId}`, data);
};

const get = (graphId: string, nodeId: string) => {
return ref.get(`${graphId}-${nodeId}`);
};

const has = (graphId: string, nodeId: string) => {
return ref.has(`${graphId}-${nodeId}`);
};

return { set, get, has } satisfies EncreCache;
};
2 changes: 2 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export { useAppHandler } from './useAppHandler';
9 changes: 9 additions & 0 deletions packages/hooks/src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as hooks from '..';

describe('hooks', () => {
test('exports modules should be defined', () => {
Object.keys(hooks).forEach((module) => {
expect(hooks[module]).toBeDefined();
});
});
});
6 changes: 6 additions & 0 deletions packages/hooks/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EncreAppHander } from './handlers';

export interface EncreAppConfig {
workflow: string;
handlers: Record<string, EncreAppHander>;
}
4 changes: 4 additions & 0 deletions packages/hooks/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Context {
pageContent: string;
metadata?: Record<string, unknown>;
}
11 changes: 11 additions & 0 deletions packages/hooks/src/types/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type EncreData = {
type:
| 'string'
| 'number'
| 'boolean'
| 'context'
| 'chat-message'
| 'object'
| 'unknown';
value: unknown;
};
Loading
Loading