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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ package-lock.json
/.pytest_cache/
/notebooks/logs/
/dist
/node_modules
20 changes: 20 additions & 0 deletions interlab_ts/actor/ActorWithMemory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BaseMemory } from './memory/BaseMemory';

export abstract class ActorWithMemory extends BaseActor {
memory: BaseMemory; // BaseMemory type is assumed

constructor(name?: string, memory?: BaseMemory, style?: { [key: string]: any }) {
super(name, style);
this.memory = memory || new DefaultMemory(); // Assuming DefaultMemory is a concrete implementation of BaseMemory
}

_observe(event: Event): void { // Event type is assumed
this.memory.addEvent(event);
}

copy(): ActorWithMemory {
const actor = Object.create(this);
actor.memory = this.memory.copy();
return actor;
}
}
19 changes: 19 additions & 0 deletions interlab_ts/actor/BaseActor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { colorForString } from "../utils";

export abstract class BaseActor {
name: string;
style: { [key: string]: any };

constructor(name?: string, style?: { [key: string]: any }) {
this.name = name || `actor-${Math.floor(Math.random() * 10000).toString().padStart(4, '0')}`;
this.style = style || {};
// Additional logic for 'color' in style, assuming 'randomColor' function exists
if (!this.style["color"]) {
this.style["color"] = colorForString(this.name); // Assume randomColor() returns a color string
}
}

abstract copy(): BaseActor;
abstract query(prompt?: any, expectedType?: any, ...kwargs: any[]): Event; // Event type is assumed
abstract observe(event: Event | any, origin?: string): void; // Event type is assumed
}
9 changes: 9 additions & 0 deletions interlab_ts/actor/Event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class ActorEvent {
data: any;
origin?: string;

constructor(data: any, origin?: string) {
this.data = data;
this.origin = origin;
}
}
11 changes: 11 additions & 0 deletions interlab_ts/actor/memory/MemoryBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export abstract class BaseMemory {
format: FormatBase; // FormatBase type is assumed

constructor(format?: FormatBase) {
this.format = format;
}

abstract copy(): BaseMemory;
abstract addEvent(event: Event): void; // Event type is assumed
abstract getEvents(query?: any): Event[]; // Event type is assumed
}
115 changes: 115 additions & 0 deletions interlab_ts/context/Context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Tag } from "./Tag";
import { ContextState } from "./ContextState";
import { generateUid } from "../utils";

export class Context {
name: string;
kind?: string;
inputs: { [key: string]: any };
result?: any;
error?: any;
state: ContextState;
uid: string; // Unique identifier for the context
children: Context[];
tags: Tag[];
startTime?: Date;
endTime?: Date;
meta?: { [key: string]: any };
// Other properties as needed

constructor(
name: string,
kind?: string,
inputs?: { [key: string]: any },
meta?: { [key: string]: any },
tags?: (string | Tag)[]
) {
this.name = name;
this.kind = kind;
this.inputs = inputs || {};
this.meta = meta;
this.result = null;
this.error = null;
this.state = ContextState.New;
this.uid = generateUid(name); // Implement this function to generate a unique ID
this.children = [];
this.tags = tags ? tags.map(Tag.intoTag) : [];
}

enter(): void {
this.state = ContextState.Open;
this.startTime = new Date();
// Add additional enter logic here
}

exit(): void {
this.state = ContextState.Finished;
this.endTime = new Date();
// Add additional exit logic here
}

addTag(tag: string | Tag): void {
const newTag = Tag.intoTag(tag);
this.tags.push(newTag);
}

addEvent(
name: string,
kind?: string,
data?: any,
meta?: { [key: string]: any },
tags?: (string | Tag)[]
): Context {
const event = new Context(name, kind, undefined, meta, tags);
this.children.push(event);
return event;
}

addInput(name: string, value: any): void {
if (!this.inputs) {
this.inputs = {};
}
if (name in this.inputs) {
throw new Error(`Input ${name} already exists`);
}
this.inputs[name] = serializeWithType(value); // Assuming serializeWithType exists
}

addInputs(inputs: { [key: string]: any }): void {
if (!this.inputs) {
this.inputs = {};
}
Object.keys(inputs).forEach(name => {
if (name in this.inputs) {
throw new Error(`Input ${name} already exists`);
}
this.inputs[name] = serializeWithType(inputs[name]);
});
}

set_result(value: any): void {
this.result = serializeWithType(value); // Assuming serializeWithType exists
}

set_error(exc: any): void {
this.state = ContextState.Error;
this.error = serializeWithType(exc); // Assuming serializeWithType exists
}

has_tag_name(tag_name: string): boolean {
return this.tags.some(tag => tag.name === tag_name);
}

find_contexts(predicate: (context: Context) => boolean): Context[] {
const result: Context[] = [];
const helper = (context: Context) => {
if (predicate(context)) {
result.push(context);
}
context.children.forEach(child => helper(child));
};
helper(this);
return result;
}
// Additional methods for handling inputs, results, errors, serialization, etc.
}
6 changes: 6 additions & 0 deletions interlab_ts/context/ContextState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ContextState {
New = "new",
Open = "open",
Finished = "finished",
Error = "error",
}
18 changes: 18 additions & 0 deletions interlab_ts/context/Tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class Tag {
name: string;
color?: string;

constructor(name: string, color?: string) {
this.name = name;
this.color = color;
}

// Static method to convert a string or Tag into a Tag object
static intoTag(obj: string | Tag): Tag {
if (typeof obj === "string") {
return new Tag(obj);
} else {
return obj;
}
}
}
19 changes: 19 additions & 0 deletions interlab_ts/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Color from 'colorjs.io';
import seedrandom from 'seedrandom';

export function colorForString(str: string, saturation = 0.7, lightness = 0.5, varyLightnessRate = 0.2, varySaturationRate = 0.3): Color {
const rng = seedrandom(str);
return new Color("hsl", [
rng() * 360,
saturation * (varySaturationRate * (2 * rng() - 1)),
lightness * (varyLightnessRate * (2 * rng() - 1))
]);
}

export function generateUid(name: string): string {
const escapeNameRe = /[^a-zA-Z0-9]/g;
const cleanedName = name.substring(0, 16).replace(escapeNameRe, "_");
const randomPart = Array.from({ length: 6 }, () => (Math.random() * 35).toString(36)).join('');
const uid = `${new Date().toISOString()}-${cleanedName}-${randomPart}`;
return uid.replace(/[:/\\]/g, "-");
}
15 changes: 15 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2020",
"jsx": "react",
"allowImportingTsExtensions": true,
"strictNullChecks": true,
"strictFunctionTypes": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "interlab",
"version": "0.4.0",
"description": "A TypeScript implementation of the InterLab project",
"scripts": {
"build": "tsc",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yourusername/interlab.git"
},
"author": "Alignment of Complex Systems Lab",
"license": "MIT",
"bugs": {
"url": "https://github.com/acsresearch/interlab/issues"
},
"homepage": "https://github.com/acsresearch/interlab#readme",
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^20.10.4",
"@types/seedrandom": "^3.0.8",
"jest": "^29.7.0",
"typescript": "^5.3"
},
"dependencies": {
"colorjs.io": "^0.4.5",
"lodash": "^4.17.21",
"seedrandom": "^3.0.5"
}
}
Loading