Explorer

definy.mcp-create-snapshot

description
The definy_create_snapshot MCP tool
type
Fn(arguments: @definy.json, request: @http.request) -> @definy.service-io
expression
fn(arguments$0, request$1) =>
  let project$2 = match @definy.json-get-string(value: $0, key: "project") {
      #some(text$3) =>
        $3,
      #none(_) =>
        ""
    };
  let branch$4 = match @definy.json-get-string(value: $0, key: "branch") {
      #some(text$5) =>
        $5,
      #none(_) =>
        ""
    };
  let message$6 = match @definy.json-get-string(value: $0, key: "message") {
      #some(text$7) =>
        $7,
      #none(_) =>
        "Change parts"
    };
  let module_read$29 = @definy.mcp-read-module-changes(items: @definy.json-get-array(value: $0, key: "modules"));
  let module_changes$30 = match $29 {
      #ok(list$31) =>
        $31,
      #error(message$33) =>
        []
    };
  let project_description$32 = @definy.json-get-string(value: $0, key: "project_description");
  let project_descriptions$48 = @definy.mcp-read-descriptions(value: @definy.json-get(value: $0, key: "project_descriptions"));
  let check$42 = match @definy.json-get(value: $0, key: "check") {
      #some(value$43) =>
        match $43 {
          #boolean(flag$44) =>
            $44,
          _ =>
            false
        },
      #none(_) =>
        false
    };
  match @core.or(left: @core.equal(left: $2, right: ""), right: @core.equal(left: $4, right: "")) {
    true =>
      @definy.mcp-text-error(text: "project and branch are required"),
    false =>
      match @definy.mcp-read-part-changes(items: @definy.json-get-array(value: $0, key: "changes")) {
        #error(message$8) =>
          @definy.mcp-text-error(text: $8),
        #ok(changes$9) =>
          match @core.and(
              left: @core.equal(left: @core.list-length(value: $9), right: 0),
              right: @core.and(
                left: match $29 {
                  #ok(list$40) =>
                    @core.equal(left: @core.list-length(value: $40), right: 0),
                  #error(message$41) =>
                    false
                },
                right: @core.and(
                  left: match $32 {
                    #some(text$38) =>
                      false,
                    #none(_) =>
                      true
                  },
                  right: @core.equal(left: @core.list-length(value: $48), right: 0)
                )
              )
            ) {
            true =>
              @definy.mcp-text-error(
                text: "give changes (a list of {part, type?, expression?, description?, delete?}), modules or project_description(s)"
              ),
            false =>
              match $29 {
                #error(message$34) =>
                  @definy.mcp-text-error(text: $34),
                #ok(read$39) =>
                  @definy.mcp-with-account(
                    then: fn(account$10) =>
                      @definy.read-branch-snapshot(
                        project: $2,
                        branch: $4,
                        then: fn(commit$11, commits$12) =>
                          match $11 {
                            #none(_) =>
                              @definy.mcp-text-error(
                                text: @std.string-join(
                                  items: ["Branch ", $4, " of project ", $2, " was not found (definy_create_branch makes one)"],
                                  separator: ""
                                )
                              ),
                            #some(found$13) =>
                              let stored$14 = @definy.snapshot-parts(modules: $13.commit.modules, index: 0);
                              @definy.read-part-contents(
                                parts: @core.list-fold(
                                  T: @definy.mcp-part-change,
                                  A: List<@definy.stored-snapshot-part>,
                                  reducer: fn(acc$15, item$16) =>
                                    match @definy.stored-part-find(parts: $14, name: $16.part, index: 0) {
                                      #some(part$17) =>
                                        @core.list-append(list: $15, item: $17),
                                      #none(_) =>
                                        $15
                                    },
                                  init: [],
                                  xs: $9
                                ),
                                then: fn(parts$18) =>
                                  match @definy.mcp-change-uploads(changes: $9, olds: $18) {
                                    #error(message$19) =>
                                      @definy.mcp-text-error(text: $19),
                                    #ok(uploads$20) =>
                                      @definy.store-write-contents(
                                        items: @core.list-fold(
                                          T: @definy.mcp-part-upload,
                                          A: List<@definy.content-item>,
                                          reducer: fn(acc$21, item$22) =>
                                            match $22.content {
                                              #some(content$23) =>
                                                @core.list-append(list: $21, item: $23),
                                              #none(_) =>
                                                $21
                                            },
                                          init: [],
                                          xs: $20
                                        ),
                                        then: fn(stored$24) =>
                                          @definy.read-snapshot-content(
                                            event_hash: $13.event_hash,
                                            then: fn(commit$25) =>
                                              match $25 {
                                                #none(_) =>
                                                  @definy.mcp-text-error(text: "The latest snapshot of the branch could not be read"),
                                                #some(content$26) =>
                                                  match @definy.apply-module-changes(modules: @definy.apply-part-changes(modules: $26.modules, uploads: $20), changes: $30) {
                                                    #error(message$35) =>
                                                      @definy.mcp-text-error(text: $35),
                                                    #ok(modules$36) =>
                                                      let content$47 = #project_commit<@core.event-content>({
                                                          project_name: $26.project_name,
                                                          branch: $26.branch,
                                                          project_description: @definy.localized-texts-update(texts: $26.project_description, english: $32, others: $48),
                                                          message: $6,
                                                          parent_commit_hash: #some($13.event_hash),
                                                          merged_commit_hash: #none,
                                                          modules: $36
                                                        });
                                                      match $42 {
                                                        true =>
                                                          @definy.mcp-check-content(
                                                            account: $10,
                                                            after_time_ms: $13.commit.time_ms,
                                                            content: $47,
                                                            then: fn(errors$45) =>
                                                              match @core.list-get(list: $45, index: 0) {
                                                                #none(_) =>
                                                                  @definy.mcp-text(text: "No errors: the type checker accepts this snapshot (no snapshot was taken)"),
                                                                #some(first$46) =>
                                                                  @definy.mcp-text-error(text: @std.string-join(items: $45, separator: "\n"))
                                                              }
                                                          ),
                                                        false =>
                                                          @definy.mcp-submit-content(
                                                            account: $10,
                                                            after_time_ms: $13.commit.time_ms,
                                                            content: $47,
                                                            then: fn(accepted$27, text$28) =>
                                                              match $27 {
                                                                true =>
                                                                  @definy.mcp-text(
                                                                    text: @std.string-join(
                                                                      items: [
                                                                        "Took a snapshot of ",
                                                                        @std.number-to-string(value: @core.list-length(value: $9)),
                                                                        " part changes",
                                                                        match @core.equal(left: @core.list-length(value: $30), right: 0) {
                                                                          true =>
                                                                            "",
                                                                          false =>
                                                                            @std.string-join(
                                                                              items: [" and ", @std.number-to-string(value: @core.list-length(value: $30)), " module changes"],
                                                                              separator: ""
                                                                            )
                                                                        },
                                                                        " on branch ",
                                                                        $4,
                                                                        " of project ",
                                                                        $2,
                                                                        "\n",
                                                                        $28
                                                                      ],
                                                                      separator: ""
                                                                    )
                                                                  ),
                                                                false =>
                                                                  @definy.mcp-text-error(text: $28)
                                                              }
                                                          )
                                                      }
                                                  }
                                              }
                                          )
                                      )
                                  }
                              )
                          }
                      )
                  )
              }
          }
      }
  }
visibility
public
definy.mcp-create-snapshot (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: qxkX3ZYA_YAmHF6t239GfFik5BR5YjUgxCo9ZpfLmLA
content hash: Weysk6U9SUOc_kxtLhJOWOfz5-CrvN6nea0Px6Zf5tc