From 512ae6b5913d4b42a5175c06f2d2b24283c207fb Mon Sep 17 00:00:00 2001 From: Mina Choi Date: Mon, 26 Oct 2026 13:22:53 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=83=80=EB=A1=9C=20=EA=B0=81=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=E2=80=94=20=EC=B5=9C=EA=B7=BC=207?= =?UTF-8?q?=EC=9D=BC=EC=97=90=20=EC=93=B4=20=EA=B0=81=EC=9D=80=20=EC=A0=9C?= =?UTF-8?q?=EC=99=B8=ED=95=98=EA=B3=A0=20=EC=83=81=EC=9C=84=2015=EA=B0=9C?= =?UTF-8?q?=20=EC=95=88=EC=97=90=EC=84=9C=20=EB=A8=B9=EA=B1=B0=EB=A6=AC?= =?UTF-8?q?=C2=B7=EA=B3=84=EC=A0=88=C2=B7=EC=A7=91=20=EA=B0=81=EB=8F=84=20?= =?UTF-8?q?=EC=84=9E=EC=96=B4=20=EA=B3=A0=EB=A5=B4=EA=B2=8C(out/tarot-hist?= =?UTF-8?q?ory.json)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/scripts/card.mjs | 12 +++++++++++- admin/scripts/plan.mjs | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/admin/scripts/card.mjs b/admin/scripts/card.mjs index 32af089..c4e4bfc 100644 --- a/admin/scripts/card.mjs +++ b/admin/scripts/card.mjs @@ -6,7 +6,7 @@ // --post 면 다 그린 뒤 날씨각 오늘 피드에 바로 올리고, --post-wait 면 승인 대기로 저장한다 (관리자 서버 /api/post). // 지역은 고르지 않는다. 카드는 서울 예보로 만들고 카드에 지역 이름은 나가지 않는다 (src/cards/data.ts BASE_REGION) import { execFileSync } from 'node:child_process' -import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs' +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { resolve } from 'node:path' const SLIDES = { tarot: 5, ootd: 4, intro: 8 } @@ -65,6 +65,16 @@ if (topic !== null || feedback !== null || (!copy && AUTO_PLAN.includes(template console.log(`주제: ${body.topic}`) for (const [k, v] of Object.entries(body.copy)) console.log(` ${k}: ${v.replaceAll('\n', ' / ')}`) copy = Buffer.from(JSON.stringify({ ...current, ...body.copy })).toString('base64url') + // 타로: 오늘 고른 각을 적어 두면 다음 7일은 다른 각이 나온다 (scripts/plan.mjs recentCodes) + if (template === 'tarot') { + const file = resolve('out/tarot-history.json') + const today = new Date(Date.now() + 9 * 3600_000).toISOString().slice(0, 10) + const history = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : {} + const codes = ['c1', 'c2', 'c3'].map((k) => body.copy[k]).filter(Boolean) + history[today] = [...new Set([...(history[today] ?? []), ...codes])] + mkdirSync(resolve('out'), { recursive: true }) + writeFileSync(file, JSON.stringify(history, null, 1)) + } } { diff --git a/admin/scripts/plan.mjs b/admin/scripts/plan.mjs index 94c0e39..78ce408 100644 --- a/admin/scripts/plan.mjs +++ b/admin/scripts/plan.mjs @@ -1,7 +1,7 @@ // 카드 문구 기획: 템플릿 칸·오늘 날씨·주제·피드백을 Claude CLI(claude -p)에 넘겨 칸별 문구를 받는다. // 관리자 서버(/api/plan)와 npm run card --topic 이 같이 쓴다. API 키 없이 이 맥에 로그인된 Claude Code를 쓴다. import { execFile } from 'node:child_process' -import { readdirSync, readFileSync } from 'node:fs' +import { existsSync, readdirSync, readFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { resolve } from 'node:path' @@ -20,6 +20,19 @@ const questionOf = (topic) => { } // 날씨 타로 카드 = 날씨각 각 카드 (서비스와 같은 각 목록) const DECK = JSON.parse(readFileSync(resolve(import.meta.dirname, '../../backend/seed/index_definition.json'), 'utf8')).map((g) => ({ id: g.code, name: g.name, icon: g.icon, meaning: g.description })) +// 최근에 쓴 각은 빼서 매일 같은 카드가 나오지 않게 한다 (card.mjs 가 기획할 때마다 적는다) +const HISTORY_FILE = resolve(import.meta.dirname, '../out/tarot-history.json') +const HISTORY_DAYS = 7 +function recentCodes() { + if (!existsSync(HISTORY_FILE)) return [] + try { + const history = JSON.parse(readFileSync(HISTORY_FILE, 'utf8')) + const cutoff = Date.now() - HISTORY_DAYS * 86400_000 + return [...new Set(Object.entries(history).filter(([day]) => new Date(day).getTime() >= cutoff).flatMap(([, codes]) => codes))] + } catch { + return [] + } +} const categoryOf = (topic) => TOPICS.find((c) => topic.includes(c.name) || c.subtopics.some((s) => topic.includes(s))) const RULES = [ @@ -56,15 +69,25 @@ const TAROT_TONE = [ '키워드(k)는 4~8자 명사구("같이 걷는 시간", "먼저 보내는 한 줄"). 행운 포인트(l)는 시간·날씨·행동 한 줄 22자 이내("저녁 8시, 17도로 걷기 딱 좋아요").', ] -function tarotSection(topic) { +/** 오늘 고를 수 있는 각: 최근 7일에 쓴 각은 뺀다 (관리자가 고정한 카드는 남긴다) */ +function availableDeck(fixed = {}) { + const recent = new Set(recentCodes()) + const keep = new Set(fixed.cards ?? []) + return DECK.filter((c) => !recent.has(c.id) || keep.has(c.id)) +} + +function tarotSection(topic, fixed = {}) { + const deck = availableDeck(fixed) + const excluded = DECK.filter((c) => !deck.includes(c)).map((c) => c.name) return [ '', '## 각 카드 (이 목록에서만 고른다. card1·card2·card3에 서로 다른 code)', questionOf(topic) ? `표지 질문(question)은 이것을 쓴다(피드백이 바꾸라고 하면 바꾼다): ${questionOf(topic)}` : '표지 질문(question)은 주제에 맞는 "오늘 ~할까요?" 꼴 질문 하나를 쓴다.', - '오늘 날씨 데이터의 오늘_각_점수에서 점수가 높은 각(오늘 실제로 하기 좋은 각) 가운데 질문과 엮을 수 있는 것을 고른다. 세 장은 서로 다른 답(하나는 긍정, 하나는 조건부, 하나는 기다리기 같은 식)이 되게 고르고, 되도록 아침·낮·저녁(밤) 각에서 하나씩 골라 세 장이 하루로 이어지게 한다.', + '오늘 날씨 데이터의 오늘_각_점수에서 점수가 높은 편(상위 15개 안)인 각 가운데 질문과 엮을 수 있는 것을 고른다. 매일 같은 야외 운동 각(산책·자전거·등산·드라이브)만 반복하지 말고 먹거리·계절·집·동네 각도 섞어 날마다 다른 카드가 나오게 한다. 세 장은 서로 다른 답(하나는 긍정, 하나는 조건부, 하나는 기다리기 같은 식)이 되게 고르고, 되도록 아침·낮·저녁(밤) 각에서 하나씩 골라 세 장이 하루로 이어지게 한다.', + excluded.length ? `최근 7일에 쓴 각이라 오늘은 고를 수 없음(목록에서 뺐다): ${excluded.join(', ')}` : '', ...TAROT_TONE, '', - ...DECK.map((c) => `- ${c.id}: ${c.name} — ${c.meaning}`), + ...deck.map((c) => `- ${c.id}: ${c.name} — ${c.meaning}`), ] } @@ -86,23 +109,23 @@ function buildPrompt(ctx, { topic, feedback, fixed = {} }) { topic ? `## 주제: ${topic}` : `## 주제: 없음 — 다음 대주제·소주제 중 오늘 날씨와 어울리는 것을 하나 골라 topic에 "대주제 소주제"로 적고 그 주제로 쓴다: ${TOPICS.map((c) => `${c.name}(${c.subtopics.join('·')})`).join(', ')}`, feedback ? `## 피드백 (반드시 반영): ${feedback}` : '', '', - ...(isTarot(ctx) ? tarotSection(topic) : []), + ...(isTarot(ctx) ? tarotSection(topic, fixed) : []), fixed.question ? `## 질문은 관리자가 정했다(그대로 쓴다): ${fixed.question}` : '', fixed.cards?.length ? `## 카드는 관리자가 골랐다(card1·card2·card3 순서 그대로): ${fixed.cards.join(', ')}` : '', '모든 칸을 채운 JSON으로만 답한다. topic에는 쓴 주제를 짧게 적는다.', ].join('\n') } -function schema(ctx) { +function schema(ctx, fixed = {}) { const properties = { topic: { type: 'string' } } for (const f of ctx.fields) if (!(isTarot(ctx) && DECK_KEY.test(f.key))) properties[f.key] = { type: 'string' } - if (isTarot(ctx)) for (const k of ['card1', 'card2', 'card3']) properties[k] = { type: 'string', enum: DECK.map((c) => c.id) } + if (isTarot(ctx)) for (const k of ['card1', 'card2', 'card3']) properties[k] = { type: 'string', enum: availableDeck(fixed).map((c) => c.id) } return { type: 'object', properties, required: Object.keys(properties), additionalProperties: false } } /** @returns {Promise<{ topic: string, copy: Record }>} */ export function runPlan(ctx, { topic = '', feedback = '', fixed = {} } = {}) { - const args = ['-p', buildPrompt(ctx, { topic, feedback, fixed }), '--output-format', 'json', '--json-schema', JSON.stringify(schema(ctx)), '--tools', '', '--no-session-persistence', '--model', 'sonnet'] + const args = ['-p', buildPrompt(ctx, { topic, feedback, fixed }), '--output-format', 'json', '--json-schema', JSON.stringify(schema(ctx, fixed)), '--tools', '', '--no-session-persistence', '--model', 'sonnet'] return new Promise((done, fail) => { execFile('claude', args, { cwd: tmpdir(), timeout: 180_000, maxBuffer: 10 * 1024 * 1024 }, (err, stdout) => { if (err) return fail(new Error(`Claude CLI 실행 실패: ${err.message}. 터미널에서 claude 로그인 상태를 확인하세요.`))