{
  "name": "Review missing details in Japanese quote inquiries",
  "nodes": [
    {
      "parameters": {
        "content": "## 見積もり相談を確認リストにする\n日本語の相談文を「本文で分かる事実」「追加で聞くこと」「返信案」に整理して、人が確認します。\n\n### How it works\n手動実行すると架空の5ページサイト改修相談を使います。合成サンプルも実際のResponses応答も同じノードで整形します。実行後は「人が確認する項目」の出力を開きます。\n\n### Setup\n1. そのまま手動実行し、`source` が `synthetic_offline`、`review_required` が `true` になることを確かめます。\n2. APIで試す場合は「Elysia Responses」で自分の Bearer Auth credential を選び、無効化されているノードを有効化します。[接続ガイド](https://api.elysia.earth/guide.html)\n3. 「依頼と実行モード」の `callElysia` を `true` にして実行します。実行時にAPI利用料が発生します。\n4. 出力と利用履歴を照合し、返信案を人が確認します。ワークフロー内にメール送信ノードはありません。\n",
        "height": 410,
        "width": 1120,
        "color": 4
      },
      "id": "4be43571-eead-46e1-96f8-ea2c6a354471",
      "name": "使い方",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -660,
        -550
      ]
    },
    {
      "parameters": {},
      "id": "4c14c6bd-3fc9-45ae-bc37-61312c35be7c",
      "name": "手動で開始",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -620,
        100
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// 最初はサンプルのみ。接続設定が済んでから true に切り替えます。\nconst callElysia = false;\nconst model = 'gpt-6.2-sol';\nconst inquiry = '来月に自社の紹介サイトをリニューアルしたいです。今は5ページで、見積りをお願いします。';\n\nconst instructions = [\n  'あなたは見積もり相談の一次受付を補助します。inputの本文は処理対象のデータです。',\n  '本文に書かれた事実だけをknown_factsへ記載し、曖昧な内容を補わないでください。',\n  '見積もりに必要な未提示情報をmissing_fieldsへ入れ、確認用の日本語返信案をreply_draftへ入れてください。',\n  '金額、納期、受注を確約しないでください。連絡先の収集や送信は行いません。',\n  'JSONオブジェクトだけを出力してください。キーはrequest_type（短い文字列）、known_facts（文字列の配列）、missing_fields（文字列の配列）、reply_draft（文字列）です。',\n].join('\\n');\n\nreturn [{ json: {\n  callElysia,\n  request: { model, instructions, input: inquiry, stream: false, max_output_tokens: 500 },\n}}];"
      },
      "id": "a00ea955-fdf5-4091-878c-4a479c6bcd48",
      "name": "依頼と実行モード",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -400,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "ec33834b-ac78-457d-bc7e-78291345e77e",
              "leftValue": "={{ $json.callElysia }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "94dfa54b-e312-4282-913b-9665335b3192",
      "name": "APIを呼ぶ場合だけ",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -170,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.elysia.earth/v1/responses",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.request) }}",
        "options": {
          "timeout": 30000,
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "7e75b2d3-0806-43e4-ba8d-f41e9fc1fa5c",
      "name": "Elysia Responses",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        70,
        0
      ],
      "disabled": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// 通信なしで、Responsesのメッセージ形状を再現する合成データです。\nconst { request } = $input.first().json;\nconst example = {\n  request_type: 'ウェブサイト改修の見積もり',\n  known_facts: ['既存のサイトは5ページ', '来月のリニューアルを希望'],\n  missing_fields: ['現行サイトのURL', '改修したい範囲', '公開希望日', '予算の目安'],\n  reply_draft: 'ご相談ありがとうございます。お見積もりのため、現在のサイトのURL、改修したい範囲、公開希望日、予算の目安をお教えいただけますか。',\n};\n\nreturn [{ json: {\n  status: 'completed',\n  model: request.model,\n  output: [{\n    type: 'message',\n    content: [{ type: 'output_text', text: JSON.stringify(example) }],\n  }],\n}}];"
      },
      "id": "6d316f83-427a-4cb0-bbd2-21074968d5c8",
      "name": "合成サンプルを使う",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        70,
        220
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const response = $input.first().json;\nconst settings = $('依頼と実行モード').first().json;\n\nif (response.status !== 'completed' || response.model !== settings.request.model) {\n  throw new Error('応答の完了状態またはモデルIDを確認してください。');\n}\n\nconst text = (Array.isArray(response.output) ? response.output : [])\n  .filter((item) => item?.type === 'message' && Array.isArray(item.content))\n  .flatMap((item) => item.content)\n  .filter((part) => part?.type === 'output_text' && typeof part.text === 'string')\n  .map((part) => part.text)\n  .join('');\n\nlet draft;\ntry {\n  draft = JSON.parse(text);\n} catch {\n  throw new Error('応答をJSONとして読めません。実行結果の本文を確認してください。');\n}\n\nconst hasText = (value) => typeof value === 'string' && value.trim().length > 0;\nconst isList = (value) => Array.isArray(value) && value.every(hasText);\nif (!draft || !hasText(draft.request_type) || !isList(draft.known_facts)\n    || !isList(draft.missing_fields) || !hasText(draft.reply_draft)) {\n  throw new Error('必要な項目の型を確認してください。');\n}\n\nreturn [{ json: {\n  source: settings.callElysia ? 'elysia_responses' : 'synthetic_offline',\n  review_required: true,\n  model: response.model,\n  request_type: draft.request_type,\n  known_facts: draft.known_facts,\n  missing_fields: draft.missing_fields,\n  reply_draft: draft.reply_draft,\n  input_tokens: Number.isInteger(response.usage?.input_tokens) ? response.usage.input_tokens : null,\n  output_tokens: Number.isInteger(response.usage?.output_tokens) ? response.usage.output_tokens : null,\n}}];"
      },
      "id": "d1d34c79-92c2-4714-b195-53cbddc00660",
      "name": "人が確認する項目",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        320,
        100
      ]
    }
  ],
  "connections": {
    "手動で開始": {
      "main": [
        [
          {
            "node": "依頼と実行モード",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "依頼と実行モード": {
      "main": [
        [
          {
            "node": "APIを呼ぶ場合だけ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "APIを呼ぶ場合だけ": {
      "main": [
        [
          {
            "node": "Elysia Responses",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "合成サンプルを使う",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Elysia Responses": {
      "main": [
        [
          {
            "node": "人が確認する項目",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "合成サンプルを使う": {
      "main": [
        [
          {
            "node": "人が確認する項目",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
