@workglow/util 0.0.121 → 0.0.123
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.js +232 -2098
- package/dist/browser.js.map +6 -25
- package/dist/bun.js +236 -2099
- package/dist/bun.js.map +6 -25
- package/dist/common.d.ts +0 -15
- package/dist/common.d.ts.map +1 -1
- package/dist/compress-browser.d.ts +7 -0
- package/dist/compress-browser.d.ts.map +1 -0
- package/dist/compress-browser.js +18 -0
- package/dist/compress-browser.js.map +10 -0
- package/dist/compress-node.d.ts +7 -0
- package/dist/compress-node.d.ts.map +1 -0
- package/dist/compress-node.js +25 -0
- package/dist/compress-node.js.map +10 -0
- package/dist/credentials/ChainedCredentialStore.d.ts.map +1 -1
- package/dist/credentials/EnvCredentialStore.d.ts.map +1 -1
- package/dist/credentials/InMemoryCredentialStore.d.ts.map +1 -1
- package/dist/di/Container.d.ts.map +1 -1
- package/dist/di/ServiceRegistry.d.ts.map +1 -1
- package/dist/events/EventEmitter.d.ts.map +1 -1
- package/dist/graph/directedAcyclicGraph.d.ts.map +1 -1
- package/dist/graph/directedGraph.d.ts.map +1 -1
- package/dist/graph/errors.d.ts.map +1 -1
- package/dist/graph/graph.d.ts.map +1 -1
- package/dist/graph-entry.d.ts +7 -0
- package/dist/graph-entry.d.ts.map +1 -0
- package/dist/graph-entry.js +539 -0
- package/dist/graph-entry.js.map +15 -0
- package/dist/logging/ConsoleLogger.d.ts.map +1 -1
- package/dist/logging/NullLogger.d.ts.map +1 -1
- package/dist/media-browser.d.ts +8 -0
- package/dist/media-browser.d.ts.map +1 -0
- package/dist/media-browser.js +73 -0
- package/dist/media-browser.js.map +11 -0
- package/dist/media-node.d.ts +8 -0
- package/dist/media-node.d.ts.map +1 -0
- package/dist/media-node.js +50 -0
- package/dist/media-node.js.map +11 -0
- package/dist/node.js +246 -2108
- package/dist/node.js.map +6 -25
- package/dist/schema-entry.d.ts +17 -0
- package/dist/schema-entry.d.ts.map +1 -0
- package/dist/schema-entry.js +774 -0
- package/dist/schema-entry.js.map +18 -0
- package/dist/telemetry/ConsoleTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/NoopTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/OTelTelemetryProvider.d.ts.map +1 -1
- package/dist/types.d.ts +0 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utilities/BaseError.d.ts.map +1 -1
- package/dist/vector/Tensor.d.ts +1 -1
- package/dist/vector/Tensor.d.ts.map +1 -1
- package/dist/vector/TypedArray.d.ts +4 -4
- package/dist/vector/TypedArray.d.ts.map +1 -1
- package/dist/worker/Worker.browser.d.ts +6 -1
- package/dist/worker/Worker.browser.d.ts.map +1 -1
- package/dist/worker/Worker.bun.d.ts +6 -1
- package/dist/worker/Worker.bun.d.ts.map +1 -1
- package/dist/worker/Worker.node.d.ts +6 -1
- package/dist/worker/Worker.node.d.ts.map +1 -1
- package/dist/worker/WorkerManager.d.ts +7 -1
- package/dist/worker/WorkerManager.d.ts.map +1 -1
- package/dist/worker/{WorkerServer.d.ts → WorkerServerBase.d.ts} +5 -4
- package/dist/worker/WorkerServerBase.d.ts.map +1 -0
- package/dist/worker-browser.js +894 -0
- package/dist/worker-browser.js.map +19 -0
- package/dist/worker-bun.js +895 -0
- package/dist/worker-bun.js.map +19 -0
- package/dist/worker-entry.d.ts +25 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-node.js +914 -0
- package/dist/worker-node.js.map +19 -0
- package/package.json +59 -19
- package/dist/mcp/McpAuthProvider.d.ts +0 -70
- package/dist/mcp/McpAuthProvider.d.ts.map +0 -1
- package/dist/mcp/McpAuthTypes.d.ts +0 -218
- package/dist/mcp/McpAuthTypes.d.ts.map +0 -1
- package/dist/mcp/McpClientUtil.browser.d.ts +0 -198
- package/dist/mcp/McpClientUtil.browser.d.ts.map +0 -1
- package/dist/mcp/McpClientUtil.node.d.ts +0 -222
- package/dist/mcp/McpClientUtil.node.d.ts.map +0 -1
- package/dist/worker/WorkerServer.d.ts.map +0 -1
package/dist/bun.js
CHANGED
|
@@ -431,975 +431,6 @@ class BaseError {
|
|
|
431
431
|
return `${this.name}: ${this.message}`;
|
|
432
432
|
}
|
|
433
433
|
}
|
|
434
|
-
|
|
435
|
-
// src/graph/errors.ts
|
|
436
|
-
class NodeAlreadyExistsError extends BaseError {
|
|
437
|
-
static type = "NodeAlreadyExistsError";
|
|
438
|
-
newNode;
|
|
439
|
-
oldNode;
|
|
440
|
-
identity;
|
|
441
|
-
constructor(newNode, oldNode, identity) {
|
|
442
|
-
super(`${JSON.stringify(newNode)} shares an identity (${String(identity)}) with ${JSON.stringify(oldNode)}`);
|
|
443
|
-
this.newNode = newNode;
|
|
444
|
-
this.oldNode = oldNode;
|
|
445
|
-
this.identity = identity;
|
|
446
|
-
this.name = "NodeAlreadyExistsError";
|
|
447
|
-
Object.setPrototypeOf(this, NodeAlreadyExistsError.prototype);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
class NodeDoesntExistError extends BaseError {
|
|
452
|
-
static type = "NodeDoesntExistError";
|
|
453
|
-
identity;
|
|
454
|
-
constructor(identity) {
|
|
455
|
-
super(`A node with identity ${String(identity)} doesn't exist in the graph`);
|
|
456
|
-
this.identity = identity;
|
|
457
|
-
this.name = "NodeDoesntExistError";
|
|
458
|
-
Object.setPrototypeOf(this, NodeDoesntExistError.prototype);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
class CycleError extends BaseError {
|
|
463
|
-
static type = "CycleError";
|
|
464
|
-
constructor(message) {
|
|
465
|
-
super(message);
|
|
466
|
-
this.name = "CycleError";
|
|
467
|
-
Object.setPrototypeOf(this, CycleError.prototype);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// src/graph/graph.ts
|
|
472
|
-
class Graph {
|
|
473
|
-
nodes;
|
|
474
|
-
adjacency;
|
|
475
|
-
nodeIdentity;
|
|
476
|
-
edgeIdentity;
|
|
477
|
-
constructor(nodeIdentity, edgeIdentity) {
|
|
478
|
-
this.nodes = new Map;
|
|
479
|
-
this.adjacency = [];
|
|
480
|
-
this.nodeIdentity = nodeIdentity;
|
|
481
|
-
this.edgeIdentity = edgeIdentity;
|
|
482
|
-
}
|
|
483
|
-
events = new EventEmitter;
|
|
484
|
-
on(name, fn) {
|
|
485
|
-
this.events.on.call(this.events, name, fn);
|
|
486
|
-
}
|
|
487
|
-
off(name, fn) {
|
|
488
|
-
this.events.off.call(this.events, name, fn);
|
|
489
|
-
}
|
|
490
|
-
emit(name, ...args) {
|
|
491
|
-
this.events.emit(name, ...args);
|
|
492
|
-
}
|
|
493
|
-
insert(node) {
|
|
494
|
-
const id = this.nodeIdentity(node);
|
|
495
|
-
const isOverwrite = this.nodes.has(id);
|
|
496
|
-
if (isOverwrite) {
|
|
497
|
-
throw new NodeAlreadyExistsError(node, this.nodes.get(id), id);
|
|
498
|
-
}
|
|
499
|
-
this.nodes.set(id, node);
|
|
500
|
-
this.adjacency.map((adj) => adj.push(null));
|
|
501
|
-
this.adjacency.push(new Array(this.adjacency.length + 1).fill(null));
|
|
502
|
-
this.emit("node-added", id);
|
|
503
|
-
return id;
|
|
504
|
-
}
|
|
505
|
-
replace(node) {
|
|
506
|
-
const id = this.nodeIdentity(node);
|
|
507
|
-
const isOverwrite = this.nodes.has(id);
|
|
508
|
-
if (!isOverwrite) {
|
|
509
|
-
throw new NodeDoesntExistError(id);
|
|
510
|
-
}
|
|
511
|
-
this.nodes.set(id, node);
|
|
512
|
-
this.emit("node-replaced", id);
|
|
513
|
-
}
|
|
514
|
-
upsert(node) {
|
|
515
|
-
const id = this.nodeIdentity(node);
|
|
516
|
-
const isOverwrite = this.nodes.has(id);
|
|
517
|
-
this.nodes.set(id, node);
|
|
518
|
-
if (!isOverwrite) {
|
|
519
|
-
this.adjacency.map((adj) => adj.push(null));
|
|
520
|
-
this.adjacency.push(new Array(this.adjacency.length + 1).fill(null));
|
|
521
|
-
this.emit("node-added", id);
|
|
522
|
-
} else {
|
|
523
|
-
this.emit("node-replaced", id);
|
|
524
|
-
}
|
|
525
|
-
return id;
|
|
526
|
-
}
|
|
527
|
-
addEdge(node1Identity, node2Identity, edge) {
|
|
528
|
-
if (edge === undefined) {
|
|
529
|
-
edge = true;
|
|
530
|
-
}
|
|
531
|
-
const node1Exists = this.nodes.has(node1Identity);
|
|
532
|
-
const node2Exists = this.nodes.has(node2Identity);
|
|
533
|
-
if (!node1Exists) {
|
|
534
|
-
throw new NodeDoesntExistError(node1Identity);
|
|
535
|
-
}
|
|
536
|
-
if (!node2Exists) {
|
|
537
|
-
throw new NodeDoesntExistError(node2Identity);
|
|
538
|
-
}
|
|
539
|
-
const node1Index = Array.from(this.nodes.keys()).indexOf(node1Identity);
|
|
540
|
-
const node2Index = Array.from(this.nodes.keys()).indexOf(node2Identity);
|
|
541
|
-
if (this.adjacency[node1Index][node2Index] === null) {
|
|
542
|
-
this.adjacency[node1Index][node2Index] = [edge];
|
|
543
|
-
} else {
|
|
544
|
-
if (!this.adjacency[node1Index][node2Index].includes(edge)) {
|
|
545
|
-
this.adjacency[node1Index][node2Index].push(edge);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
const id = this.edgeIdentity(edge, node1Identity, node2Identity);
|
|
549
|
-
this.emit("edge-added", id);
|
|
550
|
-
return id;
|
|
551
|
-
}
|
|
552
|
-
getNodes(compareFunc) {
|
|
553
|
-
const temp = Array.from(this.nodes.values());
|
|
554
|
-
if (compareFunc !== undefined) {
|
|
555
|
-
return temp.sort(compareFunc);
|
|
556
|
-
}
|
|
557
|
-
return temp;
|
|
558
|
-
}
|
|
559
|
-
getNode(nodeIdentity) {
|
|
560
|
-
return this.nodes.get(nodeIdentity);
|
|
561
|
-
}
|
|
562
|
-
hasNode(nodeIdentity) {
|
|
563
|
-
return this.nodes.has(nodeIdentity);
|
|
564
|
-
}
|
|
565
|
-
getEdges() {
|
|
566
|
-
const toReturn = [];
|
|
567
|
-
const nodeKeys = Array.from(this.nodes.keys());
|
|
568
|
-
this.adjacency.forEach((row, rowIndex) => {
|
|
569
|
-
const node1Identity = nodeKeys[rowIndex];
|
|
570
|
-
if (node1Identity != null) {
|
|
571
|
-
row.forEach((edges, colIndex) => {
|
|
572
|
-
if (edges !== null) {
|
|
573
|
-
const node2Identity = nodeKeys[colIndex];
|
|
574
|
-
if (node2Identity != null) {
|
|
575
|
-
for (const edge of edges) {
|
|
576
|
-
toReturn.push([node1Identity, node2Identity, edge]);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
});
|
|
583
|
-
return toReturn;
|
|
584
|
-
}
|
|
585
|
-
outEdges(node1Identity) {
|
|
586
|
-
const nodeKeys = Array.from(this.nodes.keys());
|
|
587
|
-
const nodeIndex = nodeKeys.indexOf(node1Identity);
|
|
588
|
-
const toReturn = [];
|
|
589
|
-
this.adjacency[nodeIndex].forEach((edges, colIndex) => {
|
|
590
|
-
if (edges !== null) {
|
|
591
|
-
const node2Identity = nodeKeys[colIndex];
|
|
592
|
-
if (node2Identity != null) {
|
|
593
|
-
for (const edge of edges) {
|
|
594
|
-
toReturn.push([node1Identity, node2Identity, edge]);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
});
|
|
599
|
-
return toReturn;
|
|
600
|
-
}
|
|
601
|
-
inEdges(node2Identity) {
|
|
602
|
-
const nodeKeys = Array.from(this.nodes.keys());
|
|
603
|
-
const node2Index = nodeKeys.indexOf(node2Identity);
|
|
604
|
-
const toReturn = [];
|
|
605
|
-
this.adjacency.forEach((row, rowIndex) => {
|
|
606
|
-
const node1Identity = nodeKeys[rowIndex];
|
|
607
|
-
const edges = row[node2Index];
|
|
608
|
-
if (edges !== null) {
|
|
609
|
-
for (const edge of edges) {
|
|
610
|
-
toReturn.push([node1Identity, node2Identity, edge]);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
return toReturn;
|
|
615
|
-
}
|
|
616
|
-
nodeEdges(nodeIdentity) {
|
|
617
|
-
return [...this.outEdges(nodeIdentity), ...this.inEdges(nodeIdentity)];
|
|
618
|
-
}
|
|
619
|
-
removeEdge(node1Identity, node2Identity, edgeIdentity) {
|
|
620
|
-
const node1Exists = this.nodes.has(node1Identity);
|
|
621
|
-
const node2Exists = this.nodes.has(node2Identity);
|
|
622
|
-
if (!node1Exists) {
|
|
623
|
-
throw new NodeDoesntExistError(node1Identity);
|
|
624
|
-
}
|
|
625
|
-
if (!node2Exists) {
|
|
626
|
-
throw new NodeDoesntExistError(node2Identity);
|
|
627
|
-
}
|
|
628
|
-
const node1Index = Array.from(this.nodes.keys()).indexOf(node1Identity);
|
|
629
|
-
const node2Index = Array.from(this.nodes.keys()).indexOf(node2Identity);
|
|
630
|
-
if (edgeIdentity === undefined) {
|
|
631
|
-
this.adjacency[node1Index][node2Index] = null;
|
|
632
|
-
} else {
|
|
633
|
-
for (const row of this.adjacency) {
|
|
634
|
-
for (const edgelist of row) {
|
|
635
|
-
if (edgelist !== null) {
|
|
636
|
-
for (let edgeIndex = 0;edgeIndex < edgelist.length; edgeIndex++) {
|
|
637
|
-
if (this.edgeIdentity(edgelist[edgeIndex], node1Identity, node2Identity) === edgeIdentity) {
|
|
638
|
-
edgelist.splice(edgeIndex, 1);
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
this.emit("edge-removed", edgeIdentity);
|
|
646
|
-
}
|
|
647
|
-
remove(nodeIdentity) {
|
|
648
|
-
if (!this.nodes.has(nodeIdentity)) {
|
|
649
|
-
throw new NodeDoesntExistError(nodeIdentity);
|
|
650
|
-
}
|
|
651
|
-
this.nodes.delete(nodeIdentity);
|
|
652
|
-
const nodeIndex = Array.from(this.nodes.keys()).indexOf(nodeIdentity);
|
|
653
|
-
this.adjacency.splice(nodeIndex, 1);
|
|
654
|
-
this.adjacency.forEach((row) => row.splice(nodeIndex, 1));
|
|
655
|
-
this.emit("node-removed", nodeIdentity);
|
|
656
|
-
}
|
|
657
|
-
removeNode(nodeIdentity) {
|
|
658
|
-
return this.remove(nodeIdentity);
|
|
659
|
-
}
|
|
660
|
-
addNode(node) {
|
|
661
|
-
return this.insert(node);
|
|
662
|
-
}
|
|
663
|
-
addNodes(nodes) {
|
|
664
|
-
return nodes.map((node) => this.insert(node));
|
|
665
|
-
}
|
|
666
|
-
addEdges(edges) {
|
|
667
|
-
return edges.map(([node1Identity, node2Identity, edge]) => this.addEdge(node1Identity, node2Identity, edge));
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
// src/graph/directedGraph.ts
|
|
672
|
-
class DirectedGraph extends Graph {
|
|
673
|
-
hasCycle = false;
|
|
674
|
-
isAcyclic() {
|
|
675
|
-
if (this.hasCycle !== undefined) {
|
|
676
|
-
return !this.hasCycle;
|
|
677
|
-
}
|
|
678
|
-
const nodeIndices = Array.from(this.nodes.keys());
|
|
679
|
-
const nodeInDegrees = new Map(Array.from(this.nodes.keys()).map((n) => [n, this.indegreeOfNode(n)]));
|
|
680
|
-
const toSearch = Array.from(nodeInDegrees).filter((pair) => pair[1] === 0);
|
|
681
|
-
let visitedNodes = 0;
|
|
682
|
-
while (toSearch.length > 0) {
|
|
683
|
-
const cur = toSearch.pop();
|
|
684
|
-
if (cur === undefined) {
|
|
685
|
-
continue;
|
|
686
|
-
}
|
|
687
|
-
const nodeIndex = nodeIndices.indexOf(cur[0]);
|
|
688
|
-
this.adjacency[nodeIndex].forEach((hasAdj, index) => {
|
|
689
|
-
if (hasAdj !== null) {
|
|
690
|
-
const currentInDegree = nodeInDegrees.get(nodeIndices[index]);
|
|
691
|
-
if (currentInDegree !== undefined) {
|
|
692
|
-
nodeInDegrees.set(nodeIndices[index], currentInDegree - 1);
|
|
693
|
-
if (currentInDegree - 1 === 0) {
|
|
694
|
-
toSearch.push([nodeIndices[index], currentInDegree - 1]);
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
});
|
|
699
|
-
visitedNodes++;
|
|
700
|
-
}
|
|
701
|
-
this.hasCycle = !(visitedNodes === this.nodes.size);
|
|
702
|
-
return visitedNodes === this.nodes.size;
|
|
703
|
-
}
|
|
704
|
-
indegreeOfNode(nodeID) {
|
|
705
|
-
const nodeIdentities = Array.from(this.nodes.keys());
|
|
706
|
-
const indexOfNode = nodeIdentities.indexOf(nodeID);
|
|
707
|
-
if (indexOfNode === -1) {
|
|
708
|
-
throw new NodeDoesntExistError(nodeID);
|
|
709
|
-
}
|
|
710
|
-
return this.adjacency.reduce((carry, row) => {
|
|
711
|
-
return carry + (row[indexOfNode] == null ? 0 : 1);
|
|
712
|
-
}, 0);
|
|
713
|
-
}
|
|
714
|
-
addEdge(sourceNodeIdentity, targetNodeIdentity, edge, skipUpdatingCyclicality = false) {
|
|
715
|
-
if (edge === undefined) {
|
|
716
|
-
edge = true;
|
|
717
|
-
}
|
|
718
|
-
if (this.hasCycle === false && !skipUpdatingCyclicality) {
|
|
719
|
-
this.hasCycle = this.wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity);
|
|
720
|
-
} else if (skipUpdatingCyclicality) {
|
|
721
|
-
this.hasCycle = undefined;
|
|
722
|
-
}
|
|
723
|
-
return super.addEdge(sourceNodeIdentity, targetNodeIdentity, edge);
|
|
724
|
-
}
|
|
725
|
-
canReachFrom(startNode, endNode) {
|
|
726
|
-
const nodeIdentities = Array.from(this.nodes.keys());
|
|
727
|
-
const startNodeIndex = nodeIdentities.indexOf(startNode);
|
|
728
|
-
const endNodeIndex = nodeIdentities.indexOf(endNode);
|
|
729
|
-
if (this.adjacency[startNodeIndex][endNodeIndex] != null) {
|
|
730
|
-
return true;
|
|
731
|
-
}
|
|
732
|
-
return this.adjacency[startNodeIndex].reduce((carry, edge, index) => {
|
|
733
|
-
if (carry || edge === null) {
|
|
734
|
-
return carry;
|
|
735
|
-
}
|
|
736
|
-
return this.canReachFrom(nodeIdentities[index], endNode);
|
|
737
|
-
}, false);
|
|
738
|
-
}
|
|
739
|
-
wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity) {
|
|
740
|
-
return this.hasCycle || sourceNodeIdentity === targetNodeIdentity || this.canReachFrom(targetNodeIdentity, sourceNodeIdentity);
|
|
741
|
-
}
|
|
742
|
-
getSubGraphStartingFrom(startNodeIdentity) {
|
|
743
|
-
const nodeIndices = Array.from(this.nodes.keys());
|
|
744
|
-
const initalNode = this.nodes.get(startNodeIdentity);
|
|
745
|
-
if (initalNode == null) {
|
|
746
|
-
throw new NodeDoesntExistError(startNodeIdentity);
|
|
747
|
-
}
|
|
748
|
-
const recur = (startNodeIdentity2, nodesToInclude) => {
|
|
749
|
-
let toReturn = [...nodesToInclude];
|
|
750
|
-
const nodeIndex = nodeIndices.indexOf(startNodeIdentity2);
|
|
751
|
-
this.adjacency[nodeIndex].forEach((hasAdj, index) => {
|
|
752
|
-
if (hasAdj !== null && nodesToInclude.find((n) => this.nodeIdentity(n) === nodeIndices[index]) == null) {
|
|
753
|
-
const newNode = this.nodes.get(nodeIndices[index]);
|
|
754
|
-
if (newNode != null) {
|
|
755
|
-
toReturn = [...recur(nodeIndices[index], toReturn), newNode];
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
});
|
|
759
|
-
return toReturn;
|
|
760
|
-
};
|
|
761
|
-
const newGraph = new DirectedGraph(this.nodeIdentity, this.edgeIdentity);
|
|
762
|
-
const nodeList = recur(startNodeIdentity, [initalNode]);
|
|
763
|
-
const includeIdents = nodeList.map((t) => this.nodeIdentity(t));
|
|
764
|
-
Array.from(this.nodes.values()).forEach((n) => {
|
|
765
|
-
if (includeIdents.includes(this.nodeIdentity(n))) {
|
|
766
|
-
newGraph.insert(n);
|
|
767
|
-
}
|
|
768
|
-
});
|
|
769
|
-
newGraph.adjacency = this.subAdj(nodeList);
|
|
770
|
-
return newGraph;
|
|
771
|
-
}
|
|
772
|
-
subAdj(include) {
|
|
773
|
-
const includeIdents = include.map((t) => this.nodeIdentity(t));
|
|
774
|
-
const nodeIndices = Array.from(this.nodes.keys());
|
|
775
|
-
return this.adjacency.reduce((carry, cur, index) => {
|
|
776
|
-
if (includeIdents.includes(nodeIndices[index])) {
|
|
777
|
-
return [...carry, cur.filter((_, index2) => includeIdents.includes(nodeIndices[index2]))];
|
|
778
|
-
} else {
|
|
779
|
-
return carry;
|
|
780
|
-
}
|
|
781
|
-
}, []);
|
|
782
|
-
}
|
|
783
|
-
getEdges() {
|
|
784
|
-
return super.getEdges();
|
|
785
|
-
}
|
|
786
|
-
removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity) {
|
|
787
|
-
super.removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity);
|
|
788
|
-
this.hasCycle = undefined;
|
|
789
|
-
}
|
|
790
|
-
remove(nodeIdentity) {
|
|
791
|
-
super.remove(nodeIdentity);
|
|
792
|
-
this.hasCycle = undefined;
|
|
793
|
-
}
|
|
794
|
-
addEdges(edges) {
|
|
795
|
-
return super.addEdges(edges);
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
// src/graph/directedAcyclicGraph.ts
|
|
800
|
-
class DirectedAcyclicGraph extends DirectedGraph {
|
|
801
|
-
_topologicallySortedNodes;
|
|
802
|
-
static fromDirectedGraph(graph) {
|
|
803
|
-
if (!graph.isAcyclic()) {
|
|
804
|
-
throw new CycleError("Can't convert that graph to a DAG because it contains a cycle");
|
|
805
|
-
}
|
|
806
|
-
const toRet = new DirectedAcyclicGraph(graph.nodeIdentity, graph.edgeIdentity);
|
|
807
|
-
toRet.nodes = graph.nodes;
|
|
808
|
-
toRet.adjacency = graph.adjacency;
|
|
809
|
-
return toRet;
|
|
810
|
-
}
|
|
811
|
-
addEdge(sourceNodeIdentity, targetNodeIdentity, edge) {
|
|
812
|
-
if (edge === undefined) {
|
|
813
|
-
edge = true;
|
|
814
|
-
}
|
|
815
|
-
if (this.wouldAddingEdgeCreateCycle(sourceNodeIdentity, targetNodeIdentity)) {
|
|
816
|
-
throw new CycleError(`Can't add edge from ${String(sourceNodeIdentity)} to ${String(targetNodeIdentity)} it would create a cycle`);
|
|
817
|
-
}
|
|
818
|
-
this._topologicallySortedNodes = undefined;
|
|
819
|
-
return super.addEdge(sourceNodeIdentity, targetNodeIdentity, edge, true);
|
|
820
|
-
}
|
|
821
|
-
insert(node) {
|
|
822
|
-
if (this._topologicallySortedNodes !== undefined) {
|
|
823
|
-
this._topologicallySortedNodes = [node, ...this._topologicallySortedNodes];
|
|
824
|
-
}
|
|
825
|
-
return super.insert(node);
|
|
826
|
-
}
|
|
827
|
-
topologicallySortedNodes() {
|
|
828
|
-
if (this._topologicallySortedNodes !== undefined) {
|
|
829
|
-
return this._topologicallySortedNodes;
|
|
830
|
-
}
|
|
831
|
-
const nodeIndices = Array.from(this.nodes.keys());
|
|
832
|
-
const nodeInDegrees = new Map(Array.from(this.nodes.keys()).map((n) => [n, this.indegreeOfNode(n)]));
|
|
833
|
-
const adjCopy = this.adjacency.map((a) => [...a]);
|
|
834
|
-
const toSearch = Array.from(nodeInDegrees).filter((pair) => pair[1] === 0);
|
|
835
|
-
if (toSearch.length === this.nodes.size) {
|
|
836
|
-
const arrayOfNodes = Array.from(this.nodes.values());
|
|
837
|
-
this._topologicallySortedNodes = arrayOfNodes;
|
|
838
|
-
return arrayOfNodes;
|
|
839
|
-
}
|
|
840
|
-
const toReturn = [];
|
|
841
|
-
while (toSearch.length > 0) {
|
|
842
|
-
const n = toSearch.pop();
|
|
843
|
-
if (n === undefined) {
|
|
844
|
-
throw new Error("Unexpected empty array");
|
|
845
|
-
}
|
|
846
|
-
const curNode = this.nodes.get(n[0]);
|
|
847
|
-
if (curNode == null) {
|
|
848
|
-
throw new Error("This should never happen");
|
|
849
|
-
}
|
|
850
|
-
toReturn.push(curNode);
|
|
851
|
-
adjCopy[nodeIndices.indexOf(n[0])]?.forEach((edge, index) => {
|
|
852
|
-
if (edge !== null) {
|
|
853
|
-
adjCopy[nodeIndices.indexOf(n[0])][index] = null;
|
|
854
|
-
const target = nodeInDegrees.get(nodeIndices[index]);
|
|
855
|
-
if (target !== undefined) {
|
|
856
|
-
nodeInDegrees.set(nodeIndices[index], target - 1);
|
|
857
|
-
if (target - 1 === 0) {
|
|
858
|
-
toSearch.push([nodeIndices[index], 0]);
|
|
859
|
-
}
|
|
860
|
-
} else {
|
|
861
|
-
throw new Error("This should never happen");
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
});
|
|
865
|
-
}
|
|
866
|
-
this._topologicallySortedNodes = toReturn;
|
|
867
|
-
return toReturn;
|
|
868
|
-
}
|
|
869
|
-
getSubGraphStartingFrom(startNodeIdentity) {
|
|
870
|
-
return DirectedAcyclicGraph.fromDirectedGraph(super.getSubGraphStartingFrom(startNodeIdentity));
|
|
871
|
-
}
|
|
872
|
-
removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity) {
|
|
873
|
-
super.removeEdge(sourceNodeIdentity, targetNodeIdentity, edgeIdentity);
|
|
874
|
-
this._topologicallySortedNodes = undefined;
|
|
875
|
-
}
|
|
876
|
-
remove(nodeIdentity) {
|
|
877
|
-
super.remove(nodeIdentity);
|
|
878
|
-
this._topologicallySortedNodes = undefined;
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
// src/json-schema/FromSchema.ts
|
|
882
|
-
var FromSchemaDefaultOptions = {
|
|
883
|
-
parseNotKeyword: true,
|
|
884
|
-
parseIfThenElseKeywords: true,
|
|
885
|
-
keepDefaultedPropertiesOptional: true,
|
|
886
|
-
references: false,
|
|
887
|
-
deserialize: false
|
|
888
|
-
};
|
|
889
|
-
// src/json-schema/SchemaUtils.ts
|
|
890
|
-
function areFormatStringsCompatible(sourceFormat, targetFormat) {
|
|
891
|
-
const formatPattern = /^[a-zA-Z][a-zA-Z0-9_-]*(?::[a-zA-Z][a-zA-Z0-9_-]*)?$/;
|
|
892
|
-
if (!formatPattern.test(sourceFormat) || !formatPattern.test(targetFormat)) {
|
|
893
|
-
return "incompatible";
|
|
894
|
-
}
|
|
895
|
-
const [sourceName, sourceNarrow] = sourceFormat.split(":");
|
|
896
|
-
const [targetName, targetNarrow] = targetFormat.split(":");
|
|
897
|
-
if (sourceName !== targetName) {
|
|
898
|
-
return "incompatible";
|
|
899
|
-
}
|
|
900
|
-
if (!sourceNarrow && !targetNarrow) {
|
|
901
|
-
return "static";
|
|
902
|
-
}
|
|
903
|
-
if (sourceNarrow && !targetNarrow) {
|
|
904
|
-
return "static";
|
|
905
|
-
}
|
|
906
|
-
if (!sourceNarrow && targetNarrow) {
|
|
907
|
-
return "runtime";
|
|
908
|
-
}
|
|
909
|
-
if (sourceNarrow === targetNarrow) {
|
|
910
|
-
return "static";
|
|
911
|
-
}
|
|
912
|
-
return "incompatible";
|
|
913
|
-
}
|
|
914
|
-
function isTypeStaticallyCompatible(sourceType, targetType) {
|
|
915
|
-
if (!targetType) {
|
|
916
|
-
return true;
|
|
917
|
-
}
|
|
918
|
-
if (!sourceType) {
|
|
919
|
-
return false;
|
|
920
|
-
}
|
|
921
|
-
const sourceTypes = Array.isArray(sourceType) ? sourceType : [sourceType];
|
|
922
|
-
const targetTypes = Array.isArray(targetType) ? targetType : [targetType];
|
|
923
|
-
return sourceTypes.some((st) => targetTypes.includes(st));
|
|
924
|
-
}
|
|
925
|
-
function mergeAllOfSchemas(schemas) {
|
|
926
|
-
if (schemas.length === 0)
|
|
927
|
-
return null;
|
|
928
|
-
if (schemas.length === 1)
|
|
929
|
-
return schemas[0];
|
|
930
|
-
let merged = {};
|
|
931
|
-
for (const schema of schemas) {
|
|
932
|
-
if (typeof schema === "boolean") {
|
|
933
|
-
if (schema === false)
|
|
934
|
-
return false;
|
|
935
|
-
continue;
|
|
936
|
-
}
|
|
937
|
-
const schemaObj = schema;
|
|
938
|
-
if (schemaObj.type !== undefined) {
|
|
939
|
-
if (merged.type === undefined) {
|
|
940
|
-
merged.type = schemaObj.type;
|
|
941
|
-
} else if (merged.type !== schemaObj.type) {
|
|
942
|
-
const mergedTypes = Array.isArray(merged.type) ? merged.type : [merged.type];
|
|
943
|
-
const schemaTypes = Array.isArray(schemaObj.type) ? schemaObj.type : [schemaObj.type];
|
|
944
|
-
const commonTypes = mergedTypes.filter((t) => schemaTypes.includes(t));
|
|
945
|
-
if (commonTypes.length === 0) {
|
|
946
|
-
return false;
|
|
947
|
-
}
|
|
948
|
-
merged.type = commonTypes.length === 1 ? commonTypes[0] : commonTypes;
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
const schemaFormat = schemaObj.format;
|
|
952
|
-
const mergedFormat = merged.format;
|
|
953
|
-
if (schemaFormat) {
|
|
954
|
-
if (!mergedFormat) {
|
|
955
|
-
merged.format = schemaFormat;
|
|
956
|
-
} else {
|
|
957
|
-
const formatCompat = areFormatStringsCompatible(mergedFormat, schemaFormat);
|
|
958
|
-
if (formatCompat === "incompatible") {
|
|
959
|
-
return false;
|
|
960
|
-
}
|
|
961
|
-
const mergedHasNarrow = mergedFormat.includes(":");
|
|
962
|
-
const schemaHasNarrow = schemaFormat.includes(":");
|
|
963
|
-
if (schemaHasNarrow && !mergedHasNarrow) {
|
|
964
|
-
merged.format = schemaFormat;
|
|
965
|
-
} else if (!schemaHasNarrow && mergedHasNarrow) {} else if (mergedFormat !== schemaFormat) {
|
|
966
|
-
return false;
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
if (schemaObj.properties && typeof schemaObj.properties === "object") {
|
|
971
|
-
if (!merged.properties) {
|
|
972
|
-
merged.properties = {};
|
|
973
|
-
}
|
|
974
|
-
const mergedProps = merged.properties;
|
|
975
|
-
const schemaProps = schemaObj.properties;
|
|
976
|
-
for (const [key, value] of Object.entries(schemaProps)) {
|
|
977
|
-
if (mergedProps[key]) {
|
|
978
|
-
const nestedMerged = mergeAllOfSchemas([mergedProps[key], value]);
|
|
979
|
-
if (nestedMerged === null || nestedMerged === false) {
|
|
980
|
-
return false;
|
|
981
|
-
}
|
|
982
|
-
mergedProps[key] = nestedMerged;
|
|
983
|
-
} else {
|
|
984
|
-
mergedProps[key] = value;
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
if (schemaObj.required && Array.isArray(schemaObj.required)) {
|
|
989
|
-
if (!merged.required) {
|
|
990
|
-
merged.required = [];
|
|
991
|
-
}
|
|
992
|
-
const mergedRequired = merged.required;
|
|
993
|
-
const schemaRequired = schemaObj.required;
|
|
994
|
-
merged.required = mergedRequired.filter((r) => schemaRequired.includes(r));
|
|
995
|
-
}
|
|
996
|
-
if (schemaObj.additionalProperties !== undefined) {
|
|
997
|
-
if (merged.additionalProperties === undefined) {
|
|
998
|
-
merged.additionalProperties = schemaObj.additionalProperties;
|
|
999
|
-
} else if (merged.additionalProperties === true && schemaObj.additionalProperties === false) {
|
|
1000
|
-
merged.additionalProperties = false;
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
if (schemaObj.items !== undefined) {
|
|
1004
|
-
if (merged.items === undefined) {
|
|
1005
|
-
merged.items = schemaObj.items;
|
|
1006
|
-
} else {
|
|
1007
|
-
const mergedItems = mergeAllOfSchemas([
|
|
1008
|
-
merged.items,
|
|
1009
|
-
schemaObj.items
|
|
1010
|
-
]);
|
|
1011
|
-
if (mergedItems === null || mergedItems === false) {
|
|
1012
|
-
return false;
|
|
1013
|
-
}
|
|
1014
|
-
merged.items = mergedItems;
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
return merged;
|
|
1019
|
-
}
|
|
1020
|
-
function isCompatibleWithUnion(sourceSchema, unionSchemas) {
|
|
1021
|
-
let hasStatic = false;
|
|
1022
|
-
let hasRuntime = false;
|
|
1023
|
-
for (const unionSchema of unionSchemas) {
|
|
1024
|
-
const compatibility = areSemanticallyCompatible(sourceSchema, unionSchema);
|
|
1025
|
-
if (compatibility === "static") {
|
|
1026
|
-
hasStatic = true;
|
|
1027
|
-
} else if (compatibility === "runtime") {
|
|
1028
|
-
hasRuntime = true;
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
if (hasStatic)
|
|
1032
|
-
return "static";
|
|
1033
|
-
if (hasRuntime)
|
|
1034
|
-
return "runtime";
|
|
1035
|
-
return "incompatible";
|
|
1036
|
-
}
|
|
1037
|
-
function areSemanticallyCompatible(sourceSchema, targetSchema) {
|
|
1038
|
-
if (sourceSchema === undefined || targetSchema === undefined) {
|
|
1039
|
-
return "incompatible";
|
|
1040
|
-
}
|
|
1041
|
-
if (typeof targetSchema === "boolean") {
|
|
1042
|
-
if (targetSchema === false)
|
|
1043
|
-
return "incompatible";
|
|
1044
|
-
if (targetSchema === true)
|
|
1045
|
-
return "static";
|
|
1046
|
-
return "incompatible";
|
|
1047
|
-
}
|
|
1048
|
-
if (typeof sourceSchema === "boolean") {
|
|
1049
|
-
if (sourceSchema === false)
|
|
1050
|
-
return "incompatible";
|
|
1051
|
-
if (sourceSchema === true)
|
|
1052
|
-
return "runtime";
|
|
1053
|
-
}
|
|
1054
|
-
if (sourceSchema.allOf && Array.isArray(sourceSchema.allOf)) {
|
|
1055
|
-
const mergedSchema = mergeAllOfSchemas(sourceSchema.allOf);
|
|
1056
|
-
if (mergedSchema === null || mergedSchema === false) {
|
|
1057
|
-
return "incompatible";
|
|
1058
|
-
}
|
|
1059
|
-
return areSemanticallyCompatible(mergedSchema, targetSchema);
|
|
1060
|
-
}
|
|
1061
|
-
const sourceType = sourceSchema.type;
|
|
1062
|
-
const targetType = targetSchema.type;
|
|
1063
|
-
if (sourceSchema.oneOf && Array.isArray(sourceSchema.oneOf)) {
|
|
1064
|
-
let hasStatic = false;
|
|
1065
|
-
let hasRuntime = false;
|
|
1066
|
-
for (const sourceOption of sourceSchema.oneOf) {
|
|
1067
|
-
const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
|
|
1068
|
-
if (compatibility === "static") {
|
|
1069
|
-
hasStatic = true;
|
|
1070
|
-
} else if (compatibility === "runtime") {
|
|
1071
|
-
hasRuntime = true;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
if (hasRuntime)
|
|
1075
|
-
return "runtime";
|
|
1076
|
-
if (hasStatic)
|
|
1077
|
-
return "static";
|
|
1078
|
-
return "incompatible";
|
|
1079
|
-
}
|
|
1080
|
-
if (sourceSchema.anyOf && Array.isArray(sourceSchema.anyOf)) {
|
|
1081
|
-
let hasStatic = false;
|
|
1082
|
-
let hasRuntime = false;
|
|
1083
|
-
for (const sourceOption of sourceSchema.anyOf) {
|
|
1084
|
-
const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
|
|
1085
|
-
if (compatibility === "static") {
|
|
1086
|
-
hasStatic = true;
|
|
1087
|
-
} else if (compatibility === "runtime") {
|
|
1088
|
-
hasRuntime = true;
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
if (hasRuntime)
|
|
1092
|
-
return "runtime";
|
|
1093
|
-
if (hasStatic)
|
|
1094
|
-
return "static";
|
|
1095
|
-
return "incompatible";
|
|
1096
|
-
}
|
|
1097
|
-
if (targetSchema.oneOf && Array.isArray(targetSchema.oneOf)) {
|
|
1098
|
-
return isCompatibleWithUnion(sourceSchema, targetSchema.oneOf);
|
|
1099
|
-
}
|
|
1100
|
-
if (targetSchema.anyOf && Array.isArray(targetSchema.anyOf)) {
|
|
1101
|
-
return isCompatibleWithUnion(sourceSchema, targetSchema.anyOf);
|
|
1102
|
-
}
|
|
1103
|
-
if (targetSchema.allOf && Array.isArray(targetSchema.allOf)) {
|
|
1104
|
-
let hasStatic = false;
|
|
1105
|
-
let hasRuntime = false;
|
|
1106
|
-
for (const allOfSchema of targetSchema.allOf) {
|
|
1107
|
-
const compatibility = areSemanticallyCompatible(sourceSchema, allOfSchema);
|
|
1108
|
-
if (compatibility === "incompatible") {
|
|
1109
|
-
return "incompatible";
|
|
1110
|
-
} else if (compatibility === "static") {
|
|
1111
|
-
hasStatic = true;
|
|
1112
|
-
} else if (compatibility === "runtime") {
|
|
1113
|
-
hasRuntime = true;
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
if (hasRuntime)
|
|
1117
|
-
return "runtime";
|
|
1118
|
-
if (hasStatic)
|
|
1119
|
-
return "static";
|
|
1120
|
-
return "incompatible";
|
|
1121
|
-
}
|
|
1122
|
-
if (sourceType === "object" && targetType === "object") {
|
|
1123
|
-
const sourceProperties = sourceSchema.properties;
|
|
1124
|
-
const targetProperties = targetSchema.properties;
|
|
1125
|
-
if (!targetProperties) {
|
|
1126
|
-
return "static";
|
|
1127
|
-
}
|
|
1128
|
-
if (!sourceProperties) {
|
|
1129
|
-
if (targetSchema.additionalProperties === false) {
|
|
1130
|
-
return "incompatible";
|
|
1131
|
-
}
|
|
1132
|
-
return "static";
|
|
1133
|
-
}
|
|
1134
|
-
const targetRequired = targetSchema.required || [];
|
|
1135
|
-
let hasStatic = true;
|
|
1136
|
-
let hasRuntime = false;
|
|
1137
|
-
for (const propName of targetRequired) {
|
|
1138
|
-
const targetProp = targetProperties?.[propName];
|
|
1139
|
-
const sourceProp = sourceProperties?.[propName];
|
|
1140
|
-
if (!sourceProp) {
|
|
1141
|
-
return "incompatible";
|
|
1142
|
-
}
|
|
1143
|
-
if (targetProp) {
|
|
1144
|
-
const propCompatibility = areSemanticallyCompatible(sourceProp, targetProp);
|
|
1145
|
-
if (propCompatibility === "incompatible") {
|
|
1146
|
-
return "incompatible";
|
|
1147
|
-
} else if (propCompatibility === "runtime") {
|
|
1148
|
-
hasRuntime = true;
|
|
1149
|
-
hasStatic = false;
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
if (targetSchema.additionalProperties === false) {
|
|
1154
|
-
const sourcePropNames = Object.keys(sourceProperties);
|
|
1155
|
-
const targetPropNames = Object.keys(targetProperties);
|
|
1156
|
-
const extraProps = sourcePropNames.filter((name) => !targetPropNames.includes(name));
|
|
1157
|
-
if (extraProps.length > 0) {
|
|
1158
|
-
return "incompatible";
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
if (hasRuntime)
|
|
1162
|
-
return "runtime";
|
|
1163
|
-
return "static";
|
|
1164
|
-
}
|
|
1165
|
-
if (sourceType === "array" && targetType === "array") {
|
|
1166
|
-
const sourceFormat2 = sourceSchema?.format;
|
|
1167
|
-
const targetFormat2 = targetSchema?.format;
|
|
1168
|
-
let formatCompatibility = null;
|
|
1169
|
-
if (sourceFormat2 && targetFormat2) {
|
|
1170
|
-
formatCompatibility = areFormatStringsCompatible(sourceFormat2, targetFormat2);
|
|
1171
|
-
if (formatCompatibility === "incompatible") {
|
|
1172
|
-
return "incompatible";
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
if (sourceFormat2 && !targetFormat2) {
|
|
1176
|
-
return "static";
|
|
1177
|
-
}
|
|
1178
|
-
if (!sourceFormat2 && targetFormat2) {
|
|
1179
|
-
return "incompatible";
|
|
1180
|
-
}
|
|
1181
|
-
const sourceItems = sourceSchema.items;
|
|
1182
|
-
const targetItems = targetSchema.items;
|
|
1183
|
-
if (sourceItems && typeof sourceItems === "object" && !Array.isArray(sourceItems) && targetItems && typeof targetItems === "object" && !Array.isArray(targetItems)) {
|
|
1184
|
-
const itemsCompatibility = areSemanticallyCompatible(sourceItems, targetItems);
|
|
1185
|
-
if (formatCompatibility === "runtime") {
|
|
1186
|
-
return "runtime";
|
|
1187
|
-
}
|
|
1188
|
-
return itemsCompatibility;
|
|
1189
|
-
}
|
|
1190
|
-
if (!targetItems) {
|
|
1191
|
-
return "static";
|
|
1192
|
-
}
|
|
1193
|
-
if (!sourceItems) {
|
|
1194
|
-
return "incompatible";
|
|
1195
|
-
}
|
|
1196
|
-
if (Array.isArray(targetItems)) {
|
|
1197
|
-
return isCompatibleWithUnion(sourceItems, targetItems);
|
|
1198
|
-
}
|
|
1199
|
-
return "static";
|
|
1200
|
-
}
|
|
1201
|
-
if (!sourceType) {
|
|
1202
|
-
const targetFormat2 = targetSchema?.format;
|
|
1203
|
-
if (targetFormat2) {
|
|
1204
|
-
return "runtime";
|
|
1205
|
-
}
|
|
1206
|
-
return "static";
|
|
1207
|
-
}
|
|
1208
|
-
if (!targetType) {
|
|
1209
|
-
const targetFormat2 = targetSchema?.format;
|
|
1210
|
-
if (targetFormat2) {
|
|
1211
|
-
const sourceFormat2 = sourceSchema?.format;
|
|
1212
|
-
if (!sourceFormat2) {
|
|
1213
|
-
return "incompatible";
|
|
1214
|
-
}
|
|
1215
|
-
return areFormatStringsCompatible(sourceFormat2, targetFormat2);
|
|
1216
|
-
}
|
|
1217
|
-
return "static";
|
|
1218
|
-
}
|
|
1219
|
-
if (!isTypeStaticallyCompatible(sourceType, targetType)) {
|
|
1220
|
-
return "incompatible";
|
|
1221
|
-
}
|
|
1222
|
-
const sourceFormat = sourceSchema?.format;
|
|
1223
|
-
const targetFormat = targetSchema?.format;
|
|
1224
|
-
if (sourceFormat && targetFormat) {
|
|
1225
|
-
return areFormatStringsCompatible(sourceFormat, targetFormat);
|
|
1226
|
-
}
|
|
1227
|
-
if (sourceFormat && !targetFormat) {
|
|
1228
|
-
return "static";
|
|
1229
|
-
}
|
|
1230
|
-
if (!sourceFormat && targetFormat) {
|
|
1231
|
-
return "incompatible";
|
|
1232
|
-
}
|
|
1233
|
-
return "static";
|
|
1234
|
-
}
|
|
1235
|
-
function areObjectSchemasSemanticallyCompatible(sourceSchema, targetSchema) {
|
|
1236
|
-
return areSemanticallyCompatible(sourceSchema, targetSchema);
|
|
1237
|
-
}
|
|
1238
|
-
// src/json-schema/SchemaValidation.ts
|
|
1239
|
-
import { compileSchema } from "@sroussey/json-schema-library";
|
|
1240
|
-
// src/json-schema/parsePartialJson.ts
|
|
1241
|
-
function parsePartialJson(text) {
|
|
1242
|
-
const trimmed = text.trim();
|
|
1243
|
-
if (!trimmed)
|
|
1244
|
-
return;
|
|
1245
|
-
try {
|
|
1246
|
-
const result = JSON.parse(trimmed);
|
|
1247
|
-
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
1248
|
-
return result;
|
|
1249
|
-
}
|
|
1250
|
-
return;
|
|
1251
|
-
} catch {}
|
|
1252
|
-
if (trimmed[0] !== "{")
|
|
1253
|
-
return;
|
|
1254
|
-
const repaired = repairJson(trimmed);
|
|
1255
|
-
if (repaired === undefined)
|
|
1256
|
-
return;
|
|
1257
|
-
try {
|
|
1258
|
-
const result = JSON.parse(repaired);
|
|
1259
|
-
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
1260
|
-
return result;
|
|
1261
|
-
}
|
|
1262
|
-
return;
|
|
1263
|
-
} catch {
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
1267
|
-
function repairJson(text) {
|
|
1268
|
-
let result = "";
|
|
1269
|
-
let i = 0;
|
|
1270
|
-
const len = text.length;
|
|
1271
|
-
const stack = [];
|
|
1272
|
-
let inString = false;
|
|
1273
|
-
let escaped = false;
|
|
1274
|
-
let lastSafeEnd = 0;
|
|
1275
|
-
while (i < len) {
|
|
1276
|
-
const ch = text[i];
|
|
1277
|
-
if (escaped) {
|
|
1278
|
-
escaped = false;
|
|
1279
|
-
result += ch;
|
|
1280
|
-
i++;
|
|
1281
|
-
continue;
|
|
1282
|
-
}
|
|
1283
|
-
if (ch === "\\") {
|
|
1284
|
-
escaped = true;
|
|
1285
|
-
result += ch;
|
|
1286
|
-
i++;
|
|
1287
|
-
continue;
|
|
1288
|
-
}
|
|
1289
|
-
if (inString) {
|
|
1290
|
-
if (ch === '"') {
|
|
1291
|
-
inString = false;
|
|
1292
|
-
result += ch;
|
|
1293
|
-
i++;
|
|
1294
|
-
lastSafeEnd = result.length;
|
|
1295
|
-
continue;
|
|
1296
|
-
}
|
|
1297
|
-
result += ch;
|
|
1298
|
-
i++;
|
|
1299
|
-
continue;
|
|
1300
|
-
}
|
|
1301
|
-
switch (ch) {
|
|
1302
|
-
case '"':
|
|
1303
|
-
inString = true;
|
|
1304
|
-
result += ch;
|
|
1305
|
-
i++;
|
|
1306
|
-
break;
|
|
1307
|
-
case "{":
|
|
1308
|
-
stack.push("}");
|
|
1309
|
-
result += ch;
|
|
1310
|
-
i++;
|
|
1311
|
-
break;
|
|
1312
|
-
case "[":
|
|
1313
|
-
stack.push("]");
|
|
1314
|
-
result += ch;
|
|
1315
|
-
i++;
|
|
1316
|
-
break;
|
|
1317
|
-
case "}":
|
|
1318
|
-
if (stack.length > 0 && stack[stack.length - 1] === "}") {
|
|
1319
|
-
stack.pop();
|
|
1320
|
-
result += ch;
|
|
1321
|
-
i++;
|
|
1322
|
-
lastSafeEnd = result.length;
|
|
1323
|
-
} else {
|
|
1324
|
-
return closeStack(result, stack);
|
|
1325
|
-
}
|
|
1326
|
-
break;
|
|
1327
|
-
case "]":
|
|
1328
|
-
if (stack.length > 0 && stack[stack.length - 1] === "]") {
|
|
1329
|
-
stack.pop();
|
|
1330
|
-
result += ch;
|
|
1331
|
-
i++;
|
|
1332
|
-
lastSafeEnd = result.length;
|
|
1333
|
-
} else {
|
|
1334
|
-
return closeStack(result, stack);
|
|
1335
|
-
}
|
|
1336
|
-
break;
|
|
1337
|
-
default:
|
|
1338
|
-
result += ch;
|
|
1339
|
-
i++;
|
|
1340
|
-
break;
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
if (inString) {
|
|
1344
|
-
result += '"';
|
|
1345
|
-
}
|
|
1346
|
-
if (stack.length === 0) {
|
|
1347
|
-
return result;
|
|
1348
|
-
}
|
|
1349
|
-
return closeStack(cleanTrailing(result), stack);
|
|
1350
|
-
}
|
|
1351
|
-
function cleanTrailing(text) {
|
|
1352
|
-
let s = text.trimEnd();
|
|
1353
|
-
let changed = true;
|
|
1354
|
-
while (changed) {
|
|
1355
|
-
changed = false;
|
|
1356
|
-
const trimmed = s.trimEnd();
|
|
1357
|
-
if (trimmed.endsWith(",")) {
|
|
1358
|
-
s = trimmed.slice(0, -1);
|
|
1359
|
-
changed = true;
|
|
1360
|
-
continue;
|
|
1361
|
-
}
|
|
1362
|
-
if (trimmed.endsWith(":")) {
|
|
1363
|
-
const withoutColon = trimmed.slice(0, -1).trimEnd();
|
|
1364
|
-
if (withoutColon.endsWith('"')) {
|
|
1365
|
-
const keyStart = withoutColon.lastIndexOf('"', withoutColon.length - 2);
|
|
1366
|
-
if (keyStart >= 0) {
|
|
1367
|
-
let before = withoutColon.slice(0, keyStart).trimEnd();
|
|
1368
|
-
if (before.endsWith(",")) {
|
|
1369
|
-
before = before.slice(0, -1);
|
|
1370
|
-
}
|
|
1371
|
-
s = before;
|
|
1372
|
-
changed = true;
|
|
1373
|
-
continue;
|
|
1374
|
-
}
|
|
1375
|
-
}
|
|
1376
|
-
s = withoutColon;
|
|
1377
|
-
changed = true;
|
|
1378
|
-
continue;
|
|
1379
|
-
}
|
|
1380
|
-
const bareTokenMatch = trimmed.match(/,\s*"[^"]*"\s*:\s*(?:tru|fal|nul|true|false|null|[\d.eE+-]+)$/);
|
|
1381
|
-
if (bareTokenMatch) {
|
|
1382
|
-
const valueStr = trimmed.slice(trimmed.lastIndexOf(":") + 1).trim();
|
|
1383
|
-
try {
|
|
1384
|
-
JSON.parse(valueStr);
|
|
1385
|
-
} catch {
|
|
1386
|
-
s = trimmed.slice(0, bareTokenMatch.index).trimEnd();
|
|
1387
|
-
if (s.endsWith(","))
|
|
1388
|
-
s = s.slice(0, -1);
|
|
1389
|
-
changed = true;
|
|
1390
|
-
continue;
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
return s;
|
|
1395
|
-
}
|
|
1396
|
-
function closeStack(text, stack) {
|
|
1397
|
-
let result = text;
|
|
1398
|
-
for (let i = stack.length - 1;i >= 0; i--) {
|
|
1399
|
-
result += stack[i];
|
|
1400
|
-
}
|
|
1401
|
-
return result;
|
|
1402
|
-
}
|
|
1403
434
|
// src/utilities/objectOfArraysAsArrayOfObjects.ts
|
|
1404
435
|
function objectOfArraysAsArrayOfObjects(data) {
|
|
1405
436
|
const keys = Object.keys(data);
|
|
@@ -1706,236 +737,6 @@ function objectOfArraysAsArrayOfObjects(data) {
|
|
|
1706
737
|
}
|
|
1707
738
|
});
|
|
1708
739
|
}
|
|
1709
|
-
// src/vector/TypedArray.ts
|
|
1710
|
-
function isTypedArray(value) {
|
|
1711
|
-
return ArrayBuffer.isView(value) && !(value instanceof DataView);
|
|
1712
|
-
}
|
|
1713
|
-
var TypedArrayType = null;
|
|
1714
|
-
var TypedArraySchemaOptions = {
|
|
1715
|
-
...FromSchemaDefaultOptions,
|
|
1716
|
-
deserialize: [
|
|
1717
|
-
{
|
|
1718
|
-
pattern: { type: "array", format: "TypedArray:Float64Array" },
|
|
1719
|
-
output: Float64Array
|
|
1720
|
-
},
|
|
1721
|
-
{
|
|
1722
|
-
pattern: { type: "array", format: "TypedArray:Float32Array" },
|
|
1723
|
-
output: Float32Array
|
|
1724
|
-
},
|
|
1725
|
-
{
|
|
1726
|
-
pattern: { type: "array", format: "TypedArray:Float16Array" },
|
|
1727
|
-
output: Float16Array
|
|
1728
|
-
},
|
|
1729
|
-
{
|
|
1730
|
-
pattern: { type: "array", format: "TypedArray:Int16Array" },
|
|
1731
|
-
output: Int16Array
|
|
1732
|
-
},
|
|
1733
|
-
{
|
|
1734
|
-
pattern: { type: "array", format: "TypedArray:Int8Array" },
|
|
1735
|
-
output: Int8Array
|
|
1736
|
-
},
|
|
1737
|
-
{
|
|
1738
|
-
pattern: { type: "array", format: "TypedArray:Uint8Array" },
|
|
1739
|
-
output: Uint8Array
|
|
1740
|
-
},
|
|
1741
|
-
{
|
|
1742
|
-
pattern: { type: "array", format: "TypedArray:Uint16Array" },
|
|
1743
|
-
output: Uint16Array
|
|
1744
|
-
},
|
|
1745
|
-
{
|
|
1746
|
-
pattern: { type: "array", format: "TypedArray" },
|
|
1747
|
-
output: TypedArrayType
|
|
1748
|
-
}
|
|
1749
|
-
]
|
|
1750
|
-
};
|
|
1751
|
-
var TypedArraySchema = (annotations = {}) => {
|
|
1752
|
-
return {
|
|
1753
|
-
type: "array",
|
|
1754
|
-
format: "TypedArray",
|
|
1755
|
-
title: "Typed Array",
|
|
1756
|
-
description: "A typed array (Float32Array, Int8Array, etc.)",
|
|
1757
|
-
...annotations
|
|
1758
|
-
};
|
|
1759
|
-
};
|
|
1760
|
-
|
|
1761
|
-
// src/vector/Tensor.ts
|
|
1762
|
-
var TensorType = {
|
|
1763
|
-
FLOAT16: "float16",
|
|
1764
|
-
FLOAT32: "float32",
|
|
1765
|
-
FLOAT64: "float64",
|
|
1766
|
-
INT8: "int8",
|
|
1767
|
-
UINT8: "uint8",
|
|
1768
|
-
INT16: "int16",
|
|
1769
|
-
UINT16: "uint16"
|
|
1770
|
-
};
|
|
1771
|
-
var TensorSchema = (annotations = {}) => ({
|
|
1772
|
-
type: "object",
|
|
1773
|
-
properties: {
|
|
1774
|
-
type: {
|
|
1775
|
-
type: "string",
|
|
1776
|
-
enum: Object.values(TensorType),
|
|
1777
|
-
title: "Type",
|
|
1778
|
-
description: "The type of the tensor"
|
|
1779
|
-
},
|
|
1780
|
-
data: TypedArraySchema({
|
|
1781
|
-
title: "Data",
|
|
1782
|
-
description: "The data of the tensor"
|
|
1783
|
-
}),
|
|
1784
|
-
shape: {
|
|
1785
|
-
type: "array",
|
|
1786
|
-
items: { type: "number" },
|
|
1787
|
-
title: "Shape",
|
|
1788
|
-
description: "The shape of the tensor (dimensions)",
|
|
1789
|
-
minItems: 1,
|
|
1790
|
-
default: [1]
|
|
1791
|
-
},
|
|
1792
|
-
normalized: {
|
|
1793
|
-
type: "boolean",
|
|
1794
|
-
title: "Normalized",
|
|
1795
|
-
description: "Whether the tensor data is normalized",
|
|
1796
|
-
default: false
|
|
1797
|
-
}
|
|
1798
|
-
},
|
|
1799
|
-
required: ["data"],
|
|
1800
|
-
additionalProperties: false,
|
|
1801
|
-
...annotations
|
|
1802
|
-
});
|
|
1803
|
-
// src/vector/TypedArrayUtils.ts
|
|
1804
|
-
var WIDTH_RANK = {
|
|
1805
|
-
Float64Array: 6,
|
|
1806
|
-
Float32Array: 5,
|
|
1807
|
-
Float16Array: 4,
|
|
1808
|
-
Int16Array: 3,
|
|
1809
|
-
Uint16Array: 3,
|
|
1810
|
-
Int8Array: 2,
|
|
1811
|
-
Uint8Array: 2
|
|
1812
|
-
};
|
|
1813
|
-
function getWidthRank(arr) {
|
|
1814
|
-
return WIDTH_RANK[arr.constructor.name] ?? 0;
|
|
1815
|
-
}
|
|
1816
|
-
function widestConstructor(sources) {
|
|
1817
|
-
let best = sources[0];
|
|
1818
|
-
for (let i = 1;i < sources.length; i++) {
|
|
1819
|
-
if (getWidthRank(sources[i]) > getWidthRank(best))
|
|
1820
|
-
best = sources[i];
|
|
1821
|
-
}
|
|
1822
|
-
return best.constructor;
|
|
1823
|
-
}
|
|
1824
|
-
function createTypedArrayFrom(sources, values) {
|
|
1825
|
-
const Ctor = widestConstructor(sources);
|
|
1826
|
-
const result = new Ctor(values.length);
|
|
1827
|
-
for (let i = 0;i < values.length; i++)
|
|
1828
|
-
result[i] = values[i];
|
|
1829
|
-
return result;
|
|
1830
|
-
}
|
|
1831
|
-
// src/vector/VectorSimilarityUtils.ts
|
|
1832
|
-
function cosineSimilarity(a, b) {
|
|
1833
|
-
if (a.length !== b.length) {
|
|
1834
|
-
throw new Error("Vectors must have the same length");
|
|
1835
|
-
}
|
|
1836
|
-
let dotProduct = 0;
|
|
1837
|
-
let normA = 0;
|
|
1838
|
-
let normB = 0;
|
|
1839
|
-
for (let i = 0;i < a.length; i++) {
|
|
1840
|
-
dotProduct += a[i] * b[i];
|
|
1841
|
-
normA += a[i] * a[i];
|
|
1842
|
-
normB += b[i] * b[i];
|
|
1843
|
-
}
|
|
1844
|
-
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
|
|
1845
|
-
if (denominator === 0) {
|
|
1846
|
-
return 0;
|
|
1847
|
-
}
|
|
1848
|
-
return dotProduct / denominator;
|
|
1849
|
-
}
|
|
1850
|
-
function jaccardSimilarity(a, b) {
|
|
1851
|
-
if (a.length !== b.length) {
|
|
1852
|
-
throw new Error("Vectors must have the same length");
|
|
1853
|
-
}
|
|
1854
|
-
let globalMin = a[0];
|
|
1855
|
-
for (let i = 0;i < a.length; i++) {
|
|
1856
|
-
globalMin = Math.min(globalMin, a[i], b[i]);
|
|
1857
|
-
}
|
|
1858
|
-
const shift = globalMin < 0 ? -globalMin : 0;
|
|
1859
|
-
let minSum = 0;
|
|
1860
|
-
let maxSum = 0;
|
|
1861
|
-
for (let i = 0;i < a.length; i++) {
|
|
1862
|
-
const shiftedA = a[i] + shift;
|
|
1863
|
-
const shiftedB = b[i] + shift;
|
|
1864
|
-
minSum += Math.min(shiftedA, shiftedB);
|
|
1865
|
-
maxSum += Math.max(shiftedA, shiftedB);
|
|
1866
|
-
}
|
|
1867
|
-
return maxSum === 0 ? 0 : minSum / maxSum;
|
|
1868
|
-
}
|
|
1869
|
-
function hammingDistance(a, b) {
|
|
1870
|
-
if (a.length !== b.length) {
|
|
1871
|
-
throw new Error("Vectors must have the same length");
|
|
1872
|
-
}
|
|
1873
|
-
let differences = 0;
|
|
1874
|
-
for (let i = 0;i < a.length; i++) {
|
|
1875
|
-
if (a[i] !== b[i]) {
|
|
1876
|
-
differences++;
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
return differences / a.length;
|
|
1880
|
-
}
|
|
1881
|
-
function hammingSimilarity(a, b) {
|
|
1882
|
-
return 1 - hammingDistance(a, b);
|
|
1883
|
-
}
|
|
1884
|
-
// src/vector/VectorUtils.ts
|
|
1885
|
-
function magnitude(arr) {
|
|
1886
|
-
return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0));
|
|
1887
|
-
}
|
|
1888
|
-
function inner(arr1, arr2) {
|
|
1889
|
-
if (arr1.length !== arr2.length) {
|
|
1890
|
-
throw new Error("Vectors must have the same length to compute inner product.");
|
|
1891
|
-
}
|
|
1892
|
-
return arr1.reduce((acc, val, i) => acc + val * arr2[i], 0);
|
|
1893
|
-
}
|
|
1894
|
-
function normalize(vector, throwOnZero = true, float32 = false) {
|
|
1895
|
-
const mag = magnitude(vector);
|
|
1896
|
-
if (mag === 0) {
|
|
1897
|
-
if (throwOnZero) {
|
|
1898
|
-
throw new Error("Cannot normalize a zero vector.");
|
|
1899
|
-
}
|
|
1900
|
-
return vector;
|
|
1901
|
-
}
|
|
1902
|
-
const normalized = Array.from(vector).map((val) => Number(val) / mag);
|
|
1903
|
-
if (float32) {
|
|
1904
|
-
return new Float32Array(normalized);
|
|
1905
|
-
}
|
|
1906
|
-
if (vector instanceof Float64Array) {
|
|
1907
|
-
return new Float64Array(normalized);
|
|
1908
|
-
}
|
|
1909
|
-
if (vector instanceof Float16Array) {
|
|
1910
|
-
return new Float16Array(normalized);
|
|
1911
|
-
}
|
|
1912
|
-
if (vector instanceof Float32Array) {
|
|
1913
|
-
return new Float32Array(normalized);
|
|
1914
|
-
}
|
|
1915
|
-
if (vector instanceof Int8Array) {
|
|
1916
|
-
return new Int8Array(normalized);
|
|
1917
|
-
}
|
|
1918
|
-
if (vector instanceof Uint8Array) {
|
|
1919
|
-
return new Uint8Array(normalized);
|
|
1920
|
-
}
|
|
1921
|
-
if (vector instanceof Int16Array) {
|
|
1922
|
-
return new Int16Array(normalized);
|
|
1923
|
-
}
|
|
1924
|
-
if (vector instanceof Uint16Array) {
|
|
1925
|
-
return new Uint16Array(normalized);
|
|
1926
|
-
}
|
|
1927
|
-
return new Float32Array(normalized);
|
|
1928
|
-
}
|
|
1929
|
-
function normalizeNumberArray(values, throwOnZero = false) {
|
|
1930
|
-
const norm = magnitude(values);
|
|
1931
|
-
if (norm === 0) {
|
|
1932
|
-
if (throwOnZero) {
|
|
1933
|
-
throw new Error("Cannot normalize a zero vector.");
|
|
1934
|
-
}
|
|
1935
|
-
return values;
|
|
1936
|
-
}
|
|
1937
|
-
return values.map((v) => v / norm);
|
|
1938
|
-
}
|
|
1939
740
|
// src/worker/WorkerManager.ts
|
|
1940
741
|
class WorkerManager {
|
|
1941
742
|
workers = new Map;
|
|
@@ -1943,14 +744,26 @@ class WorkerManager {
|
|
|
1943
744
|
workerFunctions = new Map;
|
|
1944
745
|
workerStreamFunctions = new Map;
|
|
1945
746
|
workerReactiveFunctions = new Map;
|
|
1946
|
-
|
|
1947
|
-
|
|
747
|
+
lazyFactories = new Map;
|
|
748
|
+
lazyInitPromises = new Map;
|
|
749
|
+
registerWorker(name, workerOrFactory) {
|
|
750
|
+
if (this.workers.has(name)) {
|
|
1948
751
|
throw new Error(`Worker ${name} is already registered.`);
|
|
1949
|
-
|
|
1950
|
-
this.
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
752
|
+
}
|
|
753
|
+
if (this.lazyFactories.has(name)) {
|
|
754
|
+
throw new Error(`Worker ${name} is already registered.`);
|
|
755
|
+
}
|
|
756
|
+
if (typeof workerOrFactory === "function") {
|
|
757
|
+
this.lazyFactories.set(name, workerOrFactory);
|
|
758
|
+
} else {
|
|
759
|
+
this.attachWorkerInstance(name, workerOrFactory);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
attachWorkerInstance(name, worker) {
|
|
763
|
+
this.workers.set(name, worker);
|
|
764
|
+
worker.addEventListener("error", (event) => {
|
|
765
|
+
console.error("Worker Error:", event.message, "at", event.filename, "line:", event.lineno);
|
|
766
|
+
});
|
|
1954
767
|
worker.addEventListener("messageerror", (event) => {
|
|
1955
768
|
console.error("Worker message error:", event);
|
|
1956
769
|
});
|
|
@@ -1968,6 +781,29 @@ class WorkerManager {
|
|
|
1968
781
|
});
|
|
1969
782
|
this.readyWorkers.set(name, readyPromise);
|
|
1970
783
|
}
|
|
784
|
+
async ensureWorkerReady(name) {
|
|
785
|
+
if (this.workers.has(name)) {
|
|
786
|
+
await this.readyWorkers.get(name);
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
const factory = this.lazyFactories.get(name);
|
|
790
|
+
if (!factory) {
|
|
791
|
+
throw new Error(`Worker ${name} not found.`);
|
|
792
|
+
}
|
|
793
|
+
let init = this.lazyInitPromises.get(name);
|
|
794
|
+
if (!init) {
|
|
795
|
+
init = (async () => {
|
|
796
|
+
const f = this.lazyFactories.get(name);
|
|
797
|
+
this.lazyFactories.delete(name);
|
|
798
|
+
const worker = f();
|
|
799
|
+
this.attachWorkerInstance(name, worker);
|
|
800
|
+
})();
|
|
801
|
+
this.lazyInitPromises.set(name, init);
|
|
802
|
+
}
|
|
803
|
+
await init;
|
|
804
|
+
await this.readyWorkers.get(name);
|
|
805
|
+
this.lazyInitPromises.delete(name);
|
|
806
|
+
}
|
|
1971
807
|
getWorker(name) {
|
|
1972
808
|
const worker = this.workers.get(name);
|
|
1973
809
|
if (!worker)
|
|
@@ -1975,6 +811,7 @@ class WorkerManager {
|
|
|
1975
811
|
return worker;
|
|
1976
812
|
}
|
|
1977
813
|
async callWorkerFunction(workerName, functionName, args, options) {
|
|
814
|
+
await this.ensureWorkerReady(workerName);
|
|
1978
815
|
const worker = this.workers.get(workerName);
|
|
1979
816
|
if (!worker)
|
|
1980
817
|
throw new Error(`Worker ${workerName} not found.`);
|
|
@@ -2020,6 +857,7 @@ class WorkerManager {
|
|
|
2020
857
|
});
|
|
2021
858
|
}
|
|
2022
859
|
async callWorkerReactiveFunction(workerName, functionName, args) {
|
|
860
|
+
await this.ensureWorkerReady(workerName);
|
|
2023
861
|
const worker = this.workers.get(workerName);
|
|
2024
862
|
if (!worker)
|
|
2025
863
|
return;
|
|
@@ -2051,6 +889,7 @@ class WorkerManager {
|
|
|
2051
889
|
});
|
|
2052
890
|
}
|
|
2053
891
|
async* callWorkerStreamFunction(workerName, functionName, args, options) {
|
|
892
|
+
await this.ensureWorkerReady(workerName);
|
|
2054
893
|
const worker = this.workers.get(workerName);
|
|
2055
894
|
if (!worker)
|
|
2056
895
|
throw new Error(`Worker ${workerName} not found.`);
|
|
@@ -2134,202 +973,6 @@ class WorkerManager {
|
|
|
2134
973
|
}
|
|
2135
974
|
var WORKER_MANAGER = createServiceToken("worker.manager");
|
|
2136
975
|
globalServiceRegistry.register(WORKER_MANAGER, () => new WorkerManager, true);
|
|
2137
|
-
// src/worker/WorkerServer.ts
|
|
2138
|
-
import { parentPort } from "@workglow/util";
|
|
2139
|
-
function extractTransferables(obj) {
|
|
2140
|
-
const transferables = [];
|
|
2141
|
-
const seen = new WeakSet;
|
|
2142
|
-
function findTransferables(value) {
|
|
2143
|
-
if (value && typeof value === "object" && seen.has(value)) {
|
|
2144
|
-
return;
|
|
2145
|
-
}
|
|
2146
|
-
if (value && typeof value === "object") {
|
|
2147
|
-
seen.add(value);
|
|
2148
|
-
}
|
|
2149
|
-
if (value instanceof Float32Array || value instanceof Int16Array) {
|
|
2150
|
-
transferables.push(value.buffer);
|
|
2151
|
-
} else if (value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int8Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array || value instanceof Float64Array || value instanceof BigInt64Array || value instanceof BigUint64Array) {
|
|
2152
|
-
transferables.push(value.buffer);
|
|
2153
|
-
} else if (typeof OffscreenCanvas !== "undefined" && value instanceof OffscreenCanvas) {
|
|
2154
|
-
transferables.push(value);
|
|
2155
|
-
} else if (typeof ImageBitmap !== "undefined" && value instanceof ImageBitmap) {
|
|
2156
|
-
transferables.push(value);
|
|
2157
|
-
} else if (typeof VideoFrame !== "undefined" && value instanceof VideoFrame) {
|
|
2158
|
-
transferables.push(value);
|
|
2159
|
-
} else if (typeof MessagePort !== "undefined" && value instanceof MessagePort) {
|
|
2160
|
-
transferables.push(value);
|
|
2161
|
-
} else if (value instanceof ArrayBuffer) {
|
|
2162
|
-
transferables.push(value);
|
|
2163
|
-
} else if (Array.isArray(value)) {
|
|
2164
|
-
value.forEach(findTransferables);
|
|
2165
|
-
} else if (value && typeof value === "object") {
|
|
2166
|
-
Object.values(value).forEach(findTransferables);
|
|
2167
|
-
}
|
|
2168
|
-
}
|
|
2169
|
-
findTransferables(obj);
|
|
2170
|
-
return transferables;
|
|
2171
|
-
}
|
|
2172
|
-
|
|
2173
|
-
class WorkerServer {
|
|
2174
|
-
constructor() {
|
|
2175
|
-
parentPort?.addEventListener("message", async (event) => {
|
|
2176
|
-
const msg = {
|
|
2177
|
-
type: event.type,
|
|
2178
|
-
data: event.data
|
|
2179
|
-
};
|
|
2180
|
-
await this.handleMessage(msg);
|
|
2181
|
-
});
|
|
2182
|
-
}
|
|
2183
|
-
functions = {};
|
|
2184
|
-
streamFunctions = {};
|
|
2185
|
-
reactiveFunctions = {};
|
|
2186
|
-
requestControllers = new Map;
|
|
2187
|
-
completedRequests = new Set;
|
|
2188
|
-
postResult = (id, result) => {
|
|
2189
|
-
if (this.completedRequests.has(id)) {
|
|
2190
|
-
return;
|
|
2191
|
-
}
|
|
2192
|
-
this.completedRequests.add(id);
|
|
2193
|
-
const transferables = extractTransferables(result);
|
|
2194
|
-
const uniqueTransferables = [...new Set(transferables)];
|
|
2195
|
-
postMessage({ id, type: "complete", data: result }, uniqueTransferables);
|
|
2196
|
-
};
|
|
2197
|
-
postError = (id, errorMessage) => {
|
|
2198
|
-
if (this.completedRequests.has(id)) {
|
|
2199
|
-
return;
|
|
2200
|
-
}
|
|
2201
|
-
this.completedRequests.add(id);
|
|
2202
|
-
postMessage({ id, type: "error", data: errorMessage });
|
|
2203
|
-
};
|
|
2204
|
-
postStreamChunk = (id, event) => {
|
|
2205
|
-
if (this.completedRequests.has(id)) {
|
|
2206
|
-
return;
|
|
2207
|
-
}
|
|
2208
|
-
postMessage({ id, type: "stream_chunk", data: event });
|
|
2209
|
-
};
|
|
2210
|
-
sendReady() {
|
|
2211
|
-
postMessage({
|
|
2212
|
-
type: "ready",
|
|
2213
|
-
functions: Object.keys(this.functions),
|
|
2214
|
-
streamFunctions: Object.keys(this.streamFunctions),
|
|
2215
|
-
reactiveFunctions: Object.keys(this.reactiveFunctions)
|
|
2216
|
-
});
|
|
2217
|
-
}
|
|
2218
|
-
registerFunction(name, fn) {
|
|
2219
|
-
this.functions[name] = fn;
|
|
2220
|
-
}
|
|
2221
|
-
registerReactiveFunction(name, fn) {
|
|
2222
|
-
this.reactiveFunctions[name] = fn;
|
|
2223
|
-
}
|
|
2224
|
-
registerStreamFunction(name, fn) {
|
|
2225
|
-
this.streamFunctions[name] = fn;
|
|
2226
|
-
}
|
|
2227
|
-
async handleMessage(event) {
|
|
2228
|
-
const { id, type, functionName, args, stream, reactive } = event.data;
|
|
2229
|
-
if (type === "abort") {
|
|
2230
|
-
return await this.handleAbort(id);
|
|
2231
|
-
}
|
|
2232
|
-
if (type === "call") {
|
|
2233
|
-
if (stream) {
|
|
2234
|
-
return await this.handleStreamCall(id, functionName, args);
|
|
2235
|
-
}
|
|
2236
|
-
if (reactive) {
|
|
2237
|
-
return await this.handleReactiveCall(id, functionName, args);
|
|
2238
|
-
}
|
|
2239
|
-
return await this.handleCall(id, functionName, args);
|
|
2240
|
-
}
|
|
2241
|
-
}
|
|
2242
|
-
async handleAbort(id) {
|
|
2243
|
-
if (this.requestControllers.has(id)) {
|
|
2244
|
-
const controller = this.requestControllers.get(id);
|
|
2245
|
-
controller?.abort();
|
|
2246
|
-
this.requestControllers.delete(id);
|
|
2247
|
-
this.postError(id, "Operation aborted");
|
|
2248
|
-
}
|
|
2249
|
-
}
|
|
2250
|
-
async handleReactiveCall(id, functionName, [input, output, model]) {
|
|
2251
|
-
if (!(functionName in this.reactiveFunctions)) {
|
|
2252
|
-
this.postResult(id, undefined);
|
|
2253
|
-
return;
|
|
2254
|
-
}
|
|
2255
|
-
try {
|
|
2256
|
-
const fn = this.reactiveFunctions[functionName];
|
|
2257
|
-
const result = await fn(input, output, model);
|
|
2258
|
-
this.postResult(id, result);
|
|
2259
|
-
} catch (error) {
|
|
2260
|
-
this.postError(id, error.message);
|
|
2261
|
-
}
|
|
2262
|
-
}
|
|
2263
|
-
async handleCall(id, functionName, [input, model]) {
|
|
2264
|
-
if (!(functionName in this.functions)) {
|
|
2265
|
-
this.postError(id, `Function ${functionName} not found`);
|
|
2266
|
-
return;
|
|
2267
|
-
}
|
|
2268
|
-
try {
|
|
2269
|
-
const abortController = new AbortController;
|
|
2270
|
-
this.requestControllers.set(id, abortController);
|
|
2271
|
-
const fn = this.functions[functionName];
|
|
2272
|
-
const postProgress = (progress, message, details) => {
|
|
2273
|
-
if (!this.completedRequests.has(id)) {
|
|
2274
|
-
postMessage({ id, type: "progress", data: { progress, message, details } });
|
|
2275
|
-
}
|
|
2276
|
-
};
|
|
2277
|
-
const result = await fn(input, model, postProgress, abortController.signal);
|
|
2278
|
-
this.postResult(id, result);
|
|
2279
|
-
} catch (error) {
|
|
2280
|
-
this.postError(id, error.message);
|
|
2281
|
-
} finally {
|
|
2282
|
-
this.requestControllers.delete(id);
|
|
2283
|
-
setTimeout(() => {
|
|
2284
|
-
this.completedRequests.delete(id);
|
|
2285
|
-
}, 1000);
|
|
2286
|
-
}
|
|
2287
|
-
}
|
|
2288
|
-
async handleStreamCall(id, functionName, [input, model]) {
|
|
2289
|
-
if (functionName in this.streamFunctions) {
|
|
2290
|
-
try {
|
|
2291
|
-
const abortController = new AbortController;
|
|
2292
|
-
this.requestControllers.set(id, abortController);
|
|
2293
|
-
const fn = this.streamFunctions[functionName];
|
|
2294
|
-
const iterable = fn(input, model, abortController.signal);
|
|
2295
|
-
for await (const event of iterable) {
|
|
2296
|
-
if (this.completedRequests.has(id))
|
|
2297
|
-
break;
|
|
2298
|
-
this.postStreamChunk(id, event);
|
|
2299
|
-
}
|
|
2300
|
-
this.postResult(id, undefined);
|
|
2301
|
-
} catch (error) {
|
|
2302
|
-
this.postError(id, error.message);
|
|
2303
|
-
} finally {
|
|
2304
|
-
this.requestControllers.delete(id);
|
|
2305
|
-
setTimeout(() => {
|
|
2306
|
-
this.completedRequests.delete(id);
|
|
2307
|
-
}, 1000);
|
|
2308
|
-
}
|
|
2309
|
-
} else if (functionName in this.functions) {
|
|
2310
|
-
try {
|
|
2311
|
-
const abortController = new AbortController;
|
|
2312
|
-
this.requestControllers.set(id, abortController);
|
|
2313
|
-
const fn = this.functions[functionName];
|
|
2314
|
-
const noopProgress = () => {};
|
|
2315
|
-
const result = await fn(input, model, noopProgress, abortController.signal);
|
|
2316
|
-
this.postStreamChunk(id, { type: "finish", data: result });
|
|
2317
|
-
this.postResult(id, undefined);
|
|
2318
|
-
} catch (error) {
|
|
2319
|
-
this.postError(id, error.message);
|
|
2320
|
-
} finally {
|
|
2321
|
-
this.requestControllers.delete(id);
|
|
2322
|
-
setTimeout(() => {
|
|
2323
|
-
this.completedRequests.delete(id);
|
|
2324
|
-
}, 1000);
|
|
2325
|
-
}
|
|
2326
|
-
} else {
|
|
2327
|
-
this.postError(id, `Function ${functionName} not found`);
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
|
-
}
|
|
2331
|
-
var WORKER_SERVER = createServiceToken("worker.server");
|
|
2332
|
-
globalServiceRegistry.register(WORKER_SERVER, () => new WorkerServer, true);
|
|
2333
976
|
// src/credentials/ICredentialStore.ts
|
|
2334
977
|
var CREDENTIAL_STORE = createServiceToken("credential.store");
|
|
2335
978
|
// src/credentials/InMemoryCredentialStore.ts
|
|
@@ -2570,518 +1213,6 @@ function base64ToBuf(b64) {
|
|
|
2570
1213
|
}
|
|
2571
1214
|
return buf;
|
|
2572
1215
|
}
|
|
2573
|
-
// src/mcp/McpAuthTypes.ts
|
|
2574
|
-
var mcpAuthTypes = [
|
|
2575
|
-
"none",
|
|
2576
|
-
"bearer",
|
|
2577
|
-
"client_credentials",
|
|
2578
|
-
"private_key_jwt",
|
|
2579
|
-
"static_private_key_jwt",
|
|
2580
|
-
"authorization_code"
|
|
2581
|
-
];
|
|
2582
|
-
var mcpAuthConfigSchema = {
|
|
2583
|
-
properties: {
|
|
2584
|
-
auth_type: {
|
|
2585
|
-
type: "string",
|
|
2586
|
-
enum: mcpAuthTypes,
|
|
2587
|
-
title: "Auth Type",
|
|
2588
|
-
description: "Authentication method for connecting to the MCP server",
|
|
2589
|
-
default: "none"
|
|
2590
|
-
},
|
|
2591
|
-
auth_token: {
|
|
2592
|
-
type: "string",
|
|
2593
|
-
format: "credential",
|
|
2594
|
-
title: "Bearer Token",
|
|
2595
|
-
description: "Static bearer token or API key (for bearer auth)"
|
|
2596
|
-
},
|
|
2597
|
-
auth_client_id: {
|
|
2598
|
-
type: "string",
|
|
2599
|
-
title: "Client ID",
|
|
2600
|
-
description: "OAuth client ID (for OAuth auth types)"
|
|
2601
|
-
},
|
|
2602
|
-
auth_client_secret: {
|
|
2603
|
-
type: "string",
|
|
2604
|
-
format: "credential",
|
|
2605
|
-
title: "Client Secret",
|
|
2606
|
-
description: "OAuth client secret (for client_credentials auth)"
|
|
2607
|
-
},
|
|
2608
|
-
auth_private_key: {
|
|
2609
|
-
type: "string",
|
|
2610
|
-
format: "credential",
|
|
2611
|
-
title: "Private Key",
|
|
2612
|
-
description: "PEM or JWK private key (for private_key_jwt auth)"
|
|
2613
|
-
},
|
|
2614
|
-
auth_algorithm: {
|
|
2615
|
-
type: "string",
|
|
2616
|
-
title: "Algorithm",
|
|
2617
|
-
description: "JWT signing algorithm, e.g. RS256, ES256 (for private_key_jwt auth)"
|
|
2618
|
-
},
|
|
2619
|
-
auth_jwt_bearer_assertion: {
|
|
2620
|
-
type: "string",
|
|
2621
|
-
format: "credential",
|
|
2622
|
-
title: "JWT Assertion",
|
|
2623
|
-
description: "Pre-built JWT assertion (for static_private_key_jwt auth)"
|
|
2624
|
-
},
|
|
2625
|
-
auth_redirect_url: {
|
|
2626
|
-
type: "string",
|
|
2627
|
-
format: "uri",
|
|
2628
|
-
title: "Redirect URL",
|
|
2629
|
-
description: "OAuth redirect URL (for authorization_code auth)"
|
|
2630
|
-
},
|
|
2631
|
-
auth_scope: {
|
|
2632
|
-
type: "string",
|
|
2633
|
-
title: "Scope",
|
|
2634
|
-
description: "OAuth scope (space-separated)"
|
|
2635
|
-
},
|
|
2636
|
-
auth_client_name: {
|
|
2637
|
-
type: "string",
|
|
2638
|
-
title: "Client Name",
|
|
2639
|
-
description: "Optional OAuth client display name"
|
|
2640
|
-
},
|
|
2641
|
-
auth_jwt_lifetime_seconds: {
|
|
2642
|
-
type: "number",
|
|
2643
|
-
title: "JWT Lifetime",
|
|
2644
|
-
description: "JWT lifetime in seconds (default: 300)",
|
|
2645
|
-
minimum: 1
|
|
2646
|
-
}
|
|
2647
|
-
},
|
|
2648
|
-
allOf: [
|
|
2649
|
-
{
|
|
2650
|
-
if: { properties: { auth_type: { const: "bearer" } }, required: ["auth_type"] },
|
|
2651
|
-
then: {
|
|
2652
|
-
required: ["auth_token"],
|
|
2653
|
-
properties: { auth_token: true }
|
|
2654
|
-
}
|
|
2655
|
-
},
|
|
2656
|
-
{
|
|
2657
|
-
if: {
|
|
2658
|
-
properties: { auth_type: { const: "client_credentials" } },
|
|
2659
|
-
required: ["auth_type"]
|
|
2660
|
-
},
|
|
2661
|
-
then: {
|
|
2662
|
-
required: ["auth_client_id", "auth_client_secret"],
|
|
2663
|
-
properties: {
|
|
2664
|
-
auth_client_id: true,
|
|
2665
|
-
auth_client_secret: true,
|
|
2666
|
-
auth_client_name: true,
|
|
2667
|
-
auth_scope: true
|
|
2668
|
-
}
|
|
2669
|
-
}
|
|
2670
|
-
},
|
|
2671
|
-
{
|
|
2672
|
-
if: { properties: { auth_type: { const: "private_key_jwt" } }, required: ["auth_type"] },
|
|
2673
|
-
then: {
|
|
2674
|
-
required: ["auth_client_id", "auth_private_key", "auth_algorithm"],
|
|
2675
|
-
properties: {
|
|
2676
|
-
auth_client_id: true,
|
|
2677
|
-
auth_private_key: true,
|
|
2678
|
-
auth_algorithm: true,
|
|
2679
|
-
auth_client_name: true,
|
|
2680
|
-
auth_jwt_lifetime_seconds: true,
|
|
2681
|
-
auth_scope: true
|
|
2682
|
-
}
|
|
2683
|
-
}
|
|
2684
|
-
},
|
|
2685
|
-
{
|
|
2686
|
-
if: {
|
|
2687
|
-
properties: { auth_type: { const: "static_private_key_jwt" } },
|
|
2688
|
-
required: ["auth_type"]
|
|
2689
|
-
},
|
|
2690
|
-
then: {
|
|
2691
|
-
required: ["auth_client_id", "auth_jwt_bearer_assertion"],
|
|
2692
|
-
properties: {
|
|
2693
|
-
auth_client_id: true,
|
|
2694
|
-
auth_jwt_bearer_assertion: true,
|
|
2695
|
-
auth_client_name: true,
|
|
2696
|
-
auth_scope: true
|
|
2697
|
-
}
|
|
2698
|
-
}
|
|
2699
|
-
},
|
|
2700
|
-
{
|
|
2701
|
-
if: { properties: { auth_type: { const: "authorization_code" } }, required: ["auth_type"] },
|
|
2702
|
-
then: {
|
|
2703
|
-
required: ["auth_client_id", "auth_redirect_url"],
|
|
2704
|
-
properties: {
|
|
2705
|
-
auth_client_id: true,
|
|
2706
|
-
auth_client_secret: true,
|
|
2707
|
-
auth_redirect_url: true,
|
|
2708
|
-
auth_scope: true
|
|
2709
|
-
}
|
|
2710
|
-
}
|
|
2711
|
-
}
|
|
2712
|
-
]
|
|
2713
|
-
};
|
|
2714
|
-
function isMcpAuthType(value) {
|
|
2715
|
-
return typeof value === "string" && mcpAuthTypes.includes(value);
|
|
2716
|
-
}
|
|
2717
|
-
function asNonEmptyString(value) {
|
|
2718
|
-
if (typeof value !== "string")
|
|
2719
|
-
return;
|
|
2720
|
-
const trimmed = value.trim();
|
|
2721
|
-
return trimmed === "" ? undefined : trimmed;
|
|
2722
|
-
}
|
|
2723
|
-
function asNumber(value) {
|
|
2724
|
-
return typeof value === "number" ? value : undefined;
|
|
2725
|
-
}
|
|
2726
|
-
function buildAuthConfig(flat) {
|
|
2727
|
-
const rawAuthType = flat.auth_type;
|
|
2728
|
-
if (!isMcpAuthType(rawAuthType) || rawAuthType === "none") {
|
|
2729
|
-
return;
|
|
2730
|
-
}
|
|
2731
|
-
const authType = rawAuthType;
|
|
2732
|
-
switch (authType) {
|
|
2733
|
-
case "bearer": {
|
|
2734
|
-
const token = asNonEmptyString(flat.auth_token);
|
|
2735
|
-
if (!token)
|
|
2736
|
-
return;
|
|
2737
|
-
return { type: "bearer", token };
|
|
2738
|
-
}
|
|
2739
|
-
case "client_credentials": {
|
|
2740
|
-
const client_id = asNonEmptyString(flat.auth_client_id);
|
|
2741
|
-
const client_secret = asNonEmptyString(flat.auth_client_secret);
|
|
2742
|
-
if (!client_id || !client_secret)
|
|
2743
|
-
return;
|
|
2744
|
-
return {
|
|
2745
|
-
type: "client_credentials",
|
|
2746
|
-
client_id,
|
|
2747
|
-
client_secret,
|
|
2748
|
-
client_name: asNonEmptyString(flat.auth_client_name),
|
|
2749
|
-
scope: asNonEmptyString(flat.auth_scope)
|
|
2750
|
-
};
|
|
2751
|
-
}
|
|
2752
|
-
case "private_key_jwt": {
|
|
2753
|
-
const client_id = asNonEmptyString(flat.auth_client_id);
|
|
2754
|
-
const private_key = asNonEmptyString(flat.auth_private_key);
|
|
2755
|
-
const algorithm = asNonEmptyString(flat.auth_algorithm);
|
|
2756
|
-
if (!client_id || !private_key || !algorithm)
|
|
2757
|
-
return;
|
|
2758
|
-
return {
|
|
2759
|
-
type: "private_key_jwt",
|
|
2760
|
-
client_id,
|
|
2761
|
-
private_key,
|
|
2762
|
-
algorithm,
|
|
2763
|
-
client_name: asNonEmptyString(flat.auth_client_name),
|
|
2764
|
-
jwt_lifetime_seconds: asNumber(flat.auth_jwt_lifetime_seconds),
|
|
2765
|
-
scope: asNonEmptyString(flat.auth_scope)
|
|
2766
|
-
};
|
|
2767
|
-
}
|
|
2768
|
-
case "static_private_key_jwt": {
|
|
2769
|
-
const client_id = asNonEmptyString(flat.auth_client_id);
|
|
2770
|
-
const jwt_bearer_assertion = asNonEmptyString(flat.auth_jwt_bearer_assertion);
|
|
2771
|
-
if (!client_id || !jwt_bearer_assertion)
|
|
2772
|
-
return;
|
|
2773
|
-
return {
|
|
2774
|
-
type: "static_private_key_jwt",
|
|
2775
|
-
client_id,
|
|
2776
|
-
jwt_bearer_assertion,
|
|
2777
|
-
client_name: asNonEmptyString(flat.auth_client_name),
|
|
2778
|
-
scope: asNonEmptyString(flat.auth_scope)
|
|
2779
|
-
};
|
|
2780
|
-
}
|
|
2781
|
-
case "authorization_code": {
|
|
2782
|
-
const client_id = asNonEmptyString(flat.auth_client_id);
|
|
2783
|
-
const redirect_url = asNonEmptyString(flat.auth_redirect_url);
|
|
2784
|
-
if (!client_id || !redirect_url)
|
|
2785
|
-
return;
|
|
2786
|
-
return {
|
|
2787
|
-
type: "authorization_code",
|
|
2788
|
-
client_id,
|
|
2789
|
-
client_secret: asNonEmptyString(flat.auth_client_secret),
|
|
2790
|
-
redirect_url,
|
|
2791
|
-
scope: asNonEmptyString(flat.auth_scope)
|
|
2792
|
-
};
|
|
2793
|
-
}
|
|
2794
|
-
default:
|
|
2795
|
-
return;
|
|
2796
|
-
}
|
|
2797
|
-
}
|
|
2798
|
-
// src/mcp/McpAuthProvider.ts
|
|
2799
|
-
import {
|
|
2800
|
-
ClientCredentialsProvider,
|
|
2801
|
-
PrivateKeyJwtProvider,
|
|
2802
|
-
StaticPrivateKeyJwtProvider,
|
|
2803
|
-
createPrivateKeyJwtAuth
|
|
2804
|
-
} from "@modelcontextprotocol/sdk/client/auth-extensions.js";
|
|
2805
|
-
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
2806
|
-
function normalizeServerUrl(serverUrl) {
|
|
2807
|
-
try {
|
|
2808
|
-
const u = new URL(serverUrl);
|
|
2809
|
-
return u.origin + u.pathname.replace(/\/+$/, "");
|
|
2810
|
-
} catch {
|
|
2811
|
-
return serverUrl;
|
|
2812
|
-
}
|
|
2813
|
-
}
|
|
2814
|
-
function storeKey(serverUrl, suffix) {
|
|
2815
|
-
return `mcp:oauth:${normalizeServerUrl(serverUrl)}:${suffix}`;
|
|
2816
|
-
}
|
|
2817
|
-
|
|
2818
|
-
class CredentialStoreOAuthProvider {
|
|
2819
|
-
store;
|
|
2820
|
-
serverUrl;
|
|
2821
|
-
_clientMetadata;
|
|
2822
|
-
_redirectUrl;
|
|
2823
|
-
_initialClientInfo;
|
|
2824
|
-
prepareTokenRequest;
|
|
2825
|
-
addClientAuthentication;
|
|
2826
|
-
constructor(options) {
|
|
2827
|
-
this.store = options.store;
|
|
2828
|
-
this.serverUrl = options.serverUrl;
|
|
2829
|
-
this._clientMetadata = options.clientMetadata;
|
|
2830
|
-
this._redirectUrl = options.redirectUrl;
|
|
2831
|
-
this._initialClientInfo = options.initialClientInfo;
|
|
2832
|
-
if (options.prepareTokenRequest) {
|
|
2833
|
-
this.prepareTokenRequest = options.prepareTokenRequest;
|
|
2834
|
-
}
|
|
2835
|
-
if (options.addClientAuthentication) {
|
|
2836
|
-
this.addClientAuthentication = options.addClientAuthentication;
|
|
2837
|
-
}
|
|
2838
|
-
}
|
|
2839
|
-
get redirectUrl() {
|
|
2840
|
-
return this._redirectUrl;
|
|
2841
|
-
}
|
|
2842
|
-
get clientMetadata() {
|
|
2843
|
-
return this._clientMetadata;
|
|
2844
|
-
}
|
|
2845
|
-
async clientInformation() {
|
|
2846
|
-
const raw = await this.store.get(storeKey(this.serverUrl, "client_info"));
|
|
2847
|
-
if (!raw)
|
|
2848
|
-
return this._initialClientInfo;
|
|
2849
|
-
return JSON.parse(raw);
|
|
2850
|
-
}
|
|
2851
|
-
async saveClientInformation(info) {
|
|
2852
|
-
await this.store.put(storeKey(this.serverUrl, "client_info"), JSON.stringify(info));
|
|
2853
|
-
}
|
|
2854
|
-
async tokens() {
|
|
2855
|
-
const raw = await this.store.get(storeKey(this.serverUrl, "tokens"));
|
|
2856
|
-
if (!raw)
|
|
2857
|
-
return;
|
|
2858
|
-
return JSON.parse(raw);
|
|
2859
|
-
}
|
|
2860
|
-
async saveTokens(tokens) {
|
|
2861
|
-
const expiresAt = tokens.expires_in != null ? new Date(Date.now() + tokens.expires_in * 1000) : undefined;
|
|
2862
|
-
await this.store.put(storeKey(this.serverUrl, "tokens"), JSON.stringify(tokens), {
|
|
2863
|
-
expiresAt
|
|
2864
|
-
});
|
|
2865
|
-
}
|
|
2866
|
-
async redirectToAuthorization(authorizationUrl) {
|
|
2867
|
-
throw new Error(`MCP OAuth authorization required. ` + `Open this URL to authorize: ${authorizationUrl.toString()}`);
|
|
2868
|
-
}
|
|
2869
|
-
async saveCodeVerifier(codeVerifier) {
|
|
2870
|
-
await this.store.put(storeKey(this.serverUrl, "code_verifier"), codeVerifier);
|
|
2871
|
-
}
|
|
2872
|
-
async codeVerifier() {
|
|
2873
|
-
const v = await this.store.get(storeKey(this.serverUrl, "code_verifier"));
|
|
2874
|
-
if (!v)
|
|
2875
|
-
throw new Error("No code verifier saved for this session");
|
|
2876
|
-
return v;
|
|
2877
|
-
}
|
|
2878
|
-
async saveDiscoveryState(state) {
|
|
2879
|
-
await this.store.put(storeKey(this.serverUrl, "discovery"), JSON.stringify(state));
|
|
2880
|
-
}
|
|
2881
|
-
async discoveryState() {
|
|
2882
|
-
const raw = await this.store.get(storeKey(this.serverUrl, "discovery"));
|
|
2883
|
-
if (!raw)
|
|
2884
|
-
return;
|
|
2885
|
-
return JSON.parse(raw);
|
|
2886
|
-
}
|
|
2887
|
-
async invalidateCredentials(scope) {
|
|
2888
|
-
const deleteKey = async (suffix) => {
|
|
2889
|
-
await this.store.delete(storeKey(this.serverUrl, suffix));
|
|
2890
|
-
};
|
|
2891
|
-
switch (scope) {
|
|
2892
|
-
case "all":
|
|
2893
|
-
await deleteKey("tokens");
|
|
2894
|
-
await deleteKey("client_info");
|
|
2895
|
-
await deleteKey("code_verifier");
|
|
2896
|
-
await deleteKey("discovery");
|
|
2897
|
-
break;
|
|
2898
|
-
case "client":
|
|
2899
|
-
await deleteKey("client_info");
|
|
2900
|
-
break;
|
|
2901
|
-
case "tokens":
|
|
2902
|
-
await deleteKey("tokens");
|
|
2903
|
-
break;
|
|
2904
|
-
case "verifier":
|
|
2905
|
-
await deleteKey("code_verifier");
|
|
2906
|
-
break;
|
|
2907
|
-
case "discovery":
|
|
2908
|
-
await deleteKey("discovery");
|
|
2909
|
-
break;
|
|
2910
|
-
}
|
|
2911
|
-
}
|
|
2912
|
-
}
|
|
2913
|
-
function createAuthProvider(auth, serverUrl, credentialStore) {
|
|
2914
|
-
switch (auth.type) {
|
|
2915
|
-
case "none":
|
|
2916
|
-
case "bearer":
|
|
2917
|
-
return;
|
|
2918
|
-
case "client_credentials": {
|
|
2919
|
-
if (!credentialStore) {
|
|
2920
|
-
return new ClientCredentialsProvider({
|
|
2921
|
-
clientId: auth.client_id,
|
|
2922
|
-
clientSecret: auth.client_secret,
|
|
2923
|
-
clientName: auth.client_name,
|
|
2924
|
-
scope: auth.scope
|
|
2925
|
-
});
|
|
2926
|
-
}
|
|
2927
|
-
const prepareTokenRequest = (scope) => {
|
|
2928
|
-
const params = new URLSearchParams({ grant_type: "client_credentials" });
|
|
2929
|
-
const effectiveScope = scope ?? auth.scope;
|
|
2930
|
-
if (effectiveScope)
|
|
2931
|
-
params.set("scope", effectiveScope);
|
|
2932
|
-
return params;
|
|
2933
|
-
};
|
|
2934
|
-
return new CredentialStoreOAuthProvider({
|
|
2935
|
-
store: credentialStore,
|
|
2936
|
-
serverUrl,
|
|
2937
|
-
clientMetadata: {
|
|
2938
|
-
redirect_uris: [],
|
|
2939
|
-
grant_types: ["client_credentials"],
|
|
2940
|
-
token_endpoint_auth_method: "client_secret_basic",
|
|
2941
|
-
client_name: auth.client_name
|
|
2942
|
-
},
|
|
2943
|
-
initialClientInfo: {
|
|
2944
|
-
client_id: auth.client_id,
|
|
2945
|
-
client_secret: auth.client_secret
|
|
2946
|
-
},
|
|
2947
|
-
prepareTokenRequest
|
|
2948
|
-
});
|
|
2949
|
-
}
|
|
2950
|
-
case "private_key_jwt": {
|
|
2951
|
-
if (!credentialStore) {
|
|
2952
|
-
return new PrivateKeyJwtProvider({
|
|
2953
|
-
clientId: auth.client_id,
|
|
2954
|
-
privateKey: auth.private_key,
|
|
2955
|
-
algorithm: auth.algorithm,
|
|
2956
|
-
clientName: auth.client_name,
|
|
2957
|
-
jwtLifetimeSeconds: auth.jwt_lifetime_seconds,
|
|
2958
|
-
scope: auth.scope
|
|
2959
|
-
});
|
|
2960
|
-
}
|
|
2961
|
-
const addClientAuth = createPrivateKeyJwtAuth({
|
|
2962
|
-
issuer: auth.client_id,
|
|
2963
|
-
subject: auth.client_id,
|
|
2964
|
-
privateKey: auth.private_key,
|
|
2965
|
-
alg: auth.algorithm,
|
|
2966
|
-
lifetimeSeconds: auth.jwt_lifetime_seconds
|
|
2967
|
-
});
|
|
2968
|
-
const prepareTokenRequest = (scope) => {
|
|
2969
|
-
const params = new URLSearchParams({ grant_type: "client_credentials" });
|
|
2970
|
-
const effectiveScope = scope ?? auth.scope;
|
|
2971
|
-
if (effectiveScope)
|
|
2972
|
-
params.set("scope", effectiveScope);
|
|
2973
|
-
return params;
|
|
2974
|
-
};
|
|
2975
|
-
return new CredentialStoreOAuthProvider({
|
|
2976
|
-
store: credentialStore,
|
|
2977
|
-
serverUrl,
|
|
2978
|
-
clientMetadata: {
|
|
2979
|
-
redirect_uris: [],
|
|
2980
|
-
grant_types: ["client_credentials"],
|
|
2981
|
-
token_endpoint_auth_method: "private_key_jwt",
|
|
2982
|
-
client_name: auth.client_name
|
|
2983
|
-
},
|
|
2984
|
-
initialClientInfo: { client_id: auth.client_id },
|
|
2985
|
-
prepareTokenRequest,
|
|
2986
|
-
addClientAuthentication: addClientAuth
|
|
2987
|
-
});
|
|
2988
|
-
}
|
|
2989
|
-
case "static_private_key_jwt": {
|
|
2990
|
-
if (!credentialStore) {
|
|
2991
|
-
return new StaticPrivateKeyJwtProvider({
|
|
2992
|
-
clientId: auth.client_id,
|
|
2993
|
-
jwtBearerAssertion: auth.jwt_bearer_assertion,
|
|
2994
|
-
clientName: auth.client_name,
|
|
2995
|
-
scope: auth.scope
|
|
2996
|
-
});
|
|
2997
|
-
}
|
|
2998
|
-
const assertion = auth.jwt_bearer_assertion;
|
|
2999
|
-
const addClientAuth = (_headers, params) => {
|
|
3000
|
-
params.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
|
|
3001
|
-
params.set("client_assertion", assertion);
|
|
3002
|
-
};
|
|
3003
|
-
const prepareTokenRequest = (scope) => {
|
|
3004
|
-
const params = new URLSearchParams({ grant_type: "client_credentials" });
|
|
3005
|
-
const effectiveScope = scope ?? auth.scope;
|
|
3006
|
-
if (effectiveScope)
|
|
3007
|
-
params.set("scope", effectiveScope);
|
|
3008
|
-
return params;
|
|
3009
|
-
};
|
|
3010
|
-
return new CredentialStoreOAuthProvider({
|
|
3011
|
-
store: credentialStore,
|
|
3012
|
-
serverUrl,
|
|
3013
|
-
clientMetadata: {
|
|
3014
|
-
redirect_uris: [],
|
|
3015
|
-
grant_types: ["client_credentials"],
|
|
3016
|
-
token_endpoint_auth_method: "private_key_jwt",
|
|
3017
|
-
client_name: auth.client_name
|
|
3018
|
-
},
|
|
3019
|
-
initialClientInfo: { client_id: auth.client_id },
|
|
3020
|
-
prepareTokenRequest,
|
|
3021
|
-
addClientAuthentication: addClientAuth
|
|
3022
|
-
});
|
|
3023
|
-
}
|
|
3024
|
-
case "authorization_code": {
|
|
3025
|
-
if (!credentialStore) {
|
|
3026
|
-
throw new Error("authorization_code auth requires a credential store for token persistence");
|
|
3027
|
-
}
|
|
3028
|
-
return new CredentialStoreOAuthProvider({
|
|
3029
|
-
store: credentialStore,
|
|
3030
|
-
serverUrl,
|
|
3031
|
-
clientMetadata: {
|
|
3032
|
-
redirect_uris: [auth.redirect_url],
|
|
3033
|
-
grant_types: ["authorization_code", "refresh_token"],
|
|
3034
|
-
token_endpoint_auth_method: auth.client_secret ? "client_secret_basic" : "none",
|
|
3035
|
-
scope: auth.scope
|
|
3036
|
-
},
|
|
3037
|
-
initialClientInfo: {
|
|
3038
|
-
client_id: auth.client_id,
|
|
3039
|
-
...auth.client_secret ? { client_secret: auth.client_secret } : {}
|
|
3040
|
-
},
|
|
3041
|
-
redirectUrl: auth.redirect_url
|
|
3042
|
-
});
|
|
3043
|
-
}
|
|
3044
|
-
default:
|
|
3045
|
-
return;
|
|
3046
|
-
}
|
|
3047
|
-
}
|
|
3048
|
-
async function resolveAuthSecrets(auth, credentialStore) {
|
|
3049
|
-
if (auth.type === "none")
|
|
3050
|
-
return auth;
|
|
3051
|
-
const store = credentialStore ?? getGlobalCredentialStore();
|
|
3052
|
-
async function resolve(value) {
|
|
3053
|
-
if (!value)
|
|
3054
|
-
return value;
|
|
3055
|
-
const resolved = await store.get(value);
|
|
3056
|
-
return resolved ?? value;
|
|
3057
|
-
}
|
|
3058
|
-
switch (auth.type) {
|
|
3059
|
-
case "bearer":
|
|
3060
|
-
return { ...auth, token: await resolve(auth.token) ?? auth.token };
|
|
3061
|
-
case "client_credentials":
|
|
3062
|
-
return {
|
|
3063
|
-
...auth,
|
|
3064
|
-
client_secret: await resolve(auth.client_secret) ?? auth.client_secret
|
|
3065
|
-
};
|
|
3066
|
-
case "private_key_jwt":
|
|
3067
|
-
return {
|
|
3068
|
-
...auth,
|
|
3069
|
-
private_key: await resolve(auth.private_key) ?? auth.private_key
|
|
3070
|
-
};
|
|
3071
|
-
case "static_private_key_jwt":
|
|
3072
|
-
return {
|
|
3073
|
-
...auth,
|
|
3074
|
-
jwt_bearer_assertion: await resolve(auth.jwt_bearer_assertion) ?? auth.jwt_bearer_assertion
|
|
3075
|
-
};
|
|
3076
|
-
case "authorization_code":
|
|
3077
|
-
return {
|
|
3078
|
-
...auth,
|
|
3079
|
-
client_secret: await resolve(auth.client_secret)
|
|
3080
|
-
};
|
|
3081
|
-
default:
|
|
3082
|
-
return auth;
|
|
3083
|
-
}
|
|
3084
|
-
}
|
|
3085
1216
|
// src/telemetry/ITelemetryProvider.ts
|
|
3086
1217
|
var SpanStatusCode = {
|
|
3087
1218
|
UNSET: 0,
|
|
@@ -3235,164 +1366,209 @@ function getTelemetryProvider() {
|
|
|
3235
1366
|
function setTelemetryProvider(provider) {
|
|
3236
1367
|
globalServiceRegistry.registerInstance(TELEMETRY_PROVIDER, provider);
|
|
3237
1368
|
}
|
|
3238
|
-
// src/
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
const
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
1369
|
+
// src/worker/WorkerServerBase.ts
|
|
1370
|
+
var WORKER_SERVER = createServiceToken("worker.server");
|
|
1371
|
+
function extractTransferables(obj) {
|
|
1372
|
+
const transferables = [];
|
|
1373
|
+
const seen = new WeakSet;
|
|
1374
|
+
function findTransferables(value) {
|
|
1375
|
+
if (value && typeof value === "object" && seen.has(value)) {
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
if (value && typeof value === "object") {
|
|
1379
|
+
seen.add(value);
|
|
1380
|
+
}
|
|
1381
|
+
if (value instanceof Float32Array || value instanceof Int16Array) {
|
|
1382
|
+
transferables.push(value.buffer);
|
|
1383
|
+
} else if (value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int8Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array || value instanceof Float64Array || value instanceof BigInt64Array || value instanceof BigUint64Array) {
|
|
1384
|
+
transferables.push(value.buffer);
|
|
1385
|
+
} else if (typeof OffscreenCanvas !== "undefined" && value instanceof OffscreenCanvas) {
|
|
1386
|
+
transferables.push(value);
|
|
1387
|
+
} else if (typeof ImageBitmap !== "undefined" && value instanceof ImageBitmap) {
|
|
1388
|
+
transferables.push(value);
|
|
1389
|
+
} else if (typeof VideoFrame !== "undefined" && value instanceof VideoFrame) {
|
|
1390
|
+
transferables.push(value);
|
|
1391
|
+
} else if (typeof MessagePort !== "undefined" && value instanceof MessagePort) {
|
|
1392
|
+
transferables.push(value);
|
|
1393
|
+
} else if (value instanceof ArrayBuffer) {
|
|
1394
|
+
transferables.push(value);
|
|
1395
|
+
} else if (Array.isArray(value)) {
|
|
1396
|
+
value.forEach(findTransferables);
|
|
1397
|
+
} else if (value && typeof value === "object") {
|
|
1398
|
+
Object.values(value).forEach(findTransferables);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
findTransferables(obj);
|
|
1402
|
+
return transferables;
|
|
3256
1403
|
}
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
format: "string:uri",
|
|
3274
|
-
title: "Server URL",
|
|
3275
|
-
description: "The URL of the MCP server (for sse and streamable-http transports)"
|
|
3276
|
-
},
|
|
3277
|
-
command: {
|
|
3278
|
-
type: "string",
|
|
3279
|
-
title: "Command",
|
|
3280
|
-
description: "The command to run (for stdio transport)"
|
|
3281
|
-
},
|
|
3282
|
-
args: {
|
|
3283
|
-
type: "array",
|
|
3284
|
-
items: { type: "string" },
|
|
3285
|
-
title: "Arguments",
|
|
3286
|
-
description: "Command arguments (for stdio transport)"
|
|
3287
|
-
},
|
|
3288
|
-
env: {
|
|
3289
|
-
type: "object",
|
|
3290
|
-
additionalProperties: { type: "string" },
|
|
3291
|
-
title: "Environment",
|
|
3292
|
-
description: "Environment variables (for stdio transport)"
|
|
3293
|
-
},
|
|
3294
|
-
...mcpAuthConfigSchema.properties
|
|
3295
|
-
},
|
|
3296
|
-
allOf: mcpAuthConfigSchema.allOf
|
|
3297
|
-
};
|
|
3298
|
-
async function createMcpClient(config, signal) {
|
|
3299
|
-
let transport;
|
|
3300
|
-
let auth = config.auth ?? buildAuthConfig({ ...config });
|
|
3301
|
-
if (auth && auth.type !== "none") {
|
|
3302
|
-
auth = await resolveAuthSecrets(auth, getGlobalCredentialStore());
|
|
3303
|
-
}
|
|
3304
|
-
const authProvider = config.authProvider ?? (auth && auth.type !== "none" && auth.type !== "bearer" ? createAuthProvider(auth, config.server_url ?? "", getGlobalCredentialStore()) : undefined);
|
|
3305
|
-
const headers = {
|
|
3306
|
-
...auth?.type === "bearer" ? { Authorization: `Bearer ${auth.token}` } : {}
|
|
1404
|
+
|
|
1405
|
+
class WorkerServerBase {
|
|
1406
|
+
constructor() {}
|
|
1407
|
+
functions = {};
|
|
1408
|
+
streamFunctions = {};
|
|
1409
|
+
reactiveFunctions = {};
|
|
1410
|
+
requestControllers = new Map;
|
|
1411
|
+
completedRequests = new Set;
|
|
1412
|
+
postResult = (id, result) => {
|
|
1413
|
+
if (this.completedRequests.has(id)) {
|
|
1414
|
+
return;
|
|
1415
|
+
}
|
|
1416
|
+
this.completedRequests.add(id);
|
|
1417
|
+
const transferables = extractTransferables(result);
|
|
1418
|
+
const uniqueTransferables = [...new Set(transferables)];
|
|
1419
|
+
postMessage({ id, type: "complete", data: result }, uniqueTransferables);
|
|
3307
1420
|
};
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
getLogger().warn("MCP auth is not supported for stdio transport; auth config ignored. " + "Use env vars to pass credentials to stdio servers.");
|
|
3312
|
-
}
|
|
3313
|
-
transport = new StdioClientTransport({
|
|
3314
|
-
command: config.command,
|
|
3315
|
-
args: config.args,
|
|
3316
|
-
env: config.env
|
|
3317
|
-
});
|
|
3318
|
-
break;
|
|
3319
|
-
case "sse": {
|
|
3320
|
-
transport = new SSEClientTransport(new URL(config.server_url), {
|
|
3321
|
-
authProvider,
|
|
3322
|
-
requestInit: { headers }
|
|
3323
|
-
});
|
|
3324
|
-
break;
|
|
1421
|
+
postError = (id, errorMessage) => {
|
|
1422
|
+
if (this.completedRequests.has(id)) {
|
|
1423
|
+
return;
|
|
3325
1424
|
}
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
1425
|
+
this.completedRequests.add(id);
|
|
1426
|
+
postMessage({ id, type: "error", data: errorMessage });
|
|
1427
|
+
};
|
|
1428
|
+
postStreamChunk = (id, event) => {
|
|
1429
|
+
if (this.completedRequests.has(id)) {
|
|
1430
|
+
return;
|
|
3332
1431
|
}
|
|
3333
|
-
|
|
3334
|
-
|
|
1432
|
+
postMessage({ id, type: "stream_chunk", data: event });
|
|
1433
|
+
};
|
|
1434
|
+
sendReady() {
|
|
1435
|
+
postMessage({
|
|
1436
|
+
type: "ready",
|
|
1437
|
+
functions: Object.keys(this.functions),
|
|
1438
|
+
streamFunctions: Object.keys(this.streamFunctions),
|
|
1439
|
+
reactiveFunctions: Object.keys(this.reactiveFunctions)
|
|
1440
|
+
});
|
|
3335
1441
|
}
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
signal.addEventListener("abort", () => {
|
|
3339
|
-
client.close().catch(() => {});
|
|
3340
|
-
}, { once: true });
|
|
1442
|
+
registerFunction(name, fn) {
|
|
1443
|
+
this.functions[name] = fn;
|
|
3341
1444
|
}
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
}
|
|
3345
|
-
var mcpClientFactory = {
|
|
3346
|
-
create: createMcpClient
|
|
3347
|
-
};
|
|
3348
|
-
// src/media/image.ts
|
|
3349
|
-
function parseDataUri(dataUri) {
|
|
3350
|
-
const match = dataUri.match(/^data:([^;]+);base64,(.+)$/);
|
|
3351
|
-
if (!match) {
|
|
3352
|
-
throw new Error("Invalid base64 data URI");
|
|
1445
|
+
registerReactiveFunction(name, fn) {
|
|
1446
|
+
this.reactiveFunctions[name] = fn;
|
|
3353
1447
|
}
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
base64: match[2]
|
|
3357
|
-
};
|
|
3358
|
-
}
|
|
3359
|
-
|
|
3360
|
-
// src/media/image.node.ts
|
|
3361
|
-
async function dataUriToBlob(string) {
|
|
3362
|
-
const { mimeType, base64 } = parseDataUri(string);
|
|
3363
|
-
const binary = atob(base64);
|
|
3364
|
-
const bytes = new Uint8Array(binary.length);
|
|
3365
|
-
for (let i = 0;i < binary.length; i++) {
|
|
3366
|
-
bytes[i] = binary.charCodeAt(i);
|
|
1448
|
+
registerStreamFunction(name, fn) {
|
|
1449
|
+
this.streamFunctions[name] = fn;
|
|
3367
1450
|
}
|
|
3368
|
-
|
|
3369
|
-
}
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
channels: imageData.channels
|
|
3383
|
-
};
|
|
1451
|
+
async handleMessage(event) {
|
|
1452
|
+
const { id, type, functionName, args, stream, reactive } = event.data;
|
|
1453
|
+
if (type === "abort") {
|
|
1454
|
+
return await this.handleAbort(id);
|
|
1455
|
+
}
|
|
1456
|
+
if (type === "call") {
|
|
1457
|
+
if (stream) {
|
|
1458
|
+
return await this.handleStreamCall(id, functionName, args);
|
|
1459
|
+
}
|
|
1460
|
+
if (reactive) {
|
|
1461
|
+
return await this.handleReactiveCall(id, functionName, args);
|
|
1462
|
+
}
|
|
1463
|
+
return await this.handleCall(id, functionName, args);
|
|
1464
|
+
}
|
|
3384
1465
|
}
|
|
3385
|
-
|
|
3386
|
-
|
|
1466
|
+
async handleAbort(id) {
|
|
1467
|
+
if (this.requestControllers.has(id)) {
|
|
1468
|
+
const controller = this.requestControllers.get(id);
|
|
1469
|
+
controller?.abort();
|
|
1470
|
+
this.requestControllers.delete(id);
|
|
1471
|
+
this.postError(id, "Operation aborted");
|
|
1472
|
+
}
|
|
3387
1473
|
}
|
|
3388
|
-
|
|
3389
|
-
|
|
1474
|
+
async handleReactiveCall(id, functionName, [input, output, model]) {
|
|
1475
|
+
if (!(functionName in this.reactiveFunctions)) {
|
|
1476
|
+
this.postResult(id, undefined);
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
try {
|
|
1480
|
+
const fn = this.reactiveFunctions[functionName];
|
|
1481
|
+
const result = await fn(input, output, model);
|
|
1482
|
+
this.postResult(id, result);
|
|
1483
|
+
} catch (error) {
|
|
1484
|
+
this.postError(id, error.message);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
async handleCall(id, functionName, [input, model]) {
|
|
1488
|
+
if (!(functionName in this.functions)) {
|
|
1489
|
+
this.postError(id, `Function ${functionName} not found`);
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
try {
|
|
1493
|
+
const abortController = new AbortController;
|
|
1494
|
+
this.requestControllers.set(id, abortController);
|
|
1495
|
+
const fn = this.functions[functionName];
|
|
1496
|
+
const postProgress = (progress, message, details) => {
|
|
1497
|
+
if (!this.completedRequests.has(id)) {
|
|
1498
|
+
postMessage({ id, type: "progress", data: { progress, message, details } });
|
|
1499
|
+
}
|
|
1500
|
+
};
|
|
1501
|
+
const result = await fn(input, model, postProgress, abortController.signal);
|
|
1502
|
+
this.postResult(id, result);
|
|
1503
|
+
} catch (error) {
|
|
1504
|
+
this.postError(id, error.message);
|
|
1505
|
+
} finally {
|
|
1506
|
+
this.requestControllers.delete(id);
|
|
1507
|
+
setTimeout(() => {
|
|
1508
|
+
this.completedRequests.delete(id);
|
|
1509
|
+
}, 1000);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
async handleStreamCall(id, functionName, [input, model]) {
|
|
1513
|
+
if (functionName in this.streamFunctions) {
|
|
1514
|
+
try {
|
|
1515
|
+
const abortController = new AbortController;
|
|
1516
|
+
this.requestControllers.set(id, abortController);
|
|
1517
|
+
const fn = this.streamFunctions[functionName];
|
|
1518
|
+
const iterable = fn(input, model, abortController.signal);
|
|
1519
|
+
for await (const event of iterable) {
|
|
1520
|
+
if (this.completedRequests.has(id))
|
|
1521
|
+
break;
|
|
1522
|
+
this.postStreamChunk(id, event);
|
|
1523
|
+
}
|
|
1524
|
+
this.postResult(id, undefined);
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
this.postError(id, error.message);
|
|
1527
|
+
} finally {
|
|
1528
|
+
this.requestControllers.delete(id);
|
|
1529
|
+
setTimeout(() => {
|
|
1530
|
+
this.completedRequests.delete(id);
|
|
1531
|
+
}, 1000);
|
|
1532
|
+
}
|
|
1533
|
+
} else if (functionName in this.functions) {
|
|
1534
|
+
try {
|
|
1535
|
+
const abortController = new AbortController;
|
|
1536
|
+
this.requestControllers.set(id, abortController);
|
|
1537
|
+
const fn = this.functions[functionName];
|
|
1538
|
+
const noopProgress = () => {};
|
|
1539
|
+
const result = await fn(input, model, noopProgress, abortController.signal);
|
|
1540
|
+
this.postStreamChunk(id, { type: "finish", data: result });
|
|
1541
|
+
this.postResult(id, undefined);
|
|
1542
|
+
} catch (error) {
|
|
1543
|
+
this.postError(id, error.message);
|
|
1544
|
+
} finally {
|
|
1545
|
+
this.requestControllers.delete(id);
|
|
1546
|
+
setTimeout(() => {
|
|
1547
|
+
this.completedRequests.delete(id);
|
|
1548
|
+
}, 1000);
|
|
1549
|
+
}
|
|
1550
|
+
} else {
|
|
1551
|
+
this.postError(id, `Function ${functionName} not found`);
|
|
1552
|
+
}
|
|
3390
1553
|
}
|
|
3391
|
-
throw new Error(`Unsupported image data type: ${typeof imageData}`);
|
|
3392
1554
|
}
|
|
1555
|
+
|
|
3393
1556
|
// src/worker/Worker.bun.ts
|
|
3394
1557
|
var Worker = globalThis.Worker;
|
|
3395
|
-
var
|
|
1558
|
+
var parentPort = self;
|
|
1559
|
+
class WorkerServer extends WorkerServerBase {
|
|
1560
|
+
constructor() {
|
|
1561
|
+
parentPort?.addEventListener("message", async (event) => {
|
|
1562
|
+
const msg = {
|
|
1563
|
+
type: event.type,
|
|
1564
|
+
data: event.data
|
|
1565
|
+
};
|
|
1566
|
+
await this.handleMessage(msg);
|
|
1567
|
+
});
|
|
1568
|
+
super();
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
globalServiceRegistry.register(WORKER_SERVER, () => new WorkerServer, true);
|
|
3396
1572
|
export {
|
|
3397
1573
|
uuid4,
|
|
3398
1574
|
toSQLiteTimestamp,
|
|
@@ -3404,26 +1580,10 @@ export {
|
|
|
3404
1580
|
setGlobalCredentialStore,
|
|
3405
1581
|
serialize,
|
|
3406
1582
|
resolveCredential,
|
|
3407
|
-
resolveAuthSecrets,
|
|
3408
1583
|
registerInputResolver,
|
|
3409
|
-
|
|
3410
|
-
parseDataUri,
|
|
3411
|
-
parentPort2 as parentPort,
|
|
1584
|
+
parentPort,
|
|
3412
1585
|
objectOfArraysAsArrayOfObjects,
|
|
3413
|
-
normalizeNumberArray,
|
|
3414
|
-
normalize,
|
|
3415
|
-
mcpTransportTypes,
|
|
3416
|
-
mcpServerConfigSchema,
|
|
3417
|
-
mcpClientFactory,
|
|
3418
|
-
mcpAuthTypes,
|
|
3419
|
-
mcpAuthConfigSchema,
|
|
3420
1586
|
makeFingerprint,
|
|
3421
|
-
magnitude,
|
|
3422
|
-
jaccardSimilarity,
|
|
3423
|
-
isTypedArray,
|
|
3424
|
-
inner,
|
|
3425
|
-
hammingSimilarity,
|
|
3426
|
-
hammingDistance,
|
|
3427
1587
|
globalServiceRegistry,
|
|
3428
1588
|
globalContainer,
|
|
3429
1589
|
getTelemetryProvider,
|
|
@@ -3435,49 +1595,26 @@ export {
|
|
|
3435
1595
|
deriveKey,
|
|
3436
1596
|
deepEqual,
|
|
3437
1597
|
decrypt,
|
|
3438
|
-
decompress,
|
|
3439
|
-
createTypedArrayFrom,
|
|
3440
1598
|
createServiceToken,
|
|
3441
|
-
createMcpClient,
|
|
3442
|
-
createAuthProvider,
|
|
3443
|
-
cosineSimilarity,
|
|
3444
|
-
convertImageDataToUseableForm,
|
|
3445
|
-
compress,
|
|
3446
|
-
compileSchema,
|
|
3447
1599
|
collectPropertyValues,
|
|
3448
|
-
buildAuthConfig,
|
|
3449
1600
|
bufToBase64,
|
|
3450
1601
|
base64ToBuf,
|
|
3451
|
-
areSemanticallyCompatible,
|
|
3452
|
-
areObjectSchemasSemanticallyCompatible,
|
|
3453
1602
|
WorkerServer,
|
|
3454
1603
|
WorkerManager,
|
|
3455
1604
|
Worker,
|
|
3456
1605
|
WORKER_SERVER,
|
|
3457
1606
|
WORKER_MANAGER,
|
|
3458
|
-
UnauthorizedError,
|
|
3459
|
-
TypedArraySchema,
|
|
3460
|
-
TensorType,
|
|
3461
|
-
TensorSchema,
|
|
3462
1607
|
TELEMETRY_PROVIDER,
|
|
3463
1608
|
SpanStatusCode,
|
|
3464
1609
|
ServiceRegistry,
|
|
3465
1610
|
OTelTelemetryProvider,
|
|
3466
1611
|
NullLogger,
|
|
3467
1612
|
NoopTelemetryProvider,
|
|
3468
|
-
NodeDoesntExistError,
|
|
3469
|
-
NodeAlreadyExistsError,
|
|
3470
1613
|
LOGGER,
|
|
3471
1614
|
InMemoryCredentialStore,
|
|
3472
1615
|
INPUT_RESOLVERS,
|
|
3473
|
-
Graph,
|
|
3474
|
-
FromSchemaDefaultOptions,
|
|
3475
1616
|
EventEmitter,
|
|
3476
1617
|
EnvCredentialStore,
|
|
3477
|
-
DirectedGraph,
|
|
3478
|
-
DirectedAcyclicGraph,
|
|
3479
|
-
CycleError,
|
|
3480
|
-
CredentialStoreOAuthProvider,
|
|
3481
1618
|
Container,
|
|
3482
1619
|
ConsoleTelemetryProvider,
|
|
3483
1620
|
ConsoleLogger,
|
|
@@ -3486,4 +1623,4 @@ export {
|
|
|
3486
1623
|
BaseError
|
|
3487
1624
|
};
|
|
3488
1625
|
|
|
3489
|
-
//# debugId=
|
|
1626
|
+
//# debugId=9C84CCE9541649D064756E2164756E21
|