UNPKG

@inworld/web-core

Version:
61 lines (60 loc) 3.82 kB
import { ActorType, } from '../../proto/ai/inworld/packets/packets.pb.js'; import { SCENE_PATTERN } from '../common/constants.js'; import { Character } from './character.entity.js'; export class Scene { constructor(props) { var _a, _b, _c, _d; this.name = props.name; this.characters = (_a = props.characters) !== null && _a !== void 0 ? _a : []; this.description = (_b = props.description) !== null && _b !== void 0 ? _b : ''; this.displayName = (_c = props.displayName) !== null && _c !== void 0 ? _c : ''; this.history = (_d = props.history) !== null && _d !== void 0 ? _d : []; } static fromProto({ sceneStatus, sessionHistory, }) { var _a, _b, _c, _d, _e; const characters = ((_a = sceneStatus.agents) !== null && _a !== void 0 ? _a : []).map((agent) => Character.fromProto(agent)); const history = (_c = (_b = sessionHistory === null || sessionHistory === void 0 ? void 0 : sessionHistory.sessionHistoryItems) === null || _b === void 0 ? void 0 : _b.reduce((acc, item) => { var _a; if ((_a = item.packets) === null || _a === void 0 ? void 0 : _a.length) { acc.push(...item.packets.map((packet) => { var _a, _b, _c, _d, _e; return ({ character: Character.fromProto(item.agent), packet: Object.assign(Object.assign({}, packet), { routing: Object.assign(Object.assign(Object.assign(Object.assign({}, packet.routing), { source: Object.assign(Object.assign({}, packet.routing.source), (packet.routing.source.type === ActorType.AGENT && { name: (_b = (_a = item.agent) === null || _a === void 0 ? void 0 : _a.agentId) !== null && _b !== void 0 ? _b : packet.routing.source.name, })) }), (packet.routing.target && { target: Object.assign(Object.assign({}, packet.routing.target), (packet.routing.target.type === ActorType.AGENT && { name: (_d = (_c = item.agent) === null || _c === void 0 ? void 0 : _c.agentId) !== null && _d !== void 0 ? _d : packet.routing.target.name, })), })), (((_e = packet.routing.targets) === null || _e === void 0 ? void 0 : _e.length) && { targets: packet.routing.targets.map((target) => { var _a, _b; return Object.assign(Object.assign({}, target), (target.type === ActorType.AGENT && { name: (_b = (_a = item.agent) === null || _a === void 0 ? void 0 : _a.agentId) !== null && _b !== void 0 ? _b : target.name, })); }), })) }), }); })); } return acc; }, [])) !== null && _c !== void 0 ? _c : []; // FIXME: Server send wrong scene name in case of characer resource name is used as scene name const name = SCENE_PATTERN.test(sceneStatus.sceneName) ? sceneStatus.sceneName : (_e = (_d = characters[0]) === null || _d === void 0 ? void 0 : _d.resourceName) !== null && _e !== void 0 ? _e : ''; return new Scene({ name, description: sceneStatus.sceneDescription, displayName: sceneStatus.sceneDisplayName, characters, history, }); } getCharactersByIds(ids) { return this.characters.filter((c) => ids.includes(c.id)); } getCharactersByResourceNames(names) { return this.characters.filter((c) => names.includes(c.resourceName)); } }