Skip to content

Commit 63c4a79

Browse files
authored
Merge pull request #251 from azooKey/codex/add-function-to-export-for-google-form
Extract Google Form issue creation logic
2 parents 9df5667 + 3a74f46 commit 63c4a79

2 files changed

Lines changed: 86 additions & 76 deletions

File tree

.github/workflows/google_form.yaml

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

scripts/google_form_issue.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)