Skip to content

Commit fbe2f36

Browse files
committed
Load credentials directly for publish command
1 parent 8185ffe commit fbe2f36

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

npm-app/src/cli-handlers/publish.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
22
import * as fs from 'fs'
33
import { green, red, yellow, cyan } from 'picocolors'
4-
import { Client } from '../client'
54
import { websiteUrl } from '../config'
65
import { logger } from '../utils/logger'
76
import { loadLocalAgents } from '../agents/load-agents'
87
import { getAgentsDirectory } from '../agents/agent-utils'
8+
import { getUserCredentials } from '../credentials'
99

1010
interface PublishResponse {
1111
success: boolean
@@ -17,18 +17,17 @@ interface PublishResponse {
1717

1818
/**
1919
* Handle the publish command to upload agent templates to the backend
20-
* @param agentName The name of the agent to publish (required)
21-
*/
22-
export async function handlePublish(agentName?: string): Promise<void> {
23-
const client = Client.getInstance()
20+
* @param agentId The id of the agent to publish (required)
21+
*/ export async function handlePublish(agentId?: string): Promise<void> {
22+
const user = getUserCredentials()
2423

25-
if (!client.user) {
24+
if (!user) {
2625
console.log(red('Please log in first using "login".'))
2726
return
2827
}
2928

30-
if (!agentName) {
31-
console.log(red('Agent name is required. Usage: publish <agent-name>'))
29+
if (!agentId) {
30+
console.log(red('Agent id is required. Usage: publish <agent-id>'))
3231
console.log(
3332
yellow('This prevents accidentally publishing all agents at once.')
3433
)
@@ -69,13 +68,13 @@ export async function handlePublish(agentName?: string): Promise<void> {
6968
// Find the specific agent
7069
const matchingTemplate = Object.entries(agentTemplates).find(
7170
([key, template]) =>
72-
key === agentName ||
73-
template.id === agentName ||
74-
template.displayName === agentName
71+
key === agentId ||
72+
template.id === agentId ||
73+
template.displayName === agentId
7574
)
7675

7776
if (!matchingTemplate) {
78-
console.log(red(`Agent "${agentName}" not found. Available agents:`))
77+
console.log(red(`Agent "${agentId}" not found. Available agents:`))
7978
Object.values(agentTemplates).forEach((template) => {
8079
console.log(` - ${template.displayName} (${template.id})`)
8180
})
@@ -87,10 +86,7 @@ export async function handlePublish(agentName?: string): Promise<void> {
8786
)
8887

8988
try {
90-
const result = await publishAgentTemplate(
91-
template,
92-
client.user.authToken!
93-
)
89+
const result = await publishAgentTemplate(template, user.authToken!)
9490

9591
if (result.success) {
9692
console.log(

npm-app/src/credentials.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path'
33
import os from 'os'
44
import { z } from 'zod'
55
import { logger } from './utils/logger'
6+
import { existsSync, readFileSync } from 'fs'
67

78
const credentialsSchema = z
89
.object({
@@ -48,3 +49,28 @@ export const CONFIG_DIR = path.join(
4849
// Ensure config directory exists
4950
ensureDirectoryExists(CONFIG_DIR)
5051
export const CREDENTIALS_PATH = path.join(CONFIG_DIR, 'credentials.json')
52+
53+
/**
54+
* Get user credentials from file system
55+
* @returns User object or null if not found/authenticated
56+
*/
57+
export const getUserCredentials = (): User | null => {
58+
// Read user credentials directly from file
59+
if (!existsSync(CREDENTIALS_PATH)) {
60+
return null
61+
}
62+
63+
try {
64+
const credentialsFile = readFileSync(CREDENTIALS_PATH, 'utf8')
65+
const user = userFromJson(credentialsFile)
66+
return user || null
67+
} catch (error) {
68+
logger.error(
69+
{
70+
error: error instanceof Error ? error.message : String(error),
71+
},
72+
'Error reading credentials'
73+
)
74+
return null
75+
}
76+
}

0 commit comments

Comments
 (0)