@picahq/toolkit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +97 -0
  3. package/dist/apis/action.d.ts +69 -0
  4. package/dist/apis/action.js +201 -0
  5. package/dist/apis/available-actions.d.ts +26 -0
  6. package/dist/apis/available-actions.js +41 -0
  7. package/dist/apis/available-connectors.d.ts +24 -0
  8. package/dist/apis/available-connectors.js +40 -0
  9. package/dist/apis/connections.d.ts +29 -0
  10. package/dist/apis/connections.js +74 -0
  11. package/dist/apis/execute.d.ts +42 -0
  12. package/dist/apis/execute.js +185 -0
  13. package/dist/index.d.ts +36 -0
  14. package/dist/index.js +228 -0
  15. package/dist/prompts/default.d.ts +16 -0
  16. package/dist/prompts/default.js +114 -0
  17. package/dist/prompts/knowledge.d.ts +15 -0
  18. package/dist/prompts/knowledge.js +109 -0
  19. package/dist/schemas/actions.d.ts +69 -0
  20. package/dist/schemas/actions.js +43 -0
  21. package/dist/schemas/connections.d.ts +69 -0
  22. package/dist/schemas/connections.js +42 -0
  23. package/dist/schemas/execute.d.ts +128 -0
  24. package/dist/schemas/execute.js +69 -0
  25. package/dist/schemas/index.d.ts +3 -0
  26. package/dist/schemas/index.js +3 -0
  27. package/dist/tsconfig.tsbuildinfo +1 -0
  28. package/dist/types/client.d.ts +144 -0
  29. package/dist/types/client.js +9 -0
  30. package/dist/types/index.d.ts +2 -0
  31. package/dist/types/index.js +2 -0
  32. package/dist/types/pica.d.ts +133 -0
  33. package/dist/types/pica.js +9 -0
  34. package/dist/utils/helpers.d.ts +92 -0
  35. package/dist/utils/helpers.js +206 -0
  36. package/dist/utils/index.d.ts +2 -0
  37. package/dist/utils/index.js +2 -0
  38. package/dist/utils/log-messages.d.ts +21 -0
  39. package/dist/utils/log-messages.js +26 -0
  40. package/package.json +73 -0
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Pica ToolKit - Execute API
3
+ *
4
+ * This is the execute API for the Pica ToolKit.
5
+ * This API executes actions on connected platforms via the Pica Passthrough API.
6
+ *
7
+ * @fileoverview Execute API for the Pica ToolKit
8
+ * @author Pica
9
+ */
10
+ import axios from "axios";
11
+ import FormData from "form-data";
12
+ import { normalizeActionId, resolveTemplateVariables } from "../utils";
13
+ import { listConnections } from "./connections";
14
+ import { getActionSpec } from "./action";
15
+ const PASSTHROUGH_URL = "/v1/passthrough";
16
+ /**
17
+ * Execute an action
18
+ * @param baseUrl - The base URL of the Pica API
19
+ * @param secret - The Pica API key
20
+ * @param actionSystemId - The system ID of the action to execute
21
+ * @param connectionKey - The connection key to use for executing the action
22
+ * @param data - The data to execute the action with
23
+ * @param pathVariables - The path variables to execute the action with
24
+ * @param queryParams - The query parameters to execute the action with
25
+ * @param headers - The headers to execute the action with
26
+ * @param isFormData - Whether to execute the action as a form data request
27
+ * @param isFormUrlEncoded - Whether to execute the action as a form urlencoded request
28
+ * @param returnRequestConfigWithoutExecution - Whether to return request config without executing
29
+ * @param options - The options for the Pica client
30
+ * @returns The response from the action
31
+ */
32
+ export async function executeAction({ baseUrl, secret, actionSystemId, connectionKey, data, pathVariables, queryParams, headers, isFormData, isFormUrlEncoded, returnRequestConfigWithoutExecution, options, }) {
33
+ await validateConnectionKey(baseUrl, secret, connectionKey, options);
34
+ const action = await getActionSpec(baseUrl, secret, actionSystemId, options);
35
+ if (!action) {
36
+ throw new Error(`Could not fetch knowledge for action system ID: ${actionSystemId}`);
37
+ }
38
+ // For custom actions, always include connection key in the data
39
+ if (action.tags.includes("custom")) {
40
+ const customData = {
41
+ ...data,
42
+ connectionKey
43
+ };
44
+ data = customData;
45
+ }
46
+ // Resolve template variables in the action path
47
+ const { resolvedPath, cleanedData } = resolveTemplateVariables(action.path, data, pathVariables);
48
+ // Execute passthrough request
49
+ return await executePassthrough({
50
+ baseUrl,
51
+ secret,
52
+ actionId: normalizeActionId(actionSystemId),
53
+ connectionKey,
54
+ data: cleanedData,
55
+ path: resolvedPath,
56
+ method: action.method,
57
+ queryParams,
58
+ headers,
59
+ isFormData,
60
+ isFormUrlEncoded,
61
+ returnRequestConfigWithoutExecution
62
+ });
63
+ }
64
+ /**
65
+ * Validate the connection key
66
+ * @param baseUrl - The base URL of the Pica API
67
+ * @param secret - The Pica API key
68
+ * @param connectionKey - The connection key to validate
69
+ * @param options - The options for the Pica client
70
+ * @throws An error if the connection key does not exist or is not accessible
71
+ */
72
+ async function validateConnectionKey(baseUrl, secret, connectionKey, options) {
73
+ const connections = await listConnections({ baseUrl, secret, options });
74
+ const connectionExists = connections.some(conn => conn.key === connectionKey);
75
+ if (!connectionExists) {
76
+ throw new Error(`Connection key '${connectionKey}' does not exist or is not accessible.`);
77
+ }
78
+ }
79
+ /**
80
+ * Execute a passthrough request
81
+ * @param baseUrl - The base URL of the Pica API
82
+ * @param secret - The Pica API key
83
+ * @param actionId - The ID of the action to execute
84
+ * @param connectionKey - The connection key to use for executing the action
85
+ * @param data - The data to execute the action with
86
+ * @param path - The path to execute the action on
87
+ * @param method - The method to execute the action with
88
+ * @param queryParams - The query parameters to execute the action with
89
+ * @param headers - The headers to execute the action with
90
+ * @param isFormData - Whether to execute the action as a form data request
91
+ * @param isFormUrlEncoded - Whether to execute the action as a form urlencoded request
92
+ * @param returnRequestConfigWithoutExecution - Whether to return request config without executing
93
+ * @returns The response from the passthrough request
94
+ */
95
+ async function executePassthrough({ baseUrl, secret, actionId, connectionKey, data, path, method, queryParams, headers, isFormData, isFormUrlEncoded, returnRequestConfigWithoutExecution }) {
96
+ try {
97
+ const allHeaders = {
98
+ "Content-Type": "application/json",
99
+ "x-pica-secret": secret,
100
+ "x-pica-connection-key": connectionKey,
101
+ "x-pica-action-id": actionId,
102
+ ...(isFormData ? { "Content-Type": "multipart/form-data" } : {}),
103
+ ...(isFormUrlEncoded ? { "Content-Type": "application/x-www-form-urlencoded" } : {}),
104
+ ...headers
105
+ };
106
+ // Remove Content-Type header if no data is being sent
107
+ const finalHeaders = !data
108
+ ? Object.entries(allHeaders)
109
+ .filter(([key]) => key.toLowerCase() !== "content-type")
110
+ .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
111
+ : allHeaders;
112
+ const url = `${baseUrl}${PASSTHROUGH_URL}${path.startsWith("/") ? path : "/" + path}`;
113
+ const requestConfig = {
114
+ url,
115
+ method,
116
+ headers: finalHeaders,
117
+ params: queryParams
118
+ };
119
+ if (method?.toLowerCase() !== "get") {
120
+ if (isFormData) {
121
+ const formData = new FormData();
122
+ if (data && typeof data === "object" && !Array.isArray(data)) {
123
+ Object.entries(data).forEach(([key, value]) => {
124
+ if (typeof value === "object") {
125
+ formData.append(key, JSON.stringify(value));
126
+ }
127
+ else {
128
+ formData.append(key, String(value));
129
+ }
130
+ });
131
+ }
132
+ requestConfig.data = formData;
133
+ const formHeaders = formData.getHeaders();
134
+ requestConfig.headers = {
135
+ ...requestConfig.headers,
136
+ ...formHeaders
137
+ };
138
+ }
139
+ else if (isFormUrlEncoded) {
140
+ const params = new URLSearchParams();
141
+ if (data && typeof data === "object" && !Array.isArray(data)) {
142
+ Object.entries(data).forEach(([key, value]) => {
143
+ if (typeof value === "object") {
144
+ params.append(key, JSON.stringify(value));
145
+ }
146
+ else {
147
+ params.append(key, String(value));
148
+ }
149
+ });
150
+ }
151
+ requestConfig.data = params.toString();
152
+ }
153
+ else {
154
+ requestConfig.data = data;
155
+ }
156
+ }
157
+ // Return request config without execution if requested
158
+ if (returnRequestConfigWithoutExecution) {
159
+ requestConfig.headers['x-pica-secret'] = "YOUR_PICA_SECRET_KEY_HERE";
160
+ return {
161
+ executed: false,
162
+ requestConfig
163
+ };
164
+ }
165
+ const response = await axios(requestConfig);
166
+ return {
167
+ success: true,
168
+ responseData: response.data,
169
+ requestConfig: {
170
+ ...requestConfig,
171
+ headers: {
172
+ ...requestConfig.headers,
173
+ "x-pica-secret": "****REDACTED****"
174
+ }
175
+ }
176
+ };
177
+ }
178
+ catch (error) {
179
+ console.error("Error executing passthrough request:", error);
180
+ return {
181
+ success: false,
182
+ error: JSON.stringify(error)
183
+ };
184
+ }
185
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Pica ToolKit - Main Entry Point
3
+ *
4
+ * This is the main entry point for the Pica ToolKit.
5
+ *
6
+ * @fileoverview Main entry point for the Pica ToolKit
7
+ * @author Pica
8
+ */
9
+ import { ToolSet } from "ai";
10
+ import { PicaOptions, Connection, Connector, AvailableAction } from "./types";
11
+ export declare class Pica {
12
+ private secret;
13
+ private baseUrl;
14
+ private options?;
15
+ constructor(secret: string, options?: PicaOptions);
16
+ private logConnectorInitialization;
17
+ private logActionInitialization;
18
+ /**
19
+ * @returns The system prompt for the Pica ToolKit
20
+ */
21
+ getSystemPrompt(): Promise<string>;
22
+ /**
23
+ * @returns List of connected integrations
24
+ */
25
+ getConnectedIntegrations(): Promise<Connection[]>;
26
+ /**
27
+ * @returns List of all available connectors in Pica
28
+ */
29
+ getAvailableConnectors(): Promise<Connector[]>;
30
+ /**
31
+ * @param platform - The platform to get all available actions for
32
+ * @returns List of all available actions for a given platform
33
+ */
34
+ getAvailableActions(platform: string): Promise<AvailableAction[]>;
35
+ tools(): ToolSet;
36
+ }
package/dist/index.js ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Pica ToolKit - Main Entry Point
3
+ *
4
+ * This is the main entry point for the Pica ToolKit.
5
+ *
6
+ * @fileoverview Main entry point for the Pica ToolKit
7
+ * @author Pica
8
+ */
9
+ import { tool } from "ai";
10
+ import chalk from "chalk";
11
+ import { executeAction } from "./apis/execute";
12
+ import { listConnections, clean } from "./apis/connections";
13
+ import { searchPlatformActions, getActionsKnowledge } from "./apis/action";
14
+ import { getAvailableActions } from "./apis/available-actions";
15
+ import { getAvailableConnectors } from "./apis/available-connectors";
16
+ import { generateDefaultSystemPrompt } from "./prompts/default";
17
+ import { generateKnowledgeAgentSystemPrompt } from "./prompts/knowledge";
18
+ import { LIST_PICA_INTEGRATIONS_TOOL_CONFIG, SEARCH_PLATFORM_ACTIONS_TOOL_CONFIG, GET_ACTIONS_KNOWLEDGE_TOOL_CONFIG, EXECUTE_ACTION_TOOL_CONFIG, KNOWLEDGE_AGENT_EXECUTE_ACTION_TOOL_CONFIG, PROMPT_TO_CONNECT_INTEGRATION_TOOL_CONFIG } from "./schemas";
19
+ import { isInitializingWithAllConnectors, isInitializingWithAllActions, pluralize, LOG_MESSAGES } from "./utils";
20
+ export class Pica {
21
+ secret;
22
+ baseUrl = "https://api.picaos.com";
23
+ options;
24
+ constructor(secret, options) {
25
+ if (!secret) {
26
+ throw new Error("A valid Pica API key must be provided. You can obtain your API key from the Pica dashboard: https://app.picaos.com/settings/api-keys");
27
+ }
28
+ this.secret = secret;
29
+ this.baseUrl = options?.serverUrl || "https://api.picaos.com";
30
+ this.options = options;
31
+ // Knowledge Agent Mode initialization
32
+ if (this.options?.knowledgeAgent) {
33
+ console.log(chalk.green.bold(LOG_MESSAGES.KNOWLEDGE_AGENT_INITIALIZED));
34
+ console.log(chalk.yellow(LOG_MESSAGES.KNOWLEDGE_AGENT_ACTIONS_ACCESS));
35
+ console.log(chalk.yellow(LOG_MESSAGES.KNOWLEDGE_AGENT_EXECUTE_CONFIG));
36
+ console.log(chalk.gray(LOG_MESSAGES.KNOWLEDGE_AGENT_CONNECTIONS_DISABLED));
37
+ if (this.options?.authkit) {
38
+ console.log(chalk.blue(LOG_MESSAGES.AUTHKIT_ENABLED));
39
+ }
40
+ return;
41
+ }
42
+ // Standard mode initialization
43
+ this.logConnectorInitialization();
44
+ this.logActionInitialization();
45
+ if (this.options?.authkit) {
46
+ console.log(chalk.blue(LOG_MESSAGES.AUTHKIT_ENABLED));
47
+ }
48
+ }
49
+ logConnectorInitialization() {
50
+ if (isInitializingWithAllConnectors(this.options?.connectors)) {
51
+ console.log(chalk.cyan(LOG_MESSAGES.ALL_CONNECTORS_ACCESS));
52
+ console.log(chalk.magenta(LOG_MESSAGES.LIST_CONNECTIONS_ENABLED));
53
+ }
54
+ else {
55
+ const connectorCount = this.options?.connectors?.length || 0;
56
+ console.log(chalk.cyan(LOG_MESSAGES.connectorCount(connectorCount, pluralize(connectorCount, 'connector'))));
57
+ console.log(chalk.magenta(LOG_MESSAGES.LIST_CONNECTIONS_DISABLED));
58
+ }
59
+ }
60
+ logActionInitialization() {
61
+ if (isInitializingWithAllActions(this.options?.actions)) {
62
+ console.log(chalk.cyan(LOG_MESSAGES.ALL_ACTIONS_ACCESS));
63
+ }
64
+ else {
65
+ const actionCount = this.options?.actions?.length || 0;
66
+ console.log(chalk.cyan(LOG_MESSAGES.actionCount(actionCount, pluralize(actionCount, 'action'))));
67
+ }
68
+ }
69
+ /**
70
+ * @returns The system prompt for the Pica ToolKit
71
+ */
72
+ async getSystemPrompt() {
73
+ if (this.options?.knowledgeAgent) {
74
+ return generateKnowledgeAgentSystemPrompt();
75
+ }
76
+ return generateDefaultSystemPrompt(this.options?.connectors, this.options?.actions);
77
+ }
78
+ /**
79
+ * @returns List of connected integrations
80
+ */
81
+ async getConnectedIntegrations() {
82
+ return await listConnections({
83
+ baseUrl: this.baseUrl,
84
+ secret: this.secret,
85
+ options: this.options
86
+ });
87
+ }
88
+ /**
89
+ * @returns List of all available connectors in Pica
90
+ */
91
+ async getAvailableConnectors() {
92
+ return await getAvailableConnectors({
93
+ baseUrl: this.baseUrl,
94
+ secret: this.secret,
95
+ options: this.options
96
+ });
97
+ }
98
+ /**
99
+ * @param platform - The platform to get all available actions for
100
+ * @returns List of all available actions for a given platform
101
+ */
102
+ async getAvailableActions(platform) {
103
+ return await getAvailableActions({
104
+ baseUrl: this.baseUrl,
105
+ secret: this.secret,
106
+ platform,
107
+ options: this.options
108
+ });
109
+ }
110
+ tools() {
111
+ const tools = {};
112
+ // Knowledge agents don't need connection management tools
113
+ if (!this.options?.knowledgeAgent && isInitializingWithAllConnectors(this.options?.connectors)) {
114
+ tools.listPicaIntegrations = tool({
115
+ name: LIST_PICA_INTEGRATIONS_TOOL_CONFIG.name,
116
+ description: LIST_PICA_INTEGRATIONS_TOOL_CONFIG.description,
117
+ inputSchema: LIST_PICA_INTEGRATIONS_TOOL_CONFIG.schema,
118
+ outputSchema: LIST_PICA_INTEGRATIONS_TOOL_CONFIG.outputSchema,
119
+ execute: async () => {
120
+ const connections = await listConnections({
121
+ baseUrl: this.baseUrl,
122
+ secret: this.secret,
123
+ options: this.options
124
+ });
125
+ return clean(connections);
126
+ }
127
+ });
128
+ }
129
+ tools.searchPlatformActions = tool({
130
+ name: SEARCH_PLATFORM_ACTIONS_TOOL_CONFIG.name,
131
+ description: SEARCH_PLATFORM_ACTIONS_TOOL_CONFIG.description,
132
+ inputSchema: SEARCH_PLATFORM_ACTIONS_TOOL_CONFIG.schema,
133
+ outputSchema: SEARCH_PLATFORM_ACTIONS_TOOL_CONFIG.outputSchema,
134
+ execute: async ({ platform, query }) => {
135
+ if (!this.options?.knowledgeAgent && !isInitializingWithAllActions(this.options?.actions) && this.options?.actions?.length === 0) {
136
+ throw new Error("No actions are available. Please initialize the client with specific action ids or ['*'] for all actions.");
137
+ }
138
+ const actions = await searchPlatformActions({
139
+ baseUrl: this.baseUrl,
140
+ secret: this.secret,
141
+ platform,
142
+ query,
143
+ options: this.options?.knowledgeAgent ? { ...this.options, actions: ["*"] } : this.options
144
+ });
145
+ return actions;
146
+ }
147
+ });
148
+ tools.getActionsKnowledge = tool({
149
+ name: GET_ACTIONS_KNOWLEDGE_TOOL_CONFIG.name,
150
+ description: GET_ACTIONS_KNOWLEDGE_TOOL_CONFIG.description,
151
+ inputSchema: GET_ACTIONS_KNOWLEDGE_TOOL_CONFIG.schema,
152
+ outputSchema: GET_ACTIONS_KNOWLEDGE_TOOL_CONFIG.outputSchema,
153
+ execute: async ({ systemIds }) => {
154
+ const knowledgeMap = await getActionsKnowledge({
155
+ baseUrl: this.baseUrl,
156
+ secret: this.secret,
157
+ systemIds,
158
+ options: this.options?.knowledgeAgent ? { ...this.options, actions: ["*"] } : this.options
159
+ });
160
+ return knowledgeMap;
161
+ }
162
+ });
163
+ if (this.options?.knowledgeAgent) {
164
+ tools.execute = tool({
165
+ name: KNOWLEDGE_AGENT_EXECUTE_ACTION_TOOL_CONFIG.name,
166
+ description: KNOWLEDGE_AGENT_EXECUTE_ACTION_TOOL_CONFIG.description,
167
+ inputSchema: KNOWLEDGE_AGENT_EXECUTE_ACTION_TOOL_CONFIG.schema,
168
+ outputSchema: KNOWLEDGE_AGENT_EXECUTE_ACTION_TOOL_CONFIG.outputSchema,
169
+ execute: async (params) => {
170
+ const result = await executeAction({
171
+ baseUrl: this.baseUrl,
172
+ secret: this.secret,
173
+ actionSystemId: params.actionSystemId,
174
+ connectionKey: params.connectionKey,
175
+ data: params.data,
176
+ pathVariables: params.pathVariables,
177
+ queryParams: params.queryParams,
178
+ headers: params.headers,
179
+ isFormData: params.isFormData,
180
+ isFormUrlEncoded: params.isFormUrlEncoded,
181
+ returnRequestConfigWithoutExecution: true,
182
+ options: this.options
183
+ });
184
+ return result;
185
+ }
186
+ });
187
+ }
188
+ else {
189
+ tools.execute = tool({
190
+ name: EXECUTE_ACTION_TOOL_CONFIG.name,
191
+ description: EXECUTE_ACTION_TOOL_CONFIG.description,
192
+ inputSchema: EXECUTE_ACTION_TOOL_CONFIG.schema,
193
+ outputSchema: EXECUTE_ACTION_TOOL_CONFIG.outputSchema,
194
+ execute: async (params) => {
195
+ const result = await executeAction({
196
+ baseUrl: this.baseUrl,
197
+ secret: this.secret,
198
+ actionSystemId: params.actionSystemId,
199
+ connectionKey: params.connectionKey,
200
+ data: params.data,
201
+ pathVariables: params.pathVariables,
202
+ queryParams: params.queryParams,
203
+ headers: params.headers,
204
+ isFormData: params.isFormData,
205
+ isFormUrlEncoded: params.isFormUrlEncoded,
206
+ returnRequestConfigWithoutExecution: false,
207
+ options: this.options
208
+ });
209
+ return result;
210
+ }
211
+ });
212
+ }
213
+ if (this.options?.authkit) {
214
+ tools.promptToConnectIntegration = tool({
215
+ name: PROMPT_TO_CONNECT_INTEGRATION_TOOL_CONFIG.name,
216
+ description: PROMPT_TO_CONNECT_INTEGRATION_TOOL_CONFIG.description,
217
+ inputSchema: PROMPT_TO_CONNECT_INTEGRATION_TOOL_CONFIG.schema,
218
+ outputSchema: PROMPT_TO_CONNECT_INTEGRATION_TOOL_CONFIG.outputSchema,
219
+ execute: async ({ platformName }) => {
220
+ return {
221
+ response: platformName
222
+ };
223
+ }
224
+ });
225
+ }
226
+ return tools;
227
+ }
228
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Pica ToolKit - Default System Prompt
3
+ *
4
+ * This is the default system prompt for the Pica ToolKit.
5
+ * Contains the usage instructions for all the Pica tools.
6
+ *
7
+ * @fileoverview Default system prompt for the Pica ToolKit
8
+ * @author Pica
9
+ */
10
+ /**
11
+ * Generates the default system prompt with optional connector and action information
12
+ * @param connectors - Array of connector keys to include in the prompt
13
+ * @param actions - Array of action systemIds to include in the prompt
14
+ * @returns The formatted system prompt string
15
+ */
16
+ export declare const generateDefaultSystemPrompt: (connectors?: string[], actions?: string[]) => string;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Pica ToolKit - Default System Prompt
3
+ *
4
+ * This is the default system prompt for the Pica ToolKit.
5
+ * Contains the usage instructions for all the Pica tools.
6
+ *
7
+ * @fileoverview Default system prompt for the Pica ToolKit
8
+ * @author Pica
9
+ */
10
+ import { isInitializingWithAllConnectors, isInitializingWithAllActions, parseConnectionKey, pluralize } from "../utils";
11
+ const INTEGRATIONS_TOOL_PLACEHOLDER = '{{LIST_PICA_INTEGRATIONS_TOOL_CONFIG}}';
12
+ const LIST_PICA_INTEGRATIONS_TOOL_CONFIG = `
13
+ Tool: **listPicaConnections** - A tool to list user's connected integrations
14
+ - Shows available platforms and their connection keys
15
+ - Use connection keys EXACTLY as provided (including prefixes like 'test::')
16
+ `;
17
+ const DEFAULT_SYSTEM_PROMPT = `
18
+ === PICA: INTEGRATION ASSISTANT ===\n
19
+ Everything below is for Pica, your integration assistant that can instantly connect your AI agents to external APIs.
20
+
21
+ Current Time: ${new Date().toLocaleString('en-US', { timeZone: 'GMT' })} (GMT)
22
+ ${INTEGRATIONS_TOOL_PLACEHOLDER}
23
+ ## Workflow rules you MUST follow for every request:
24
+
25
+ Step 1: **searchPlatformActions** - Find available actions on a platform
26
+ - Search by platform name (e.g., 'google-sheets', 'posthog', 'notion')
27
+ - When passing the query, pass a descriptive intent phrase without the platform name.
28
+ For example, if the platform is 'gmail', and the user's main query is
29
+ "Fetch my 5 latest emails from Gmail and research the senders using exa",
30
+ then the query for getting Gmail actions should be {platform: gmail, query: "Fetch my 5 latest emails"}.
31
+ Another request would be then made for {platform: exa, query: "Research a sender"}.
32
+ - When you are selecting the action, select the action that is most relevant to the user's query.
33
+ If you are between two very similar actions and one has a 'custom' tag, select the action with the 'custom' tag.
34
+ - Returns actions with system IDs and basic info
35
+
36
+ Step 2: **getActionsKnowledge** - Get detailed documentation for actions
37
+ - Takes array of system IDs from search results
38
+ - Returns comprehensive API documentation and usage examples
39
+ - Essential before executing actions
40
+
41
+ Step 3: **execute** - Execute an action on a connected platform
42
+ IMPORTANT: Must always call getActionsKnowledge before executing an action.
43
+
44
+ Required Parameters:
45
+ * platform: The target platform (e.g., 'google-sheets', 'posthog')
46
+ * actionSystemId: The action system ID from searchPlatformActions results
47
+ * connectionKey: The connection key for authentication (use exactly as provided)
48
+ * data: The request payload
49
+
50
+ Optional Parameters:
51
+ * pathVariables: Values for path template variables like {{spreadsheetId}}
52
+ * queryParams: Query parameters to append to the URL
53
+ * headers: Additional HTTP headers
54
+ * isFormData: Set to true to send data as multipart/form-data
55
+ * isFormUrlEncoded: Set to true to send data as application/x-www-form-urlencoded
56
+
57
+ ## Best Practices:
58
+ - **Always use keys VERBATIM**: Connection keys and system IDs must be used exactly as returned
59
+ - **Follow the workflow**: search → knowledge → execute for best results
60
+ - **Read documentation**: Use getActionsKnowledge tool before executing any actions
61
+ - **Handle templates**: Path variables like {{spreadsheetId}} need actual values provided in pathVariables
62
+ - **Use correct data format**: Check if action requires isFormData or isFormUrlEncoded based on API requirements
63
+ - **Provide clear summaries**: Explain what actions were taken and results achieved
64
+ - **Confirm with user**: Before executing a destructive action, confirm with the user that you have the correct action and data.
65
+
66
+ ## Error Handling:
67
+ - If actions fail, read the error message, check required parameters from the knowledge and then try again with the correct parameters.
68
+ - Always provide helpful error messages and next steps
69
+ - Ask the user for additional information if needed
70
+
71
+ ### Smart Error Recovery:
72
+ - When execution fails, intelligently search for alternative actions based on the failed action's error message and intent.
73
+ - Fetch the actions knowledge for the alternative actions and then execute the alternative action.
74
+ - Perform this until the action succeeds or there are no more alternative actions.
75
+
76
+ Be thorough, accurate, and helpful in assisting users with their integration workflows.
77
+ `;
78
+ /**
79
+ * Builds a formatted section listing connected integrations
80
+ * @param connectors - Array of connector keys to format
81
+ * @returns Formatted string of connected integrations or empty string
82
+ */
83
+ const buildConnectedIntegrationsSection = (connectors) => {
84
+ return connectors?.map(connector => {
85
+ const { platform } = parseConnectionKey(connector);
86
+ return `- ${platform} (connection key: '${connector}')`;
87
+ }).join('\n') || "";
88
+ };
89
+ /**
90
+ * Generates the default system prompt with optional connector and action information
91
+ * @param connectors - Array of connector keys to include in the prompt
92
+ * @param actions - Array of action systemIds to include in the prompt
93
+ * @returns The formatted system prompt string
94
+ */
95
+ export const generateDefaultSystemPrompt = (connectors, actions) => {
96
+ const shouldShowAllConnectors = isInitializingWithAllConnectors(connectors);
97
+ const shouldShowAllActions = isInitializingWithAllActions(actions);
98
+ const toolConfig = shouldShowAllConnectors ? LIST_PICA_INTEGRATIONS_TOOL_CONFIG : "";
99
+ let systemPrompt = DEFAULT_SYSTEM_PROMPT.replace(INTEGRATIONS_TOOL_PLACEHOLDER, toolConfig);
100
+ if (!shouldShowAllConnectors && connectors?.length) {
101
+ const connectionsSection = buildConnectedIntegrationsSection(connectors);
102
+ systemPrompt += `\nConnected integrations:\n${connectionsSection}`;
103
+ }
104
+ if (!shouldShowAllActions) {
105
+ const actionCount = actions?.length || 0;
106
+ if (actionCount === 0) {
107
+ systemPrompt += `\n\nAvailable Actions:\nNo actions are currently available. To enable actions, the client must be initialized with specific action ids or "*" for all actions.`;
108
+ }
109
+ else {
110
+ systemPrompt += `\n\nAvailable Actions:\nClient has access to ${actionCount} ${pluralize(actionCount, 'action')}. Only these actions will be available when searching or executing.`;
111
+ }
112
+ }
113
+ return systemPrompt;
114
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Pica ToolKit - Knowledge Agent System Prompt
3
+ *
4
+ * This is the knowledge agent system prompt for the Pica ToolKit.
5
+ * Used when knowledgeAgent mode is enabled to generate Edge Function prompts.
6
+ *
7
+ * @fileoverview Knowledge agent system prompt for the Pica ToolKit
8
+ * @author Pica
9
+ */
10
+ /**
11
+ * Generates the knowledge agent system prompt
12
+ * Knowledge agents have access to all actions by default and don't need connection management
13
+ * @returns The formatted knowledge agent system prompt string
14
+ */
15
+ export declare const generateKnowledgeAgentSystemPrompt: () => string;