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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { ModuleRegistry } from "./moduleConfig"

export namespace AddonDef {
  export type AttributeType =
    | "string"
    | "bool"
    | "int32"
    | "int64"
    | "Uint32"
    | "Uint64"
    | "float64"
    | "array"
    | "buf"

  export type Attribute = {
    type: AttributeType
  }

  export type PropertyDefinition = {
    name: string
    attributes: Attribute
  }

  export type Command = {
    name: string
    property?: PropertyDefinition[]
    required?: string[]
    result?: {
      property: PropertyDefinition[]
      required?: string[]
    }
  }

  export type ApiEndpoint = {
    name: string
    property?: PropertyDefinition[]
  }

  export type Api = {
    property?: Record<string, Attribute>
    cmd_in?: Command[]
    cmd_out?: Command[]
    data_in?: ApiEndpoint[]
    data_out?: ApiEndpoint[]
    audio_frame_in?: ApiEndpoint[]
    audio_frame_out?: ApiEndpoint[]
    video_frame_in?: ApiEndpoint[]
    video_frame_out?: ApiEndpoint[]
  }

  export type Module = {
    name: string
    defaultProperty: Property
    api: Api
  }
}

type Property = {
  [key: string]: any
}

type Node = {
  name: string
  addon: string
  extensionGroup: string
  app: string
  property?: Property
}
type Command = {
  name: string // Command name
  dest: Array<Destination> // Destination connections
}

type Data = {
  name: string // Data type name
  dest: Array<Destination> // Destination connections
}

type AudioFrame = {
  name: string // Audio frame type name
  dest: Array<Destination> // Destination connections
}

type VideoFrame = {
  name: string // Video frame type name
  dest: Array<Destination> // Destination connections
}

type MsgConversion = {
  type: string // Type of message conversion
  rules: Array<{
    path: string // Path in the data structure
    conversionMode: string // Mode of conversion (e.g., "replace", "append")
    value?: string // Optional value for the conversion
    originalPath?: string // Optional original path for mapping
  }>
  keepOriginal?: boolean // Whether to keep the original data
}

type Destination = {
  app: string // Application identifier
  extension: string // Extension name
  msgConversion?: MsgConversion // Optional message conversion rules
}

type Connection = {
  app: string // Application identifier
  extension: string // Extension name
  cmd?: Array<Command> // Optional command connections
  data?: Array<Data> // Optional data connections
  audio_frame?: Array<AudioFrame> // Optional audio frame connections
  video_frame?: Array<VideoFrame> // Optional video frame connections
}

type Graph = {
  id: string
  autoStart: boolean
  nodes: Node[]
  connections: Connection[]
}

enum GraphConnProtocol {
  CMD = "cmd",
  DATA = "data",
  AUDIO_FRAME = "audio_frame",
  VIDEO_FRAME = "video_frame",
}

class GraphEditor {
  private static sharedApp: string = "localhost"

  /**
   * Set a shared app value.
   */
  static setApp(app: string): void {
    GraphEditor.sharedApp = app
  }

  /**
   * Add a node to the graph
   */
  static addNode(
    graph: Graph,
    addon: string,
    name: string,
    group: string = "default",
    properties: Record<string, any> = {},
  ): Node {
    if (graph.nodes.some((node) => node.name === name)) {
      throw new Error(
        `Node with name "${name}" already exists in graph "${graph.id}".`,
      )
    }

    const node: Node = {
      name,
      addon,
      extensionGroup: group,
      app: GraphEditor.sharedApp,
      property: properties,
    }

    graph.nodes.push(node)
    return node
  }

  /**
   * Remove a node from the graph
   */
  static removeNode(graph: Graph, nodeName: string): Node {
    const nodeIndex = graph.nodes.findIndex((node) => node.name === nodeName)
    if (nodeIndex === -1) {
      throw new Error(`Node "${nodeName}" not found in graph "${graph.id}".`)
    }
    const node = graph.nodes.splice(nodeIndex, 1)[0]
    return node;
  }

  /**
   * Update node properties
   */
  static updateNode(
    graph: Graph,
    nodeName: string,
    properties: Record<string, any>,
  ): void {
    const node = graph.nodes.find((node) => node.name === nodeName)
    if (!node) {
      throw new Error(`Node "${nodeName}" not found in graph "${graph.id}".`)
    }

    // Update properties (remove property if value is empty)
    node.property = {
      ...node.property,
      ...Object.fromEntries(
        Object.entries(properties).filter(([_, value]) => value !== ""),
      ),
    }
  }

  static updateNodeProperty(
    graph: Graph,
    nodeName: string,
    properties: Record<string, any>,
  ): boolean {
    const node = this.findNode(graph, nodeName)
    if (!node) return false

    node.property = {
      ...node.property,
      ...Object.fromEntries(
        Object.entries(properties).filter(([_, value]) => value !== ""),
      ),
    }

    return true
  }

  static removeNodeProperties(
    graph: Graph,
    nodeName: string,
    properties: string[],
  ): boolean {
    const node = this.findNode(graph, nodeName)
    if (!node) return false

    properties.forEach((prop) => {
      if (node.property) delete node.property[prop]
    })

    return true
  }

  /**
   * Add a connection to the graph across all protocols (cmd, data, audio_frame, video_frame)
   */
  static addConnection(
    graph: Graph,
    source: string,
    destination: string,
    protocolLabel: GraphConnProtocol,
    protocolName: string, // Name for the protocol object
  ): void {
    // Find the source connection in the graph
    let connection = graph.connections.find(
      (conn) =>
        conn.extension === source,
    )

    if (!connection) {
      // If no connection exists, create a new one
      connection = {
        app: GraphEditor.sharedApp,
        extension: source,
      }
      graph.connections.push(connection)
    }

    // Handle protocol-specific addition
    const protocolField = protocolLabel.toLowerCase() as keyof Connection
    if (!connection[protocolField]) {
      connection[protocolField] = [] as any
    }

    const protocolArray = connection[protocolField] as Array<
      Command | Data | AudioFrame | VideoFrame
    >

    // Check if the protocol object exists
    let protocolObject = protocolArray.find(
      (item) => item.name === protocolName,
    )
    if (!protocolObject) {
      protocolObject = {
        name: protocolName,
        dest: [],
      }
      protocolArray.push(protocolObject)
    }

    // Check if the destination already exists
    if (
      protocolObject.dest.some(
        (dest) => dest.extension === destination,
      )
    ) {
      throw new Error(
        `Destination "${destination}" already exists in protocol "${protocolLabel}" with name "${protocolName}".`,
      )
    }

    // Add the destination
    protocolObject.dest.push({
      app: GraphEditor.sharedApp,
      extension: destination,
    })
  }
  static addOrUpdateConnection(
    graph: Graph,
    source: string,
    destination: string,
    protocolLabel: GraphConnProtocol,
    protocolName: string, // Explicit name of the protocol object
  ): void {
    let connection = this.findConnection(graph, source)

    if (connection) {
      // Add or update destination in the existing connection
      const protocolField = protocolLabel.toLowerCase() as keyof Connection
      if (!connection[protocolField]) {
        connection[protocolField] = [] as any
      }

      const protocolArray = connection[protocolField] as Array<
        Command | Data | AudioFrame | VideoFrame
      >

      // Find the protocol object by name
      let protocolObject = protocolArray.find(
        (item) => item.name === protocolName,
      )
      if (!protocolObject) {
        // Create a new protocol object if it doesn't exist
        protocolObject = {
          name: protocolName,
          dest: [],
        }
        protocolArray.push(protocolObject)
      }

      // Check if the destination already exists
      if (
        !protocolObject.dest.some(
          (dest) => dest.extension === destination,
        )
      ) {
        // Add the new destination
        protocolObject.dest.push({
          app: GraphEditor.sharedApp,
          extension: destination,
        })
      }
    } else {
      // Add a new connection if none exists
      this.addConnection(
        graph,
        source,
        destination,
        protocolLabel,
        protocolName,
      )
    }
  }
  /**
 * Remove a connection from the graph across all protocols (cmd, data, audio_frame, video_frame)
 */
  static removeConnection(
    graph: Graph,
    source: string,
    destination?: string,
    protocolLabel?: GraphConnProtocol,
    protocolName?: string, // Optional name of the protocol object
  ): void {
    // Find the source connection in the graph
    const connectionIndex = graph.connections.findIndex(
      (conn) =>
        conn.extension === source,
    );

    if (connectionIndex === -1) {
      console.warn(`Source "${source}" not found in the graph. Operation ignored.`);
      return; // Exit the function if the connection does not exist
    }

    const connection = graph.connections[connectionIndex];

    // If protocolLabel is provided, handle protocol-specific removal
    if (protocolLabel) {
      const protocolField = protocolLabel.toLowerCase() as keyof Connection;
      const protocolArray = connection[protocolField] as Array<
        Command | Data | AudioFrame | VideoFrame
      >;

      if (!protocolArray) {
        console.warn(
          `Protocol "${protocolLabel}" does not exist for source "${source}". Operation ignored.`
        );
        return; // Exit the function if the protocol does not exist
      }

      const protocolObjectIndex = protocolArray.findIndex(
        (item) => item.name === protocolName,
      );

      if (protocolObjectIndex === -1) {
        console.warn(
          `Protocol object with name "${protocolName}" not found in protocol "${protocolLabel}". Operation ignored.`
        );
        return; // Exit the function if the protocol object does not exist
      }

      if (destination) {
        // Remove a specific destination
        protocolArray[protocolObjectIndex].dest = protocolArray[
          protocolObjectIndex
        ].dest.filter((dest) => dest.extension !== destination);

        // Remove the protocol object if it has no destinations
        if (protocolArray[protocolObjectIndex].dest.length === 0) {
          protocolArray.splice(protocolObjectIndex, 1);
        }
      } else {
        // Remove the entire protocol object
        protocolArray.splice(protocolObjectIndex, 1);
      }
    } else {
      // If no protocolLabel is provided, remove the entire connection
      graph.connections.splice(connectionIndex, 1);
    }

    // Clean up empty connections
    GraphEditor.removeEmptyConnections(graph);
  }

  static findNode(graph: Graph, nodeName: string): Node | null {
    return graph.nodes.find((node) => node.name === nodeName) || null
  }

  static findNodeByPredicate(graph: Graph, predicate: (node: Node) => boolean): Node | null {
    return graph.nodes.find(predicate) || null
  }

  static findConnection(
    graph: Graph,
    extension: string,
  ): Connection | null {
    return (
      graph.connections.find(
        (conn) =>
          conn.extension === extension,
      ) || null
    )
  }

  static removeEmptyConnections(graph: Graph): void {
    graph.connections = graph.connections.filter((connection) => {
      // Filter each protocol to remove empty destination objects
      connection.cmd = Array.isArray(connection.cmd)
        ? connection.cmd.filter((cmd) => cmd.dest?.length > 0)
        : undefined;
      if (!connection.cmd?.length) delete connection.cmd;

      connection.data = Array.isArray(connection.data)
        ? connection.data.filter((data) => data.dest?.length > 0)
        : undefined;
      if (!connection.data?.length) delete connection.data;

      connection.audio_frame = Array.isArray(connection.audio_frame)
        ? connection.audio_frame.filter((audio) => audio.dest?.length > 0)
        : undefined;
      if (!connection.audio_frame?.length) delete connection.audio_frame;

      connection.video_frame = Array.isArray(connection.video_frame)
        ? connection.video_frame.filter((video) => video.dest?.length > 0)
        : undefined;
      if (!connection.video_frame?.length) delete connection.video_frame;

      // Check if at least one protocol remains
      return (
        connection.cmd?.length ||
        connection.data?.length ||
        connection.audio_frame?.length ||
        connection.video_frame?.length
      );
    });
  }



  static removeNodeAndConnections(graph: Graph, addon: string): void {
    // Remove the node
    const node = this.removeNode(graph, addon)

    // Remove all connections involving this node
    graph.connections = graph.connections.filter((connection) => {
      const isSource =
        connection.extension === `${node.name}`
      const protocols = ["cmd", "data", "audio_frame", "video_frame"] as const

      protocols.forEach((protocol) => {
        if (connection[protocol]) {
          connection[protocol].forEach((item) => item.dest = item.dest.filter(d => d.extension !== node.name),
          )
        }
      })

      // Return true if connection still has content, false to remove it
      return (
        !isSource &&
        (connection.cmd?.length ||
          connection.data?.length ||
          connection.audio_frame?.length ||
          connection.video_frame?.length)
      )
    })
    // Clean up empty connections
    GraphEditor.removeEmptyConnections(graph);
  }

  /**
 * Link a tool to an LLM node by creating the appropriate connections.
 */
  static linkTool(graph: Graph, llmNode: Node, toolNode: Node, tool: ModuleRegistry.ToolModule): void {
    // Create the connection from the LLM node to the tool node
    GraphEditor.addOrUpdateConnection(
      graph,
      `${llmNode.name}`,
      `${toolNode.name}`,
      GraphConnProtocol.CMD,
      "tool_call"
    );

    // Create the connection from the tool node back to the LLM node
    GraphEditor.addOrUpdateConnection(
      graph,
      `${toolNode.name}`,
      `${llmNode.name}`,
      GraphConnProtocol.CMD,
      "tool_register"
    );

    const rtcModule = GraphEditor.findNodeByPredicate(graph, (node) => node.addon.includes("rtc"));
    if (toolNode.addon.includes("vision") && rtcModule) {
      // Create the connection from the RTC node to the tool node to deliver video frame
      GraphEditor.addOrUpdateConnection(
        graph,
        `${rtcModule.name}`,
        `${toolNode.name}`,
        GraphConnProtocol.VIDEO_FRAME,
        "video_frame"
      );
    }

    const messageCollector = GraphEditor.findNodeByPredicate(graph, ((node) => node.addon.includes("message_collector")))
    if (tool.options.outputContentText && messageCollector) {
      GraphEditor.addOrUpdateConnection(
        graph,
        `${toolNode.name}`,
        `${messageCollector.name}`,
        GraphConnProtocol.DATA,
        "content_data"
      )
    }
  }

  static enableRTCVideoSubscribe(graph: Graph, enabled: Boolean): void {
    const rtcNode = GraphEditor.findNodeByPredicate(graph, (node) => node.addon.includes("rtc"));
    if (!rtcNode) {
      throw new Error("RTC node not found in the graph.");
    }

    if (enabled) {
      GraphEditor.updateNodeProperty(graph, rtcNode.name, {
        subscribe_video_pix_fmt: 4,
        subscribe_video: true,
      });
    } else {
      GraphEditor.removeNodeProperties(graph, rtcNode.name, ["subscribe_video_pix_fmt", "subscribe_video"]);
    }
  }
}

export type { Graph, Node, Connection, Command, Destination }

export { GraphEditor, GraphConnProtocol as ProtocolLabel }