-
Notifications
You must be signed in to change notification settings - Fork 46
Add cloud package and CLI integration #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,333
−767
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2254022
Add cloud package and CLI integration
willwashburn 4ec1f33
Update and simplify cloud CLI tests
willwashburn 0ee3f33
Add cloud dependency and improve CLI error
willwashburn 0242641
Fix cloud bundled deps and CLI polish
c7facb9
Merge origin/main into cloud
1f5bbef
Merge branch 'main' into cloud
willwashburn 1c72b6b
Make dark hero background transparent
willwashburn a59358a
cloud: add sync excludes, improve error handling
willwashburn e860ede
Fix CodeQL high-severity alerts in cloud package
willwashburn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "name": "@agent-relay/cloud", | ||
| "version": "3.2.18", | ||
| "description": "Cloud SDK for Agent Relay — auth, workflow execution, and provider connections", | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "README.md" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "rm -rf dist", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest" | ||
| }, | ||
| "dependencies": { | ||
| "@agent-relay/config": "3.2.18", | ||
| "@aws-sdk/client-s3": "^3.1004.0", | ||
| "ignore": "^7.0.5", | ||
| "tar": "^7.5.10" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^22.19.3", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^3.2.4" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/AgentWorkforce/relay.git", | ||
| "directory": "packages/cloud" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { REFRESH_WINDOW_MS } from "./types.js"; | ||
|
|
||
| export type CloudApiClientOptions = { | ||
| apiUrl: string; | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| accessTokenExpiresAt: string; | ||
| refreshTokenExpiresAt?: string; | ||
| }; | ||
|
|
||
| export type CloudApiClientSnapshot = { | ||
| apiUrl: string; | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| accessTokenExpiresAt: string; | ||
| refreshTokenExpiresAt?: string; | ||
| }; | ||
|
|
||
| function trimLeadingSlash(p: string): string { | ||
| return p.replace(/^\/+/, ""); | ||
| } | ||
|
|
||
| function withTrailingSlash(p: string): string { | ||
| return p.endsWith("/") ? p : `${p}/`; | ||
| } | ||
|
|
||
| export function buildApiUrl(apiUrl: string, p: string): URL { | ||
| return new URL(trimLeadingSlash(p), withTrailingSlash(apiUrl)); | ||
| } | ||
|
|
||
| export class CloudApiClient { | ||
| private accessToken: string; | ||
| private refreshToken: string; | ||
| private accessTokenExpiresAt: string; | ||
| private refreshTokenExpiresAt?: string; | ||
| private refreshPromise: Promise<void> | null = null; | ||
|
|
||
| constructor(private readonly options: CloudApiClientOptions) { | ||
| this.accessToken = options.accessToken; | ||
| this.refreshToken = options.refreshToken; | ||
| this.accessTokenExpiresAt = options.accessTokenExpiresAt; | ||
| this.refreshTokenExpiresAt = options.refreshTokenExpiresAt; | ||
| } | ||
|
|
||
| static fromEnv(env: NodeJS.ProcessEnv): CloudApiClient | null { | ||
| const apiUrl = env.CLOUD_API_URL?.trim(); | ||
| const accessToken = env.CLOUD_API_ACCESS_TOKEN?.trim(); | ||
| const refreshToken = env.CLOUD_API_REFRESH_TOKEN?.trim(); | ||
| const accessTokenExpiresAt = env.CLOUD_API_ACCESS_TOKEN_EXPIRES_AT?.trim(); | ||
| const refreshTokenExpiresAt = env.CLOUD_API_REFRESH_TOKEN_EXPIRES_AT?.trim(); | ||
|
|
||
| if (!apiUrl || !accessToken || !refreshToken || !accessTokenExpiresAt) { | ||
| return null; | ||
| } | ||
|
|
||
| return new CloudApiClient({ | ||
| apiUrl, | ||
| accessToken, | ||
| refreshToken, | ||
| accessTokenExpiresAt, | ||
| refreshTokenExpiresAt, | ||
| }); | ||
| } | ||
|
|
||
| snapshot(): CloudApiClientSnapshot { | ||
| return { | ||
| apiUrl: this.options.apiUrl, | ||
| accessToken: this.accessToken, | ||
| refreshToken: this.refreshToken, | ||
| accessTokenExpiresAt: this.accessTokenExpiresAt, | ||
| ...(this.refreshTokenExpiresAt ? { refreshTokenExpiresAt: this.refreshTokenExpiresAt } : {}), | ||
| }; | ||
| } | ||
|
|
||
| async fetch(p: string, init: RequestInit = {}): Promise<Response> { | ||
| await this.refresh(); | ||
|
|
||
| const response = await fetch(buildApiUrl(this.options.apiUrl, p), { | ||
| ...init, | ||
| headers: this.buildHeaders(init.headers), | ||
| }); | ||
|
|
||
| if (response.status !== 401) { | ||
| return response; | ||
| } | ||
|
|
||
| await this.refresh(true); | ||
|
|
||
| return fetch(buildApiUrl(this.options.apiUrl, p), { | ||
| ...init, | ||
| headers: this.buildHeaders(init.headers), | ||
| }); | ||
| } | ||
|
|
||
| async revoke(): Promise<void> { | ||
| const response = await fetch(buildApiUrl(this.options.apiUrl, "/api/v1/auth/token/revoke"), { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ token: this.refreshToken }), | ||
| }); | ||
|
|
||
| if (!response.ok && response.status !== 404) { | ||
| throw new Error(`Failed to revoke API token: ${response.status} ${response.statusText}`); | ||
| } | ||
| } | ||
|
|
||
| private async refresh(force = false): Promise<void> { | ||
| if (this.refreshPromise) { | ||
| return this.refreshPromise; | ||
| } | ||
|
|
||
| if (!force && !this.shouldRefresh()) { | ||
| return; | ||
| } | ||
|
|
||
| this.refreshPromise = this.doRefresh().finally(() => { | ||
| this.refreshPromise = null; | ||
| }); | ||
|
|
||
| return this.refreshPromise; | ||
| } | ||
|
|
||
| private async doRefresh(): Promise<void> { | ||
| const response = await fetch(buildApiUrl(this.options.apiUrl, "/api/v1/auth/token/refresh"), { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ refreshToken: this.refreshToken }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to refresh API token: ${response.status} ${response.statusText}`); | ||
| } | ||
|
|
||
| const payload = (await response.json()) as { | ||
| accessToken?: string; | ||
| accessTokenExpiresAt?: string; | ||
| refreshToken?: string; | ||
| refreshTokenExpiresAt?: string; | ||
| }; | ||
|
|
||
| if (!payload.accessToken || !payload.accessTokenExpiresAt || !payload.refreshToken) { | ||
| throw new Error("Refresh response missing token fields"); | ||
| } | ||
|
|
||
| this.accessToken = payload.accessToken; | ||
| this.accessTokenExpiresAt = payload.accessTokenExpiresAt; | ||
| this.refreshToken = payload.refreshToken; | ||
| this.refreshTokenExpiresAt = payload.refreshTokenExpiresAt; | ||
| } | ||
|
|
||
| private buildHeaders(headers: HeadersInit | undefined): Headers { | ||
| const merged = new Headers(headers); | ||
| merged.set("Authorization", `Bearer ${this.accessToken}`); | ||
| return merged; | ||
| } | ||
|
|
||
| private shouldRefresh(): boolean { | ||
| const expiresAt = Date.parse(this.accessTokenExpiresAt); | ||
| if (Number.isNaN(expiresAt)) { | ||
| return true; | ||
| } | ||
|
|
||
| return expiresAt - Date.now() <= REFRESH_WINDOW_MS; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.