Skip to content

Commit 761dd15

Browse files
committed
sdk error handling changes, adjust sdk install copy, add prod sdk git release script
1 parent 28108ca commit 761dd15

File tree

8 files changed

+152
-19
lines changed

8 files changed

+152
-19
lines changed

sdk-prod-git-release.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
# Exit immediately on error
4+
set -e
5+
6+
# Config
7+
DEFAULT_BRANCH="develop"
8+
TEMP_BRANCH="sdk-prod-temp"
9+
10+
# Get version from sdk/package.json
11+
VERSION=$(jq -r .version sdk/package.json)
12+
RELEASE_BRANCH="sdk-release-prod@$VERSION"
13+
14+
# Make sure we're on the default branch
15+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
16+
if [ "$CURRENT_BRANCH" != "$DEFAULT_BRANCH" ]; then
17+
echo "⚠️ You must be on '$DEFAULT_BRANCH' branch to run this script."
18+
exit 1
19+
fi
20+
21+
# Delete temp branch if it exists
22+
if git show-ref --quiet refs/heads/$TEMP_BRANCH; then
23+
echo "🧹 Deleting existing $TEMP_BRANCH branch..."
24+
git branch -D $TEMP_BRANCH
25+
fi
26+
27+
# Create temp branch
28+
echo "🔀 Creating temporary branch: $TEMP_BRANCH"
29+
git checkout -b $TEMP_BRANCH
30+
31+
# Install dependencies
32+
echo "🔄 Installing dependencies..."
33+
cd sdk
34+
npm install
35+
36+
# Build SDK
37+
echo "🏗️ Building SDK..."
38+
# Set build env vars
39+
(
40+
export NEXT_PUBLIC_SITE_URL="https://agentsmith.app"
41+
export NEXT_PUBLIC_SUPABASE_URL="https://jcnpgqhjaoppfhbkcnpv.supabase.co"
42+
export NEXT_PUBLIC_SUPABASE_ANON_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpjbnBncWhqYW9wcGZoYmtjbnB2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzgwMzA2OTUsImV4cCI6MjA1MzYwNjY5NX0.n_gN9QTv9qY7FlA8wyoww8Nd4PFv9EMiXiDgGOQ2Axw"
43+
npm run build
44+
)
45+
46+
cd ..
47+
48+
# Force add dist/ and node_modules/
49+
echo "➕ Adding sdk/dist and sdk/node_modules..."
50+
git add -f sdk/dist
51+
git add -f sdk/node_modules
52+
53+
# Commit production build
54+
echo "📦 Committing production SDK build..."
55+
git commit -m "Build production SDK $VERSION"
56+
57+
# Subtree split and push to release branch
58+
echo "🌳 Splitting subtree to $RELEASE_BRANCH..."
59+
git subtree split --prefix=sdk -b "$RELEASE_BRANCH"
60+
git push -f origin "$RELEASE_BRANCH"
61+
62+
# Checkout default branch and clean up
63+
echo "🔙 Switching back to $DEFAULT_BRANCH"
64+
git checkout $DEFAULT_BRANCH
65+
66+
# Delete local temp branch if it exists
67+
if git show-ref --quiet refs/heads/$TEMP_BRANCH; then
68+
echo "🧹 Deleting existing local temp branch $TEMP_BRANCH..."
69+
git branch -D $TEMP_BRANCH
70+
fi
71+
72+
# Delete local release branch if it exists
73+
if git show-ref --quiet refs/heads/"$RELEASE_BRANCH"; then
74+
echo "🧹 Deleting existing local release branch $RELEASE_BRANCH..."
75+
git branch -D "$RELEASE_BRANCH"
76+
fi
77+
78+
echo "✅ Production SDK release $VERSION pushed to $RELEASE_BRANCH!"

sdk/src/AgentsmithClient.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export class AgentsmithClient<Agency extends GenericAgency> {
3939
this.supabaseAnonKey = options.supabaseAnonKey ?? process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;
4040
this.agentsmithDirectory = options.agentsmithDirectory ?? defaultAgentsmithDirectory;
4141

42+
if (!this.sdkApiKey || this.sdkApiKey === '') {
43+
throw new Error('Agentsmith SDK API key is required to initialize the agentsmith client');
44+
}
45+
46+
if (!this.projectUuid || this.projectUuid === '') {
47+
throw new Error('Project UUID is required to initialize the agentsmith client');
48+
}
49+
50+
if (!this.agentsmithApiRoot || this.agentsmithApiRoot === '') {
51+
throw new Error('Agentsmith API root is required to initialize the agentsmith client');
52+
}
53+
4254
this.initializePromise = this.initialize();
4355
this.initializeGlobals();
4456
}
@@ -56,6 +68,9 @@ export class AgentsmithClient<Agency extends GenericAgency> {
5668
body: JSON.stringify({ apiKey: sdkApiKey }),
5769
});
5870
const data = (await response.json()) as SdkExchangeResponse;
71+
if ('error' in data) {
72+
throw new Error(data.error);
73+
}
5974
return data.jwt;
6075
} catch (error) {
6176
throw new Error('Failed to exchange API key for JWT');

sdk/src/Prompt.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class Prompt<Agency extends GenericAgency, PromptArg extends PromptIdenti
107107

108108
if (this.argVersion === 'latest' && prompt.latestVersion === null) {
109109
throw new Error(
110-
`No published version found for prompt ${this.slug} while trying to fetch latest version from file system`,
110+
`No published version found for prompt ${this.slug} while trying to fetch latest version from file system, either publish a version or use a specific version number to use the draft.`,
111111
);
112112
}
113113

@@ -163,13 +163,16 @@ export class Prompt<Agency extends GenericAgency, PromptArg extends PromptIdenti
163163
this.variables = [];
164164
return;
165165
}
166-
console.error('Failed to read variables from the file system', err);
166+
console.error(
167+
`Failed to read variables from the file system for ${this.slug}@${this.argVersion}`,
168+
err,
169+
);
167170
}
168171

169-
console.log('successfully read prompt from the file system');
172+
console.log(`Successfully read ${this.slug}@${this.argVersion} from the file system`);
170173
return;
171174
} catch (err) {
172-
console.error('Failed to fetch prompt from the file system', err);
175+
console.error(`Failed to fetch ${this.slug}@${this.argVersion} from the file system`, err);
173176
}
174177

175178
const fetchSpecificVersion = this.argVersion && this.argVersion !== 'latest';
@@ -185,6 +188,7 @@ export class Prompt<Agency extends GenericAgency, PromptArg extends PromptIdenti
185188
.single();
186189

187190
if (error) {
191+
console.error(`Failed to fetch ${this.slug}@${this.argVersion} from remote`, error);
188192
throw error;
189193
}
190194

@@ -209,14 +213,15 @@ export class Prompt<Agency extends GenericAgency, PromptArg extends PromptIdenti
209213
.eq('status', 'PUBLISHED');
210214

211215
if (error) {
216+
console.error(`Failed to fetch ${this.slug}@${this.argVersion} from remote`, error);
212217
throw error;
213218
}
214219

215220
const latestVersion = data.sort((a, b) => compareSemanticVersions(b.version, a.version))[0];
216221

217222
if (!latestVersion) {
218223
throw new Error(
219-
`No published version found for prompt ${this.slug} while trying to fetch latest version`,
224+
`No published version found for prompt ${this.slug}@${this.argVersion} while trying to fetch latest version, either publish a version or use a specific version number to use the draft.`,
220225
);
221226
}
222227

src/app/(api)/api/[apiVersion]/sdk-exchange/route.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ export async function POST(request: Request) {
3232
jwt = exchangeResult.jwt;
3333
expiresAt = exchangeResult.expiresAt;
3434
} catch (error) {
35-
logger.error(error, 'Failed to exchange API key for JWT, exchange failed');
36-
return NextResponse.json({ error: 'Invalid API key' }, { status: 401 });
37-
}
38-
39-
if (jwt === null || expiresAt === null) {
40-
logger.error('Failed to exchange API key for JWT, exchange failed');
41-
return NextResponse.json({ error: 'Invalid API key' }, { status: 401 });
35+
logger.error(error, 'Failed to exchange API key for JWT');
36+
return NextResponse.json(
37+
{ error: 'Failed to exchange API key, check your SDK API key and ensure it is valid.' },
38+
{ status: 401 },
39+
);
4240
}
4341

4442
logger.info('Successfully exchanged API key for JWT');

src/components/modals/connect-project.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export const ConnectProjectModal = (props: ConnectProjectModalProps) => {
8787
try {
8888
await connectProject({
8989
projectUuid: values.projectId,
90-
agentsmithFolder: values.agentsmithFolder,
90+
// agentsmithFolder: values.agentsmithFolder,
91+
agentsmithFolder: 'agentsmith',
9192
projectRepositoryId: values.projectRepositoryId,
9293
organizationUuid: selectedOrganization!.uuid,
9394
});
@@ -169,7 +170,7 @@ export const ConnectProjectModal = (props: ConnectProjectModalProps) => {
169170
)}
170171
/>
171172

172-
<FormField
173+
{/* <FormField
173174
control={form.control}
174175
name="agentsmithFolder"
175176
render={({ field }) => (
@@ -180,7 +181,7 @@ export const ConnectProjectModal = (props: ConnectProjectModalProps) => {
180181
</FormControl>
181182
</FormItem>
182183
)}
183-
/>
184+
/> */}
184185

185186
<Button type="submit" className="w-full">
186187
Connect Project

src/lib/supabase/server-api-key.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ export const exchangeApiKeyForJwt = async (
7575
});
7676

7777
if (error) {
78-
logger.error('Error getting organization by API key hash', error);
78+
logger.error(error, 'Error getting organization by API key hash');
7979
throw new Error('Failed to get organization by API key hash');
8080
}
8181

8282
if (!data) {
83+
logger.error('No data returned from get_organization_by_api_key_hash');
8384
throw new Error('No data returned from get_organization_by_api_key_hash');
8485
}
8586

src/page-components/ProjectPage.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type ProjectPageProps = {
2424
export const ProjectPage = (props: ProjectPageProps) => {
2525
const { projectData } = props;
2626

27-
const [connectProjectModalOpen, setConnectProjectModalOpen] = useState(false);
27+
const [connectProjectModalOpen, setConnectProjectModalOpen] = useState(true);
2828

2929
const handleDownloadTypesClick = async () => {
3030
const response = await generateTypes(projectData.id);
@@ -170,12 +170,41 @@ const compiledPrompt = helloWorldPrompt.compile({
170170
<Info size={16} />
171171
<AlertTitle>1. Installation</AlertTitle>
172172
<AlertDescription>
173+
<div className="text-xs">
174+
During alpha phase we are only building the SDK to github branches, npm registry
175+
coming soon!
176+
</div>
177+
Install the Agentsmith SDK via npm:
178+
<div className="flex items-center gap-1 mt-2">
179+
<div className="font-mono bg-muted p-2 rounded-md flex items-center gap-2">
180+
<Terminal size={16} className="inline-block mr-2" />
181+
<span>
182+
npm install
183+
git+ssh://git@github.com/chad-syntax/agentsmith.git#sdk-release-prod@0.0.1
184+
</span>
185+
</div>
186+
<Button
187+
variant="ghost"
188+
size="icon"
189+
onClick={() => {
190+
navigator.clipboard.writeText(
191+
'npm install git+ssh://git@github.com/chad-syntax/agentsmith.git#sdk-release-prod@0.0.1',
192+
);
193+
toast.success('Copied code to clipboard');
194+
}}
195+
className="hover:text-primary"
196+
>
197+
<Copy className="w-4 h-4" />
198+
</Button>
199+
</div>
200+
</AlertDescription>
201+
{/* <AlertDescription>
173202
Install the Agentsmith SDK using npm or yarn:
174203
<div className="mt-2 font-mono bg-muted p-2 rounded-md">
175204
<Terminal size={16} className="inline-block mr-2" />
176205
npm install @agentsmith/sdk
177206
</div>
178-
</AlertDescription>
207+
</AlertDescription> */}
179208
</Alert>
180209
<Alert variant="default" className="mt-4">
181210
<Info size={16} />

src/types/api-responses.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export type SdkExchangeResponse = {
1+
export type SdkExchangeResponseSuccess = {
22
jwt: string;
33
expiresAt: number;
44
};
5+
6+
export type SdkExchangeResponseError = {
7+
error: string;
8+
};
9+
10+
export type SdkExchangeResponse = SdkExchangeResponseSuccess | SdkExchangeResponseError;

0 commit comments

Comments
 (0)