Repository navigation
Expand file tree
/
Copy pathagent-task.ts
More file actions
63 lines (54 loc) · 2.21 KB
/
Copy pathagent-task.ts
File metadata and controls
63 lines (54 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { AgentTask, ClerkClient } from '@clerk/backend';
import { createClerkClient } from '@clerk/backend';
export type CreateAgentTaskParams = Parameters<ClerkClient['agentTasks']['create']>[0] &
(
| {
/**
* The API URL for your Clerk instance.
* If not provided, falls back to the `CLERK_API_URL` environment variable.
*/
apiUrl?: string;
/**
* The secret key for your Clerk instance.
* If not provided, falls back to the `CLERK_SECRET_KEY` environment variable.
*/
secretKey?: string;
clerkClient?: never;
}
| {
/**
* The Clerk client to use to create the agent task.
* If not provided, a new Clerk client will be created.
*/
clerkClient?: ClerkClient;
apiUrl?: string;
secretKey?: string;
}
);
export const ERROR_MISSING_SECRET_KEY =
'A secretKey is required to create agent tasks. ' +
'Pass it directly or set the CLERK_SECRET_KEY environment variable.';
export const ERROR_MISSING_API_URL =
'An apiUrl is required to create agent tasks. ' + 'Pass it directly or set the CLERK_API_URL environment variable.';
export const ERROR_AGENT_TASK_FAILED = 'Failed to create agent task: ';
/**
* Creates an agent task using the Clerk Backend API and returns its URL.
*
* @internal Framework-specific wrappers should call this after resolving the secret key.
*
* @experimental This is an experimental API for the Agent Tasks feature that is available under a private beta,
* and the API is subject to change. It is advised to [pin](https://clerk.com/docs/pinning) the SDK version
* and the clerk-js version to avoid breaking changes.
*/
export async function createAgentTestingTask(params: CreateAgentTaskParams): Promise<AgentTask> {
const { apiUrl, secretKey, clerkClient, ...taskParams } = params;
if (!clerkClient && !secretKey) {
throw new Error(ERROR_MISSING_SECRET_KEY);
}
const client = clerkClient ?? createClerkClient({ apiUrl, secretKey });
try {
return await client.agentTasks.create(taskParams);
} catch (error) {
throw new Error(ERROR_AGENT_TASK_FAILED + (error instanceof Error ? error.message : String(error)));
}
}