Repository navigation
Expand file tree
/
Copy pathinit-env.js
More file actions
218 lines (190 loc) · 6.65 KB
/
Copy pathinit-env.js
File metadata and controls
218 lines (190 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const ROOT_DIR = path.dirname(__filename);
const SKILLS_DIR = path.join(ROOT_DIR, 'skills');
export const SKILL_ENV_SPEC = {
'sci-search': {
description: 'Literature search: Web of Science, PubMed/NCBI, and optional Zotero handoff.',
vars: {
WOS_API_KEY: 'Web of Science Starter API key. Optional; enables WoS search.',
NCBI_API_KEY: 'NCBI E-utilities API key. Optional; increases PubMed rate limits.',
NCBI_EMAIL: 'Contact email sent to NCBI E-utilities. Optional but recommended by NCBI.',
NCBI_TOOL: 'NCBI tool name. Optional; defaults to sci-search.',
ZOTERO_API_KEY: 'Zotero API key. Optional; shared with sci-zotero if configured.',
ZOTERO_USER_ID: 'Zotero personal library user ID. Optional; shared with sci-zotero if configured.'
}
},
'sci-zotero': {
description: 'Zotero library integration.',
vars: {
ZOTERO_API_KEY: 'Zotero API key. Required for Zotero operations.',
ZOTERO_USER_ID: 'Zotero personal library user ID. Use this or ZOTERO_GROUP_ID.',
ZOTERO_GROUP_ID: 'Zotero group library ID. Use this or ZOTERO_USER_ID.'
}
},
'sci-ppt': {
description: 'Academic PPT generation with optional AI parsing.',
vars: {
ANTHROPIC_API_KEY: 'Anthropic API key. Optional; used by the AI parser when available.',
OPENAI_API_KEY: 'OpenAI API key. Optional fallback for the AI parser.'
}
}
};
function parseEnv(content) {
const values = {};
for (const rawLine of content.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line || line.startsWith('#') || !line.includes('=')) {
continue;
}
const idx = line.indexOf('=');
const key = line.slice(0, idx).trim();
const value = line.slice(idx + 1).trim().replace(/^['"]|['"]$/g, '');
values[key] = value;
}
return values;
}
function serializeEnv(skillName, spec, values) {
const lines = [
'# Aut_Sci_Write per-skill environment file.',
'# This file may contain API keys. Do not commit, publish, or redistribute it.',
`# Skill: ${skillName}`,
`# Purpose: ${spec.description}`,
''
];
for (const [key, help] of Object.entries(spec.vars)) {
lines.push(`# ${help}`);
lines.push(`${key}=${values[key] || ''}`);
lines.push('');
}
return lines.join('\n');
}
function ensureIgnoreEntry(filePath, entry) {
let content = '';
if (fs.existsSync(filePath)) {
content = fs.readFileSync(filePath, 'utf8');
}
const entries = new Set(
content
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
);
if (entries.has(entry)) {
return false;
}
const prefix = content && !content.endsWith('\n') ? '\n' : '';
fs.appendFileSync(filePath, `${prefix}${entry}\n`, 'utf8');
return true;
}
function readInstalledEnvValues(installedSkillNames) {
const envBySkill = {};
const valuesByKey = {};
const conflicts = [];
for (const skillName of installedSkillNames) {
const envPath = path.join(SKILLS_DIR, skillName, '.env');
const values = fs.existsSync(envPath)
? parseEnv(fs.readFileSync(envPath, 'utf8'))
: {};
envBySkill[skillName] = values;
for (const [key, value] of Object.entries(values)) {
if (!value) {
continue;
}
if (valuesByKey[key] === undefined) {
valuesByKey[key] = { value, source: `${skillName}/.env` };
} else if (valuesByKey[key].value !== value) {
// Same key set to different non-empty values in multiple skills.
// The first-seen value wins; warn so the user knows which is used.
conflicts.push(
`${key}: keeping ${valuesByKey[key].source}, ignoring different value in ${skillName}/.env`
);
}
}
}
return { envBySkill, valuesByKey, conflicts };
}
export function initializeSkillEnvs() {
if (!fs.existsSync(SKILLS_DIR)) {
return {
created: [],
updated: [],
synced: [],
conflicts: [],
skipped: true,
message: '[Aut_Sci_Write] No skills directory found; skipped .env initialization.'
};
}
const installedSkillNames = Object.keys(SKILL_ENV_SPEC)
.filter((skillName) => fs.existsSync(path.join(SKILLS_DIR, skillName)));
if (installedSkillNames.length === 0) {
return {
created: [],
updated: [],
synced: [],
conflicts: [],
skipped: true,
message: '[Aut_Sci_Write] No configurable skills found; skipped .env initialization.'
};
}
const { envBySkill, valuesByKey, conflicts } = readInstalledEnvValues(installedSkillNames);
const created = [];
const updated = [];
const synced = [];
ensureIgnoreEntry(path.join(ROOT_DIR, '.gitignore'), '.env');
for (const skillName of installedSkillNames) {
const skillDir = path.join(SKILLS_DIR, skillName);
const envPath = path.join(skillDir, '.env');
const gitignorePath = path.join(skillDir, '.gitignore');
const spec = SKILL_ENV_SPEC[skillName];
const values = { ...envBySkill[skillName] };
const existed = fs.existsSync(envPath);
ensureIgnoreEntry(gitignorePath, '.env');
for (const key of Object.keys(spec.vars)) {
if (!values[key] && valuesByKey[key]?.value) {
values[key] = valuesByKey[key].value;
synced.push(`${key}: ${valuesByKey[key].source} -> ${skillName}/.env`);
}
}
const next = serializeEnv(skillName, spec, values);
const previous = existed ? fs.readFileSync(envPath, 'utf8') : '';
if (!existed || previous !== next) {
fs.writeFileSync(envPath, next, 'utf8');
(existed ? updated : created).push(path.relative(ROOT_DIR, envPath));
}
}
let message = '[Aut_Sci_Write] Per-skill .env files are already initialized.';
if (created.length || updated.length || synced.length) {
message = '[Aut_Sci_Write] Per-skill .env initialization complete.';
}
return {
created,
updated,
synced,
conflicts,
skipped: false,
message
};
}
function printResult(result) {
console.log(result.message);
if (result.created.length) {
console.log(` Created: ${result.created.join(', ')}`);
}
if (result.updated.length) {
console.log(` Updated: ${result.updated.join(', ')}`);
}
if (result.synced.length) {
console.log(` Reused duplicate config: ${result.synced.join('; ')}`);
}
if (result.conflicts?.length) {
console.log(` Warning: conflicting values for shared keys: ${result.conflicts.join('; ')}`);
}
console.log(' Notice: .env files may contain API keys. Do not commit, publish, or redistribute them.');
}
if (path.resolve(process.argv[1] || '') === __filename) {
printResult(initializeSkillEnvs());
}