accordproject / template-archive

Smart Legal Contracts & Templating System
https://accordproject.org/projects/cicero/
Apache License 2.0
281 stars 119 forks source link

Cicero-server not working offline: options are not passed to "Template.fromDirectory()" in "initTemplateInstance()" #734

Open mgiofinc opened 2 years ago

mgiofinc commented 2 years ago

Bug Report 🐛

When Template.fromDirectory() is called within initTemplateInstance() (in packages/cicero-server/app.js), the options are not passed. This in particular prevents to use cicero-server offline.

Expected Behavior

When "offline" option is set true in the request body, cicero-server does not try to update external models.

Current Behavior

When "offline" option is set true in the request body, cicero-server tries anyway to update external models.

Steps to Reproduce

  1. Install cicero-server as explained in the Readme.md
  2. Do the Draft request call (it will download external models)
  3. Go offline and do the following call:
    curl --request POST \
    --url http://localhost:6001/draft/latedeliveryandpenalty \
    --header 'accept: application/json' \
    --header 'content-type: application/json' \
    --data '{
    "data": {
        "$class": "org.accordproject.latedeliveryandpenalty.LateDeliveryAndPenaltyContract",
        "contractId": "ecd6257e-2ffe-4ef1-8a5c-38ca9084a829",
        "buyer": {
            "$class": "org.accordproject.cicero.contract.AccordParty",
            "partyId": "Dan"
        },
        "seller": {
            "$class": "org.accordproject.cicero.contract.AccordParty",
            "partyId": "Steve"
        },
        "forceMajeure": true,
        "penaltyDuration": {
            "$class": "org.accordproject.time.Duration",
            "amount": 9,
            "unit": "days"
        },
        "penaltyPercentage": 1000.5,
        "capPercentage": 88,
        "termination": {
            "$class": "org.accordproject.time.Duration",
            "amount": 5,
            "unit": "weeks"
        },
        "fractionalPart": "days"
    },
    "options": {
        "unquoteVariables": true,
        "offline": true    <------------------------------ ADDED OPTION
    }
    }'
  4. The following error is returned:
    Error: Failed to load model file. Job: https://models.accordproject.org/accordproject/contract.cto Details: Error: getaddrinfo EAI_AGAIN models.accordproject.org
    at PromisePoolExecutor.handleJobError [as errorHandler] (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@accordproject/concerto-core/lib/introspect/loaders/modelfiledownloader.js:31:11)
    at /usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@supercharge/promise-pool/dist/promise-pool-executor.js:154:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 0)
    at async PromisePoolExecutor.drainActiveTasks (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@supercharge/promise-pool/dist/promise-pool-executor.js:187:9)
    at async PromisePoolExecutor.drained (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@supercharge/promise-pool/dist/promise-pool-executor.js:177:9)
    at async PromisePoolExecutor.start (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@supercharge/promise-pool/dist/promise-pool-executor.js:85:16)
    at async APModelManager.updateExternalModels (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@accordproject/concerto-core/lib/modelmanager.js:276:36)
    at async APModelManager.addAPModelFiles (/usr/local/lib/node_modules/@accordproject/cicero-server/node_modules/@accordproject/ergo-compiler/lib/apmodelmanager.js:102:40)

Possible Implementation

Change initTemplateInstance() with something like:

async function initTemplateInstance(req) {
    let options = {};
    if(Object.prototype.hasOwnProperty.call(req.body,'options')) {
        options = req.body.options;
    }
    const template = await Template.fromDirectory(`${process.env.CICERO_DIR}/${req.params.template}`, options);
    return new Clause(template);
}
dselman commented 2 years ago

Thanks for the report. I agree that allowing Cicero server to be used offline would be useful. It sounds like adding a new environment variable to specify offline usage of the server would be a good idea?

mehmettokgoz commented 2 years ago

Thanks for the report @mgiofinc 🙌

This support is now added for cicero-server. See the new template loading function: cicero-server/app.js#L273. When we merge open PRs for other methods, all of them will allow you to pass options in the request body.

mgiofinc commented 2 years ago

Thanks for the update @mehmettokgoz.
Unfortunately it seems it only applies to /invoke/:template, but not to /trigger/:template, /parse/:template and /draft/:template.
Could it be extended also to them?

mehmettokgoz commented 2 years ago

@mgiofinc Yes, that's right. I didn't refactor trigger, parse and draft yet but they will be updated accordingly. Additionally, initialize, normalize, get, compile, and archive methods have this support natively. You can see the implementations from cicero/pulls.

mgiofinc commented 1 year ago

Hello and happy new year!

@mehmettokgoz I was wondering if it is there any update about the refactoring of /trigger/:template, /parse/:template and /draft/:template.

Thanks in advance.

AMalfez commented 1 year ago

Hi! Is someone still working on this? If not can i ? Thanks...

thepiyush-303 commented 4 months ago

Hello, I would love to contribute in this organization. can I ?