@andreprado/agentkit 0.1.0-alpha.16 → 0.1.0-alpha.18
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 +8 -0
- package/docs/guides/add-channel.md +4 -2
- package/docs/guides/add-managed-composio.md +161 -0
- package/docs/guides/channel-security.md +36 -39
- package/docs/guides/connect-telegram.md +3 -1
- package/docs/guides/debug-channel.md +141 -0
- package/docs/guides/prepare-deploy.md +7 -10
- package/docs/llms-full.txt +19 -5
- package/docs/llms.txt +11 -2
- package/package.json +1 -3
- package/src/cli/cloud-client.ts +12 -0
- package/src/cli/commands/channels.ts +119 -7
- package/src/cli/commands/transcribe.ts +171 -0
- package/src/cli/deploy-chat-ui.ts +146 -3
- package/src/cli/deploy-readiness.ts +112 -3
- package/src/cli/help.ts +19 -4
- package/src/cli/index.ts +198 -3
- package/src/create-project.ts +4 -32
- package/src/index.ts +149 -0
- package/src/runtime/chat.ts +2 -1
- package/src/runtime/config.ts +133 -0
- package/src/runtime/core/manifest.ts +31 -2
- package/src/runtime/deploy-readiness.ts +31 -1
- package/src/runtime/inspect.ts +109 -4
- package/src/runtime/integrations/composio.ts +423 -0
- package/src/runtime/skills.ts +95 -0
- package/src/runtime/targets/cloudflare/build.ts +395 -10
- package/src/runtime/tool-runner.ts +2 -1
- package/src/templates/skills/agentkit-capsule/SKILL.md +1 -0
- package/src/templates/skills/agentkit-channels/SKILL.md +2 -0
- package/src/templates/skills/agentkit-channels/references/channel-debugging.md +3 -1
- package/src/templates/skills/agentkit-channels/references/telegram.md +3 -1
- package/src/templates/skills/agentkit-deploy/SKILL.md +1 -1
- package/src/templates/skills/agentkit-integrations/SKILL.md +75 -0
- package/src/templates/skills/agentkit-troubleshooting/SKILL.md +3 -2
- package/docs/guides/agentkit-skills-architecture.md +0 -472
- package/docs/guides/channels-implementation-map.md +0 -243
- package/docs/guides/channels-production-handoff.md +0 -118
- package/docs/portable-deploy-release-checklist.md +0 -41
package/src/runtime/inspect.ts
CHANGED
|
@@ -4,9 +4,15 @@ import type { AgentChannel, AgentProvider, AgentRuntime, AccessMode } from "../i
|
|
|
4
4
|
import type { LoadedAgentCapsule } from "./config";
|
|
5
5
|
import { loadAgentCapsule } from "./config";
|
|
6
6
|
import { loadCapsuleEnv } from "./env";
|
|
7
|
+
import {
|
|
8
|
+
createManagedComposioTools,
|
|
9
|
+
managedComposioRequiredSecrets,
|
|
10
|
+
resolveManagedComposioIntegrations,
|
|
11
|
+
} from "./integrations/composio";
|
|
7
12
|
import { resolveKnowledgeConfig } from "./knowledge/config";
|
|
8
13
|
import { KNOWLEDGE_SEARCH_TOOL_NAME } from "./knowledge/tool";
|
|
9
14
|
import { resolveRuntimeTimeZone } from "./prompt-context";
|
|
15
|
+
import { checkDefaultSkillStatus, type DefaultSkillStatus } from "./skills";
|
|
10
16
|
import { resolveTranscriptionConfig } from "./transcription";
|
|
11
17
|
|
|
12
18
|
export type SecretState = "set" | "missing";
|
|
@@ -21,6 +27,7 @@ export type AgentInspectState = {
|
|
|
21
27
|
};
|
|
22
28
|
prompt: string;
|
|
23
29
|
tools: string[];
|
|
30
|
+
integrations: AgentInspectIntegration[];
|
|
24
31
|
channels: AgentInspectChannel[];
|
|
25
32
|
transcription: {
|
|
26
33
|
enabled: boolean;
|
|
@@ -71,6 +78,8 @@ export type AgentInspectState = {
|
|
|
71
78
|
secrets: Record<string, SecretState>;
|
|
72
79
|
managedSecrets: string[];
|
|
73
80
|
userSecrets: string[];
|
|
81
|
+
skills: AgentInspectSkills;
|
|
82
|
+
warnings: AgentInspectWarning[];
|
|
74
83
|
};
|
|
75
84
|
|
|
76
85
|
export type AgentInspectDatabase = {
|
|
@@ -97,23 +106,48 @@ export type AgentInspectChannel = {
|
|
|
97
106
|
audio?: AgentChannel["audio"];
|
|
98
107
|
};
|
|
99
108
|
|
|
109
|
+
export type AgentInspectSkills = {
|
|
110
|
+
status: "current" | "missing" | "outdated" | "not_checked";
|
|
111
|
+
checked: number;
|
|
112
|
+
missing: string[];
|
|
113
|
+
outdated: string[];
|
|
114
|
+
syncCommand: string | null;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export type AgentInspectWarning = {
|
|
118
|
+
code: string;
|
|
119
|
+
message: string;
|
|
120
|
+
command?: string;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export type AgentInspectIntegration = {
|
|
124
|
+
name: string;
|
|
125
|
+
provider: "composio";
|
|
126
|
+
mode: "managed";
|
|
127
|
+
toolkits: string[];
|
|
128
|
+
tools: string[];
|
|
129
|
+
userId: string;
|
|
130
|
+
};
|
|
131
|
+
|
|
100
132
|
export async function inspectAgentCapsule(
|
|
101
133
|
cwd = process.cwd(),
|
|
102
134
|
env: Record<string, string | undefined> = process.env,
|
|
103
135
|
): Promise<AgentInspectState> {
|
|
104
136
|
const capsule = await loadAgentCapsule(cwd);
|
|
105
|
-
return buildInspectState(capsule, await loadCapsuleEnv(capsule.root, env));
|
|
137
|
+
return buildInspectState(capsule, await loadCapsuleEnv(capsule.root, env), await checkDefaultSkillStatus(capsule.root));
|
|
106
138
|
}
|
|
107
139
|
|
|
108
140
|
export function buildInspectState(
|
|
109
141
|
capsule: LoadedAgentCapsule,
|
|
110
142
|
env: Record<string, string | undefined> = process.env,
|
|
143
|
+
skillStatus: DefaultSkillStatus | null = null,
|
|
111
144
|
): AgentInspectState {
|
|
112
145
|
const declaredSecrets = new Set(capsule.config.secrets);
|
|
113
146
|
const knowledge = resolveKnowledgeConfig(capsule.config);
|
|
147
|
+
const integrationTools = createManagedComposioTools(capsule.config);
|
|
114
148
|
const transcription = resolveTranscriptionConfig(capsule.config.transcription);
|
|
115
149
|
|
|
116
|
-
for (const tool of capsule.config.tools ?? []) {
|
|
150
|
+
for (const tool of [...integrationTools, ...(capsule.config.tools ?? [])]) {
|
|
117
151
|
for (const secret of tool.secrets ?? []) {
|
|
118
152
|
declaredSecrets.add(secret);
|
|
119
153
|
}
|
|
@@ -134,7 +168,10 @@ export function buildInspectState(
|
|
|
134
168
|
}
|
|
135
169
|
|
|
136
170
|
const managedSecrets = managedCloudSecrets(capsule);
|
|
137
|
-
const
|
|
171
|
+
const managedSecretSet = new Set(managedSecrets);
|
|
172
|
+
const userSecrets = Array.from(declaredSecrets)
|
|
173
|
+
.filter((secret) => !managedSecretSet.has(secret))
|
|
174
|
+
.sort();
|
|
138
175
|
const secrets: Record<string, SecretState> = {};
|
|
139
176
|
|
|
140
177
|
for (const secret of userSecrets) {
|
|
@@ -143,6 +180,9 @@ export function buildInspectState(
|
|
|
143
180
|
|
|
144
181
|
const database = inspectDatabase(capsule);
|
|
145
182
|
|
|
183
|
+
const skills = inspectSkills(skillStatus);
|
|
184
|
+
const warnings = inspectWarnings(capsule, transcription, skills);
|
|
185
|
+
|
|
146
186
|
return {
|
|
147
187
|
agent: capsule.config.name,
|
|
148
188
|
runtime: capsule.config.runtime,
|
|
@@ -155,7 +195,16 @@ export function buildInspectState(
|
|
|
155
195
|
}),
|
|
156
196
|
},
|
|
157
197
|
prompt: toCapsulePath(capsule.root, capsule.instructionsPath),
|
|
158
|
-
tools: (capsule.config.tools ?? []).map((tool) => tool.name),
|
|
198
|
+
tools: [...integrationTools, ...(capsule.config.tools ?? [])].map((tool) => tool.name),
|
|
199
|
+
integrations: resolveManagedComposioIntegrations(capsule.config).map((integration) => ({
|
|
200
|
+
name: integration.name,
|
|
201
|
+
provider: integration.provider,
|
|
202
|
+
mode: integration.mode,
|
|
203
|
+
toolkits: integration.toolkits,
|
|
204
|
+
tools: integration.allowedTools,
|
|
205
|
+
userId: integration.userId,
|
|
206
|
+
confirmExternalWrites: integration.confirmExternalWrites !== false,
|
|
207
|
+
})),
|
|
159
208
|
channels: (capsule.config.channels ?? []).map((channel) => ({
|
|
160
209
|
name: channel.name,
|
|
161
210
|
type: channel.type,
|
|
@@ -208,6 +257,8 @@ export function buildInspectState(
|
|
|
208
257
|
secrets,
|
|
209
258
|
managedSecrets,
|
|
210
259
|
userSecrets,
|
|
260
|
+
skills,
|
|
261
|
+
warnings,
|
|
211
262
|
};
|
|
212
263
|
}
|
|
213
264
|
|
|
@@ -237,6 +288,56 @@ function inspectDatabase(capsule: LoadedAgentCapsule): AgentInspectDatabase {
|
|
|
237
288
|
};
|
|
238
289
|
}
|
|
239
290
|
|
|
291
|
+
function inspectSkills(status: DefaultSkillStatus | null): AgentInspectSkills {
|
|
292
|
+
if (!status) {
|
|
293
|
+
return {
|
|
294
|
+
status: "not_checked",
|
|
295
|
+
checked: 0,
|
|
296
|
+
missing: [],
|
|
297
|
+
outdated: [],
|
|
298
|
+
syncCommand: null,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
status: status.status,
|
|
304
|
+
checked: status.checked,
|
|
305
|
+
missing: status.missing,
|
|
306
|
+
outdated: status.outdated,
|
|
307
|
+
syncCommand: status.status === "current" ? null : "npm run agentkit -- skills sync",
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function inspectWarnings(
|
|
312
|
+
capsule: LoadedAgentCapsule,
|
|
313
|
+
transcription: ReturnType<typeof resolveTranscriptionConfig>,
|
|
314
|
+
skills: AgentInspectSkills,
|
|
315
|
+
): AgentInspectWarning[] {
|
|
316
|
+
const warnings: AgentInspectWarning[] = [];
|
|
317
|
+
|
|
318
|
+
if (skills.status === "missing" || skills.status === "outdated") {
|
|
319
|
+
warnings.push({
|
|
320
|
+
code: "local_skills_outdated",
|
|
321
|
+
message: "Local AgentKit skills are older than or missing from the installed package docs.",
|
|
322
|
+
command: "npm run agentkit -- skills sync",
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (
|
|
327
|
+
transcription.enabled &&
|
|
328
|
+
transcription.provider === "openai" &&
|
|
329
|
+
(capsule.config.channels ?? []).some((channel) => channel.type === "telegram" && channel.audio?.mode === "transcribe")
|
|
330
|
+
) {
|
|
331
|
+
warnings.push({
|
|
332
|
+
code: "telegram_audio_openai_format",
|
|
333
|
+
message: "Telegram voice notes arrive as OGG/Opus; use Groq for the default Telegram voice-note transcription path.",
|
|
334
|
+
command: "Set transcription.provider to \"groq\" and secret to \"GROQ_API_KEY\".",
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return warnings;
|
|
339
|
+
}
|
|
340
|
+
|
|
240
341
|
function managedCloudSecrets(capsule: LoadedAgentCapsule): string[] {
|
|
241
342
|
const secrets = new Set<string>();
|
|
242
343
|
|
|
@@ -245,5 +346,9 @@ function managedCloudSecrets(capsule: LoadedAgentCapsule): string[] {
|
|
|
245
346
|
secrets.add("TURSO_DATABASE_URL");
|
|
246
347
|
}
|
|
247
348
|
|
|
349
|
+
for (const secret of managedComposioRequiredSecrets(capsule.config)) {
|
|
350
|
+
secrets.add(secret);
|
|
351
|
+
}
|
|
352
|
+
|
|
248
353
|
return Array.from(secrets).sort();
|
|
249
354
|
}
|
|
@@ -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
|
+
}
|