@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.
- package/README.md +67 -6
- package/docs/guides/add-channel.md +118 -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 +97 -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-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 +303 -55
- package/docs/llms.txt +57 -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 +1315 -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 +479 -7
- 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 +8 -1
- package/src/runtime/channels/discord.ts +896 -0
- package/src/runtime/channels/slack.ts +646 -0
- package/src/runtime/channels/telegram.ts +466 -23
- package/src/runtime/channels/whatsapp-meta.ts +9 -0
- package/src/runtime/channels/whatsapp-zapster.ts +677 -40
- package/src/runtime/channels.ts +86 -3
- package/src/runtime/chat.ts +130 -38
- package/src/runtime/config.ts +483 -18
- 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 +759 -41
- 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 +1430 -185
- 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 +104 -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-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,535 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
|
3
|
+
import { join, relative, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { findAgentCapsuleRoot } from "../runtime/config";
|
|
6
|
+
import { readLocalDeployStateIfExists } from "../runtime/core/deploy-state";
|
|
7
|
+
import { AgentKitError, isAgentKitError } from "../runtime/errors";
|
|
8
|
+
import { defaultDeployChatAccessTokenName, defaultDeployChatAccessTokenPath } from "./constants";
|
|
9
|
+
|
|
10
|
+
export async function startDeployChatUiServer(
|
|
11
|
+
cwd: string,
|
|
12
|
+
options: { port: number; tokenFile?: string },
|
|
13
|
+
): Promise<{ url: string; deployUrl: string; tokenSource: string; stop(): Promise<void> }> {
|
|
14
|
+
const root = await findAgentCapsuleRoot(cwd);
|
|
15
|
+
const state = await readLocalDeployStateIfExists(root);
|
|
16
|
+
|
|
17
|
+
if (!state?.url) {
|
|
18
|
+
throw new Error("No hosted deploy URL found. Run agentkit deploy first.");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const tokenPath = options.tokenFile
|
|
22
|
+
? resolve(root, options.tokenFile)
|
|
23
|
+
: join(root, defaultDeployChatAccessTokenPath);
|
|
24
|
+
const hostname = "localhost";
|
|
25
|
+
const deployUrl = state.url;
|
|
26
|
+
const server = createServer(async (request, response) => {
|
|
27
|
+
try {
|
|
28
|
+
const requestUrl = new URL(request.url ?? "/", `http://${request.headers.host ?? `${hostname}:${options.port}`}`);
|
|
29
|
+
|
|
30
|
+
if (request.method === "GET" && requestUrl.pathname === "/") {
|
|
31
|
+
sendHtml(response, renderDeployChatUi({ deployUrl }));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (request.method === "GET" && requestUrl.pathname === "/health") {
|
|
36
|
+
sendJson(response, 200, { status: "ok", target: "deploy-chat-ui", deployUrl });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (request.method === "GET" && requestUrl.pathname === "/_agentkit") {
|
|
41
|
+
sendJson(response, 200, {
|
|
42
|
+
deployUrl,
|
|
43
|
+
access: "proxied",
|
|
44
|
+
tokenSource: process.env.AGENTKIT_DEPLOY_ACCESS_TOKEN ? "AGENTKIT_DEPLOY_ACCESS_TOKEN" : relativePath(root, tokenPath),
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (request.method === "POST" && (requestUrl.pathname === "/v1/chat" || requestUrl.pathname === "/chat")) {
|
|
50
|
+
const token = await readDeployAccessTokenForUi(tokenPath);
|
|
51
|
+
const body = await readIncomingBody(request);
|
|
52
|
+
const contentTypeHeader = request.headers["content-type"];
|
|
53
|
+
const contentType = Array.isArray(contentTypeHeader)
|
|
54
|
+
? contentTypeHeader[0] ?? "application/json"
|
|
55
|
+
: contentTypeHeader ?? "application/json";
|
|
56
|
+
const upstream = await fetch(new URL("/v1/chat", deployUrl).href, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: {
|
|
59
|
+
"Authorization": `Bearer ${token}`,
|
|
60
|
+
"Content-Type": contentType,
|
|
61
|
+
},
|
|
62
|
+
body,
|
|
63
|
+
});
|
|
64
|
+
const text = await upstream.text();
|
|
65
|
+
|
|
66
|
+
response.writeHead(upstream.status, {
|
|
67
|
+
"content-type": upstream.headers.get("content-type") ?? "application/json",
|
|
68
|
+
});
|
|
69
|
+
response.end(text);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
request.method === "GET" &&
|
|
75
|
+
(requestUrl.pathname === "/v1/conversations" ||
|
|
76
|
+
/^\/v1\/conversations\/[^/]+(?:\/trace)?$/.test(requestUrl.pathname))
|
|
77
|
+
) {
|
|
78
|
+
const token = await readDeployAccessTokenForUi(tokenPath);
|
|
79
|
+
const upstream = await fetch(new URL(requestUrl.pathname, deployUrl).href, {
|
|
80
|
+
method: "GET",
|
|
81
|
+
headers: {
|
|
82
|
+
Authorization: `Bearer ${token}`,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const text = await upstream.text();
|
|
86
|
+
|
|
87
|
+
response.writeHead(upstream.status, {
|
|
88
|
+
"content-type": upstream.headers.get("content-type") ?? "application/json",
|
|
89
|
+
});
|
|
90
|
+
response.end(text);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
sendJson(response, 404, { error: { code: "not_found", message: "Route not found." } });
|
|
95
|
+
} catch (error) {
|
|
96
|
+
sendJson(response, 500, {
|
|
97
|
+
error: {
|
|
98
|
+
code: isAgentKitError(error) ? error.code : "deploy_chat_ui_error",
|
|
99
|
+
message: error instanceof Error ? error.message : String(error),
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await new Promise<void>((resolvePromise, reject) => {
|
|
106
|
+
server.once("error", reject);
|
|
107
|
+
server.listen(options.port, hostname, () => {
|
|
108
|
+
server.off("error", reject);
|
|
109
|
+
resolvePromise();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
url: `http://${hostname}:${options.port}`,
|
|
115
|
+
deployUrl,
|
|
116
|
+
tokenSource: process.env.AGENTKIT_DEPLOY_ACCESS_TOKEN ? "AGENTKIT_DEPLOY_ACCESS_TOKEN" : relativePath(root, tokenPath),
|
|
117
|
+
stop: () =>
|
|
118
|
+
new Promise<void>((resolvePromise, reject) => {
|
|
119
|
+
server.close((error) => (error ? reject(error) : resolvePromise()));
|
|
120
|
+
}),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function readDeployAccessTokenForUi(tokenPath: string): Promise<string> {
|
|
125
|
+
if (process.env.AGENTKIT_DEPLOY_ACCESS_TOKEN) {
|
|
126
|
+
return process.env.AGENTKIT_DEPLOY_ACCESS_TOKEN;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let payload: unknown;
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
payload = JSON.parse(await readFile(tokenPath, "utf8"));
|
|
133
|
+
} catch {
|
|
134
|
+
throw new AgentKitError(
|
|
135
|
+
"deploy_access_token_missing",
|
|
136
|
+
`Could not read a clean deploy access token file at ${tokenPath}. Run: agentkit access token create ${defaultDeployChatAccessTokenName} --out ${defaultDeployChatAccessTokenPath}`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const token =
|
|
141
|
+
payload && typeof payload === "object"
|
|
142
|
+
? (payload as { access_token?: { token?: unknown }; token?: unknown; accessToken?: unknown }).access_token?.token ??
|
|
143
|
+
(payload as { token?: unknown }).token ??
|
|
144
|
+
(payload as { accessToken?: unknown }).accessToken
|
|
145
|
+
: undefined;
|
|
146
|
+
|
|
147
|
+
if (typeof token !== "string" || token.trim().length === 0) {
|
|
148
|
+
throw new AgentKitError(
|
|
149
|
+
"deploy_access_token_invalid",
|
|
150
|
+
`Deploy access token file ${tokenPath} does not contain access_token.token. Recreate it with: agentkit access token create ${defaultDeployChatAccessTokenName} --out ${defaultDeployChatAccessTokenPath}`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return token.trim();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readIncomingBody(request: IncomingMessage): Promise<string> {
|
|
158
|
+
return new Promise((resolvePromise, reject) => {
|
|
159
|
+
let body = "";
|
|
160
|
+
request.setEncoding("utf8");
|
|
161
|
+
request.on("data", (chunk) => {
|
|
162
|
+
body += chunk;
|
|
163
|
+
});
|
|
164
|
+
request.on("end", () => resolvePromise(body));
|
|
165
|
+
request.on("error", reject);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function sendJson(response: ServerResponse, status: number, body: unknown): void {
|
|
170
|
+
response.writeHead(status, { "content-type": "application/json; charset=utf-8" });
|
|
171
|
+
response.end(JSON.stringify(body, null, 2));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function sendHtml(response: ServerResponse, body: string): void {
|
|
175
|
+
response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
|
|
176
|
+
response.end(body);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function renderDeployChatUi(input: { deployUrl: string }): string {
|
|
180
|
+
const deployUrl = escapeHtml(input.deployUrl);
|
|
181
|
+
|
|
182
|
+
return `<!doctype html>
|
|
183
|
+
<html lang="en">
|
|
184
|
+
<head>
|
|
185
|
+
<meta charset="utf-8">
|
|
186
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
187
|
+
<title>AgentKit Hosted Chat</title>
|
|
188
|
+
<style>
|
|
189
|
+
:root {
|
|
190
|
+
color-scheme: light;
|
|
191
|
+
--bg: #f6f7f5;
|
|
192
|
+
--panel: #ffffff;
|
|
193
|
+
--ink: #182022;
|
|
194
|
+
--muted: #667175;
|
|
195
|
+
--line: #d9ded9;
|
|
196
|
+
--accent: #176b5d;
|
|
197
|
+
--danger: #9f2d2d;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
* { box-sizing: border-box; }
|
|
201
|
+
|
|
202
|
+
body {
|
|
203
|
+
margin: 0;
|
|
204
|
+
min-height: 100svh;
|
|
205
|
+
background: var(--bg);
|
|
206
|
+
color: var(--ink);
|
|
207
|
+
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
main {
|
|
211
|
+
min-height: 100svh;
|
|
212
|
+
display: grid;
|
|
213
|
+
grid-template-rows: auto 1fr auto;
|
|
214
|
+
background: var(--panel);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
header {
|
|
218
|
+
border-bottom: 1px solid var(--line);
|
|
219
|
+
padding: 18px 22px;
|
|
220
|
+
display: grid;
|
|
221
|
+
gap: 10px;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
h1 {
|
|
225
|
+
margin: 0 0 4px;
|
|
226
|
+
font-size: 20px;
|
|
227
|
+
line-height: 1.2;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
.topline {
|
|
231
|
+
display: flex;
|
|
232
|
+
align-items: start;
|
|
233
|
+
justify-content: space-between;
|
|
234
|
+
gap: 12px;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
code {
|
|
238
|
+
color: var(--muted);
|
|
239
|
+
font-size: 12px;
|
|
240
|
+
overflow-wrap: anywhere;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.meta {
|
|
244
|
+
display: flex;
|
|
245
|
+
flex-wrap: wrap;
|
|
246
|
+
gap: 8px 14px;
|
|
247
|
+
color: var(--muted);
|
|
248
|
+
font-size: 12px;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#messages {
|
|
252
|
+
padding: 22px;
|
|
253
|
+
overflow: auto;
|
|
254
|
+
display: flex;
|
|
255
|
+
flex-direction: column;
|
|
256
|
+
gap: 12px;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.message {
|
|
260
|
+
max-width: 780px;
|
|
261
|
+
display: grid;
|
|
262
|
+
gap: 4px;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
.message.user {
|
|
266
|
+
align-self: end;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.role {
|
|
270
|
+
color: var(--muted);
|
|
271
|
+
font-size: 12px;
|
|
272
|
+
font-weight: 700;
|
|
273
|
+
text-transform: uppercase;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
.bubble {
|
|
277
|
+
border: 1px solid var(--line);
|
|
278
|
+
border-radius: 8px;
|
|
279
|
+
padding: 12px 14px;
|
|
280
|
+
white-space: pre-wrap;
|
|
281
|
+
line-height: 1.45;
|
|
282
|
+
background: #fbfbfa;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
.user .bubble {
|
|
286
|
+
border-color: #b8d8ce;
|
|
287
|
+
background: #e8f5f0;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
.error .bubble {
|
|
291
|
+
border-color: #e1b1b1;
|
|
292
|
+
color: var(--danger);
|
|
293
|
+
background: #fff6f6;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
#trace {
|
|
297
|
+
border-top: 1px solid var(--line);
|
|
298
|
+
padding: 12px 22px;
|
|
299
|
+
display: grid;
|
|
300
|
+
gap: 8px;
|
|
301
|
+
background: #fbfbfa;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
.tool-call {
|
|
305
|
+
border: 1px solid var(--line);
|
|
306
|
+
border-radius: 8px;
|
|
307
|
+
padding: 10px 12px;
|
|
308
|
+
display: grid;
|
|
309
|
+
gap: 6px;
|
|
310
|
+
font-size: 12px;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
.tool-head {
|
|
314
|
+
display: flex;
|
|
315
|
+
flex-wrap: wrap;
|
|
316
|
+
gap: 8px;
|
|
317
|
+
align-items: center;
|
|
318
|
+
color: var(--muted);
|
|
319
|
+
font-weight: 700;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
.tool-call pre {
|
|
323
|
+
margin: 0;
|
|
324
|
+
max-height: 180px;
|
|
325
|
+
overflow: auto;
|
|
326
|
+
white-space: pre-wrap;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
form {
|
|
330
|
+
border-top: 1px solid var(--line);
|
|
331
|
+
padding: 14px;
|
|
332
|
+
display: grid;
|
|
333
|
+
grid-template-columns: 1fr auto;
|
|
334
|
+
gap: 10px;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
textarea {
|
|
338
|
+
min-height: 52px;
|
|
339
|
+
max-height: 180px;
|
|
340
|
+
resize: vertical;
|
|
341
|
+
border: 1px solid var(--line);
|
|
342
|
+
border-radius: 8px;
|
|
343
|
+
padding: 12px;
|
|
344
|
+
color: var(--ink);
|
|
345
|
+
font: inherit;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
button {
|
|
349
|
+
min-width: 96px;
|
|
350
|
+
border: 0;
|
|
351
|
+
border-radius: 8px;
|
|
352
|
+
padding: 0 18px;
|
|
353
|
+
color: white;
|
|
354
|
+
background: var(--accent);
|
|
355
|
+
font: inherit;
|
|
356
|
+
font-weight: 700;
|
|
357
|
+
cursor: pointer;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
button.secondary {
|
|
361
|
+
min-height: 34px;
|
|
362
|
+
color: var(--ink);
|
|
363
|
+
background: #eef1ee;
|
|
364
|
+
border: 1px solid var(--line);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
button:disabled {
|
|
368
|
+
cursor: wait;
|
|
369
|
+
opacity: 0.65;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
@media (max-width: 640px) {
|
|
373
|
+
form {
|
|
374
|
+
grid-template-columns: 1fr;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
button {
|
|
378
|
+
min-height: 44px;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
</style>
|
|
382
|
+
</head>
|
|
383
|
+
<body>
|
|
384
|
+
<main>
|
|
385
|
+
<header>
|
|
386
|
+
<div class="topline">
|
|
387
|
+
<div>
|
|
388
|
+
<h1>AgentKit Hosted Chat</h1>
|
|
389
|
+
<code>${deployUrl}</code>
|
|
390
|
+
</div>
|
|
391
|
+
<button class="secondary" id="new-conversation" type="button">New conversation</button>
|
|
392
|
+
</div>
|
|
393
|
+
<div class="meta">
|
|
394
|
+
<span>Conversation: <code id="conversation-id">none</code></span>
|
|
395
|
+
<span>Tool calls: <code id="tool-count">0</code></span>
|
|
396
|
+
</div>
|
|
397
|
+
</header>
|
|
398
|
+
<section id="messages" aria-live="polite"></section>
|
|
399
|
+
<section id="trace" aria-live="polite"></section>
|
|
400
|
+
<form id="form">
|
|
401
|
+
<textarea id="message" name="message" placeholder="Type a message" required autofocus></textarea>
|
|
402
|
+
<button id="send" type="submit">Send</button>
|
|
403
|
+
</form>
|
|
404
|
+
</main>
|
|
405
|
+
<script>
|
|
406
|
+
const form = document.querySelector("#form");
|
|
407
|
+
const textarea = document.querySelector("#message");
|
|
408
|
+
const send = document.querySelector("#send");
|
|
409
|
+
const messages = document.querySelector("#messages");
|
|
410
|
+
const trace = document.querySelector("#trace");
|
|
411
|
+
const conversationLabel = document.querySelector("#conversation-id");
|
|
412
|
+
const toolCount = document.querySelector("#tool-count");
|
|
413
|
+
const newConversation = document.querySelector("#new-conversation");
|
|
414
|
+
let conversationId = null;
|
|
415
|
+
|
|
416
|
+
function addMessage(role, content, className = "") {
|
|
417
|
+
const wrapper = document.createElement("article");
|
|
418
|
+
wrapper.className = ["message", role, className].filter(Boolean).join(" ");
|
|
419
|
+
const label = document.createElement("div");
|
|
420
|
+
label.className = "role";
|
|
421
|
+
label.textContent = role;
|
|
422
|
+
const bubble = document.createElement("div");
|
|
423
|
+
bubble.className = "bubble";
|
|
424
|
+
bubble.textContent = content;
|
|
425
|
+
wrapper.append(label, bubble);
|
|
426
|
+
messages.append(wrapper);
|
|
427
|
+
messages.scrollTop = messages.scrollHeight;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function setConversationId(value) {
|
|
431
|
+
conversationId = value || null;
|
|
432
|
+
conversationLabel.textContent = conversationId || "none";
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function renderTrace(payload) {
|
|
436
|
+
const calls = [];
|
|
437
|
+
for (const run of payload.runs || []) {
|
|
438
|
+
for (const call of run.toolCalls || []) {
|
|
439
|
+
calls.push(call);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
toolCount.textContent = String(calls.length);
|
|
444
|
+
trace.innerHTML = "";
|
|
445
|
+
|
|
446
|
+
for (const call of calls.slice(-8)) {
|
|
447
|
+
const item = document.createElement("article");
|
|
448
|
+
item.className = "tool-call";
|
|
449
|
+
const head = document.createElement("div");
|
|
450
|
+
head.className = "tool-head";
|
|
451
|
+
head.textContent = [call.name, call.status, call.errorMessage].filter(Boolean).join(" · ");
|
|
452
|
+
const input = document.createElement("pre");
|
|
453
|
+
input.textContent = JSON.stringify(call.input || {}, null, 2);
|
|
454
|
+
item.append(head, input);
|
|
455
|
+
trace.append(item);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async function refreshTrace() {
|
|
460
|
+
if (!conversationId) {
|
|
461
|
+
toolCount.textContent = "0";
|
|
462
|
+
trace.innerHTML = "";
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const response = await fetch("/v1/conversations/" + encodeURIComponent(conversationId) + "/trace");
|
|
467
|
+
const payload = await response.json();
|
|
468
|
+
if (response.ok) renderTrace(payload);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
newConversation.addEventListener("click", () => {
|
|
472
|
+
setConversationId(null);
|
|
473
|
+
messages.innerHTML = "";
|
|
474
|
+
trace.innerHTML = "";
|
|
475
|
+
toolCount.textContent = "0";
|
|
476
|
+
textarea.focus();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
textarea.addEventListener("keydown", (event) => {
|
|
480
|
+
if (event.key !== "Enter" || event.shiftKey || event.isComposing) return;
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
if (send.disabled) return;
|
|
483
|
+
form.requestSubmit();
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
form.addEventListener("submit", async (event) => {
|
|
487
|
+
event.preventDefault();
|
|
488
|
+
const message = textarea.value.trim();
|
|
489
|
+
if (!message) return;
|
|
490
|
+
|
|
491
|
+
addMessage("user", message);
|
|
492
|
+
textarea.value = "";
|
|
493
|
+
send.disabled = true;
|
|
494
|
+
|
|
495
|
+
try {
|
|
496
|
+
const response = await fetch("/v1/chat", {
|
|
497
|
+
method: "POST",
|
|
498
|
+
headers: { "content-type": "application/json" },
|
|
499
|
+
body: JSON.stringify({ message, conversationId }),
|
|
500
|
+
});
|
|
501
|
+
const payload = await response.json();
|
|
502
|
+
|
|
503
|
+
if (!response.ok) {
|
|
504
|
+
throw new Error(payload.error?.message || "Request failed");
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
setConversationId(payload.conversationId || conversationId);
|
|
508
|
+
addMessage("assistant", payload.message?.content || "");
|
|
509
|
+
await refreshTrace();
|
|
510
|
+
} catch (error) {
|
|
511
|
+
addMessage("error", error instanceof Error ? error.message : String(error), "error");
|
|
512
|
+
await refreshTrace().catch(() => {});
|
|
513
|
+
} finally {
|
|
514
|
+
send.disabled = false;
|
|
515
|
+
textarea.focus();
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
</script>
|
|
519
|
+
</body>
|
|
520
|
+
</html>`;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function escapeHtml(value: string): string {
|
|
524
|
+
return value
|
|
525
|
+
.replace(/&/g, "&")
|
|
526
|
+
.replace(/</g, "<")
|
|
527
|
+
.replace(/>/g, ">")
|
|
528
|
+
.replace(/"/g, """)
|
|
529
|
+
.replace(/'/g, "'");
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function relativePath(root: string, path: string): string {
|
|
533
|
+
const output = relative(root, path);
|
|
534
|
+
return output.startsWith("..") ? path : output || ".";
|
|
535
|
+
}
|