File size: 2,077 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type {DynamicContextNodeBase} from "../dynamic_context_base.js";

import {app} from "scripts/app.js";
import {NodeTypesString} from "../constants.js";
import {getConnectedOutputNodesAndFilterPassThroughs} from "../utils.js";
import {INodeInputSlot, INodeOutputSlot, INodeSlot, LGraphNode} from "typings/litegraph.js";

export let SERVICE: ContextService;

const OWNED_PREFIX = "+";
const REGEX_PREFIX = /^[\+⚠️]\s*/;
const REGEX_EMPTY_INPUT = /^\+\s*$/;

export function stripContextInputPrefixes(name: string) {
  return name.replace(REGEX_PREFIX, "");
}

export function getContextOutputName(inputName: string) {
  if (inputName === "base_ctx") return "CONTEXT";
  return stripContextInputPrefixes(inputName).toUpperCase();
}

export enum InputMutationOperation {
  "UNKNOWN",
  "ADDED",
  "REMOVED",
  "RENAMED",
}

export type InputMutation = {
  operation: InputMutationOperation;
  node: DynamicContextNodeBase;
  slotIndex: number;
  slot: INodeSlot;
};

export class ContextService {

  constructor() {
    if (SERVICE) {
      throw new Error("ContextService was already instantiated.");
    }
  }

  onInputChanges(node: any, mutation: InputMutation) {
    const childCtxs = getConnectedOutputNodesAndFilterPassThroughs(
      node,
      node,
      0,
    ) as DynamicContextNodeBase[];
    for (const childCtx of childCtxs) {
      childCtx.handleUpstreamMutation(mutation);
    }
  }

  getDynamicContextInputsData(node: DynamicContextNodeBase) {
    return node
      .getContextInputsList()
      .map((input: INodeInputSlot, index: number) => ({
        name: stripContextInputPrefixes(input.name),
        type: String(input.type),
        index,
      }))
      .filter((i) => i.type !== "*");
  }

  getDynamicContextOutputsData(node: LGraphNode) {
    return node.outputs.map((output: INodeOutputSlot, index: number) => ({
      name: stripContextInputPrefixes(output.name),
      type: String(output.type),
      index,
    }));
  }
}

SERVICE = new ContextService();