Files
metabuilder/workflow/core/utils/priority-queue.ts
johndoe6345789 5ac579a2ed feat: Add Python plugins from AutoMetabuilder + restructure workflow folder
Restructure workflow/ for multi-language plugin support:
- Rename src/ to core/ (engine code: DAG executor, registry, types)
- Create executor/{cpp,python,ts}/ for language-specific runtimes
- Consolidate plugins to plugins/{ts,python}/ by language then category

Add 80+ Python plugins from AutoMetabuilder in 14 categories:
- control: bot control, switch logic, state management
- convert: type conversions (json, boolean, dict, list, number, string)
- core: AI requests, context management, tool calls
- dict: dictionary operations (get, set, keys, values, merge)
- list: list operations (concat, find, sort, slice, filter)
- logic: boolean logic (and, or, xor, equals, comparisons)
- math: arithmetic operations (add, subtract, multiply, power, etc.)
- string: string manipulation (concat, split, replace, format)
- notifications: Slack, Discord integrations
- test: assertion helpers and test suite runner
- tools: file operations, git, docker, testing utilities
- utils: filtering, mapping, reducing, condition branching
- var: variable store operations (get, set, delete, exists)
- web: Flask server, environment variables, JSON handling

Add language executor runtimes:
- TypeScript: direct import execution (default, fast startup)
- Python: child process with JSON stdin/stdout communication
- C++: placeholder for native FFI bindings (Phase 3)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 16:08:08 +00:00

110 lines
2.4 KiB
TypeScript

/**
* Priority Queue implementation for workflow DAG execution
* Used to manage node execution order with priority-based scheduling
*/
export interface QueueItem<T> {
item: T;
priority: number;
}
export class PriorityQueue<T> {
private heap: QueueItem<T>[] = [];
/**
* Insert item with priority (lower number = higher priority)
*/
enqueue(item: T, priority: number): void {
const queueItem: QueueItem<T> = { item, priority };
this.heap.push(queueItem);
this._bubbleUp(this.heap.length - 1);
}
/**
* Remove and return highest priority item
*/
dequeue(): QueueItem<T> | undefined {
if (this.heap.length === 0) return undefined;
const top = this.heap[0];
const bottom = this.heap.pop();
if (this.heap.length > 0 && bottom) {
this.heap[0] = bottom;
this._bubbleDown(0);
}
return top;
}
/**
* Check if queue is empty
*/
isEmpty(): boolean {
return this.heap.length === 0;
}
/**
* Get queue size
*/
size(): number {
return this.heap.length;
}
/**
* Move element up to maintain heap property
*/
private _bubbleUp(index: number): void {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[index].priority < this.heap[parentIndex].priority) {
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
} else {
break;
}
}
}
/**
* Move element down to maintain heap property
*/
private _bubbleDown(index: number): void {
const length = this.heap.length;
while (true) {
let smallest = index;
const leftChild = 2 * index + 1;
const rightChild = 2 * index + 2;
if (leftChild < length && this.heap[leftChild].priority < this.heap[smallest].priority) {
smallest = leftChild;
}
if (rightChild < length && this.heap[rightChild].priority < this.heap[smallest].priority) {
smallest = rightChild;
}
if (smallest !== index) {
[this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
index = smallest;
} else {
break;
}
}
}
/**
* Peek at highest priority item without removing
*/
peek(): QueueItem<T> | undefined {
return this.heap[0];
}
/**
* Clear the queue
*/
clear(): void {
this.heap = [];
}
}