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

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.
@@ -0,0 +1,48 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFile } from "node:fs/promises";
3
+
4
+ import type { AgentBuildResult, AgentManifestWithBundle } from "../runtime/targets/cloudflare/build";
5
+ import { AgentKitError } from "../runtime/errors";
6
+ import type { HostedDeployArtifact } from "./contracts";
7
+
8
+ export async function createHostedDeployArtifact(build: AgentBuildResult): Promise<HostedDeployArtifact> {
9
+ const [manifestSource, workerSource, wranglerSource] = await Promise.all([
10
+ readFile(build.manifestPath, "utf8"),
11
+ readFile(build.workerPath, "utf8"),
12
+ readFile(build.wranglerPath, "utf8"),
13
+ ]);
14
+ const manifest = JSON.parse(manifestSource) as AgentManifestWithBundle;
15
+ const workerSha256 = sha256(workerSource);
16
+
17
+ if (workerSha256 !== build.bundleSha256 || workerSha256 !== manifest.bundle.sha256) {
18
+ throw new AgentKitError("artifact_integrity_error", "Worker SHA-256 does not match the build manifest.");
19
+ }
20
+
21
+ const artifact: HostedDeployArtifact = {
22
+ manifest,
23
+ worker: {
24
+ content_type: "application/javascript",
25
+ source: workerSource,
26
+ sha256: workerSha256,
27
+ },
28
+ wrangler: {
29
+ content_type: "application/json",
30
+ source: wranglerSource,
31
+ },
32
+ };
33
+
34
+ if (build.tursoSchemaPath) {
35
+ const source = await readFile(build.tursoSchemaPath, "utf8");
36
+ artifact.turso_schema = {
37
+ content_type: "text/sql",
38
+ source,
39
+ sha256: sha256(source),
40
+ };
41
+ }
42
+
43
+ return artifact;
44
+ }
45
+
46
+ function sha256(source: string): string {
47
+ return createHash("sha256").update(source).digest("hex");
48
+ }
@@ -0,0 +1,79 @@
1
+ import { mkdir, writeFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+
4
+ import type { AgentBuildResult } from "../runtime/targets/cloudflare/build";
5
+ import type { LocalDeployState } from "../runtime/core/deploy-state";
6
+ import { AgentKitError } from "../runtime/errors";
7
+ import { createHostedDeployArtifact } from "./artifact";
8
+ import type { AgentKitCloudDeployOptions, CreateDeployRequest, CreateDeployResponse } from "./contracts";
9
+
10
+ export async function deployBuildToAgentKitCloud(
11
+ build: AgentBuildResult,
12
+ options: AgentKitCloudDeployOptions,
13
+ ): Promise<CreateDeployResponse> {
14
+ const artifact = await createHostedDeployArtifact(build);
15
+ const headers = new Headers({
16
+ "Content-Type": "application/json",
17
+ });
18
+ const apiToken = options.apiToken ?? process.env.AGENTKIT_CLOUD_API_TOKEN;
19
+
20
+ if (apiToken) {
21
+ headers.set("Authorization", `Bearer ${apiToken}`);
22
+ }
23
+
24
+ const response = await fetch(new URL("/v1/deploys", normalizeApiUrl(options.apiUrl)), {
25
+ method: "POST",
26
+ headers,
27
+ body: JSON.stringify({
28
+ project_name: artifact.manifest.name,
29
+ anonymous: options.anonymous ?? !apiToken,
30
+ artifact,
31
+ } satisfies CreateDeployRequest),
32
+ });
33
+ const payload = await response.json().catch(() => null);
34
+
35
+ if (!response.ok) {
36
+ const code =
37
+ payload && typeof payload === "object" && "error" in payload
38
+ ? String((payload as { error?: { code?: unknown } }).error?.code ?? "cloud_deploy_failed")
39
+ : "cloud_deploy_failed";
40
+ const message =
41
+ payload && typeof payload === "object" && "error" in payload
42
+ ? String((payload as { error?: { message?: unknown } }).error?.message ?? "AgentKit Cloud deploy failed.")
43
+ : `AgentKit Cloud deploy failed with HTTP ${response.status}.`;
44
+ throw new AgentKitError(code, message);
45
+ }
46
+
47
+ return payload as CreateDeployResponse;
48
+ }
49
+
50
+ export async function writeLocalDeployState(root: string, deploy: CreateDeployResponse): Promise<string> {
51
+ const statePath = join(root, ".agentkit/deploy.json");
52
+ const state: LocalDeployState = {
53
+ project_id: deploy.project.id,
54
+ deploy_id: deploy.deploy.id,
55
+ url: deploy.deploy.url,
56
+ target: "cloudflare",
57
+ runtime: "edge",
58
+ resources: {
59
+ cloudflare: {
60
+ worker_url: deploy.deploy.url,
61
+ durable_object_class: "AgentKitConversationStore",
62
+ },
63
+ },
64
+ owner_state: deploy.project.owner_state,
65
+ access: {
66
+ mode: deploy.deploy.access.mode,
67
+ private_token: deploy.deploy.access.private_token ?? undefined,
68
+ },
69
+ created_at: new Date().toISOString(),
70
+ };
71
+
72
+ await mkdir(join(root, ".agentkit"), { recursive: true });
73
+ await writeFile(statePath, `${JSON.stringify(state, null, 2)}\n`);
74
+ return statePath;
75
+ }
76
+
77
+ function normalizeApiUrl(value: string): string {
78
+ return value.endsWith("/") ? value : `${value}/`;
79
+ }
@@ -0,0 +1,47 @@
1
+ import type { AgentManifestWithBundle } from "../runtime/targets/cloudflare/build";
2
+
3
+ export type HostedDeployArtifact = {
4
+ manifest: AgentManifestWithBundle;
5
+ worker: {
6
+ content_type: "application/javascript";
7
+ source: string;
8
+ sha256: string;
9
+ };
10
+ wrangler: {
11
+ content_type: "application/json";
12
+ source: string;
13
+ };
14
+ turso_schema?: {
15
+ content_type: "text/sql";
16
+ source: string;
17
+ sha256: string;
18
+ };
19
+ };
20
+
21
+ export type CreateDeployRequest = {
22
+ project_name: string;
23
+ anonymous: boolean;
24
+ artifact: HostedDeployArtifact;
25
+ };
26
+
27
+ export type CreateDeployResponse = {
28
+ project: {
29
+ id: string;
30
+ owner_state: "anonymous" | "claimed";
31
+ };
32
+ deploy: {
33
+ id: string;
34
+ status: "ready" | "failed";
35
+ url: string;
36
+ access: {
37
+ mode: string;
38
+ private_token?: string;
39
+ };
40
+ };
41
+ };
42
+
43
+ export type AgentKitCloudDeployOptions = {
44
+ apiUrl: string;
45
+ apiToken?: string;
46
+ anonymous?: boolean;
47
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./artifact";
2
+ export * from "./client";
3
+ export * from "./contracts";
package/src/index.ts CHANGED
@@ -244,7 +244,7 @@ export { runToolFromCwd } from "./runtime/tool-runner";
244
244
  export { startAgentDevServer } from "./runtime/dev-server";
245
245
  export { inspectAgentCapsule } from "./runtime/inspect";
246
246
  export { buildAgentCapsule } from "./runtime/build";
247
- export { deployBuildToAgentKitCloud, startAgentKitCloudServer } from "./runtime/deploy";
247
+ export { deployBuildToAgentKitCloud } from "./runtime/deploy";
248
248
  export { AgentKitError } from "./runtime/errors";
249
249
  export type {
250
250
  ChannelAdapter,
@@ -1 +1 @@
1
- export * from "./targets/cloudflare/deploy";
1
+ export * from "../cloud";
@@ -662,6 +662,10 @@ export default {
662
662
  });
663
663
  }
664
664
 
665
+ if (request.method === "GET" && url.pathname === "/health") {
666
+ return Response.json({ status: "ok", agent: manifest.name });
667
+ }
668
+
665
669
  if (request.method === "POST" && (url.pathname === "/chat" || url.pathname === "/v1/chat")) {
666
670
  return handleChat(request, env);
667
671
  }
@@ -156,7 +156,7 @@ Start building immediately:
156
156
 
157
157
  ## Local Commands
158
158
 
159
- - \`npm install\`: install capsule dependencies.
159
+ - \`npm install\`: restore capsule dependencies if this capsule used \`--no-install\`, install failed, or \`node_modules\` was deleted.
160
160
  - \`npm run dev\`: run the local Agent Capsule runtime.
161
161
  - \`npm run chat -- --message "hello"\`: send one local chat message.
162
162
  - \`npm run eval\`: run agent evals.
@@ -166,7 +166,7 @@ Start building immediately:
166
166
  - \`npm run agentkit -- db seed --file seed.sql\`: apply local fixture data after migrate.
167
167
  - \`npm run agentkit -- db shell\`: inspect local development data when needed.
168
168
  - \`npm run agentkit -- inspect\`: print machine-readable capsule state.
169
- - \`npm run agentkit -- env set <NAME> <VALUE>\`: write a local secret value to ignored \`.env\`.
169
+ - \`printf %s "$VALUE" | npm run agentkit -- env set <NAME> --stdin\`: write a local secret value to ignored \`.env\` without putting it in shell history.
170
170
  - \`npm run agentkit -- env list\`: list local secret names without printing values.
171
171
  - \`npm run agentkit -- docs full\`: print the full AgentKit contract path.
172
172
 
@@ -210,9 +210,9 @@ Use \`ctx.db\` as the canonical helper. \`ctx.database\` and \`ctx.storage.sql\`
210
210
  - AgentKit owns the hosted runtime, managed database, file storage, and production secret injection.
211
211
  - Local scaffold, dev, chat, eval, inspect, database, and build commands are token-free.
212
212
  - Hosted deploy requires an invited AgentKit Cloud alpha token. If no token is stored yet, ask the owner for one and run \`npm run agentkit -- login --token <token>\`.
213
- - Put production secret values into managed secrets with \`npm run agentkit -- secret set <NAME> <VALUE>\`, not into committed files.
213
+ - Put production secret values into managed secrets with \`npm run agentkit -- secret set <NAME> --from-local-env\`, \`--from-env\`, or \`--stdin\`, not into committed files or shell history.
214
214
  - The user should not choose a deploy target, create hosted databases, create buckets, copy production secrets into this capsule, or run operator/admin commands.
215
- - After a hosted deploy, create client-facing deploy access tokens with \`npm run agentkit -- access token create <name>\`.
215
+ - Hosted deploy writes the local chat/UI deploy access token to \`.agentkit/chat-access-token.json\`. Create extra client-facing tokens with \`npm run agentkit -- access token create <name> --out <path>\`.
216
216
 
217
217
  ## Rules
218
218
 
@@ -249,16 +249,17 @@ Turn the request into a working local capsule:
249
249
  ## Local Commands
250
250
 
251
251
  \`\`\`sh
252
- npm install
253
252
  npm run typecheck
254
253
  npm run agentkit -- inspect
255
254
  npm run chat -- --message "hello"
256
255
  \`\`\`
257
256
 
257
+ \`agentkit new\` installs dependencies by default. Run \`npm install\` only if the capsule was created with \`--no-install\`, install failed, or \`node_modules\` was deleted.
258
+
258
259
  Set local development secrets without opening code:
259
260
 
260
261
  \`\`\`sh
261
- npm run agentkit -- env set OPENAI_API_KEY "<value>"
262
+ printf %s "$OPENAI_API_KEY" | npm run agentkit -- env set OPENAI_API_KEY --stdin
262
263
  npm run agentkit -- inspect
263
264
  npm run chat -- --message "hello"
264
265
  \`\`\`
@@ -283,9 +284,9 @@ This capsule is hosted-deploy ready by default.
283
284
  2. Put agent-owned tables in \`schema.sql\`. AgentKit applies it locally and migrates/provisions hosted storage during deploy.
284
285
  3. Run \`npm run agentkit -- build\` only when you want to validate the artifact locally.
285
286
  4. If the owner has not logged in yet, ask for an invited AgentKit Cloud alpha token and run \`npm run agentkit -- login --token <token>\`.
286
- 5. Put production secrets into managed secrets with \`npm run agentkit -- secret set <NAME> <VALUE>\`.
287
+ 5. Put production secrets into managed secrets with \`npm run agentkit -- secret set <NAME> --from-local-env\`, \`--from-env\`, or \`--stdin\`.
287
288
  6. Run \`npm run agentkit -- deploy\`.
288
- 7. Create client-facing deploy access tokens with \`npm run agentkit -- access token create <name>\` when a website or app needs to call the hosted agent.
289
+ 7. Run \`npm run agentkit -- chat-ui --deploy\` to test the hosted agent through a local UI using the auto-created \`.agentkit/chat-access-token.json\`. Create extra client-facing deploy access tokens with \`npm run agentkit -- access token create <name> --out <path>\` when a separate website or app needs its own credential.
289
290
 
290
291
  AgentKit owns hosted infrastructure and production secrets. Do not put production secret values in this capsule. Do not run operator/admin commands from a user capsule.
291
292
  `,
@@ -316,11 +317,12 @@ Generated by AgentKit as an Agent Capsule.
316
317
  ## Setup
317
318
 
318
319
  \`\`\`sh
319
- npm install
320
320
  npm run chat -- --message "hello"
321
321
  npm run dev
322
322
  \`\`\`
323
323
 
324
+ \`agentkit new\` installs dependencies by default. Run \`npm install\` only if this capsule was created with \`--no-install\`, install failed, or \`node_modules\` was deleted.
325
+
324
326
  \`agentkit.config.ts\` uses the built-in \`test/fake\` provider by default, so the first chat works without editing \`.env\`.
325
327
  When a real provider or tool needs a local development secret, keep the required name in \`.env.schema\`, keep the value in ignored \`.env\`, and run AgentKit commands normally; the local runtime loads \`.env\` directly.
326
328
 
@@ -199,13 +199,13 @@ Start building immediately:
199
199
 
200
200
  ## Local Commands
201
201
 
202
- - \`npm install\`: install capsule dependencies.
202
+ - \`npm install\`: restore capsule dependencies if this capsule used \`--no-install\`, install failed, or \`node_modules\` was deleted.
203
203
  - \`npm run chat -- --message "hello"\`: send one local chat message.
204
204
  - \`npm run agentkit -- tool lookup_order --input '{"orderId":"A100"}'\`: test the example tool directly.
205
205
  - \`npm run eval\`: run agent evals.
206
206
  - \`npm run dev\`: run the local Agent Capsule runtime.
207
207
  - \`npm run agentkit -- inspect\`: print machine-readable capsule state.
208
- - \`npm run agentkit -- env set <NAME> <VALUE>\`: write a local secret value to ignored \`.env\`.
208
+ - \`printf %s "$VALUE" | npm run agentkit -- env set <NAME> --stdin\`: write a local secret value to ignored \`.env\` without putting it in shell history.
209
209
  - \`npm run agentkit -- env list\`: list local secret names without printing values.
210
210
  - \`npm run agentkit -- docs full\`: print the full AgentKit contract path.
211
211
 
@@ -213,9 +213,9 @@ Start building immediately:
213
213
 
214
214
  - Local scaffold, chat, eval, dev, inspect, tool, and build commands are token-free.
215
215
  - Hosted deploy requires an invited AgentKit Cloud alpha token. If no token is stored yet, ask the owner for one and run \`npm run agentkit -- login --token <token>\`.
216
- - Put production secret values into managed secrets with \`npm run agentkit -- secret set <NAME> <VALUE>\`, not into committed files.
216
+ - Put production secret values into managed secrets with \`npm run agentkit -- secret set <NAME> --from-local-env\`, \`--from-env\`, or \`--stdin\`, not into committed files or shell history.
217
217
  - Deploy with \`npm run agentkit -- deploy\`, then check \`npm run agentkit -- deploy status\`.
218
- - Create client-facing deploy access tokens with \`npm run agentkit -- access token create <name>\` when a website or app needs to call the hosted agent.
218
+ - Hosted deploy writes the local chat/UI deploy access token to \`.agentkit/chat-access-token.json\`. Create extra client-facing tokens with \`npm run agentkit -- access token create <name> --out <path>\` when a separate website or app needs its own credential.
219
219
  - Do not run operator/admin commands from a user capsule.
220
220
 
221
221
  ## Files
@@ -257,17 +257,18 @@ Turn the request into a working local capsule:
257
257
  ## Local Commands
258
258
 
259
259
  \`\`\`sh
260
- npm install
261
260
  npm run typecheck
262
261
  npm run agentkit -- inspect
263
262
  npm run agentkit -- tool lookup_order --input '{"orderId":"A100"}'
264
263
  npm run eval
265
264
  \`\`\`
266
265
 
266
+ \`agentkit new\` installs dependencies by default. Run \`npm install\` only if the capsule was created with \`--no-install\`, install failed, or \`node_modules\` was deleted.
267
+
267
268
  Set local development secrets without opening code:
268
269
 
269
270
  \`\`\`sh
270
- npm run agentkit -- env set OPENAI_API_KEY "<value>"
271
+ printf %s "$OPENAI_API_KEY" | npm run agentkit -- env set OPENAI_API_KEY --stdin
271
272
  npm run agentkit -- inspect
272
273
  npm run chat -- --message "hello"
273
274
  \`\`\`
@@ -305,9 +306,9 @@ This capsule is deploy-ready by default.
305
306
  1. Keep tools edge-safe and use \`ctx.db\` instead of importing database drivers.
306
307
  2. Run \`npm run agentkit -- build\` only when you want to validate the artifact locally.
307
308
  3. If the owner has not logged in yet, ask for an invited AgentKit Cloud alpha token and run \`npm run agentkit -- login --token <token>\`.
308
- 4. Put production secrets into managed secrets with \`npm run agentkit -- secret set <NAME> <VALUE>\`.
309
+ 4. Put production secrets into managed secrets with \`npm run agentkit -- secret set <NAME> --from-local-env\`, \`--from-env\`, or \`--stdin\`.
309
310
  5. Run \`npm run agentkit -- deploy\`.
310
- 6. Create client-facing deploy access tokens with \`npm run agentkit -- access token create <name>\` when a website or app needs to call the hosted agent.
311
+ 6. Run \`npm run agentkit -- chat-ui --deploy\` to test the hosted agent through a local UI using the auto-created \`.agentkit/chat-access-token.json\`. Create extra client-facing deploy access tokens with \`npm run agentkit -- access token create <name> --out <path>\` when a separate website or app needs its own credential.
311
312
 
312
313
  AgentKit owns hosted infrastructure and production secrets. Do not put production secret values in this capsule. Do not run operator/admin commands from a user capsule.
313
314
  `,
@@ -334,13 +335,14 @@ Generated by AgentKit as a support Agent Capsule.
334
335
  ## Setup
335
336
 
336
337
  \`\`\`sh
337
- npm install
338
338
  npm run chat -- --message "hello"
339
339
  npm run agentkit -- tool lookup_order --input '{"orderId":"A100"}'
340
340
  npm run eval
341
341
  npm run dev
342
342
  \`\`\`
343
343
 
344
+ \`agentkit new\` installs dependencies by default. Run \`npm install\` only if this capsule was created with \`--no-install\`, install failed, or \`node_modules\` was deleted.
345
+
344
346
  The support template includes a local \`lookup_order\` TypeScript tool and uses \`test/fake\` by default.
345
347
  `,
346
348
  },