@andreprado/agentkit 0.1.0-alpha.2 → 0.1.0-alpha.21
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.
- package/README.md +68 -6
- package/docs/guides/add-channel.md +189 -7
- package/docs/guides/add-knowledge.md +144 -0
- package/docs/guides/add-managed-composio.md +163 -0
- package/docs/guides/add-tool.md +1 -1
- package/docs/guides/channel-security.md +128 -32
- package/docs/guides/connect-discord.md +178 -0
- package/docs/guides/connect-slack.md +126 -0
- package/docs/guides/connect-telegram.md +78 -1
- package/docs/guides/connect-whatsapp-evolution.md +121 -0
- package/docs/guides/connect-whatsapp-uazapi.md +126 -0
- package/docs/guides/connect-whatsapp-zapster.md +112 -8
- package/docs/guides/create-agent.md +45 -4
- package/docs/guides/debug-channel.md +147 -0
- package/docs/guides/improve-from-production.md +151 -0
- package/docs/guides/prepare-deploy.md +47 -17
- package/docs/guides/replay-production-traces.md +72 -0
- package/docs/guides/run-evals.md +147 -20
- package/docs/guides/security-rules.md +7 -6
- package/docs/guides/send-feedback.md +135 -0
- package/docs/guides/use-provider.md +27 -3
- package/docs/llms-full.txt +348 -55
- package/docs/llms.txt +62 -7
- package/package.json +2 -5
- package/src/cli/args.ts +57 -0
- package/src/cli/cloud-client.ts +377 -0
- package/src/cli/commands/channels.ts +1586 -0
- package/src/cli/commands/feedback.ts +438 -0
- package/src/cli/commands/knowledge.ts +136 -0
- package/src/cli/commands/transcribe.ts +171 -0
- package/src/cli/constants.ts +4 -0
- package/src/cli/deploy-chat-ui.ts +535 -0
- package/src/cli/deploy-readiness.ts +481 -0
- package/src/cli/flags.ts +162 -0
- package/src/cli/help.ts +236 -0
- package/src/cli/index.ts +1167 -1005
- package/src/cli/process.ts +31 -0
- package/src/cloud/artifact.ts +139 -0
- package/src/cloud/client.ts +80 -0
- package/src/cloud/contracts.ts +63 -0
- package/src/cloud/index.ts +3 -0
- package/src/create-project.ts +21 -6
- package/src/index.ts +517 -8
- package/src/providers/pi.ts +70 -16
- package/src/providers/test.ts +88 -1
- package/src/providers/types.ts +7 -0
- package/src/runtime/channel-buffer.ts +30 -0
- package/src/runtime/channel-test-harness.ts +21 -1
- package/src/runtime/channels/discord.ts +896 -0
- package/src/runtime/channels/generic-webhook.ts +225 -0
- package/src/runtime/channels/slack.ts +646 -0
- package/src/runtime/channels/telegram.ts +466 -23
- package/src/runtime/channels/whatsapp-evolution.ts +1357 -0
- package/src/runtime/channels/whatsapp-meta.ts +9 -0
- package/src/runtime/channels/whatsapp-uazapi.ts +1327 -0
- package/src/runtime/channels/whatsapp-zapster.ts +677 -40
- package/src/runtime/channels.ts +87 -4
- package/src/runtime/chat.ts +130 -38
- package/src/runtime/config.ts +519 -19
- package/src/runtime/core/manifest.ts +103 -5
- package/src/runtime/core/targets.ts +5 -5
- package/src/runtime/database.ts +93 -2
- package/src/runtime/db-commands.ts +9 -0
- package/src/runtime/deploy-readiness.ts +46 -4
- package/src/runtime/deploy.ts +1 -1
- package/src/runtime/dev-server.ts +779 -45
- package/src/runtime/env.ts +8 -3
- package/src/runtime/evals.ts +589 -43
- package/src/runtime/improve.ts +868 -0
- package/src/runtime/inspect.ts +194 -4
- package/src/runtime/integrations/composio.ts +423 -0
- package/src/runtime/knowledge/chunk.ts +333 -0
- package/src/runtime/knowledge/config.ts +135 -0
- package/src/runtime/knowledge/embeddings.ts +133 -0
- package/src/runtime/knowledge/ingest.ts +521 -0
- package/src/runtime/knowledge/prompt-policy.ts +30 -0
- package/src/runtime/knowledge/retrieve.ts +303 -0
- package/src/runtime/knowledge/schema.ts +100 -0
- package/src/runtime/knowledge/tool.ts +64 -0
- package/src/runtime/knowledge/vector.ts +258 -0
- package/src/runtime/prompt-context.ts +141 -0
- package/src/runtime/runtime-contract.ts +86 -8
- package/src/runtime/skills.ts +95 -0
- package/src/runtime/spec.ts +152 -0
- package/src/runtime/sync.ts +144 -0
- package/src/runtime/targets/cloudflare/build.ts +1468 -203
- package/src/runtime/targets/container/server.ts +1 -1
- package/src/runtime/targets/vps/deploy.ts +26 -9
- package/src/runtime/tool-runner.ts +9 -1
- package/src/runtime/tools.ts +128 -2
- package/src/runtime/traces.ts +41 -0
- package/src/runtime/transcription.ts +483 -0
- package/src/storage/sqlite.ts +149 -3
- package/src/templates/blank.ts +76 -17
- package/src/templates/dentista.ts +1011 -0
- package/src/templates/index.ts +2 -0
- package/src/templates/skills/agentkit-build-agent/SKILL.md +52 -0
- package/src/templates/skills/agentkit-build-agent/templates/appointment-intake.instructions.md +21 -0
- package/src/templates/skills/agentkit-build-agent/templates/sales-qualifier.instructions.md +17 -0
- package/src/templates/skills/agentkit-build-agent/templates/support-agent.instructions.md +16 -0
- package/src/templates/skills/agentkit-capsule/SKILL.md +70 -0
- package/src/templates/skills/agentkit-capsule/references/docs-router.md +15 -0
- package/src/templates/skills/agentkit-channels/SKILL.md +127 -0
- package/src/templates/skills/agentkit-channels/references/channel-buffering.md +65 -0
- package/src/templates/skills/agentkit-channels/references/channel-debugging.md +66 -0
- package/src/templates/skills/agentkit-channels/references/discord.md +93 -0
- package/src/templates/skills/agentkit-channels/references/slack.md +56 -0
- package/src/templates/skills/agentkit-channels/references/telegram.md +72 -0
- package/src/templates/skills/agentkit-channels/references/whatsapp-evolution.md +57 -0
- package/src/templates/skills/agentkit-channels/references/whatsapp-uazapi.md +61 -0
- package/src/templates/skills/agentkit-channels/references/whatsapp-zapster.md +77 -0
- package/src/templates/skills/agentkit-database/SKILL.md +45 -0
- package/src/templates/skills/agentkit-database/templates/appointments.schema.sql +15 -0
- package/src/templates/skills/agentkit-database/templates/leads.schema.sql +17 -0
- package/src/templates/skills/agentkit-deploy/SKILL.md +50 -0
- package/src/templates/skills/agentkit-evals/SKILL.md +109 -0
- package/src/templates/skills/agentkit-evals/templates/multi-turn.eval.md +29 -0
- package/src/templates/skills/agentkit-evals/templates/no-leak.eval.md +18 -0
- package/src/templates/skills/agentkit-evals/templates/smoke.eval.md +18 -0
- package/src/templates/skills/agentkit-evals/templates/tool-call.eval.md +27 -0
- package/src/templates/skills/agentkit-improve/SKILL.md +86 -0
- package/src/templates/skills/agentkit-improve/references/replay-side-effects.md +18 -0
- package/src/templates/skills/agentkit-improve/references/trace-packets.md +22 -0
- package/src/templates/skills/agentkit-improve/templates/regression.eval.md +18 -0
- package/src/templates/skills/agentkit-integrations/SKILL.md +76 -0
- package/src/templates/skills/agentkit-knowledge/SKILL.md +43 -0
- package/src/templates/skills/agentkit-knowledge/templates/faq.md +14 -0
- package/src/templates/skills/agentkit-knowledge/templates/policies.md +14 -0
- package/src/templates/skills/agentkit-knowledge/templates/prices.csv +3 -0
- package/src/templates/skills/agentkit-prompts/SKILL.md +47 -0
- package/src/templates/skills/agentkit-prompts/templates/knowledge-grounded-faq.instructions.md +11 -0
- package/src/templates/skills/agentkit-provider/SKILL.md +60 -0
- package/src/templates/skills/agentkit-security/SKILL.md +56 -0
- package/src/templates/skills/agentkit-tools/SKILL.md +37 -0
- package/src/templates/skills/agentkit-tools/examples/database-write.tool.md +35 -0
- package/src/templates/skills/agentkit-tools/examples/eval-safe-external-action.tool.md +37 -0
- package/src/templates/skills/agentkit-tools/examples/lookup-order.tool.md +46 -0
- package/src/templates/skills/agentkit-troubleshooting/SKILL.md +76 -0
- package/src/templates/support.ts +77 -18
- package/docs/guides/channels-production-handoff.md +0 -99
- package/docs/portable-deploy-release-checklist.md +0 -41
- package/src/runtime/targets/cloudflare/deploy.ts +0 -5475
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import type { AgentConfig, AgentTool, ComposioManagedIntegration } from "../../index";
|
|
2
|
+
import { AgentKitError } from "../errors";
|
|
3
|
+
|
|
4
|
+
export const MANAGED_COMPOSIO_TOOL_NAME = "agentkit_composio_execute";
|
|
5
|
+
export const MANAGED_COMPOSIO_API_KEY_SECRET = "COMPOSIO_API_KEY";
|
|
6
|
+
export const MANAGED_COMPOSIO_WEBHOOK_SECRET = "COMPOSIO_WEBHOOK_SECRET";
|
|
7
|
+
export const MANAGED_COMPOSIO_ENTITLEMENT = "managed_composio";
|
|
8
|
+
export const MANAGED_COMPOSIO_USER_ID_BINDING = "AGENTKIT_MANAGED_COMPOSIO_USER_ID";
|
|
9
|
+
|
|
10
|
+
export type ResolvedComposioManagedIntegration = ComposioManagedIntegration & {
|
|
11
|
+
userId: string;
|
|
12
|
+
allowedTools: string[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ManagedComposioResolutionOptions = {
|
|
16
|
+
userId?: string;
|
|
17
|
+
userIdScope?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type ManagedComposioToolInput = {
|
|
21
|
+
toolSlug: string;
|
|
22
|
+
arguments: Record<string, unknown>;
|
|
23
|
+
confirmed?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type ManagedComposioToolOutput = {
|
|
27
|
+
successful: boolean;
|
|
28
|
+
data: unknown;
|
|
29
|
+
error: unknown;
|
|
30
|
+
logId: string | null;
|
|
31
|
+
toolkit: string;
|
|
32
|
+
toolSlug: string;
|
|
33
|
+
userId: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type ComposioActionGuide = {
|
|
37
|
+
summary: string;
|
|
38
|
+
argumentNotes: string[];
|
|
39
|
+
dangerousDefaults?: string[];
|
|
40
|
+
examples?: Array<Record<string, unknown>>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function resolveManagedComposioIntegrations(
|
|
44
|
+
config: Pick<AgentConfig, "name" | "integrations">,
|
|
45
|
+
options: ManagedComposioResolutionOptions = {},
|
|
46
|
+
): ResolvedComposioManagedIntegration[] {
|
|
47
|
+
return (config.integrations ?? [])
|
|
48
|
+
.filter((integration): integration is ComposioManagedIntegration => integration.provider === "composio" && integration.mode === "managed")
|
|
49
|
+
.map((integration) => ({
|
|
50
|
+
...integration,
|
|
51
|
+
userId: options.userId ?? managedComposioUserId(config.name, options.userIdScope),
|
|
52
|
+
allowedTools: allowedComposioTools(integration),
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function hasManagedComposioIntegration(config: Pick<AgentConfig, "name" | "integrations">): boolean {
|
|
57
|
+
return resolveManagedComposioIntegrations(config).length > 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function managedComposioRequiredSecrets(config: Pick<AgentConfig, "name" | "integrations">): string[] {
|
|
61
|
+
return hasManagedComposioIntegration(config) ? [MANAGED_COMPOSIO_API_KEY_SECRET] : [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function createManagedComposioTools(
|
|
65
|
+
config: Pick<AgentConfig, "name" | "integrations">,
|
|
66
|
+
options: ManagedComposioResolutionOptions = {},
|
|
67
|
+
): AgentTool[] {
|
|
68
|
+
const integrations = resolveManagedComposioIntegrations(config, options);
|
|
69
|
+
|
|
70
|
+
return integrations.flatMap((integration) => {
|
|
71
|
+
if (integration.allowedTools.length === 0) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const toolkitByTool = new Map<string, string>();
|
|
76
|
+
|
|
77
|
+
for (const [toolkit, tools] of Object.entries(integration.tools ?? {})) {
|
|
78
|
+
for (const tool of tools) {
|
|
79
|
+
toolkitByTool.set(tool, toolkit);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const tool: AgentTool<ManagedComposioToolInput, ManagedComposioToolOutput> = {
|
|
84
|
+
name: MANAGED_COMPOSIO_TOOL_NAME,
|
|
85
|
+
description: [
|
|
86
|
+
"Executes one explicitly allowed AgentKit-managed Composio action for this deployed agent. Use only for configured connected apps.",
|
|
87
|
+
integration.confirmExternalWrites === false
|
|
88
|
+
? "External write confirmation is disabled for this integration by agentkit.config.ts."
|
|
89
|
+
: "For write actions, pass confirmed: true only after the user confirms the exact external change.",
|
|
90
|
+
describeAllowedComposioActions(integration.allowedTools),
|
|
91
|
+
].join("\n\n"),
|
|
92
|
+
visibility: "user",
|
|
93
|
+
secrets: [MANAGED_COMPOSIO_API_KEY_SECRET],
|
|
94
|
+
permissions: integration.allowedTools.map((toolName) => `composio:${toolName.toLowerCase()}`),
|
|
95
|
+
timeoutMs: 60_000,
|
|
96
|
+
inputSchema: {
|
|
97
|
+
type: "object",
|
|
98
|
+
properties: {
|
|
99
|
+
toolSlug: {
|
|
100
|
+
type: "string",
|
|
101
|
+
enum: integration.allowedTools,
|
|
102
|
+
description: "Allowed Composio action slug to execute.",
|
|
103
|
+
},
|
|
104
|
+
arguments: {
|
|
105
|
+
type: "object",
|
|
106
|
+
additionalProperties: true,
|
|
107
|
+
description:
|
|
108
|
+
"Structured arguments for the selected Composio action. Use the action notes in the tool description; do not rely on Composio defaults for dates, times, durations, recipients, or write behavior.",
|
|
109
|
+
},
|
|
110
|
+
confirmed: {
|
|
111
|
+
type: "boolean",
|
|
112
|
+
description:
|
|
113
|
+
"Set true only after explicit user confirmation for external write actions such as create, update, delete, send, patch, move, insert, clear, remove, import, or quick add.",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
required: ["toolSlug", "arguments"],
|
|
117
|
+
additionalProperties: false,
|
|
118
|
+
},
|
|
119
|
+
outputSchema: {
|
|
120
|
+
type: "object",
|
|
121
|
+
properties: {
|
|
122
|
+
successful: { type: "boolean" },
|
|
123
|
+
data: { type: ["object", "array", "string", "number", "boolean", "null"] },
|
|
124
|
+
error: { type: ["object", "string", "null"] },
|
|
125
|
+
logId: { type: ["string", "null"] },
|
|
126
|
+
toolkit: { type: "string" },
|
|
127
|
+
toolSlug: { type: "string" },
|
|
128
|
+
userId: { type: "string" },
|
|
129
|
+
},
|
|
130
|
+
required: ["successful", "toolkit", "toolSlug", "userId"],
|
|
131
|
+
additionalProperties: true,
|
|
132
|
+
},
|
|
133
|
+
async execute(input, ctx) {
|
|
134
|
+
const toolkit = toolkitByTool.get(input.toolSlug);
|
|
135
|
+
|
|
136
|
+
if (!toolkit || !integration.allowedTools.includes(input.toolSlug)) {
|
|
137
|
+
throw new AgentKitError(
|
|
138
|
+
"composio_tool_not_allowed",
|
|
139
|
+
`Composio action "${input.toolSlug}" is not allowed by this agent's managed Composio settings.`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
validateComposioArguments(input.toolSlug, input.arguments);
|
|
144
|
+
validateComposioConfirmation(input.toolSlug, input.confirmed, integration);
|
|
145
|
+
|
|
146
|
+
const result = await executeComposioTool({
|
|
147
|
+
apiKey: ctx.secrets[MANAGED_COMPOSIO_API_KEY_SECRET],
|
|
148
|
+
userId: integration.userId,
|
|
149
|
+
toolSlug: input.toolSlug,
|
|
150
|
+
arguments: input.arguments,
|
|
151
|
+
signal: ctx.signal,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
successful: result.successful,
|
|
156
|
+
data: result.data ?? null,
|
|
157
|
+
error: result.error ?? null,
|
|
158
|
+
logId: result.log_id ?? null,
|
|
159
|
+
toolkit,
|
|
160
|
+
toolSlug: input.toolSlug,
|
|
161
|
+
userId: integration.userId,
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
render(output) {
|
|
165
|
+
if (output.successful) {
|
|
166
|
+
return `Composio action ${output.toolSlug} completed.`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
text: `Composio action ${output.toolSlug} did not complete successfully.`,
|
|
171
|
+
blocks: [
|
|
172
|
+
{
|
|
173
|
+
type: "card",
|
|
174
|
+
title: "Composio connection or action issue",
|
|
175
|
+
subtitle: typeof output.error === "string" ? output.error : undefined,
|
|
176
|
+
fields: [
|
|
177
|
+
{ label: "Toolkit", value: output.toolkit },
|
|
178
|
+
{ label: "Action", value: output.toolSlug },
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
return [tool];
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function managedComposioUserId(agentName: string, scope = "local"): string {
|
|
191
|
+
return `ak_agent_${stableHash(`${scope}:${agentName}`).slice(0, 24)}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function allowedComposioTools(integration: ComposioManagedIntegration): string[] {
|
|
195
|
+
return Array.from(new Set(Object.values(integration.tools ?? {}).flat())).sort();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function describeAllowedComposioActions(actions: string[]): string {
|
|
199
|
+
const lines = ["Allowed Composio actions:"];
|
|
200
|
+
|
|
201
|
+
for (const action of actions) {
|
|
202
|
+
const guide = COMPOSIO_ACTION_GUIDES[action];
|
|
203
|
+
|
|
204
|
+
if (!guide) {
|
|
205
|
+
lines.push(`- ${action}: Pass explicit arguments from the current Composio action schema. Do not invent fields or rely on hidden defaults.`);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
lines.push(`- ${action}: ${guide.summary}`);
|
|
210
|
+
|
|
211
|
+
for (const note of guide.argumentNotes) {
|
|
212
|
+
lines.push(` - ${note}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const warning of guide.dangerousDefaults ?? []) {
|
|
216
|
+
lines.push(` - Dangerous default: ${warning}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
for (const example of guide.examples ?? []) {
|
|
220
|
+
lines.push(` - Example arguments: ${JSON.stringify(example)}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return lines.join("\n");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function validateComposioArguments(toolSlug: string, args: Record<string, unknown>): void {
|
|
228
|
+
if (toolSlug !== "GOOGLECALENDAR_CREATE_EVENT") {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const start = args.start_datetime;
|
|
233
|
+
|
|
234
|
+
if (typeof start !== "string" || !isUtcDateTime(start)) {
|
|
235
|
+
throw new AgentKitError(
|
|
236
|
+
"composio_argument_invalid",
|
|
237
|
+
"GOOGLECALENDAR_CREATE_EVENT requires start_datetime as an explicit UTC RFC3339 timestamp ending in Z or +00:00. Convert user-local times using the runtime time zone before calling Composio.",
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const minutes = args.event_duration_minutes;
|
|
242
|
+
const hours = args.event_duration_hour;
|
|
243
|
+
|
|
244
|
+
if (!isPositiveNumber(minutes) && !isPositiveNumber(hours)) {
|
|
245
|
+
throw new AgentKitError(
|
|
246
|
+
"composio_argument_required",
|
|
247
|
+
"GOOGLECALENDAR_CREATE_EVENT requires explicit event_duration_minutes or event_duration_hour. Composio defaults event_duration_minutes to 30, so AgentKit blocks implicit duration.",
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function validateComposioConfirmation(
|
|
253
|
+
toolSlug: string,
|
|
254
|
+
confirmed: boolean | undefined,
|
|
255
|
+
integration: ComposioManagedIntegration,
|
|
256
|
+
): void {
|
|
257
|
+
if (integration.confirmExternalWrites === false || !isComposioWriteAction(toolSlug)) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (confirmed !== true) {
|
|
262
|
+
throw new AgentKitError(
|
|
263
|
+
"composio_confirmation_required",
|
|
264
|
+
`Composio action ${toolSlug} changes an external system. Ask the user to confirm the exact action and call again with confirmed: true.`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function isComposioWriteAction(toolSlug: string): boolean {
|
|
270
|
+
return /_(?:BATCH|CLEAR|CREATE|DELETE|DUPLICATE|IMPORT|INSERT|MOVE|PATCH|QUICK_ADD|REMOVE|REVOKE|SEND|UPDATE)(?:_|$)/.test(
|
|
271
|
+
toolSlug,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function isUtcDateTime(value: string): boolean {
|
|
276
|
+
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|\+00:00)$/.test(value);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isPositiveNumber(value: unknown): boolean {
|
|
280
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const COMPOSIO_ACTION_GUIDES: Record<string, ComposioActionGuide> = {
|
|
284
|
+
GOOGLECALENDAR_EVENTS_LIST: {
|
|
285
|
+
summary: "List events from a calendar before answering schedule/availability questions.",
|
|
286
|
+
argumentNotes: [
|
|
287
|
+
"calendarId is required by Composio; use primary unless the user selected a different calendar.",
|
|
288
|
+
"Use explicit timeMin and timeMax RFC3339 bounds for the requested window.",
|
|
289
|
+
"Pass timeZone when the user or business timezone matters.",
|
|
290
|
+
],
|
|
291
|
+
examples: [
|
|
292
|
+
{
|
|
293
|
+
calendarId: "primary",
|
|
294
|
+
timeMin: "2026-06-03T00:00:00-03:00",
|
|
295
|
+
timeMax: "2026-06-04T00:00:00-03:00",
|
|
296
|
+
singleEvents: true,
|
|
297
|
+
orderBy: "startTime",
|
|
298
|
+
timeZone: "America/Sao_Paulo",
|
|
299
|
+
},
|
|
300
|
+
],
|
|
301
|
+
},
|
|
302
|
+
GOOGLECALENDAR_CREATE_EVENT: {
|
|
303
|
+
summary: "Create an event only after confirming the exact title, date, start time, duration, timezone, and attendees/location when relevant.",
|
|
304
|
+
argumentNotes: [
|
|
305
|
+
"start_datetime is required and must be UTC RFC3339, for example 2026-06-03T15:00:00Z for 12:00 in America/Sao_Paulo.",
|
|
306
|
+
"calendar_id defaults to primary; pass it explicitly when known.",
|
|
307
|
+
"event_duration_minutes or event_duration_hour must be explicit.",
|
|
308
|
+
"Set timezone to the user/business IANA timezone when known.",
|
|
309
|
+
],
|
|
310
|
+
dangerousDefaults: [
|
|
311
|
+
"event_duration_minutes defaults to 30 if omitted.",
|
|
312
|
+
"calendar_id defaults to primary if omitted.",
|
|
313
|
+
],
|
|
314
|
+
examples: [
|
|
315
|
+
{
|
|
316
|
+
calendar_id: "primary",
|
|
317
|
+
summary: "Client call",
|
|
318
|
+
start_datetime: "2026-06-03T15:00:00Z",
|
|
319
|
+
event_duration_minutes: 60,
|
|
320
|
+
timezone: "America/Sao_Paulo",
|
|
321
|
+
},
|
|
322
|
+
],
|
|
323
|
+
},
|
|
324
|
+
GOOGLECALENDAR_UPDATE_EVENT: {
|
|
325
|
+
summary: "Update an existing event only after identifying the event and confirming the exact change.",
|
|
326
|
+
argumentNotes: [
|
|
327
|
+
"Read or find the event first so event_id/calendar_id are correct.",
|
|
328
|
+
"When changing time, use explicit UTC RFC3339 values and preserve duration unless the user changed it.",
|
|
329
|
+
"Do not infer cancellation/deletion from vague wording.",
|
|
330
|
+
],
|
|
331
|
+
},
|
|
332
|
+
GMAIL_FETCH_EMAILS: {
|
|
333
|
+
summary: "Fetch emails for read/search requests.",
|
|
334
|
+
argumentNotes: ["Use narrow query/window arguments when available; do not fetch broad mailbox history unnecessarily."],
|
|
335
|
+
},
|
|
336
|
+
GMAIL_SEND_EMAIL: {
|
|
337
|
+
summary: "Send email only after confirming recipients, subject, body, and attachments/links.",
|
|
338
|
+
argumentNotes: ["Pass recipient fields explicitly and avoid using draft-like placeholders in production sends."],
|
|
339
|
+
},
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
async function executeComposioTool(input: {
|
|
343
|
+
apiKey: string | undefined;
|
|
344
|
+
userId: string;
|
|
345
|
+
toolSlug: string;
|
|
346
|
+
arguments: Record<string, unknown>;
|
|
347
|
+
signal: AbortSignal;
|
|
348
|
+
}): Promise<{ successful: boolean; data?: unknown; error?: unknown; log_id?: string }> {
|
|
349
|
+
if (!input.apiKey) {
|
|
350
|
+
throw new AgentKitError("composio_secret_missing", "AgentKit-managed Composio requires COMPOSIO_API_KEY at runtime.");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const response = await fetch(
|
|
354
|
+
`https://backend.composio.dev/api/v3.1/tools/execute/${encodeURIComponent(input.toolSlug)}`,
|
|
355
|
+
{
|
|
356
|
+
method: "POST",
|
|
357
|
+
headers: {
|
|
358
|
+
"content-type": "application/json",
|
|
359
|
+
"x-api-key": input.apiKey,
|
|
360
|
+
},
|
|
361
|
+
body: JSON.stringify({
|
|
362
|
+
user_id: input.userId,
|
|
363
|
+
arguments: input.arguments,
|
|
364
|
+
}),
|
|
365
|
+
signal: input.signal,
|
|
366
|
+
},
|
|
367
|
+
);
|
|
368
|
+
const payload = await response.json().catch(() => null);
|
|
369
|
+
|
|
370
|
+
if (!response.ok) {
|
|
371
|
+
throw new AgentKitError(
|
|
372
|
+
"composio_execution_failed",
|
|
373
|
+
`Composio action ${input.toolSlug} failed with HTTP ${response.status}: ${readComposioError(payload)}`,
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (!isRecord(payload)) {
|
|
378
|
+
return { successful: true, data: payload };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
successful: payload.successful !== false,
|
|
383
|
+
data: payload.data,
|
|
384
|
+
error: payload.error,
|
|
385
|
+
log_id: typeof payload.log_id === "string" ? payload.log_id : undefined,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function readComposioError(payload: unknown): string {
|
|
390
|
+
if (isRecord(payload)) {
|
|
391
|
+
if (typeof payload.error === "string") {
|
|
392
|
+
return payload.error;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (isRecord(payload.error) && typeof payload.error.message === "string") {
|
|
396
|
+
return payload.error.message;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return "unknown error";
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function stableHash(value: string): string {
|
|
404
|
+
let first = 2166136261;
|
|
405
|
+
let second = 0x811c9dc5;
|
|
406
|
+
let third = 0x01000193;
|
|
407
|
+
|
|
408
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
409
|
+
const code = value.charCodeAt(index);
|
|
410
|
+
first ^= code;
|
|
411
|
+
first = Math.imul(first, 16777619);
|
|
412
|
+
second ^= code + index;
|
|
413
|
+
second = Math.imul(second, 1597334677);
|
|
414
|
+
third ^= code * (index + 1);
|
|
415
|
+
third = Math.imul(third, 2246822519);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return [first, second, third].map((hash) => (hash >>> 0).toString(16).padStart(8, "0")).join("");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
422
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
423
|
+
}
|