@k2b/cloud 0.37.0 → 0.38.0

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 (64) hide show
  1. package/package.json +4 -2
  2. package/src/_internal/help-catalog.ts +123 -4
  3. package/src/_internal/help.ts +27 -6
  4. package/src/account/Avatar.tsx +20 -2
  5. package/src/account/ui.ts +1 -1
  6. package/src/ai/browser.ts +1 -0
  7. package/src/ai/chart-block.ts +44 -13
  8. package/src/ai/chat/chart-block.tsx +2 -11
  9. package/src/ai/chat/messages.ts +10 -0
  10. package/src/ai/chat/web-tools.tsx +23 -6
  11. package/src/ai/chat-tasks.ts +65 -1
  12. package/src/ai/default-tool-contracts.ts +30 -13
  13. package/src/ai/fetch-file-tool.ts +37 -3
  14. package/src/ai/migrate.ts +19 -2
  15. package/src/ai/readable-html.ts +1057 -0
  16. package/src/api/help.ts +23 -4
  17. package/src/api/mcp.ts +8 -2
  18. package/src/browser/FileChooser.tsx +11 -3
  19. package/src/browser/choose-files.browser-harness.tsx +4 -1
  20. package/src/browser/file-chooser-messages.ts +4 -0
  21. package/src/browser/file-providers.ts +14 -1
  22. package/src/browser/provider-folder.tsx +58 -5
  23. package/src/browser/resource-picker.tsx +5 -1
  24. package/src/cli/admin/workflows.ts +1 -0
  25. package/src/config/env.ts +14 -0
  26. package/src/contracts/file-provider.ts +48 -1
  27. package/src/contracts/outgoing-mail.ts +8 -1
  28. package/src/server/help.ts +136 -10
  29. package/src/server/services/geo.ts +21 -4
  30. package/src/services/allowed-network-fetch.ts +189 -0
  31. package/src/services/help/index.ts +46 -5
  32. package/src/services/help/store.ts +26 -5
  33. package/src/services/help/types.ts +10 -2
  34. package/src/services/index.ts +1 -0
  35. package/src/services/outgoing-mail/dispatcher.ts +7 -2
  36. package/src/services/outgoing-mail/resolve-host.ts +39 -0
  37. package/src/services/outgoing-mail/test-send.ts +18 -3
  38. package/src/services/outgoing-mail/transport.ts +24 -13
  39. package/src/services/settings/index.ts +48 -26
  40. package/src/shared/help.ts +41 -0
  41. package/src/shared/index.ts +9 -1
  42. package/src/shared/markdown/extensions/guided-help.ts +208 -64
  43. package/src/shared/markdown/formula.ts +8 -8
  44. package/src/shared/markdown/index.ts +72 -11
  45. package/src/ssr/Layout.tsx +20 -2
  46. package/src/ssr/LayoutHelp.browser-harness.tsx +130 -27
  47. package/src/ssr/LayoutHelp.tsx +569 -410
  48. package/src/ssr/ShellRuntime.island.tsx +17 -25
  49. package/src/ssr/help-messages.ts +60 -38
  50. package/src/ssr/help.ts +3 -0
  51. package/src/ssr/layout-help-article.ts +41 -0
  52. package/src/ssr/layout-help-navigation.ts +0 -12
  53. package/src/ssr/layout-help-registry.tsx +4 -11
  54. package/src/ssr/layout-help-search.ts +9 -12
  55. package/src/ssr/layout-help-start.ts +68 -0
  56. package/src/ssr/platform-messages.ts +4 -0
  57. package/src/ssr/shell-data.tsx +7 -0
  58. package/src/ssr/shell-shortcuts.ts +8 -0
  59. package/src/styles/effects.css +688 -185
  60. package/src/styles/resource-search.css +25 -0
  61. package/src/workflows/store/events.ts +24 -0
  62. package/src/workflows/store/index.ts +8 -0
  63. package/src/workflows/store/observability.ts +6 -1
  64. package/src/workflows/store/retention.ts +157 -0
@@ -59,6 +59,9 @@ export const cloudAiChartKindSchema = () =>
59
59
 
60
60
  const chartParts = (limits = true) => {
61
61
  const ChartLabel = limits ? z.string().trim().min(1).max(80) : z.union([z.string(), z.number().transform(String)]);
62
+ const ChartTooltip = z
63
+ .strictObject({ label: ChartLabel.optional(), format: cloudAiNumberFormatSchema().optional() })
64
+ .describe("All kinds: what tooltips and the data table call the value and how they format it; axes keep their own format.");
62
65
  // Missing observations stay numeric so the renderer can omit them with its finite-value checks.
63
66
  const ChartNumber = limits
64
67
  ? z.number()
@@ -96,6 +99,7 @@ const chartParts = (limits = true) => {
96
99
  );
97
100
  return {
98
101
  ChartLabel,
102
+ ChartTooltip,
99
103
  ChartNumber,
100
104
  list,
101
105
  ChartReferences,
@@ -148,6 +152,7 @@ const chartKinds = (limits = true) => {
148
152
  ChartBins,
149
153
  ChartCategory,
150
154
  ChartCommon,
155
+ ChartTooltip,
151
156
  ChartLabel,
152
157
  ChartPoints,
153
158
  ChartSeries,
@@ -159,11 +164,12 @@ const chartKinds = (limits = true) => {
159
164
  ChartCell,
160
165
  ChartBarGauge,
161
166
  } = chartParts(limits);
167
+ const ChartKindCommon = { ...ChartCommon, tooltip: ChartTooltip.optional() };
162
168
  const numberFormat = cloudAiNumberFormatSchema();
163
169
  return z.discriminatedUnion("kind", [
164
170
  z.object({
165
171
  kind: z.literal("bar"),
166
- ...ChartCommon,
172
+ ...ChartKindCommon,
167
173
  data: list(ChartCategory, 1, CLOUD_AI_CHART_MAX_VALUES),
168
174
  yAxis: ChartAxis.optional(),
169
175
  colorByBar: z.boolean().optional(),
@@ -173,7 +179,7 @@ const chartKinds = (limits = true) => {
173
179
  }),
174
180
  z.object({
175
181
  kind: z.literal("line"),
176
- ...ChartCommon,
182
+ ...ChartKindCommon,
177
183
  series: list(
178
184
  ChartSeries(
179
185
  limits
@@ -192,7 +198,7 @@ const chartKinds = (limits = true) => {
192
198
  }),
193
199
  z.object({
194
200
  kind: z.literal("scatter"),
195
- ...ChartCommon,
201
+ ...ChartKindCommon,
196
202
  series: list(ChartSeries(limits ? ChartPoints.min(1) : ChartPoints), 1, CLOUD_AI_CHART_MAX_SERIES),
197
203
  xAxis: ChartAxis.optional(),
198
204
  yAxis: ChartAxis.optional(),
@@ -201,7 +207,7 @@ const chartKinds = (limits = true) => {
201
207
  }),
202
208
  z.object({
203
209
  kind: z.enum(["pie", "donut"]),
204
- ...ChartCommon,
210
+ ...ChartKindCommon,
205
211
  data: list(ChartSlice, 1, CLOUD_AI_CHART_MAX_VALUES),
206
212
  legend: z.boolean().optional(),
207
213
  showLabels: z.boolean().optional(),
@@ -209,14 +215,14 @@ const chartKinds = (limits = true) => {
209
215
  }),
210
216
  z.object({
211
217
  kind: z.literal("histogram"),
212
- ...ChartCommon,
218
+ ...ChartKindCommon,
213
219
  data: list(ChartNumber, 1, CLOUD_AI_CHART_MAX_VALUES),
214
220
  bins: ChartBins.optional(),
215
221
  references: ChartReferences.optional(),
216
222
  }),
217
223
  z.object({
218
224
  kind: z.literal("gauge"),
219
- ...ChartCommon,
225
+ ...ChartKindCommon,
220
226
  value: ChartNumber,
221
227
  min: z.number().optional(),
222
228
  max: z.number().optional(),
@@ -227,20 +233,20 @@ const chartKinds = (limits = true) => {
227
233
  }),
228
234
  z.object({
229
235
  kind: z.literal("sparkline"),
230
- ...ChartCommon,
236
+ ...ChartKindCommon,
231
237
  data: list(ChartNumber, 2, CLOUD_AI_CHART_MAX_VALUES),
232
238
  area: z.boolean().optional(),
233
239
  }),
234
240
  z.object({
235
241
  kind: z.literal("boxplot"),
236
- ...ChartCommon,
242
+ ...ChartKindCommon,
237
243
  data: list(ChartBox, 1, CLOUD_AI_CHART_MAX_VALUES),
238
244
  yAxis: ChartAxis.optional(),
239
245
  references: ChartReferences.optional(),
240
246
  }),
241
247
  z.object({
242
248
  kind: z.literal("heatmap"),
243
- ...ChartCommon,
249
+ ...ChartKindCommon,
244
250
  data: list(ChartCell, 1, CLOUD_AI_VIEW_MAX_MARKS),
245
251
  format: numberFormat.optional(),
246
252
  showValues: z.boolean().optional(),
@@ -249,7 +255,7 @@ const chartKinds = (limits = true) => {
249
255
  }),
250
256
  z.object({
251
257
  kind: z.literal("barGauge"),
252
- ...ChartCommon,
258
+ ...ChartKindCommon,
253
259
  // The shared renderer draws 280 units high and a bar gauge row is 28 units, so 8 rows fit without a taller drawing.
254
260
  data: list(ChartBarGauge, 1, CLOUD_AI_CHART_MAX_SERIES, `Use at most ${CLOUD_AI_CHART_MAX_SERIES} bar gauge items.`),
255
261
  min: z.number().optional(),
@@ -260,7 +266,7 @@ const chartKinds = (limits = true) => {
260
266
  }),
261
267
  z.object({
262
268
  kind: z.literal("stat"),
263
- ...ChartCommon,
269
+ ...ChartKindCommon,
264
270
  value: ChartNumber,
265
271
  format: numberFormat.optional(),
266
272
  unit: ChartUnit.optional(),
@@ -336,11 +342,22 @@ export const cloudAiGroupSchema = () => {
336
342
  };
337
343
 
338
344
  const flatChartSchema = () => {
339
- const { ChartAxis, ChartBins, ChartCommon, ChartLabel, ChartPoints, ChartSeries, ChartReferences, ChartThresholds, ChartUnit } =
340
- chartParts();
345
+ const {
346
+ ChartAxis,
347
+ ChartBins,
348
+ ChartCommon,
349
+ ChartLabel,
350
+ ChartPoints,
351
+ ChartSeries,
352
+ ChartReferences,
353
+ ChartThresholds,
354
+ ChartUnit,
355
+ ChartTooltip,
356
+ } = chartParts();
341
357
  return z.object({
342
358
  kind: cloudAiChartKindSchema().optional().describe("Single charts: kind. Omit for groups."),
343
359
  ...ChartCommon,
360
+ tooltip: ChartTooltip.optional(),
344
361
  // One item object preserves every kind's fields before canonical validation; unions would strip overlapping items.
345
362
  data: z
346
363
  .union([
@@ -1,9 +1,11 @@
1
1
  import type { ClientRequest, IncomingHttpHeaders, IncomingMessage } from "node:http";
2
2
  import { type RequestOptions as HttpsRequestOptions, request as httpsRequest } from "node:https";
3
3
  import { z } from "zod";
4
+ import { DOCUMENT_EXTRACTION_MAX_INPUT_BYTES, DOCUMENT_EXTRACTION_MAX_OUTPUT_BYTES } from "../services/document-extraction";
4
5
  import { type PublicNetworkAddress, resolvePublicNetworkAddresses } from "../services/network-security";
5
6
  import { AI_FILES_MAX_FILE_BYTES_DEFAULT, aiFileStore, guessAiMediaType, normalizeAiFilePath } from "./files-store";
6
7
  import { authorizeWebRead } from "./firecrawl-tools";
8
+ import { htmlToReadableMarkdown } from "./readable-html";
7
9
  import { defineAiTool } from "./tools";
8
10
 
9
11
  const FETCH_FILE_MAX_REDIRECTS = 5;
@@ -38,6 +40,7 @@ export const CloudAiFetchFileOutputSchema = z.object({
38
40
  size: z.number().int().nonnegative(),
39
41
  mediaType: z.string(),
40
42
  url: z.string().url(),
43
+ readable: z.object({ path: z.string(), size: z.number().int().nonnegative(), note: z.string() }).optional(),
41
44
  });
42
45
 
43
46
  const publicHttpsUrl = (rawUrl: string): URL => {
@@ -309,20 +312,51 @@ export const runCloudAiFetchFile = async (
309
312
  bytes: downloaded.bytes,
310
313
  mediaType: responseMediaType(downloaded.headers, path),
311
314
  });
312
- return { path: stat.path, size: stat.size, mediaType: stat.mediaType, url: downloaded.url.toString() };
315
+ const result = { path: stat.path, size: stat.size, mediaType: stat.mediaType, url: downloaded.url.toString() };
316
+ if (stat.mediaType !== "text/html" && stat.mediaType !== "application/xhtml+xml") return result;
317
+ try {
318
+ const contentType = downloaded.headers["content-type"];
319
+ const markdown = await htmlToReadableMarkdown(downloaded.bytes, {
320
+ baseUrl: downloaded.url,
321
+ contentType: Array.isArray(contentType) ? contentType[0] : contentType,
322
+ // The same bounds as any other document converted to Markdown for the model.
323
+ maxInputBytes: DOCUMENT_EXTRACTION_MAX_INPUT_BYTES,
324
+ maxBytes: DOCUMENT_EXTRACTION_MAX_OUTPUT_BYTES,
325
+ signal: context.signal,
326
+ });
327
+ if (!markdown) return result;
328
+ const basename = stat.path.slice(stat.path.lastIndexOf("/") + 1).replace(/\.(?:html?|xhtml|xht)$/i, "");
329
+ const readable = await aiFileStore.createAssistantFile({
330
+ conversationId: context.conversationId,
331
+ path: `/imports/${basename}.md`,
332
+ bytes: new TextEncoder().encode(markdown),
333
+ mediaType: "text/markdown",
334
+ });
335
+ return {
336
+ ...result,
337
+ readable: {
338
+ path: readable.path,
339
+ size: readable.size,
340
+ note: "Readable Markdown of the page's main content. Read this path first; use the raw path only when you need the HTML markup.",
341
+ },
342
+ };
343
+ } catch {
344
+ // The raw import already succeeded; a readable copy is best-effort, including on cancellation.
345
+ return result;
346
+ }
313
347
  };
314
348
 
315
349
  export const createCloudAiFetchFileTool = (dependencies: FetchFileDependencies = {}) =>
316
350
  defineAiTool({
317
351
  name: "fetch_file",
318
352
  description:
319
- "Download one exact public HTTPS file URL into the current conversation. Use this for images, raw GitHub files, documents, or other source files the user wants inspected. This does not browse or clone repositories, authenticate to websites, or fetch private network targets. After downloading, inspect the returned path with read_file or view_image and use present when the user should receive the file.",
353
+ "Download one exact public HTTPS file URL into this conversation for inspection. Supports images, documents, raw repository files, and web pages; no repository browsing, website authentication, or private network access. For web pages, read readable.path (main-content Markdown) first with read_file when returned; use raw HTML only when markup matters. Inspect other returned paths with read_file or view_image and use present to deliver files.",
320
354
  inputSchema: CloudAiFetchFileInputSchema,
321
355
  outputSchema: CloudAiFetchFileOutputSchema,
322
356
  approval: "never",
323
357
  timeoutMs: 90_000,
324
358
  promptHint:
325
- "fetch an exact public HTTPS source file into the chat when the user provides a file link; inspect it with read_file or view_image and present it when useful.",
359
+ "fetch an exact public HTTPS source file into the chat; for web pages read readable.path (main-content Markdown) first with read_file when returned; use raw HTML only when markup matters. Inspect other files with read_file or view_image; present when useful.",
326
360
  }).server(async (input, context) => {
327
361
  const url = publicHttpsUrl(input.url);
328
362
  const allowedForChat = await authorizeWebRead("file", url.toString(), context);
package/src/ai/migrate.ts CHANGED
@@ -516,6 +516,12 @@ export const migrateCloudAi = async (): Promise<void> => {
516
516
  WHERE status = 'completed' AND run_config->>'kind' = 'chat'
517
517
  `.simple();
518
518
 
519
+ await sql`
520
+ CREATE INDEX IF NOT EXISTS idx_ai_turns_background_finished
521
+ ON ai.turns(completed_at, id)
522
+ WHERE run_config ? 'background' AND status IN ('completed', 'failed', 'aborted')
523
+ `.simple();
524
+
519
525
  await sql`
520
526
  CREATE TABLE IF NOT EXISTS ai.conversation_resource_refs (
521
527
  conversation_id UUID NOT NULL REFERENCES ai.conversations(id) ON DELETE CASCADE,
@@ -999,12 +1005,23 @@ export const migrateCloudAi = async (): Promise<void> => {
999
1005
  ON ai.chat_task_occurrences(request_key)
1000
1006
  `.simple();
1001
1007
 
1008
+ await sql`
1009
+ CREATE INDEX IF NOT EXISTS idx_ai_chat_task_occurrences_turn
1010
+ ON ai.chat_task_occurrences(turn_id)
1011
+ `.simple();
1012
+
1002
1013
  await sql`
1003
1014
  CREATE INDEX IF NOT EXISTS idx_ai_chat_task_occurrences_queued
1004
1015
  ON ai.chat_task_occurrences(created_at ASC, id ASC)
1005
1016
  WHERE state = 'queued'
1006
1017
  `.simple();
1007
1018
 
1019
+ await sql`
1020
+ CREATE INDEX IF NOT EXISTS idx_ai_chat_task_occurrences_finished
1021
+ ON ai.chat_task_occurrences(completed_at, id)
1022
+ WHERE state IN ('completed', 'failed')
1023
+ `.simple();
1024
+
1008
1025
  await sql`
1009
1026
  CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_chat_task_occurrences_active_task
1010
1027
  ON ai.chat_task_occurrences(task_id)
@@ -1273,7 +1290,7 @@ export const migrateCloudAi = async (): Promise<void> => {
1273
1290
  CONSTRAINT ai_memory_learning_runs_kind_check CHECK (run_kind IN ('turn', 'workflow')),
1274
1291
  CONSTRAINT ai_memory_learning_runs_evidence_check CHECK (
1275
1292
  (run_kind = 'turn' AND evidence_key IS NULL)
1276
- OR (run_kind = 'workflow' AND turn_id IS NOT NULL AND evidence_key IS NOT NULL)
1293
+ OR (run_kind = 'workflow' AND evidence_key IS NOT NULL)
1277
1294
  ),
1278
1295
  CONSTRAINT ai_memory_learning_runs_changes_check CHECK (jsonb_typeof(changes) = 'array')
1279
1296
  )
@@ -1299,7 +1316,7 @@ export const migrateCloudAi = async (): Promise<void> => {
1299
1316
  await sql`
1300
1317
  ALTER TABLE ai.memory_learning_runs ADD CONSTRAINT ai_memory_learning_runs_evidence_check CHECK (
1301
1318
  (run_kind = 'turn' AND evidence_key IS NULL)
1302
- OR (run_kind = 'workflow' AND turn_id IS NOT NULL AND evidence_key IS NOT NULL)
1319
+ OR (run_kind = 'workflow' AND evidence_key IS NOT NULL)
1303
1320
  )
1304
1321
  `.simple();
1305
1322
  await sql`