|
| 1 | +async function createIssueFromForm({ github, context, core, fetch }) { |
| 2 | + const payload = context.payload.client_payload; |
| 3 | + const content = payload.content; |
| 4 | + // Extract the word after "単語を入力してください:" if present |
| 5 | + const wordMatch = content.match(/^単語を入力してください:\s*(.+)$/m); |
| 6 | + const word = wordMatch ? wordMatch[1].trim() : null; |
| 7 | + |
| 8 | + // Extract supplementary info |
| 9 | + const supplementMatch = content.match(/^この単語について補足すべき情報があれば記載してください:\s*(.*)$/m); |
| 10 | + const supplement = supplementMatch ? supplementMatch[1].trim() : ''; |
| 11 | + |
| 12 | + let filteredContent = content; |
| 13 | + if (supplement) { |
| 14 | + try { |
| 15 | + const res = await fetch('https://models.github.ai/inference/chat/completions', { |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, |
| 20 | + }, |
| 21 | + body: JSON.stringify({ |
| 22 | + model: 'openai/gpt-4o', |
| 23 | + messages: [ |
| 24 | + { |
| 25 | + role: 'system', |
| 26 | + content: 'You check whether the user text is toxic. If toxic, rewrite it into Japanese cat-speak. Respond in JSON.' |
| 27 | + }, |
| 28 | + { role: 'user', content: supplement } |
| 29 | + ], |
| 30 | + response_format: { |
| 31 | + type: 'json_schema', |
| 32 | + json_schema: { |
| 33 | + name: 'toxicity', |
| 34 | + schema: { |
| 35 | + type: 'object', |
| 36 | + properties: { |
| 37 | + toxic: { type: 'boolean' }, |
| 38 | + cat: { type: 'string' } |
| 39 | + }, |
| 40 | + required: ['toxic', 'cat'], |
| 41 | + additionalProperties: false |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }) |
| 46 | + }); |
| 47 | + if (res.ok) { |
| 48 | + const data = await res.json(); |
| 49 | + const msg = data?.choices?.[0]?.message?.content; |
| 50 | + const result = JSON.parse(msg); |
| 51 | + core.info(`LLM verdict: ${JSON.stringify(result)}`); |
| 52 | + if (result.toxic) { |
| 53 | + filteredContent = content.replace( |
| 54 | + /^この単語について補足すべき情報があれば記載してください:\s*(.*)$/m, |
| 55 | + `この単語について補足すべき情報があれば記載してください: ${result.cat}` |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (err) { |
| 60 | + core.warning(`LLM filtering failed: ${err}`); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const titlePrefix = word |
| 65 | + ? `vocabulary: add 「${word}」` |
| 66 | + : 'Form response'; |
| 67 | + const title = `${titlePrefix} (${payload.filename})`; |
| 68 | + |
| 69 | + const body = [ |
| 70 | + 'Google Formに辞書追加のリクエストがありました。対応を検討してください。', |
| 71 | + '', |
| 72 | + '```', |
| 73 | + filteredContent, |
| 74 | + '```' |
| 75 | + ].join('\n'); |
| 76 | + await github.rest.issues.create({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + title, |
| 80 | + body |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +module.exports = { createIssueFromForm }; |
0 commit comments