-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useCloudThreadListRuntime (#1290)
* feat: useCloudThreadListRuntime * fix * fixes * fixes
- Loading branch information
Showing
17 changed files
with
484 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export * from "./attachment"; | ||
export * from "./core"; | ||
export * from "./local"; | ||
// export * from "./remote-thread-list"; | ||
export * from "./dangerous-in-browser"; | ||
export * from "./edge"; | ||
export * from "./external-store"; | ||
export * from "./dangerous-in-browser"; | ||
export * from "./speech"; | ||
export * from "./attachment"; | ||
export * from "./feedback"; | ||
export * from "./local"; | ||
export * from "./remote-thread-list"; | ||
export * from "./speech"; |
This file contains 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
11 changes: 11 additions & 0 deletions
11
packages/react/src/runtimes/remote-thread-list/cloud/AssistantCloud.tsx
This file contains 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,11 @@ | ||
import { AssistantCloudAPI, AssistantCloudConfig } from "./AssistantCloudAPI"; | ||
import { AssistantCloudThreads } from "./AssistantCloudThreads"; | ||
|
||
export class AssistantCloud { | ||
public readonly threads; | ||
|
||
constructor(config: AssistantCloudConfig) { | ||
const api = new AssistantCloudAPI(config); | ||
this.threads = new AssistantCloudThreads(api); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/react/src/runtimes/remote-thread-list/cloud/AssistantCloudAPI.tsx
This file contains 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,81 @@ | ||
import { | ||
AssistantCloudAuthStrategy, | ||
AssistantCloudJWTAuthStrategy, | ||
AssistantCloudAPIKeyAuthStrategy, | ||
} from "./AssistantCloudAuthStrategy"; | ||
|
||
export type AssistantCloudConfig = | ||
| { | ||
baseUrl: string; | ||
// TODO use baseUrl to construct the projectId | ||
unstable_projectId: string; | ||
authToken(): Promise<string>; | ||
} | ||
| { | ||
apiKey: string; | ||
workspaceId: string; | ||
}; | ||
|
||
export class AssistantCloudAPI { | ||
private _tokenManager: AssistantCloudAuthStrategy; | ||
private _baseUrl; | ||
|
||
constructor(config: AssistantCloudConfig) { | ||
if ("authToken" in config) { | ||
this._baseUrl = config.baseUrl; | ||
this._tokenManager = new AssistantCloudJWTAuthStrategy( | ||
config.unstable_projectId, | ||
config.authToken, | ||
); | ||
} else { | ||
this._baseUrl = "https://api.assistant-ui.com"; | ||
this._tokenManager = new AssistantCloudAPIKeyAuthStrategy( | ||
config.apiKey, | ||
config.workspaceId, | ||
); | ||
} | ||
} | ||
|
||
public async makeRequest( | ||
endpoint: string, | ||
options: { | ||
method?: "POST" | "PUT" | "DELETE" | undefined; | ||
query?: Record<string, string | number | boolean> | undefined; | ||
body?: object | undefined; | ||
} = {}, | ||
) { | ||
const authHeaders = await this._tokenManager.getAuthHeaders(); | ||
const headers = { | ||
...authHeaders, | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const queryParams = new URLSearchParams(); | ||
if (options.query) { | ||
for (const [key, value] of Object.entries(options.query)) { | ||
if (value === false) continue; | ||
if (value === true) { | ||
queryParams.set(key, "true"); | ||
} else { | ||
queryParams.set(key, value.toString()); | ||
} | ||
} | ||
} | ||
|
||
const url = new URL(`${this._baseUrl}${endpoint}`); | ||
url.search = queryParams.toString(); | ||
|
||
const response = await fetch(url, { | ||
method: options.method ?? "GET", | ||
headers, | ||
body: options.body ? JSON.stringify(options.body) : null, | ||
}); | ||
|
||
if (!response.ok) { | ||
// TODO better error handling | ||
throw new Error(`Request failed with status ${response.status}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
} |
Oops, something went wrong.