@ghx-dev/core 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ghx",
3
3
  "description": "GitHub execution router for AI agents — 70 capabilities with deterministic routing and normalized output",
4
- "version": "0.2.2",
4
+ "version": "0.3.0",
5
5
  "author": {
6
6
  "name": "Arye Kogan"
7
7
  },
@@ -180,9 +180,97 @@ function createSafeCliCommandRunner(options) {
180
180
  };
181
181
  }
182
182
 
183
+ // src/core/auth/resolve-token.ts
184
+ import { statSync } from "fs";
185
+ import { mkdir, readFile, rename, unlink, writeFile } from "fs/promises";
186
+ import { homedir } from "os";
187
+ import { join } from "path";
188
+ var CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
189
+ var GH_AUTH_TIMEOUT_MS = 5e3;
190
+ function resolveHostname() {
191
+ return process.env.GH_HOST || "github.com";
192
+ }
193
+ function resolveCacheDir() {
194
+ const base = process.env.XDG_CACHE_HOME || join(homedir(), ".cache");
195
+ return join(base, "ghx", "tokens");
196
+ }
197
+ function resolveCachePath() {
198
+ return join(resolveCacheDir(), resolveHostname());
199
+ }
200
+ async function readCachedToken() {
201
+ const cachePath = resolveCachePath();
202
+ try {
203
+ const stat = statSync(cachePath);
204
+ if (Date.now() - stat.mtimeMs > CACHE_TTL_MS) {
205
+ return null;
206
+ }
207
+ } catch {
208
+ return null;
209
+ }
210
+ try {
211
+ const content = await readFile(cachePath, "utf8");
212
+ const trimmed = content.trim();
213
+ return trimmed.length > 0 ? trimmed : null;
214
+ } catch {
215
+ return null;
216
+ }
217
+ }
218
+ async function writeCachedToken(token) {
219
+ const cacheDir = resolveCacheDir();
220
+ const cachePath = resolveCachePath();
221
+ const tmpPath = `${cachePath}.${process.pid}.tmp`;
222
+ await mkdir(cacheDir, { recursive: true, mode: 448 });
223
+ await writeFile(tmpPath, token, { mode: 384 });
224
+ await rename(tmpPath, cachePath);
225
+ }
226
+ async function resolveFromGhCli() {
227
+ const runner = createSafeCliCommandRunner();
228
+ const hostname = resolveHostname();
229
+ const args = hostname === "github.com" ? ["auth", "token"] : ["auth", "token", "--hostname", hostname];
230
+ try {
231
+ const result = await runner.run("gh", args, GH_AUTH_TIMEOUT_MS);
232
+ if (result.exitCode !== 0) {
233
+ return null;
234
+ }
235
+ const token = result.stdout.trim();
236
+ return token.length > 0 ? token : null;
237
+ } catch {
238
+ return null;
239
+ }
240
+ }
241
+ async function resolveGithubToken() {
242
+ const githubToken = process.env.GITHUB_TOKEN?.trim();
243
+ if (githubToken && githubToken.length > 0) {
244
+ return { token: githubToken, source: "env" };
245
+ }
246
+ const ghToken = process.env.GH_TOKEN?.trim();
247
+ if (ghToken && ghToken.length > 0) {
248
+ return { token: ghToken, source: "env" };
249
+ }
250
+ const cached = await readCachedToken();
251
+ if (cached !== null) {
252
+ return { token: cached, source: "cache" };
253
+ }
254
+ const ghCliToken = await resolveFromGhCli();
255
+ if (ghCliToken !== null) {
256
+ await writeCachedToken(ghCliToken).catch(() => {
257
+ });
258
+ return { token: ghCliToken, source: "gh-cli" };
259
+ }
260
+ throw new Error(
261
+ "GitHub token not found. Set GITHUB_TOKEN or GH_TOKEN environment variable, or authenticate with: gh auth login"
262
+ );
263
+ }
264
+ async function invalidateTokenCache() {
265
+ try {
266
+ await unlink(resolveCachePath());
267
+ } catch {
268
+ }
269
+ }
270
+
183
271
  // src/core/registry/index.ts
184
272
  import { readdirSync, readFileSync } from "fs";
185
- import { dirname, extname, join } from "path";
273
+ import { dirname, extname, join as join2 } from "path";
186
274
  import { fileURLToPath } from "url";
187
275
  import { load as parseYaml } from "js-yaml";
188
276
 
@@ -381,7 +469,7 @@ var operationCardSchema = {
381
469
  var validateCard = ajv.compile(operationCardSchema);
382
470
  function cardDirectory() {
383
471
  const currentDir = dirname(fileURLToPath(import.meta.url));
384
- return join(currentDir, "cards");
472
+ return join2(currentDir, "cards");
385
473
  }
386
474
  function loadCardsFromYaml() {
387
475
  const directory = cardDirectory();
@@ -472,7 +560,7 @@ function loadCardsFromYaml() {
472
560
  return a.localeCompare(b);
473
561
  });
474
562
  return entries.map((entry) => {
475
- const filePath = join(directory, entry);
563
+ const filePath = join2(directory, entry);
476
564
  const raw = readFileSync(filePath, "utf8");
477
565
  const parsed = parseYaml(raw);
478
566
  const validation = validateOperationCard(parsed);
@@ -745,7 +833,7 @@ function createLogger(config) {
745
833
  }
746
834
  function write(level, msg, ctx) {
747
835
  if (levelIndex(level) < minIndex) return;
748
- const version = true ? "0.2.2" : "unknown";
836
+ const version = true ? "0.3.0" : "unknown";
749
837
  const entry = {
750
838
  ts: (/* @__PURE__ */ new Date()).toISOString(),
751
839
  pid: config.pid,
@@ -6116,6 +6204,8 @@ function createGithubClient(transport) {
6116
6204
 
6117
6205
  export {
6118
6206
  createSafeCliCommandRunner,
6207
+ resolveGithubToken,
6208
+ invalidateTokenCache,
6119
6209
  listOperationCards,
6120
6210
  getOperationCard,
6121
6211
  extractArrayItemHints,
@@ -6129,4 +6219,4 @@ export {
6129
6219
  createGithubClientFromToken,
6130
6220
  createGithubClient
6131
6221
  };
6132
- //# sourceMappingURL=chunk-T3L2VDOS.js.map
6222
+ //# sourceMappingURL=chunk-7A73AXLY.js.map