@andreprado/agentkit 0.1.0-alpha.2 → 0.1.0-alpha.20

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 (135) hide show
  1. package/README.md +67 -6
  2. package/docs/guides/add-channel.md +118 -7
  3. package/docs/guides/add-knowledge.md +144 -0
  4. package/docs/guides/add-managed-composio.md +163 -0
  5. package/docs/guides/add-tool.md +1 -1
  6. package/docs/guides/channel-security.md +97 -32
  7. package/docs/guides/connect-discord.md +178 -0
  8. package/docs/guides/connect-slack.md +126 -0
  9. package/docs/guides/connect-telegram.md +78 -1
  10. package/docs/guides/connect-whatsapp-zapster.md +112 -8
  11. package/docs/guides/create-agent.md +45 -4
  12. package/docs/guides/debug-channel.md +147 -0
  13. package/docs/guides/improve-from-production.md +151 -0
  14. package/docs/guides/prepare-deploy.md +47 -17
  15. package/docs/guides/replay-production-traces.md +72 -0
  16. package/docs/guides/run-evals.md +147 -20
  17. package/docs/guides/security-rules.md +7 -6
  18. package/docs/guides/send-feedback.md +135 -0
  19. package/docs/guides/use-provider.md +27 -3
  20. package/docs/llms-full.txt +303 -55
  21. package/docs/llms.txt +57 -7
  22. package/package.json +2 -5
  23. package/src/cli/args.ts +57 -0
  24. package/src/cli/cloud-client.ts +377 -0
  25. package/src/cli/commands/channels.ts +1315 -0
  26. package/src/cli/commands/feedback.ts +438 -0
  27. package/src/cli/commands/knowledge.ts +136 -0
  28. package/src/cli/commands/transcribe.ts +171 -0
  29. package/src/cli/constants.ts +4 -0
  30. package/src/cli/deploy-chat-ui.ts +535 -0
  31. package/src/cli/deploy-readiness.ts +481 -0
  32. package/src/cli/flags.ts +162 -0
  33. package/src/cli/help.ts +236 -0
  34. package/src/cli/index.ts +1167 -1005
  35. package/src/cli/process.ts +31 -0
  36. package/src/cloud/artifact.ts +139 -0
  37. package/src/cloud/client.ts +80 -0
  38. package/src/cloud/contracts.ts +63 -0
  39. package/src/cloud/index.ts +3 -0
  40. package/src/create-project.ts +21 -6
  41. package/src/index.ts +479 -7
  42. package/src/providers/pi.ts +70 -16
  43. package/src/providers/test.ts +88 -1
  44. package/src/providers/types.ts +7 -0
  45. package/src/runtime/channel-buffer.ts +30 -0
  46. package/src/runtime/channel-test-harness.ts +8 -1
  47. package/src/runtime/channels/discord.ts +896 -0
  48. package/src/runtime/channels/slack.ts +646 -0
  49. package/src/runtime/channels/telegram.ts +466 -23
  50. package/src/runtime/channels/whatsapp-meta.ts +9 -0
  51. package/src/runtime/channels/whatsapp-zapster.ts +677 -40
  52. package/src/runtime/channels.ts +86 -3
  53. package/src/runtime/chat.ts +130 -38
  54. package/src/runtime/config.ts +483 -18
  55. package/src/runtime/core/manifest.ts +103 -5
  56. package/src/runtime/core/targets.ts +5 -5
  57. package/src/runtime/database.ts +93 -2
  58. package/src/runtime/db-commands.ts +9 -0
  59. package/src/runtime/deploy-readiness.ts +46 -4
  60. package/src/runtime/deploy.ts +1 -1
  61. package/src/runtime/dev-server.ts +759 -41
  62. package/src/runtime/env.ts +8 -3
  63. package/src/runtime/evals.ts +589 -43
  64. package/src/runtime/improve.ts +868 -0
  65. package/src/runtime/inspect.ts +194 -4
  66. package/src/runtime/integrations/composio.ts +423 -0
  67. package/src/runtime/knowledge/chunk.ts +333 -0
  68. package/src/runtime/knowledge/config.ts +135 -0
  69. package/src/runtime/knowledge/embeddings.ts +133 -0
  70. package/src/runtime/knowledge/ingest.ts +521 -0
  71. package/src/runtime/knowledge/prompt-policy.ts +30 -0
  72. package/src/runtime/knowledge/retrieve.ts +303 -0
  73. package/src/runtime/knowledge/schema.ts +100 -0
  74. package/src/runtime/knowledge/tool.ts +64 -0
  75. package/src/runtime/knowledge/vector.ts +258 -0
  76. package/src/runtime/prompt-context.ts +141 -0
  77. package/src/runtime/runtime-contract.ts +86 -8
  78. package/src/runtime/skills.ts +95 -0
  79. package/src/runtime/spec.ts +152 -0
  80. package/src/runtime/sync.ts +144 -0
  81. package/src/runtime/targets/cloudflare/build.ts +1430 -185
  82. package/src/runtime/targets/container/server.ts +1 -1
  83. package/src/runtime/targets/vps/deploy.ts +26 -9
  84. package/src/runtime/tool-runner.ts +9 -1
  85. package/src/runtime/tools.ts +128 -2
  86. package/src/runtime/traces.ts +41 -0
  87. package/src/runtime/transcription.ts +483 -0
  88. package/src/storage/sqlite.ts +149 -3
  89. package/src/templates/blank.ts +76 -17
  90. package/src/templates/dentista.ts +1011 -0
  91. package/src/templates/index.ts +2 -0
  92. package/src/templates/skills/agentkit-build-agent/SKILL.md +52 -0
  93. package/src/templates/skills/agentkit-build-agent/templates/appointment-intake.instructions.md +21 -0
  94. package/src/templates/skills/agentkit-build-agent/templates/sales-qualifier.instructions.md +17 -0
  95. package/src/templates/skills/agentkit-build-agent/templates/support-agent.instructions.md +16 -0
  96. package/src/templates/skills/agentkit-capsule/SKILL.md +70 -0
  97. package/src/templates/skills/agentkit-capsule/references/docs-router.md +15 -0
  98. package/src/templates/skills/agentkit-channels/SKILL.md +104 -0
  99. package/src/templates/skills/agentkit-channels/references/channel-buffering.md +65 -0
  100. package/src/templates/skills/agentkit-channels/references/channel-debugging.md +66 -0
  101. package/src/templates/skills/agentkit-channels/references/discord.md +93 -0
  102. package/src/templates/skills/agentkit-channels/references/slack.md +56 -0
  103. package/src/templates/skills/agentkit-channels/references/telegram.md +72 -0
  104. package/src/templates/skills/agentkit-channels/references/whatsapp-zapster.md +77 -0
  105. package/src/templates/skills/agentkit-database/SKILL.md +45 -0
  106. package/src/templates/skills/agentkit-database/templates/appointments.schema.sql +15 -0
  107. package/src/templates/skills/agentkit-database/templates/leads.schema.sql +17 -0
  108. package/src/templates/skills/agentkit-deploy/SKILL.md +50 -0
  109. package/src/templates/skills/agentkit-evals/SKILL.md +109 -0
  110. package/src/templates/skills/agentkit-evals/templates/multi-turn.eval.md +29 -0
  111. package/src/templates/skills/agentkit-evals/templates/no-leak.eval.md +18 -0
  112. package/src/templates/skills/agentkit-evals/templates/smoke.eval.md +18 -0
  113. package/src/templates/skills/agentkit-evals/templates/tool-call.eval.md +27 -0
  114. package/src/templates/skills/agentkit-improve/SKILL.md +86 -0
  115. package/src/templates/skills/agentkit-improve/references/replay-side-effects.md +18 -0
  116. package/src/templates/skills/agentkit-improve/references/trace-packets.md +22 -0
  117. package/src/templates/skills/agentkit-improve/templates/regression.eval.md +18 -0
  118. package/src/templates/skills/agentkit-integrations/SKILL.md +76 -0
  119. package/src/templates/skills/agentkit-knowledge/SKILL.md +43 -0
  120. package/src/templates/skills/agentkit-knowledge/templates/faq.md +14 -0
  121. package/src/templates/skills/agentkit-knowledge/templates/policies.md +14 -0
  122. package/src/templates/skills/agentkit-knowledge/templates/prices.csv +3 -0
  123. package/src/templates/skills/agentkit-prompts/SKILL.md +47 -0
  124. package/src/templates/skills/agentkit-prompts/templates/knowledge-grounded-faq.instructions.md +11 -0
  125. package/src/templates/skills/agentkit-provider/SKILL.md +60 -0
  126. package/src/templates/skills/agentkit-security/SKILL.md +56 -0
  127. package/src/templates/skills/agentkit-tools/SKILL.md +37 -0
  128. package/src/templates/skills/agentkit-tools/examples/database-write.tool.md +35 -0
  129. package/src/templates/skills/agentkit-tools/examples/eval-safe-external-action.tool.md +37 -0
  130. package/src/templates/skills/agentkit-tools/examples/lookup-order.tool.md +46 -0
  131. package/src/templates/skills/agentkit-troubleshooting/SKILL.md +76 -0
  132. package/src/templates/support.ts +77 -18
  133. package/docs/guides/channels-production-handoff.md +0 -99
  134. package/docs/portable-deploy-release-checklist.md +0 -41
  135. package/src/runtime/targets/cloudflare/deploy.ts +0 -5475
@@ -1,15 +1,28 @@
1
1
  import type { AgentChannel, ChannelProvider, ChannelType } from "../index";
2
2
 
3
3
  export type ChannelMessageRole = "user" | "assistant";
4
- export type ChannelMessageContentType = "text";
4
+ export type ChannelMessageContentType = "text" | "audio";
5
5
  export type ChannelEventKind = "message" | "delivery_status" | "unsupported";
6
6
  export type ChannelDeliveryDirection = "inbound" | "outbound";
7
7
  export type ChannelDeliveryState =
8
+ | "webhook_received"
8
9
  | "received"
9
10
  | "validated"
11
+ | "audio_received"
12
+ | "audio_downloaded"
13
+ | "transcribing"
14
+ | "transcribed"
10
15
  | "duplicate"
16
+ | "buffered"
11
17
  | "queued"
12
18
  | "running"
19
+ | "agent_completed"
20
+ | "adapter_stubbed"
21
+ | "provider_request_built"
22
+ | "provider_sent"
23
+ | "outbound_sent"
24
+ | "provider_failed"
25
+ | "synthetic_expected_failure"
13
26
  | "sent"
14
27
  | "delivered"
15
28
  | "failed"
@@ -36,11 +49,41 @@ export type ChannelExternalIdentity = {
36
49
  redactedDisplay?: string;
37
50
  };
38
51
 
52
+ export type ChannelReplyTarget =
53
+ | {
54
+ type: "discord_interaction";
55
+ applicationId: string;
56
+ interactionId: string;
57
+ interactionToken: string;
58
+ }
59
+ | {
60
+ type: "discord_message";
61
+ channelId: string;
62
+ messageId: string;
63
+ }
64
+ | {
65
+ type: "slack_message";
66
+ channelId: string;
67
+ messageTs: string;
68
+ threadTs?: string;
69
+ };
70
+
71
+ export type ChannelAudioAttachment = {
72
+ providerMediaId: string;
73
+ kind: "voice" | "audio";
74
+ mimeType?: string;
75
+ filename?: string;
76
+ durationSeconds?: number;
77
+ sizeBytes?: number;
78
+ downloadUrl?: string;
79
+ };
80
+
39
81
  export type NormalizedChannelMessage = {
40
82
  role: ChannelMessageRole;
41
83
  contentType: ChannelMessageContentType;
42
84
  content: string;
43
85
  providerMessageId?: string;
86
+ audio?: ChannelAudioAttachment;
44
87
  };
45
88
 
46
89
  export type NormalizedChannelEvent = ChannelAdapterId & {
@@ -53,6 +96,7 @@ export type NormalizedChannelEvent = ChannelAdapterId & {
53
96
  receivedAt: string;
54
97
  unsupportedReason?: string;
55
98
  providerMetadata?: Record<string, unknown>;
99
+ replyTarget?: ChannelReplyTarget;
56
100
  };
57
101
 
58
102
  export type WebhookVerificationInput = RawWebhookEvent & {
@@ -70,26 +114,63 @@ export type WebhookVerificationResult =
70
114
  message: string;
71
115
  };
72
116
 
117
+ export type ChannelWebhookResponse = {
118
+ status?: number;
119
+ headers?: Record<string, string>;
120
+ body: string | Record<string, unknown>;
121
+ };
122
+
73
123
  export type ChannelSendInput = ChannelAdapterId & {
74
124
  channelId: string;
75
125
  conversationId: string;
76
126
  externalIdentity: ChannelExternalIdentity;
127
+ replyTarget?: ChannelReplyTarget;
77
128
  message: NormalizedChannelMessage;
78
129
  secrets: Record<string, string>;
79
130
  };
80
131
 
132
+ export type ChannelMediaDownloadInput = ChannelAdapterId & {
133
+ channelId: string;
134
+ message: NormalizedChannelMessage;
135
+ secrets: Record<string, string>;
136
+ maxBytes?: number;
137
+ };
138
+
139
+ export type ChannelMediaDownloadResult =
140
+ | {
141
+ ok: true;
142
+ audio: Uint8Array;
143
+ filename: string;
144
+ mimeType?: string;
145
+ durationSeconds?: number;
146
+ sizeBytes?: number;
147
+ providerMetadata?: Record<string, unknown>;
148
+ }
149
+ | {
150
+ ok: false;
151
+ retryable: boolean;
152
+ code:
153
+ | "channel_secret_missing"
154
+ | "channel_audio_download_unavailable"
155
+ | "channel_audio_too_large"
156
+ | "channel_provider_unavailable"
157
+ | "channel_media_download_failed";
158
+ message: string;
159
+ providerMetadata?: Record<string, unknown>;
160
+ };
161
+
81
162
  export type ChannelSendResult =
82
163
  | {
83
164
  ok: true;
84
165
  providerRequestId?: string;
85
166
  providerMessageId?: string;
86
- status: "sent" | "delivered";
167
+ status: "stubbed" | "sent" | "delivered";
87
168
  providerMetadata?: Record<string, unknown>;
88
169
  }
89
170
  | {
90
171
  ok: false;
91
172
  retryable: boolean;
92
- code: "channel_secret_missing" | "channel_provider_unavailable" | "channel_send_failed";
173
+ code: "channel_secret_missing" | "channel_provider_unavailable" | "channel_send_failed" | "synthetic_expected_failure";
93
174
  message: string;
94
175
  providerRequestId?: string;
95
176
  providerMetadata?: Record<string, unknown>;
@@ -111,8 +192,10 @@ export type ChannelStatusResult = {
111
192
  export type ChannelAdapter = ChannelAdapterId & {
112
193
  requiredSecrets: string[];
113
194
  normalizeInbound(input: RawWebhookEvent): Promise<NormalizedChannelEvent[]> | NormalizedChannelEvent[];
195
+ downloadMedia?(input: ChannelMediaDownloadInput): Promise<ChannelMediaDownloadResult>;
114
196
  sendMessage(input: ChannelSendInput): Promise<ChannelSendResult>;
115
197
  verifyWebhook?(input: WebhookVerificationInput): Promise<WebhookVerificationResult> | WebhookVerificationResult;
198
+ getWebhookResponse?(input: WebhookVerificationInput): Promise<ChannelWebhookResponse | null> | ChannelWebhookResponse | null;
116
199
  getStatus?(input: ChannelStatusInput): Promise<ChannelStatusResult> | ChannelStatusResult;
117
200
  };
118
201
 
@@ -1,18 +1,25 @@
1
1
  import { getProviderAdapter } from "../providers";
2
2
  import type { AgentMessage, ProviderRunResult } from "../providers";
3
3
  import type { ToolRuntimeContext } from "../index";
4
- import type { PersistedChatRun } from "../storage/sqlite";
4
+ import type { ConversationRecord, ConversationSummary, PersistedChatRun } from "../storage/sqlite";
5
5
  import type { LoadedAgentCapsule } from "./config";
6
6
  import { loadAgentCapsule } from "./config";
7
7
  import { openPreparedCapsuleStore } from "./database";
8
8
  import { loadCapsuleEnv } from "./env";
9
9
  import { AgentKitError, isAgentKitError } from "./errors";
10
+ import { createManagedComposioTools } from "./integrations/composio";
11
+ import { syncConfiguredKnowledgeSources } from "./knowledge/ingest";
12
+ import { appendKnowledgePromptPolicy } from "./knowledge/prompt-policy";
13
+ import { createKnowledgeSearchTool } from "./knowledge/tool";
14
+ import { appendRuntimePromptContext, buildRuntimeClockContext } from "./prompt-context";
10
15
  import { createToolRuntime } from "./tools";
11
16
 
12
17
  export type RunAgentMessageOptions = {
13
18
  message: string;
19
+ conversationId?: string;
14
20
  signal?: AbortSignal;
15
21
  runtime?: Partial<ToolRuntimeContext>;
22
+ now?: Date;
16
23
  };
17
24
 
18
25
  export type AgentRunResult = ProviderRunResult & PersistedChatRun;
@@ -36,46 +43,66 @@ export async function runAgentMessage(
36
43
 
37
44
  const adapter = getProviderAdapter(capsule.config.provider);
38
45
  const env = await loadCapsuleEnv(capsule.root);
39
- const messages: AgentMessage[] = [{ role: "user", content }];
46
+ await syncConfiguredKnowledgeSources(capsule);
47
+ const conversationId = normalizeConversationId(options.conversationId);
40
48
  const { store } = await openPreparedCapsuleStore(capsule);
41
- const conversation = store.createConversation({
42
- agentName: capsule.config.name,
43
- title: titleFromMessage(content),
44
- });
45
- const userMessage = store.appendMessage({
46
- conversationId: conversation.id,
47
- role: "user",
48
- content,
49
- });
50
- const run = store.createRun({
51
- conversationId: conversation.id,
52
- provider: capsule.config.provider.name,
53
- model: capsule.config.provider.model,
54
- });
55
- const toolRuntime = createToolRuntime({
56
- tools: capsule.config.tools ?? [],
57
- store,
58
- runId: run.id,
59
- conversationId: conversation.id,
60
- env,
61
- signal: options.signal,
62
- runtime: {
63
- environment: "local",
64
- invocation: "chat",
65
- target: "bun",
66
- database: "sqlite",
67
- ...options.runtime,
68
- },
69
- });
49
+ let runId: string | null = null;
70
50
 
71
51
  try {
52
+ const { conversation, previousMessages } = resolveConversation(store, {
53
+ agentName: capsule.config.name,
54
+ conversationId,
55
+ firstMessage: content,
56
+ });
57
+ const messages: AgentMessage[] = [...previousMessages, { role: "user", content }];
58
+ const userMessage = store.appendMessage({
59
+ conversationId: conversation.id,
60
+ role: "user",
61
+ content,
62
+ });
63
+ const run = store.createRun({
64
+ conversationId: conversation.id,
65
+ provider: capsule.config.provider.name,
66
+ model: capsule.config.provider.model,
67
+ });
68
+ runId = run.id;
69
+ const runtimeTools = withKnowledgeTools(capsule);
70
+ const clock = buildRuntimeClockContext({
71
+ timeZone: capsule.config.timeZone,
72
+ env,
73
+ now: options.now,
74
+ });
75
+ const toolRuntime = createToolRuntime({
76
+ tools: runtimeTools,
77
+ store,
78
+ runId: run.id,
79
+ conversationId: conversation.id,
80
+ env,
81
+ signal: options.signal,
82
+ clock,
83
+ runtime: {
84
+ environment: "local",
85
+ invocation: "chat",
86
+ target: "bun",
87
+ database: "sqlite",
88
+ ...options.runtime,
89
+ },
90
+ });
72
91
  const result = await adapter.run({
73
92
  agent: {
74
93
  name: capsule.config.name,
75
94
  runtime: capsule.config.runtime,
76
95
  provider: capsule.config.provider,
77
96
  },
78
- instructions: withToolVisibilityInstructions(capsule.instructions, capsule.config.tools ?? []),
97
+ instructions: appendRuntimePromptContext(
98
+ withToolVisibilityInstructions(
99
+ appendKnowledgePromptPolicy(capsule.instructions, capsule.config),
100
+ runtimeTools,
101
+ ),
102
+ {
103
+ clock,
104
+ },
105
+ ),
79
106
  messages,
80
107
  tools: toolRuntime.definitions,
81
108
  toolRuntime,
@@ -103,12 +130,14 @@ export async function runAgentMessage(
103
130
  assistantMessageId: assistantMessage.id,
104
131
  };
105
132
  } catch (error) {
106
- store.completeRun({
107
- runId: run.id,
108
- status: "failed",
109
- errorCode: isAgentKitError(error) ? error.code : "runtime_error",
110
- errorMessage: error instanceof Error ? error.message : String(error),
111
- });
133
+ if (runId) {
134
+ store.completeRun({
135
+ runId,
136
+ status: "failed",
137
+ errorCode: isAgentKitError(error) ? error.code : "runtime_error",
138
+ errorMessage: error instanceof Error ? error.message : String(error),
139
+ });
140
+ }
112
141
 
113
142
  throw error;
114
143
  } finally {
@@ -116,6 +145,69 @@ export async function runAgentMessage(
116
145
  }
117
146
  }
118
147
 
148
+ function withKnowledgeTools(capsule: LoadedAgentCapsule): NonNullable<LoadedAgentCapsule["config"]["tools"]> {
149
+ const tools = [...createManagedComposioTools(capsule.config), ...(capsule.config.tools ?? [])];
150
+ const knowledgeTool = createKnowledgeSearchTool(capsule);
151
+
152
+ if (knowledgeTool) {
153
+ return [knowledgeTool, ...tools];
154
+ }
155
+
156
+ return tools;
157
+ }
158
+
159
+ function resolveConversation(
160
+ store: Awaited<ReturnType<typeof openPreparedCapsuleStore>>["store"],
161
+ input: {
162
+ agentName: string;
163
+ conversationId?: string;
164
+ firstMessage: string;
165
+ },
166
+ ): { conversation: ConversationSummary; previousMessages: AgentMessage[] } {
167
+ if (input.conversationId) {
168
+ const existing = store.getConversation(input.conversationId);
169
+
170
+ if (existing) {
171
+ return {
172
+ conversation: existing,
173
+ previousMessages: toProviderMessages(existing),
174
+ };
175
+ }
176
+ }
177
+
178
+ const conversation = store.createConversation({
179
+ id: input.conversationId,
180
+ agentName: input.agentName,
181
+ title: titleFromMessage(input.firstMessage),
182
+ });
183
+
184
+ return {
185
+ conversation,
186
+ previousMessages: [],
187
+ };
188
+ }
189
+
190
+ function toProviderMessages(conversation: ConversationRecord): AgentMessage[] {
191
+ return conversation.messages.map((message) => ({
192
+ role: message.role,
193
+ content: message.content,
194
+ }));
195
+ }
196
+
197
+ function normalizeConversationId(value: string | undefined): string | undefined {
198
+ if (value === undefined) {
199
+ return undefined;
200
+ }
201
+
202
+ const conversationId = value.trim();
203
+
204
+ if (!conversationId) {
205
+ throw new AgentKitError("validation_error", "conversationId must be a non-empty string when provided.");
206
+ }
207
+
208
+ return conversationId;
209
+ }
210
+
119
211
  function withToolVisibilityInstructions(instructions: string, tools: LoadedAgentCapsule["config"]["tools"] = []): string {
120
212
  const internalTools = tools.filter((tool) => tool.visibility === "internal").map((tool) => tool.name).sort();
121
213