MCPcopy Create free account
hub / github.com/code100x/mobile-magic / ArtifactProcessor

Class ArtifactProcessor

mobile-magic/apps/worker/parser.ts:12–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10*/
11
12export class ArtifactProcessor {
13 public currentArtifact: string;
14 private onFileContent: (filePath: string, fileContent: string) => void;
15 private onShellCommand: (shellCommand: string) => void;
16
17 constructor(currentArtifact: string, onFileContent: (filePath: string, fileContent: string) => void, onShellCommand: (shellCommand: string) => void) {
18 this.currentArtifact = currentArtifact;
19 this.onFileContent = onFileContent;
20 this.onShellCommand = onShellCommand;
21 }
22
23 append(artifact: string) {
24 this.currentArtifact += artifact;
25 }
26
27 parse() {
28 const latestActionStart = this.currentArtifact.split("\n").findIndex((line) => line.includes("<boltAction type="));
29 const latestActionEnd = this.currentArtifact.split("\n").findIndex((line) => line.includes("</boltAction>")) ?? (this.currentArtifact.split("\n").length - 1);
30
31 if (latestActionStart === -1) {
32 return;
33 }
34
35 const latestActionType = this.currentArtifact.split("\n")[latestActionStart].split("type=")[1].split(" ")[0].split(">")[0];
36 const latestActionContent = this.currentArtifact.split("\n").slice(latestActionStart, latestActionEnd + 1).join("\n");
37
38 try {
39 if (latestActionType === "\"shell\"") {
40 let shellCommand = latestActionContent.split('\n').slice(1).join('\n');
41 if (shellCommand.includes("</boltAction>")) {
42 shellCommand = shellCommand.split("</boltAction>")[0];
43 this.currentArtifact = this.currentArtifact.split(latestActionContent)[1];
44 this.onShellCommand(shellCommand);
45 }
46 } else if (latestActionType === "\"file\"") {
47 const filePath = this.currentArtifact.split("\n")[latestActionStart].split("filePath=")[1].split(">")[0];
48 let fileContent = latestActionContent.split("\n").slice(1).join("\n");
49 if (fileContent.includes("</boltAction>")) {
50 fileContent = fileContent.split("</boltAction>")[0];
51 this.currentArtifact = this.currentArtifact.split(latestActionContent)[1];
52 this.onFileContent(filePath.split("\"")[1], fileContent);
53 }
54 }
55 } catch(e) {}
56 }
57}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected