Skip to content

Commit

Permalink
fut: some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
fulcanelly committed Oct 9, 2024
1 parent cc4566e commit f14179c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 34 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# What is it ?

it's library for easier chat-bot state managment/peristion

currently only telegram API supported, further extension for discrod API possible

## Adding new state


Describe state using actions like `say`, `expect`, `switchState` and others

```ts

export async function mainState() {

await say('Enter a')

const a = Number(await expect_())

await say('Enter b')

const b = Number(await expect_())

await say("Reuslt is " + (a + b))

await switchState('mainState')
}
```

And then add that state to AllStates to safe typing

```ts
declare module 'chat-toolkit' {
interface AllStates {
mainState: typeof mainState
}
}
```


# Modifing actions

# TODO

### Adjusting global state


# TODO

```ts
declare module 'chat-toolkit' {
interface GlobalSharedAppContext {

}
}
```


## TODO
- [ ] Notifications
- [ ] Custom expects
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "",
"private": true,
"license": "UNLICENSED",
"main": "src/index.ts",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
Expand All @@ -24,6 +25,7 @@
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@types/ramda": "^0.30.2",
"dotenv": "^16.4.5",
"moment": "^2.30.1",
"ramda": "^0.30.1",
"reflect-metadata": "^0.2.0",
Expand Down
27 changes: 19 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
// import * as tctx from './telegram/context'
import { Context } from './state/state'
import { _disableRecording, _onRestoreDoRun, escape_, expect_, random, say, suggest, suggestIt, switchState } from './lib/implicit_state'
import { AllStates, Context } from './state/state'
import { createPrivateTelegramContext } from './telegram/context'
import { createTelegramHandler } from './telegram/handler'



export { Context }

export interface Test {
a: number
export {
createPrivateTelegramContext,
createTelegramHandler
}

// export const createPrivateTelegramContext = tctx.createPrivateTelegramContext
export {
suggestIt,
_disableRecording,
_onRestoreDoRun,
escape_,
say,
random,
suggest,
switchState,
expect_
}

export { createPrivateTelegramContext, createTelegramHandler }
export {
AllStates
}
39 changes: 22 additions & 17 deletions src/lib/detect-blocking-states.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runWithImplicitState } from "./implicit_state";
import { allStates, StateParams, SwitchStateError } from "../state/state";
import { StateParams, SwitchStateError } from "../state/state";
import { commonLowLevel } from "../telegram/context";

import * as R from 'ramda';
Expand All @@ -10,7 +10,7 @@ function blockingStateDetectionParams(): StateParams {
return {
...commonLowLevel(),

async say(): Promise<void> {},
async say(): Promise<void> { },

escape() {
const proxy = new Proxy({}, {
Expand Down Expand Up @@ -45,16 +45,6 @@ function blockingStateDetectionParams(): StateParams {
} as StateParams
}

const fixtures = {
requiredArgumentsByState: {
itemSoldState: {
type: 'drugs',
name: 'WEED',
count: 8,
pricePerOne: 13
}
}
}

// what is simple state ?
// its state that is non blocking (in own sense)
Expand Down Expand Up @@ -83,13 +73,28 @@ export const stateTypes = {
simple: new Array<string>(),
}

export async function detectBlockingStates() {



// example
const fixtures = {
requiredArgumentsByState: {
itemSoldState: {

count: 8,
pricePerOne: 13
}
}
}

// TODO
export async function detectBlockingStates(allStates: Object) {

console.log('Trying to detect simple states')
const params = blockingStateDetectionParams()
for (const stateName in allStates) {
const state = allStates[stateName]
const args = fixtures.requiredArgumentsByState[stateName] ?? {}
// const args = fixtures.requiredArgumentsByState[stateName] ?? {}
const args = {}

try {
// logger.silly(stateName)
Expand All @@ -98,8 +103,8 @@ export async function detectBlockingStates() {
if (e instanceof BlockingActionDetectedError) {
stateTypes.blocking.push(stateName)

// } else if (e instanceof SwitchStateError) {
// stateTypes.simple.push(stateName)
// } else if (e instanceof SwitchStateError) {
// stateTypes.simple.push(stateName)

} else {
stateTypes.unknown.push(stateName)
Expand Down
7 changes: 1 addition & 6 deletions src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type LowLevelAction = {

say: (text: string, params?: SayParams) => Promise<void>
expect: () => Promise<string>
switchState: <T>(state: T, args?: SecondArgument<T>) => Promise<never>
switchState: <T extends keyof AllStates>(state: T, args?: Parameters<AllStates[T]>[0]) => Promise<never>
escape: <T>(action: (user: EscapeData) => Promise<T> | T) => Promise<T>
random: () => Promise<number>

Expand All @@ -63,10 +63,6 @@ export type HighLevelActions = {

//todo make it more generic not so coupled

export const allStates = {

}


export interface AllStates {

Expand All @@ -76,7 +72,6 @@ export type SuggestIt<T> = {
option: (text: string, action: () => T | Promise<T>) => SuggestIt<T>;
extra: (e: Media) => SuggestIt<T>
exec: () => Promise<T>;

};


Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"incremental": false,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
Expand Down
3 changes: 1 addition & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/test/a.ts"],
entry: ["src/index.ts"],
format: ["cjs", "esm"], // Build for commonJS and ESmodules
dts: true, // Generate declaration file (.d.ts)
splitting: false,
sourcemap: true,
clean: true,
target: 'es2022',

});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotenv@^16.4.5:
version "16.4.5"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==

eastasianwidth@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
Expand Down

0 comments on commit f14179c

Please sign in to comment.