@lovelybunch/api 1.0.18 → 1.0.21

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,12 +1,48 @@
1
+ import { homedir } from 'os';
2
+ import { join } from 'path';
3
+ import { existsSync, readFileSync } from 'fs';
4
+ // Function to get global config API key as fallback
5
+ function getGlobalApiKey(provider) {
6
+ try {
7
+ const platform = process.platform;
8
+ let configDir;
9
+ if (platform === 'win32') {
10
+ configDir = join(process.env.APPDATA || homedir(), 'coconuts');
11
+ }
12
+ else if (platform === 'darwin') {
13
+ configDir = join(homedir(), 'Library', 'Application Support', 'coconuts');
14
+ }
15
+ else {
16
+ configDir = join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'coconuts');
17
+ }
18
+ const configFile = join(configDir, 'config.json');
19
+ if (!existsSync(configFile)) {
20
+ return null;
21
+ }
22
+ const config = JSON.parse(readFileSync(configFile, 'utf-8'));
23
+ return config.apiKeys?.[provider] || null;
24
+ }
25
+ catch (error) {
26
+ // Silently fail and return null
27
+ return null;
28
+ }
29
+ }
1
30
  export async function POST(c) {
2
31
  try {
3
32
  const { message, model, context, contextContent } = await c.req.json();
4
33
  if (!message) {
5
34
  return c.json({ error: "Message is required" }, 400);
6
35
  }
7
- const openRouterKey = process.env.OPENROUTER_API_KEY;
36
+ // Try environment variable first, then global config
37
+ let openRouterKey = process.env.OPENROUTER_API_KEY;
38
+ if (!openRouterKey) {
39
+ openRouterKey = getGlobalApiKey('openrouter');
40
+ }
8
41
  if (!openRouterKey) {
9
- return c.json({ error: "OpenRouter API key not configured" }, 500);
42
+ return c.json({
43
+ error: "OpenRouter API key not configured",
44
+ hint: "Set OPENROUTER_API_KEY environment variable or run 'coconut config set-key -p openrouter' to configure globally"
45
+ }, 500);
10
46
  }
11
47
  const systemPrompt = getSystemPrompt(context, contextContent);
12
48
  const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
@@ -1,3 +1,4 @@
1
+ import { config as dotenvConfig } from 'dotenv';
1
2
  import { serve } from '@hono/node-server';
2
3
  import { createNodeWebSocket } from '@hono/node-ws';
3
4
  import { Hono } from 'hono';
@@ -9,6 +10,8 @@ import { getGlobalTerminalManager } from './lib/terminal/global-manager.js';
9
10
  import { fileURLToPath } from 'url';
10
11
  const __filename = fileURLToPath(import.meta.url);
11
12
  const __dirname = path.dirname(__filename);
13
+ // Load environment variables from .env file in project root
14
+ dotenvConfig({ path: path.resolve(__dirname, '../../../.env') });
12
15
  const app = new Hono();
13
16
  // Enable CORS for development
14
17
  app.use('/api/*', cors({
package/dist/server.js CHANGED
@@ -1,8 +1,15 @@
1
+ import { config as dotenvConfig } from 'dotenv';
1
2
  import { serve } from '@hono/node-server';
2
3
  import { createNodeWebSocket } from '@hono/node-ws';
3
4
  import { Hono } from 'hono';
4
5
  import { cors } from 'hono/cors';
5
6
  import { getGlobalTerminalManager } from './lib/terminal/global-manager.js';
7
+ import path from 'path';
8
+ import { fileURLToPath } from 'url';
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = path.dirname(__filename);
11
+ // Load environment variables from .env file in project root
12
+ dotenvConfig({ path: path.resolve(__dirname, '../../../.env') });
6
13
  const app = new Hono();
7
14
  // Enable CORS for frontend
8
15
  app.use('*', cors({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovelybunch/api",
3
- "version": "1.0.18",
3
+ "version": "1.0.21",
4
4
  "type": "module",
5
5
  "main": "dist/server-with-static.js",
6
6
  "exports": {
@@ -32,8 +32,9 @@
32
32
  "dependencies": {
33
33
  "@hono/node-server": "^1.13.7",
34
34
  "@hono/node-ws": "^1.0.6",
35
- "@lovelybunch/types": "^1.0.18",
36
- "@lovelybunch/core": "^1.0.18",
35
+ "@lovelybunch/core": "^1.0.21",
36
+ "@lovelybunch/types": "^1.0.21",
37
+ "dotenv": "^17.2.1",
37
38
  "fuse.js": "^7.0.0",
38
39
  "gray-matter": "^4.0.3",
39
40
  "hono": "^4.9.5",