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

chore: Add circular dependencies check and fix corresponding errors #322

Merged
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
11 changes: 11 additions & 0 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ jobs:
- name: Linelint
uses: fernandrone/linelint@master
id: linelint

circular-deps:
runs-on: ubuntu-latest
name: Check for circular dependencies
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build node env
uses: ./.github/actions/build_package
- name: Check for circular dependencies
run: yarn circular-deps
3 changes: 2 additions & 1 deletion __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
} from '@proto/ai/inworld/packets/packets_pb';
import { v4 } from 'uuid';

import { Capabilities, Extension, User } from '../src/common/data_structures';
import { Capabilities, User } from '../src/common/data_structures';
import { Extension } from '../src/common/data_structures/extension';
import { protoTimestamp } from '../src/common/helpers';
import { Character } from '../src/entities/character.entity';
import {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"lint:fix": "yarn run lint:check --fix",
"prettier:check": "yarn prettier --check \"./{src,__tests__,examples}/**/*.{js,jsx,ts,tsx}\" --ignore-path .gitignore",
"prettier:format": "yarn prettier --write \"./{src,__tests__,examples}/**/*.{js,jsx,ts,tsx}\" --ignore-path .gitignore",
"prepare": "husky install"
"prepare": "husky install",
"circular-deps": "madge --circular --extensions ts,tsx src"
},
"devDependencies": {
"@release-it/keep-a-changelog": "^5.0.0",
Expand All @@ -60,6 +61,7 @@
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-html-reporter": "^3.10.2",
"madge": "^8.0.0",
"prettier": "^3.2.5",
"release-it": "^17.1.1",
"ts-jest": "^29.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/clients/inworld.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
Awaitable,
Client,
ClientConfiguration,
Extension,
GenerateSessionTokenFn,
GetterSetter,
InternalClientConfiguration,
User,
} from '../common/data_structures';
import { Extension } from '../common/data_structures/extension';
import { SCENE_HAS_INVALID_FORMAT } from '../common/errors';
import { Logger } from '../common/logger';
import { Capability } from '../entities/capability.entity';
Expand Down
49 changes: 49 additions & 0 deletions src/common/data_structures/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
CurrentSceneStatus,
InworldPacket as ProtoPacket,
} from '@proto/ai/inworld/packets/packets_pb';

import { Character } from '../../entities/character.entity';
import { InworldPacket } from '../../entities/packets/inworld_packet.entity';
import {
AudioSessionStartPacketParams,
CancelResponsesProps,
SendPacketParams,
TriggerParameter,
} from './index';

export interface Extension<
InworldPacketT extends InworldPacket = InworldPacket,
> {
convertPacketFromProto?: (proto: ProtoPacket) => InworldPacketT;
beforeLoadScene?: (packets: ProtoPacket[]) => ProtoPacket[];
afterLoadScene?: (res: CurrentSceneStatus) => void;
}

export interface ConvesationInterface<
InworldPacketT extends InworldPacket = InworldPacket,
> {
getConversationId(): string;
getParticipants(): string[];
getCharacters(): Character[];
changeParticipants(participants: string[]): void;
updateParticipants(participants: string[]): Promise<InworldPacketT>;
sendText: (text: string) => Promise<InworldPacketT>;
sendAudio: (chunk: string) => Promise<InworldPacketT>;
sendTrigger: (
name: string,
parameters?: { parameters?: TriggerParameter[]; character?: Character },
) => Promise<InworldPacketT>;
sendAudioSessionStart(
params?: AudioSessionStartPacketParams,
): Promise<InworldPacketT>;
sendAudioSessionEnd(): Promise<InworldPacketT>;
sendCancelResponse(
cancelResponses?: CancelResponsesProps,
): Promise<InworldPacketT>;
sendTTSPlaybackMute(isMuted: boolean): Promise<InworldPacketT>;
sendNarratedAction(text: string): Promise<InworldPacketT>;
sendCustomPacket(
getPacket: (params: SendPacketParams) => ProtoPacket,
): Promise<InworldPacketT>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import {
} from '@proto/ai/inworld/engine/configuration/configuration_pb';
import {
Continuation,
CurrentSceneStatus,
InworldPacket as ProtoPacket,
} from '@proto/ai/inworld/packets/packets_pb';
import { Character } from 'entities/character.entity';

import { SessionContinuationProps } from '../entities/continuation/session_continuation.entity';
import { InworldPacket } from '../entities/packets/inworld_packet.entity';
import { SessionToken } from '../entities/session_token.entity';
import { ConversationService } from '../services/conversation.service';
import { Character } from '../../entities/character.entity';
import { SessionContinuationProps } from '../../entities/continuation/session_continuation.entity';
import { SessionToken } from '../../entities/session_token.entity';

export interface ApiKey {
key: string;
secret: string;
}

export enum ConversationState {
ACTIVE = 'ACTIVE',
PROCESSING = 'PROCESSING',
INACTIVE = 'INACTIVE',
}

export interface Capabilities {
audio?: boolean;
debugInfo?: boolean;
Expand Down Expand Up @@ -90,13 +93,6 @@ export interface CancelResponsesProps {
utteranceId?: string[];
}

export interface SessionTokenProps {
token: string;
type: string;
expirationTime: Date;
sessionId: string;
}

export interface GetterSetter<T> {
get: () => Awaitable<T | undefined>;
set: (entity: T) => Awaitable<any>;
Expand All @@ -108,14 +104,6 @@ export enum ConnectionState {
INACTIVE = 'INACTIVE',
}

export interface Extension<
InworldPacketT extends InworldPacket = InworldPacket,
> {
convertPacketFromProto?: (proto: ProtoPacket) => InworldPacketT;
beforeLoadScene?: (packets: ProtoPacket[]) => ProtoPacket[];
afterLoadScene?: (res: CurrentSceneStatus) => void;
}

export interface CustomParameter {
name: string;
value: string;
Expand Down Expand Up @@ -166,12 +154,6 @@ export enum InworldLatencyReportType {
PING_PONG = 'PING_PONG',
}

export enum ConversationState {
ACTIVE = 'ACTIVE',
PROCESSING = 'PROCESSING',
INACTIVE = 'INACTIVE',
}

export enum ConversationIntializeState {
ACTIVE = 'ACTIVE',
PROCESSING = 'PROCESSING',
Expand Down Expand Up @@ -207,13 +189,6 @@ export interface AudioSessionStartPacketParams {
understandingMode?: UnderstandingMode;
}

export interface ConversationMapItem<
InworldPacketT extends InworldPacket = InworldPacket,
> {
service: ConversationService<InworldPacketT>;
state: ConversationState;
}

export enum ConversationParticipant {
USER = 'USER',
}
Expand Down
9 changes: 7 additions & 2 deletions src/entities/session_token.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { AccessToken } from '@proto/ai/inworld/engine/world-engine_pb';

import { SessionTokenProps } from '../common/data_structures';

const TIME_DIFF_MS = 50 * 60 * 1000; // 5 minutes

export interface SessionTokenProps {
token: string;
type: string;
expirationTime: Date;
sessionId: string;
}

export class SessionToken {
token: string;
type: string;
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import {
LogsEventLogDetail,
MicrophoneMode,
ProtobufValue,
SessionTokenProps,
TaskParameter,
TriggerParameter,
UnderstandingMode,
User,
UserProfile,
UserProfileField,
} from './common/data_structures';
import { Extension } from './common/data_structures/extension';
import * as InworldTriggers from './common/inworld_triggers';
import { Character } from './entities/character.entity';
import {
Expand Down Expand Up @@ -73,7 +73,10 @@ import { TextEvent } from './entities/packets/text.entity';
import { TriggerEvent } from './entities/packets/trigger.entity';
import { Scene } from './entities/scene.entity';
import { Session } from './entities/session.entity';
import { SessionToken } from './entities/session_token.entity';
import {
SessionToken,
SessionTokenProps,
} from './entities/session_token.entity';
import { ConversationService } from './services/conversation.service';
import {
FeedbackDislikeProps,
Expand Down Expand Up @@ -109,6 +112,7 @@ export {
ErrorReconnectionType,
ErrorResourceType,
ErrorType,
Extension,
Feedback,
FeedbackDislikeProps,
FeedbackLikeProps,
Expand Down
15 changes: 11 additions & 4 deletions src/services/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import {
Awaitable,
ChangeSceneProps,
ConnectionState,
ConversationMapItem,
ConversationState,
Extension,
GenerateSessionTokenFn,
GetterSetter,
InternalClientConfiguration,
InworldConversationEventType,
User,
} from '../common/data_structures';
import {
ConvesationInterface,
Extension,
} from '../common/data_structures/extension';
import { calculateTimeDifference } from '../common/helpers';
import { Logger } from '../common/logger';
import { Capability } from '../entities/capability.entity';
Expand Down Expand Up @@ -87,8 +89,13 @@ export class ConnectionService<
onError: (err: InworldError) => Awaitable<void>;
onMessage: ((message: ProtoPacket) => Awaitable<void>) | undefined;

readonly conversations: Map<string, ConversationMapItem<InworldPacketT>> =
new Map();
readonly conversations: Map<
string,
{
service: ConvesationInterface<InworldPacketT>;
state: ConversationState;
}
> = new Map();

constructor(props: ConnectionProps<InworldPacketT>) {
this.connectionProps = props;
Expand Down
7 changes: 4 additions & 3 deletions src/services/conversation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {
AudioSessionStartPacketParams,
CancelResponsesProps,
ConversationParticipant,
ConversationState,
SendPacketParams,
TriggerParameter,
} from '../common/data_structures';
import { ConversationState } from '../common/data_structures';
import { ConvesationInterface } from '../common/data_structures/extension';
import { MULTI_CHAR_NARRATED_ACTIONS } from '../common/errors';
import { Character } from '../entities/character.entity';
import { InworldPacket } from '../entities/packets/inworld_packet.entity';
import { EventFactory } from '../factories/event';
import { ConnectionService } from './connection.service';

export interface PacketQueueItem<
InworldPacketT extends InworldPacket = InworldPacket,
> {
Expand All @@ -27,7 +27,8 @@ export interface PacketQueueItem<

export class ConversationService<
InworldPacketT extends InworldPacket = InworldPacket,
> {
> implements ConvesationInterface<InworldPacketT>
{
private connection: ConnectionService<InworldPacketT>;
private conversationId: string;
private participants: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/services/gprc/world_engine_client_grpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import { CLIENT_ID, SCENE_PATTERN } from '../../common/constants';
import {
ApiKey,
Awaitable,
Extension,
InternalClientConfiguration,
User,
} from '../../common/data_structures';
import { Extension } from '../../common/data_structures/extension';
import { grpcOptions } from '../../common/helpers';
import { Logger } from '../../common/logger';
import { SessionContinuation } from '../../entities/continuation/session_continuation.entity';
Expand Down
2 changes: 1 addition & 1 deletion src/services/inworld_connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
ChangeSceneProps,
ConversationIntializeState,
ConversationParticipant,
ConversationState,
TriggerParameter,
} from '../common/data_structures';
import { ConversationState } from '../common/data_structures';
import {
CHARACTER_HAS_INVALID_FORMAT,
CURRENT_CHARACTER_NOT_SET,
Expand Down
Loading
Loading