Skip to content
Open
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
79 changes: 79 additions & 0 deletions src/adapters/automaton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { RoutingOptions } from '../utils/router';

import { Router } from '../utils';

export type Automata = 'ADD_USER_TO_ALL_GROUPS' | 'REMOVE_USER_FROM_ALL_GROUPS';

export type AutomatonStatus = 'EXECUTING' | 'COMPLETED' | 'FAILED';

export type AutomatonParameterType = 'string' | 'date' | 'long' | 'double';

export interface AutomatonParameter {
objectType: AutomatonParameterType;
value: string | number | null;
}

export interface AutomatonParameters {
[key: string]: AutomatonParameter;
}

export interface AutomatedReadOutView {
jobId: number | null;
error: string | null;
status: AutomatonStatus | null;
}


/**
* Starts an automaton job for a given project.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/automaton/{AUTOMATA}`
*
* @example
* import { automatonAdapter } from 'epicenter-libs';
* const job = await automatonAdapter.start('ADD_USER_TO_ALL_GROUPS', {
* userId: { objectType: 'string', value: 'user123' },
* });
*
* @param automata The automaton action to execute
* @param parameters Parameters for the automaton job, keyed by parameter name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.addNonce] If true, adds a nonce to the request
* @returns promise that resolves to the automaton job status
*/
export async function start(
automata: Automata,
parameters: AutomatonParameters,
optionals: {
addNonce?: boolean;
} & RoutingOptions = {},
): Promise<AutomatedReadOutView> {
const { addNonce, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ addNonce })
.post(`/automaton/${automata}`, {
body: { parameters },
...routingOptions,
}).then(({ body }) => body);
Comment thread
sparklerfish marked this conversation as resolved.
}


/**
* Gets the status of an automaton job.
* Base URL: GET `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/automaton/{JOB_ID}`
*
* @example
* import { automatonAdapter } from 'epicenter-libs';
* const status = await automatonAdapter.getStatus(12345);
*
* @param jobId The job ID returned from starting an automaton
* @param [optionals] Optional arguments; pass network call options overrides here.
* @returns promise that resolves to the automaton job status
*/
export async function getStatus(
jobId: number,
optionals: RoutingOptions = {},
): Promise<AutomatedReadOutView> {
return await new Router()
.get(`/automaton/${jobId}`, optionals)
.then(({ body }) => body);
}
117 changes: 117 additions & 0 deletions src/adapters/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import type { RoutingOptions } from '../utils/router';
import type { ModelLanguage, Morphology } from '../utils/constants';

import { Router } from '../utils';

export interface StellaModelTool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The model tool types (and V1ExecutionContext/ExecutionContext themselves) are already defined (and a bit more accurate to the Java definitions) in the runAdapter. context.ts re‑declares all of these with looser/divergent shapes (e.g. version: unknown vs run's string, cinFiles?: unknown[] | null vs run's string[], plus stray | nulls). Let's move the existing runAdapter definitions out into constants.ts and import into both adapters from there so there's a single source of truth. Suggested plan:

  • constants.ts --
export interface StellaModelTool {
    objectType: 'stella';
    gameMode?: boolean;
}

export interface VensimModelTool {
    objectType: 'vensim';
    sensitivityMode?: boolean;
    cinFiles?: string[];
}

export type ModelTool = StellaModelTool | VensimModelTool;

export interface V1ExecutionContext {
    presets?: Record<string, Record<string, unknown>>;
    mappedFiles?: Record<string, string>;
    version: string; // optionally narrow to 'V1' to match the Java ExecutionContextVersion enum
    tool?: ModelTool;
}

export interface ExecutionContext extends V1ExecutionContext {}
  • run.ts -- delete its local StellaModelTool/VensimModelTool (:225,:230) and V1ExecutionContext/ExecutionContext (:216,:223); import them from constants, exactly as it already does for MORPHOLOGY. ModelContextDefaults.tool can stay StellaModelTool | VensimModelTool or become ModelTool.
  • context.ts -- delete its local copies (:6‑24: StellaModelTool, VensimModelTool, ModelTool, V1ExecutionContext); import ModelTool and V1ExecutionContext from constants
  • types.ts -- point the re‑exports at one source: StellaModelTool, VensimModelTool, ModelTool, V1ExecutionContext, ExecutionContext all from ./utils/constants, instead of the current split

One thing to leave alone: the two ModelContext types are different concepts — run's structured ModelContext extends V2ModelContext vs context's free‑form { [key: string]: unknown } blob returned by context.get() (and types.ts already disambiguates them via the ContextModelContext alias). Those shouldn't be merged.

objectType: 'stella';
gameMode?: boolean | null;
}

export interface VensimModelTool {
objectType: 'vensim';
sensitivityMode?: boolean | null;
cinFiles?: unknown[] | null;
}

export type ModelTool = StellaModelTool | VensimModelTool;

export interface V1ExecutionContext {
version: unknown;
presets?: Record<string, object> | null;
mappedFiles?: Record<string, string> | null;
tool?: ModelTool;
}

export interface ModelContext {
[key: string]: unknown;
}


/**
* Gets the model context for a given model file.
* Base URL: GET `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/{MODEL_FILE}`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* const context = await contextAdapter.get('model.vmf');
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type for the model context
* @returns promise that resolves to the model context
*/
export async function get(
modelFile: string,
optionals: {
morphology?: Morphology;
} & RoutingOptions = {},
): Promise<ModelContext> {
const { morphology, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ morphology })
.get(`/context/${modelFile}`, routingOptions)
.then(({ body }) => body);
}


/**
* Upgrades the model context for a given model file.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/upgrade/{MODEL_FILE}`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* await contextAdapter.upgrade('model.vmf', { morphology: 'SINGULAR' });
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type for the upgrade
* @returns promise that resolves to void on success
*/
export async function upgrade(
modelFile: string,
optionals: {
morphology?: Morphology;
} & RoutingOptions = {},
): Promise<void> {
const { morphology, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ morphology })
.post(`/context/upgrade/${modelFile}`, routingOptions)
.then(({ body }) => body);
}


/**
* Verifies a model context configuration.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/verify`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* await contextAdapter.verify('model.vmf', {
* modelLanguage: 'VENSIM',
* morphology: 'SINGULAR',
* });
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type
* @param [optionals.modelLanguage] The model language
* @param [optionals.executionContext] The execution context configuration
* @returns promise that resolves to void on success
*/
export async function verify(
modelFile: string,
optionals: {
morphology?: Morphology;
modelLanguage?: ModelLanguage;
executionContext?: V1ExecutionContext;
} & RoutingOptions = {},
): Promise<void> {
const { morphology, modelLanguage, executionContext, ...routingOptions } = optionals;
return await new Router()
.post('/context/verify', {
body: { modelFile, morphology, modelLanguage, executionContext },
...routingOptions,
}).then(({ body }) => body);
}
Loading
Loading