pgkiln 0.51.0 → 0.52.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 (89) hide show
  1. package/SECURITY.md +5 -0
  2. package/db/migrations/121_ai_agents.sql +94 -0
  3. package/db/migrations/122_team_development.sql +186 -0
  4. package/db/migrations/123_workflow_outcomes.sql +407 -0
  5. package/db/migrations/124_component_details.sql +51 -0
  6. package/docs/apex-feature-parity.md +12 -10
  7. package/docs/guide/01-installation.md +7 -3
  8. package/docs/guide/03-builder.md +121 -7
  9. package/docs/guide/04-pages-and-regions.md +71 -21
  10. package/docs/guide/05-items.md +7 -1
  11. package/docs/guide/06-processing.md +123 -8
  12. package/docs/guide/07-dynamic-actions.md +34 -0
  13. package/docs/guide/09-reference.md +2 -1
  14. package/docs/guide/11-from-apex.md +2 -1
  15. package/docs/guide/12-development.md +44 -8
  16. package/docs/guide/20-ai-agents.md +4 -2
  17. package/examples/hr/README.md +2 -0
  18. package/examples/hr/hr_64_ai_agent.sql +97 -0
  19. package/examples/hr/hr_65_ask_reports.sql +21 -0
  20. package/examples/hr/hr_66_team_development.sql +58 -0
  21. package/examples/hr/hr_67_workflow_outcomes.sql +113 -0
  22. package/examples/hr/hr_68_component_details.sql +96 -0
  23. package/package.json +1 -1
  24. package/public/app.css +40 -0
  25. package/public/app.js +134 -20
  26. package/public/builder.css +83 -1
  27. package/public/builder.js +47 -0
  28. package/src/ai/chat.ts +18 -18
  29. package/src/ai/service.ts +35 -11
  30. package/src/ai/types.ts +7 -2
  31. package/src/app.ts +6 -0
  32. package/src/builder/advisor.ts +6 -3
  33. package/src/builder/ai-agents.ts +372 -0
  34. package/src/builder/ai-builder.ts +3 -3
  35. package/src/builder/ai.ts +13 -9
  36. package/src/builder/assistant.ts +22 -7
  37. package/src/builder/components.ts +38 -9
  38. package/src/builder/designer.ts +8 -3
  39. package/src/builder/feedback.ts +6 -1
  40. package/src/builder/locks.ts +7 -1
  41. package/src/builder/region-help.ts +2 -2
  42. package/src/builder/routes.ts +4 -1
  43. package/src/builder/shared.ts +1 -1
  44. package/src/builder/team.ts +991 -0
  45. package/src/builder/ui.ts +2 -1
  46. package/src/builder/workflow-migrate.ts +210 -0
  47. package/src/builder/workflows.ts +29 -6
  48. package/src/builder/workspace-home.ts +2 -0
  49. package/src/builder/workspace-transfer.ts +1 -1
  50. package/src/cli/replace.ts +7 -1
  51. package/src/i18n/ar.ts +17 -0
  52. package/src/i18n/cs.ts +17 -0
  53. package/src/i18n/da.ts +17 -0
  54. package/src/i18n/de.ts +17 -0
  55. package/src/i18n/el.ts +17 -0
  56. package/src/i18n/es.ts +17 -0
  57. package/src/i18n/fi.ts +17 -0
  58. package/src/i18n/fr.ts +17 -0
  59. package/src/i18n/he.ts +17 -0
  60. package/src/i18n/it.ts +17 -0
  61. package/src/i18n/ja.ts +17 -0
  62. package/src/i18n/ko.ts +17 -0
  63. package/src/i18n/nb.ts +17 -0
  64. package/src/i18n/pl.ts +17 -0
  65. package/src/i18n/pt.ts +17 -0
  66. package/src/i18n/ru.ts +17 -0
  67. package/src/i18n/sv.ts +17 -0
  68. package/src/i18n/tr.ts +17 -0
  69. package/src/i18n/uk.ts +17 -0
  70. package/src/i18n/zh.ts +17 -0
  71. package/src/i18n.ts +34 -0
  72. package/src/instance.ts +2 -0
  73. package/src/metadata.ts +9 -3
  74. package/src/runtime/ai-agents.ts +365 -0
  75. package/src/runtime/ai-filter.ts +385 -107
  76. package/src/runtime/assistant.ts +64 -11
  77. package/src/runtime/context.ts +2 -0
  78. package/src/runtime/ir-phrases.ts +812 -0
  79. package/src/runtime/ir-plan.ts +449 -0
  80. package/src/runtime/items.ts +1 -1
  81. package/src/runtime/regions.ts +6 -2
  82. package/src/runtime/render.ts +1 -1
  83. package/src/runtime/report.ts +1 -1
  84. package/src/runtime/tasks.ts +63 -12
  85. package/src/task-events.ts +232 -0
  86. package/src/team-samples.ts +74 -0
  87. package/src/team.ts +342 -0
  88. package/src/workflow.ts +135 -18
  89. package/src/workspace-export.ts +16 -1
package/src/ai/service.ts CHANGED
@@ -4,7 +4,7 @@ import { ClaudeProvider } from './anthropic.ts';
4
4
  import { OpenAiProvider } from './openai.ts';
5
5
  import { AiError, type AiProvider, type GenerateRequest, type GenerateResult, type ProviderName } from './types.ts';
6
6
 
7
- // AI services (migration 060): an administrator configures them in the
7
+ // AI services (migration 060; Gemini and OpenAI-compatible endpoints: 121): an administrator configures them in the
8
8
  // builder (Workspace utilities → AI services) and allows each application
9
9
  // to use some of them, with optional limits per day. Every call goes
10
10
  // through generate() here: the service must exist, be enabled and be
@@ -35,33 +35,57 @@ export const DEFAULT_CLAUDE_MODEL = 'claude-opus-5-5';
35
35
  export const MAX_PROMPT_CHARS = 200_000;
36
36
  export const MAX_SYSTEM_CHARS = 50_000;
37
37
 
38
- const ENV_KEY: Record<ProviderName, string> = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY' };
38
+ const ENV_KEY: Record<ProviderName, string> = {
39
+ anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY', gemini: 'GEMINI_API_KEY', openai_compatible: 'OPENAI_COMPATIBLE_API_KEY',
40
+ };
39
41
 
40
42
  export const envKeyName = (p: ProviderName) => ENV_KEY[p];
41
43
 
44
+ /** (121) The providers, as the builder names them. */
45
+ export const PROVIDER_LABELS: Record<ProviderName, string> = {
46
+ anthropic: 'Claude', openai: 'OpenAI', gemini: 'Gemini', openai_compatible: 'OpenAI-compatible',
47
+ };
48
+ export const providerLabel = (p: string) => PROVIDER_LABELS[p as ProviderName] ?? p;
49
+
50
+ /** (121) Google's OpenAI-compatible endpoint for Gemini (used when a Gemini service names no base URL). */
51
+ export const GEMINI_BASE_URL = 'https://generativelanguage.googleapis.com/v1beta/openai/';
52
+
53
+ /** The base URL a service's SDK client uses: its own, Gemini's default, or the SDK's default (undefined). */
54
+ export const baseUrlFor = (s: Pick<AiService, 'provider' | 'base_url'>) => s.base_url || (s.provider === 'gemini' ? GEMINI_BASE_URL : undefined);
55
+
42
56
  /** Where the service's key comes from: stored (encrypted), the server's environment, or nowhere. */
43
57
  export function keySource(s: Pick<AiService, 'provider' | 'api_key_enc'>): 'stored' | 'env' | 'missing' {
44
58
  if (s.api_key_enc) return 'stored';
45
59
  return process.env[ENV_KEY[s.provider]] ? 'env' : 'missing';
46
60
  }
47
61
 
62
+ /**
63
+ * The service's API key, decrypted only here (never stored elsewhere): the stored one, else the server's
64
+ * environment variable. An OpenAI-compatible endpoint may need none (a local Ollama): the SDK then sends a
65
+ * placeholder.
66
+ */
67
+ export function serviceKey(s: AiService): string {
68
+ let key: string | undefined;
69
+ try {
70
+ key = s.api_key_enc ? decryptSecret(s.api_key_enc) : process.env[ENV_KEY[s.provider]];
71
+ } catch (e) {
72
+ throw new AiError('config', `AI service ${s.name}: ${(e as Error).message}`);
73
+ }
74
+ if (!key && s.provider === 'openai_compatible') return 'none';
75
+ if (!key) throw new AiError('config', `AI service ${s.name} has no API key (enter one in the builder, or set ${ENV_KEY[s.provider]} on the server).`);
76
+ return key;
77
+ }
78
+
48
79
  export async function loadService(name: string): Promise<AiService | undefined> {
49
80
  return owner.one<AiService>('select * from meta.ai_service where name = $1', [name.toUpperCase()]);
50
81
  }
51
82
 
52
83
  /** The provider of a service, with its key (decrypted only here, never stored elsewhere). */
53
84
  export function providerFor(s: AiService): AiProvider {
54
- let apiKey: string | undefined;
55
- try {
56
- apiKey = s.api_key_enc ? decryptSecret(s.api_key_enc) : process.env[ENV_KEY[s.provider]];
57
- } catch (e) {
58
- throw new AiError('config', `AI service ${s.name}: ${(e as Error).message}`);
59
- }
60
- if (!apiKey) throw new AiError('config', `AI service ${s.name} has no API key (enter one in the builder, or set ${ENV_KEY[s.provider]} on the server).`);
61
85
  const conf = {
62
86
  model: s.model,
63
- apiKey,
64
- baseUrl: s.base_url,
87
+ apiKey: serviceKey(s),
88
+ baseUrl: baseUrlFor(s),
65
89
  timeoutMs: s.timeout_s * 1000,
66
90
  maxTokens: s.max_tokens,
67
91
  effort: s.effort,
package/src/ai/types.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  // The provider interface of pgkiln's AI features (src/ai/): one request in,
2
2
  // one answer out, whatever the provider. Providers live in anthropic.ts
3
3
  // (Claude, with the official @anthropic-ai/sdk) and openai.ts (the official
4
- // openai SDK); service.ts picks one for an AI service and adds the checks,
4
+ // openai SDK: OpenAI itself, Google Gemini through its OpenAI-compatible
5
+ // endpoint, and any OpenAI-compatible endpoint such as Ollama, vLLM or Azure
6
+ // OpenAI); service.ts picks one for an AI service and adds the checks,
5
7
  // limits and the usage log.
6
8
 
7
- export type ProviderName = 'anthropic' | 'openai';
9
+ export type ProviderName = 'anthropic' | 'openai' | 'gemini' | 'openai_compatible';
10
+
11
+ /** Providers that speak OpenAI's Chat Completions (the openai SDK). */
12
+ export const isOpenAiFamily = (p: ProviderName) => p !== 'anthropic';
8
13
 
9
14
  export interface ProviderConfig {
10
15
  /** the model, exactly as the developer entered it (never changed or downgraded) */
package/src/app.ts CHANGED
@@ -16,6 +16,7 @@ import { webSourceRoutes } from './builder/websources.ts';
16
16
  import { sqlRoutes } from './builder/sql.ts';
17
17
  import { automationRoutes } from './builder/automations.ts';
18
18
  import { workflowBuilderRoutes } from './builder/workflows.ts';
19
+ import { workflowMigrateRoutes } from './builder/workflow-migrate.ts';
19
20
  import { regionSettingsRoutes } from './builder/region-settings.ts';
20
21
  import { templateRoutes } from './builder/templates.ts';
21
22
  import { searchRoutes } from './builder/search.ts';
@@ -24,6 +25,7 @@ import { topSqlRoutes } from './builder/top-sql.ts';
24
25
  import { diagnosticsRoutes } from './builder/diagnostics.ts';
25
26
  import { ldapRoutes } from './builder/ldap.ts';
26
27
  import { aiRoutes } from './builder/ai.ts';
28
+ import { aiAgentRoutes } from './builder/ai-agents.ts';
27
29
  import { mailRoutes } from './builder/mail.ts';
28
30
  import { backupRoutes } from './builder/backups.ts';
29
31
  import { availabilityRoutes } from './builder/availability.ts';
@@ -55,6 +57,7 @@ import { dataReporterRoutes } from './runtime/data-reporter.ts';
55
57
  import { datasetRoutes } from './builder/datasets.ts';
56
58
  import { reporterBuilderRoutes } from './builder/reporter.ts';
57
59
  import { workspaceHomeRoutes } from './builder/workspace-home.ts';
60
+ import { teamRoutes } from './builder/team.ts';
58
61
  import { formDesignerRoutes } from './runtime/form-designer.ts';
59
62
  import { formDesignerBuilderRoutes } from './builder/form-designer.ts';
60
63
  import { builderErrorPages, builderServerError } from './builder/errors.ts';
@@ -215,6 +218,7 @@ export async function buildApp(opts: { logger?: boolean } = {}) {
215
218
  await app.register(layoutRoutes);
216
219
  await app.register(automationRoutes);
217
220
  await app.register(workflowBuilderRoutes);
221
+ await app.register(workflowMigrateRoutes);
218
222
  await app.register(reportSettingsRoutes);
219
223
  await app.register(regionSettingsRoutes);
220
224
  await app.register(templateRoutes);
@@ -225,6 +229,7 @@ export async function buildApp(opts: { logger?: boolean } = {}) {
225
229
  await app.register(diagnosticsRoutes);
226
230
  await app.register(ldapRoutes);
227
231
  await app.register(aiRoutes);
232
+ await app.register(aiAgentRoutes);
228
233
  await app.register(mailRoutes);
229
234
  await app.register(backupRoutes);
230
235
  await app.register(availabilityRoutes);
@@ -251,6 +256,7 @@ export async function buildApp(opts: { logger?: boolean } = {}) {
251
256
  await app.register(aiBuilderRoutes);
252
257
  await app.register(blueprintRoutes);
253
258
  await app.register(workspaceHomeRoutes);
259
+ await app.register(teamRoutes);
254
260
  app.get('/', async (_req, reply) => reply.redirect('/builder/home'));
255
261
  return app;
256
262
  }
@@ -6,7 +6,7 @@ import { devDbForApp } from '../workspace-db.ts';
6
6
  import { html, type Raw } from '../html.ts';
7
7
  import { icon } from '../icons.ts';
8
8
  import { templateProblem } from '../runtime/document.ts';
9
- import { invokeStepReferences, stepProblems, stepWarnings } from '../workflow.ts';
9
+ import { invokeStepReferences, isQuery, outcomeMap, stepProblems, stepWarnings } from '../workflow.ts';
10
10
  import { handlerProblems } from '../runtime/rest.ts';
11
11
  import { plpgsqlBlock } from '../runtime/rest-handlers.ts';
12
12
  import { processProblems } from '../runtime/processes.ts';
@@ -272,6 +272,8 @@ export async function advise(appId: number, tx?: pg.ClientBase, opts: { db?: Db
272
272
  if (st?.type === 'sql' && typeof st.code === 'string') checks.push([`${v}step ${st.name}: code`, st.code, 'statements']);
273
273
  if (st?.type === 'task' && typeof st.owners === 'string' && st.owners.trim()) checks.push([`${v}step ${st.name}: owners`, st.owners, 'select']);
274
274
  if (st?.type === 'switch' && Array.isArray(st.cases)) for (const cs of st.cases) if (typeof cs?.when === 'string') checks.push([`${v}step ${st.name}: when`, cs.when, 'boolean']);
275
+ // (123) a send_mail step's recipients query
276
+ if (st?.type === 'send_mail' && typeof st.to === 'string' && isQuery(st.to)) checks.push([`${v}step ${st.name}: to`, st.to, 'select']);
275
277
  for (const [field, sql, shape] of checks) {
276
278
  checked++;
277
279
  const problem = await checkSql(c, sql, shape);
@@ -369,7 +371,8 @@ export async function advise(appId: number, tx?: pg.ClientBase, opts: { db?: Db
369
371
  if (kind === 'rest_module') for (const problem of handlerProblems(row.handlers)) missing(e, 'Handlers (JSON)', problem);
370
372
  if (kind === 'workflow_definition') {
371
373
  const tasks = new Set(all.filter((x) => x.kind === 'task_definition').map((x) => x.row.name.toUpperCase()));
372
- for (const problem of stepProblems(row.steps, tasks)) missing(e, 'Steps (JSON)', problem);
374
+ const outcomes = outcomeMap(all.filter((x) => x.kind === 'task_definition').map((x) => x.row));
375
+ for (const problem of stepProblems(row.steps, tasks, { outcomes, drafts: false })) missing(e, 'Steps (JSON)', problem);
373
376
  for (const warning of stepWarnings(row.steps)) missing(e, 'Steps (JSON)', warning, 'warning');
374
377
  // invoke_api steps: REST data sources, parameters, web credentials, variables (sprint 32)
375
378
  const refs = {
@@ -382,7 +385,7 @@ export async function advise(appId: number, tx?: pg.ClientBase, opts: { db?: Db
382
385
  for (const warning of invokeRefs.warnings) missing(e, 'Steps (JSON)', warning, 'warning');
383
386
  if (row.dev_version) {
384
387
  const field = `Steps of version ${row.dev_version} (development)`;
385
- for (const problem of stepProblems(row.dev_steps, tasks)) missing(e, field, problem, 'warning');
388
+ for (const problem of stepProblems(row.dev_steps, tasks, { outcomes })) missing(e, field, problem, 'warning');
386
389
  for (const warning of stepWarnings(row.dev_steps)) missing(e, field, warning, 'warning');
387
390
  const devRefs = invokeStepReferences(row.dev_steps, refs);
388
391
  for (const problem of [...devRefs.errors, ...devRefs.warnings]) missing(e, field, problem, 'warning');
@@ -0,0 +1,372 @@
1
+ import type { FastifyInstance, FastifyReply } from 'fastify';
2
+ import { providerLabel } from '../ai/service.ts';
3
+ import { owner } from '../db.ts';
4
+ import { html, raw, type Raw } from '../html.ts';
5
+ import { icon } from '../icons.ts';
6
+ import { agentProblems, aiToolProblems, definitionProblems, EFFORTS, type AiAgent, type AiTool } from '../runtime/ai-agents.ts';
7
+ import type { Session } from '../session.ts';
8
+ import { appOr404 } from './forms.ts';
9
+ import { appHeader, back, BASE, csrf, developer, flash, input, region, select, send, shell, type Req } from './ui.ts';
10
+
11
+ // Shared Components → Generative AI → AI Agents (migration 121): the
12
+ // application's AI tools and AI agents, kept as two JSON lists in meta.app
13
+ // (ai_tools, ai_agents) and exported with the application. Each form checks
14
+ // its entry (src/runtime/ai-agents.ts) before it saves; the runtime checks
15
+ // again. Also the "Tool calls" region of Activity → AI usage.
16
+
17
+ type Body = Record<string, string | string[] | undefined>;
18
+ const one = (v: string | string[] | undefined) => (Array.isArray(v) ? v[0] : v) ?? '';
19
+ const all = (v: string | string[] | undefined) => (v === undefined ? [] : [v].flat());
20
+
21
+ const TOOL_TYPES: [string, string][] = [
22
+ ['sql', 'SQL query (read-only, as the application\'s role)'],
23
+ ['function', 'PL/pgSQL function (may change data)'],
24
+ ['rest', 'REST data source'],
25
+ ['javascript', 'JavaScript action in the browser'],
26
+ ['navigate', 'Navigate: a link to a page'],
27
+ ];
28
+
29
+ const when = (d: Date | string) => new Date(d).toISOString().slice(0, 19).replace('T', ' ');
30
+ const area = (name: string, label: string, value: unknown, rows: number, help: string, code = false) => html`<div class="field" data-wide><label class="label" for="f_${name}">${label}</label>
31
+ <textarea id="f_${name}" name="${name}" rows="${rows}"${code ? raw(' class="code" spellcheck="false"') : ''}>${value ?? ''}</textarea><small class="help">${help}</small></div>`;
32
+
33
+ async function schemes(appId: number) {
34
+ return (await owner.query<{ name: string }>('select name from meta.authz_scheme where app_id = $1 order by name', [appId])).rows.map((x) => x.name);
35
+ }
36
+
37
+ const authzSelect = (names: string[], cur: unknown, help: string) =>
38
+ select('authz', 'Authorization scheme', cur ?? '', [['', '- none: everyone who may use the page -'], ['MUST_NOT_BE_PUBLIC_USER', 'Must not be the public user'], ...names.map((n): [string, string] => [n, n]),
39
+ ...(typeof cur === 'string' && cur && cur !== 'MUST_NOT_BE_PUBLIC_USER' && !names.includes(cur) ? [[cur, `${cur} (does not exist)`] as [string, string]] : [])], help);
40
+
41
+ const crumbs = (a: { id: number; name: string }, last?: string): [string, string?][] => [
42
+ ['App Builder', BASE], [a.name, `${BASE}/apps/${a.id}`], ['Shared Components', `${BASE}/apps/${a.id}/shared`],
43
+ ...(last ? [['AI Agents', `${BASE}/apps/${a.id}/ai-agents`], [last]] as [string, string?][] : [['AI Agents']] as [string, string?][]),
44
+ ];
45
+
46
+ // ---------------------------------------------------------------- forms
47
+
48
+ async function agentForm(a: any, s: Session, x: Partial<AiAgent>, original: string, problems: string[]): Promise<Raw> {
49
+ const services = (await owner.query<{ name: string; provider: string; model: string }>(
50
+ `select s.name, s.provider, s.model from meta.ai_service s join meta.app_ai_service x on x.service_id = s.id and x.app_id = $1 where s.enabled order by s.name`, [a.id])).rows;
51
+ const tools = (a.ai_tools ?? []) as AiTool[];
52
+ const chosen = new Set(x.tools ?? []);
53
+ const svc: [string, string][] = [['', '- choose -'], ...services.map((v): [string, string] => [v.name, `${v.name} (${providerLabel(v.provider)}, ${v.model})`])];
54
+ if (x.service && !services.some((v) => v.name === x.service)) svc.push([x.service, `${x.service} (not available to this application)`]);
55
+ const usedBy = original
56
+ ? (await owner.query<{ page_no: number; title: string | null; page_id: number; id: number }>(
57
+ `select p.page_no, r.title, p.id as page_id, r.id from meta.region r join meta.page p on p.id = r.page_id
58
+ where p.app_id = $1 and r.type = 'ai_assistant' and upper(r.config->>'agent') = $2 order by p.page_no, r.seq`, [a.id, original])).rows
59
+ : [];
60
+ return html`${problems.length ? html`<div class="alert alert-error" role="alert">${problems.join(' ')}</div>` : ''}
61
+ ${services.length ? '' : html`<p class="muted">This application may not use any AI service yet: an administrator adds one under Workspace utilities → AI services and allows this application to use it.</p>`}
62
+ <form method="post" action="${BASE}/apps/${a.id}/ai-agents/agent">${csrf(s)}<input type="hidden" name="original" value="${original}">
63
+ <fieldset class="prop-group"><legend>Agent</legend><div class="form-grid">
64
+ ${input('name', 'Name', x.name ?? '', { required: true, placeholder: 'e.g. SALES_AGENT', help: 'Upper case letters, digits and _. AI assistant regions name the agent.' })}
65
+ ${input('description', 'Description', x.description ?? '')}
66
+ ${select('service', 'AI service', x.service ?? '', svc, 'The conversation, the tools\' results and the system prompt go to this service\'s provider.')}
67
+ ${select('effort', 'Reasoning effort', x.effort ?? '', [['', 'The service\'s'], ...EFFORTS.map((e): [string, string] => [e, e])],
68
+ 'Claude: the output effort (thinking depth). OpenAI, Gemini and OpenAI-compatible: reasoning_effort (leave empty for models without reasoning).')}
69
+ ${input('max_steps', 'Maximum steps', x.max_steps ?? '', { type: 'number', placeholder: '5', help: 'Rounds of tool calls per question (1 – 20); then the model must answer.' })}
70
+ ${input('max_tokens', 'Maximum output tokens', x.max_tokens ?? '', { type: 'number', placeholder: 'the service\'s', help: 'Per call to the model (never above the service\'s maximum).' })}
71
+ ${authzSelect(await schemes(a.id), x.authz, 'Only users who pass it may use the agent (regions show a message to the others).')}
72
+ ${area('system', 'System prompt', x.system ?? '', 6, 'Who the agent is, what it may answer and when to use which tool. &ITEM. is replaced by the item\'s value as delimited data.')}
73
+ </div></fieldset>
74
+ <fieldset class="prop-group"><legend>Tools</legend>
75
+ ${tools.length
76
+ ? html`<p class="muted u-mt0">The agent acts only through the tools you tick.</p>${tools.map((t) => html`<div class="field"><label class="check"><input type="checkbox" name="tools" value="${t.name}"${chosen.has(t.name) ? raw(' checked') : ''}> <b>${t.name}</b> <span class="muted">(${t.type}) ${t.description}</span></label></div>`)}`
77
+ : html`<p class="muted u-mt0">No AI tools yet: <a href="${BASE}/apps/${a.id}/ai-agents/tool">add one</a>.</p>`}
78
+ </fieldset>
79
+ <div class="buttons"><button class="btn btn-hot">${original ? 'Save' : 'Add agent'}</button></div>
80
+ </form>
81
+ ${original ? html`<h3>Used by</h3>${usedBy.length
82
+ ? html`<ul>${usedBy.map((r) => html`<li><a href="${BASE}/pages/${r.page_id}?c=region-${r.id}">Page ${r.page_no}: ${r.title ?? 'AI assistant'}</a></li>`)}</ul>`
83
+ : html`<p class="muted">No AI assistant region uses this agent yet (Page Designer → an AI assistant region → AI agent).</p>`}
84
+ <form method="post" action="${BASE}/apps/${a.id}/ai-agents/agent/delete" class="danger-zone">${csrf(s)}<input type="hidden" name="name" value="${original}">
85
+ <button class="btn btn-danger" data-confirm="Delete agent ${original}? Regions that use it show that the assistant is not set up.">Delete agent</button></form>` : ''}`;
86
+ }
87
+
88
+ async function toolForm(a: any, s: Session, x: Partial<AiTool> & { parameters_text?: string }, original: string, problems: string[]): Promise<Raw> {
89
+ const sources = (await owner.query<{ name: string }>('select name from meta.rest_source where app_id = $1 order by name', [a.id])).rows.map((r) => r.name);
90
+ const params = x.parameters_text ?? (x.parameters ? JSON.stringify(x.parameters, null, 2) : '');
91
+ const usedBy = original ? ((a.ai_agents ?? []) as AiAgent[]).filter((g) => (g.tools ?? []).includes(original)).map((g) => g.name) : [];
92
+ return html`${problems.length ? html`<div class="alert alert-error" role="alert">${problems.join(' ')}</div>` : ''}
93
+ <form method="post" action="${BASE}/apps/${a.id}/ai-agents/tool">${csrf(s)}<input type="hidden" name="original" value="${original}">
94
+ <fieldset class="prop-group"><legend>Tool</legend><div class="form-grid">
95
+ ${input('name', 'Name', x.name ?? '', { required: true, placeholder: 'e.g. top_earners', help: 'Letters, digits and _: the model calls the tool by this name.' })}
96
+ ${select('type', 'Type', x.type ?? 'sql', TOOL_TYPES, 'What the tool does; each type uses the fields below that name it.')}
97
+ ${area('description', 'Description', x.description ?? '', 2, 'Tells the model what the tool does and when to use it.')}
98
+ ${area('sql', 'SQL query (SQL)', x.sql ?? '', 6, 'One SELECT with the parameters as :NAME bind values (and :APP_USER, items). Runs as the application\'s database role (grants and row level security apply) in a read-only transaction.', true)}
99
+ ${input('function', 'Function (PL/pgSQL)', x.function ?? '', { placeholder: 'e.g. sales.give_discount', help: 'schema.name, called as the application\'s role with named arguments: each parameter name in lower case is an argument name (P_ORDER → p_order). It may change data.' })}
100
+ ${select('source', 'REST data source (REST)', x.source ?? '', [['', '-'], ...sources.map((n): [string, string] => [n, n])], 'The parameters become the source\'s parameters.')}
101
+ ${input('action', 'Action (JavaScript)', x.action ?? '', { placeholder: 'e.g. highlightOrder', help: 'After the answer the browser calls the function registered with pgkiln.actions.register(name) with {value: the arguments}, and the dynamic actions of the custom event of this name.' })}
102
+ ${input('pages', 'Pages (Navigate)', (x.pages ?? []).join(', '), { placeholder: 'e.g. 3, 7', help: 'The page numbers the agent may link to. pgkiln offers only those the user may open and builds the URL (item values with their checksum).' })}
103
+ ${area('parameters', 'Parameters (JSON; not for Navigate)', params, 6, '{"DNAME": {"type": "string", "description": "A department name"}, "LIMIT": {"type": "integer", "optional": true}}. Types: string, integer, number, boolean, date; "enum" for a list of texts.', true)}
104
+ ${input('max_rows', 'Maximum rows', x.max_rows ?? '', { type: 'number', placeholder: '50', help: 'Rows the model gets back (1 – 500).' })}
105
+ ${input('timeout_s', 'Time limit (seconds)', x.timeout_s ?? '', { type: 'number', placeholder: '15', help: 'SQL and function tools (1 – 60).' })}
106
+ ${authzSelect(await schemes(a.id), x.authz, 'Only users who pass it are offered the tool.')}
107
+ </div></fieldset>
108
+ <div class="buttons"><button class="btn btn-hot">${original ? 'Save' : 'Add tool'}</button></div>
109
+ </form>
110
+ ${original ? html`<h3>Used by</h3>${usedBy.length ? html`<p>${usedBy.join(', ')}</p>` : html`<p class="muted">No agent uses this tool yet.</p>`}
111
+ <form method="post" action="${BASE}/apps/${a.id}/ai-agents/tool/delete" class="danger-zone">${csrf(s)}<input type="hidden" name="name" value="${original}">
112
+ <button class="btn btn-danger" data-confirm="Delete tool ${original}?">Delete tool</button></form>` : ''}`;
113
+ }
114
+
115
+ // ---------------------------------------------------------------- reading the forms
116
+
117
+ const intOrUndef = (v: string, what: string, errors: string[]) => {
118
+ const t = v.trim();
119
+ if (!t) return undefined;
120
+ const n = Number(t);
121
+ if (!Number.isInteger(n)) errors.push(`${what} is a whole number.`);
122
+ return n;
123
+ };
124
+
125
+ function agentFrom(b: Body, errors: string[]): AiAgent {
126
+ const x: AiAgent = { name: one(b.name).trim().toUpperCase(), service: one(b.service).trim().toUpperCase() };
127
+ const text = (k: 'description' | 'system', max: number) => {
128
+ const v = one(b[k]).replace(/\r\n?/g, '\n').trim().slice(0, max);
129
+ if (v) x[k] = v;
130
+ };
131
+ text('description', 500);
132
+ text('system', 20_000);
133
+ const effort = one(b.effort);
134
+ if (effort) x.effort = effort as AiAgent['effort'];
135
+ const tools = all(b.tools).map((t) => t.trim()).filter(Boolean);
136
+ if (tools.length) x.tools = [...new Set(tools)];
137
+ const steps = intOrUndef(one(b.max_steps), 'Maximum steps', errors);
138
+ if (steps !== undefined) x.max_steps = steps;
139
+ const tokens = intOrUndef(one(b.max_tokens), 'Maximum output tokens', errors);
140
+ if (tokens !== undefined) x.max_tokens = tokens;
141
+ const authz = one(b.authz).trim();
142
+ if (authz) x.authz = authz;
143
+ return x;
144
+ }
145
+
146
+ function toolFrom(b: Body, errors: string[]): AiTool & { parameters_text?: string } {
147
+ const type = one(b.type) as AiTool['type'];
148
+ const x: AiTool & { parameters_text?: string } = { name: one(b.name).trim(), type, description: one(b.description).replace(/\r\n?/g, '\n').trim() };
149
+ if (type === 'sql') x.sql = one(b.sql).replace(/\r\n?/g, '\n').trim();
150
+ if (type === 'function') x.function = one(b.function).trim();
151
+ if (type === 'rest') x.source = one(b.source).trim().toUpperCase();
152
+ if (type === 'javascript') x.action = one(b.action).trim();
153
+ if (type === 'navigate') x.pages = one(b.pages).split(/[\s,;]+/).filter(Boolean).map(Number);
154
+ const params = one(b.parameters).trim();
155
+ if (params && type !== 'navigate') {
156
+ try {
157
+ x.parameters = JSON.parse(params);
158
+ } catch {
159
+ errors.push('Parameters: not valid JSON.');
160
+ x.parameters_text = params;
161
+ }
162
+ }
163
+ if (type === 'sql' || type === 'function' || type === 'rest') {
164
+ const rows = intOrUndef(one(b.max_rows), 'Maximum rows', errors);
165
+ if (rows !== undefined) x.max_rows = rows;
166
+ }
167
+ if (type === 'sql' || type === 'function') {
168
+ const t = intOrUndef(one(b.timeout_s), 'Time limit', errors);
169
+ if (t !== undefined) x.timeout_s = t;
170
+ }
171
+ const authz = one(b.authz).trim();
172
+ if (authz) x.authz = authz;
173
+ return x;
174
+ }
175
+
176
+ /** Change the application's lists in one transaction (row locked); `change` returns problems to refuse with. */
177
+ async function update(appId: number, change: (agents: AiAgent[], tools: AiTool[]) => string[] | Promise<string[]>) {
178
+ return owner.tx(async (c) => {
179
+ const row = (await c.query<{ ai_agents: AiAgent[]; ai_tools: AiTool[] }>('select ai_agents, ai_tools from meta.app where id = $1 for update', [appId])).rows[0];
180
+ const agents = [...(row?.ai_agents ?? [])];
181
+ const tools = [...(row?.ai_tools ?? [])];
182
+ const problems = await change(agents, tools);
183
+ if (problems.length) return problems;
184
+ await c.query('update meta.app set ai_agents = $2, ai_tools = $3, updated_at = now() where id = $1', [appId, JSON.stringify(agents), JSON.stringify(tools)]);
185
+ return [];
186
+ });
187
+ }
188
+
189
+ // ---------------------------------------------------------------- AI usage: tool calls
190
+
191
+ /** The "Tool calls" region of Activity → AI usage: the latest calls with their arguments. */
192
+ export async function toolCallTable(appId: number): Promise<Raw> {
193
+ const rows = (await owner.query('select * from meta.ai_tool_call where app_id = $1 order by id desc limit 100', [appId])).rows;
194
+ return region('Tool calls', html`<p class="muted u-mt0">Every tool call of the application's AI assistants and agents, with the arguments the model gave (kept 30 days).</p>
195
+ <div class="table-wrap"><table class="report report-reflow">
196
+ <thead><tr><th>When (UTC)</th><th class="num">Page</th><th>User</th><th>Agent</th><th>Tool</th><th>Arguments</th><th class="num">ms</th><th>Status</th></tr></thead>
197
+ <tbody>${rows.length ? rows.map((r) => html`<tr>
198
+ <td data-label="When">${when(r.at)}</td><td class="num" data-label="Page">${r.page_no ?? ''}</td><td data-label="User">${r.username ?? ''}</td>
199
+ <td data-label="Agent">${r.agent ?? html`<span class="muted">region</span>`}</td><td data-label="Tool">${r.tool} <span class="muted">${r.type}</span></td>
200
+ <td data-label="Arguments"><code class="ai-args">${JSON.stringify(r.arguments)}</code></td><td class="num" data-label="ms">${r.duration_ms}</td>
201
+ <td data-label="Status"><span class="tag${r.status === 'ok' ? ' tag-ok' : ' tag-error'}">${r.status}</span>${r.message ? html` <span class="muted">${r.message}</span>` : ''}</td></tr>`)
202
+ : html`<tr><td colspan="8" class="empty">No tool calls yet.</td></tr>`}</tbody></table></div>`);
203
+ }
204
+
205
+ // ---------------------------------------------------------------- routes
206
+
207
+ export async function aiAgentRoutes(app: FastifyInstance) {
208
+ const load = async (req: Req, reply: FastifyReply) => {
209
+ const s = await developer(req, reply);
210
+ if (!s) return null;
211
+ const a = await appOr404(req.params.id);
212
+ if (!a) {
213
+ reply.code(404).send('Not found');
214
+ return null;
215
+ }
216
+ return { s, a };
217
+ };
218
+
219
+ app.get(`${BASE}/apps/:id(^\\d+$)/ai-agents`, async (req: Req, reply) => {
220
+ const ok = await load(req, reply);
221
+ if (!ok) return;
222
+ const { s, a } = ok;
223
+ const agents = (a.ai_agents ?? []) as AiAgent[];
224
+ const tools = (a.ai_tools ?? []) as AiTool[];
225
+ const problems = definitionProblems(agents, tools);
226
+ const main = html`${appHeader(a, 'shared')}
227
+ <div class="title-row"><h1>AI Agents</h1></div>
228
+ <p class="muted u-mt0">An agent answers in an AI assistant region and acts only through the tools you give it: SQL queries (read-only, as the application's database role, so row level security applies), PL/pgSQL functions, REST data sources, JavaScript actions in the browser, and links to pages (pgkiln builds the URL and its checksum, only for pages the user may open). What users type and the tools' results go to the agent's AI service. <a href="${BASE}/apps/${a.id}/ai">AI usage</a> lists every tool call.</p>
229
+ ${problems.length ? html`<div class="alert alert-error" role="alert">${problems.join(' ')}</div>` : ''}
230
+ <div class="columns">
231
+ ${region('Agents', html`<div class="table-wrap"><table class="report report-reflow"><thead><tr><th>Agent</th><th>AI service</th><th>Tools</th><th>Authorization</th></tr></thead>
232
+ <tbody>${agents.length ? agents.map((g) => html`<tr><td data-label="Agent"><a href="${BASE}/apps/${a.id}/ai-agents/agent?name=${encodeURIComponent(g.name)}">${g.name}</a>${g.description ? html` <span class="muted">${g.description}</span>` : ''}</td>
233
+ <td data-label="AI service">${g.service}${g.effort ? html` <span class="muted">(${g.effort})</span>` : ''}</td><td data-label="Tools">${(g.tools ?? []).join(', ')}</td><td data-label="Authorization">${g.authz ?? ''}</td></tr>`)
234
+ : html`<tr><td colspan="4" class="empty">No AI agents yet.</td></tr>`}</tbody></table></div>
235
+ <p><a class="btn" href="${BASE}/apps/${a.id}/ai-agents/agent">${icon('plus')} Add agent</a></p>`)}
236
+ ${region('Tools', html`<div class="table-wrap"><table class="report report-reflow"><thead><tr><th>Tool</th><th>Type</th><th>Authorization</th></tr></thead>
237
+ <tbody>${tools.length ? tools.map((t) => html`<tr><td data-label="Tool"><a href="${BASE}/apps/${a.id}/ai-agents/tool?name=${encodeURIComponent(t.name)}">${t.name}</a> <span class="muted">${t.description}</span></td>
238
+ <td data-label="Type">${t.type}</td><td data-label="Authorization">${t.authz ?? ''}</td></tr>`)
239
+ : html`<tr><td colspan="3" class="empty">No AI tools yet.</td></tr>`}</tbody></table></div>
240
+ <p><a class="btn" href="${BASE}/apps/${a.id}/ai-agents/tool">${icon('plus')} Add tool</a></p>`)}
241
+ </div>`;
242
+ return send(reply, s, shell(s, 'AI Agents', crumbs(a), main));
243
+ });
244
+
245
+ app.get(`${BASE}/apps/:id(^\\d+$)/ai-agents/agent`, async (req: Req, reply) => {
246
+ const ok = await load(req, reply);
247
+ if (!ok) return;
248
+ const { s, a } = ok;
249
+ const name = String((req.query as Record<string, unknown>)?.name ?? '').toUpperCase();
250
+ const x = name ? ((a.ai_agents ?? []) as AiAgent[]).find((g) => g.name === name) : {};
251
+ if (!x) return reply.code(404).send('Not found');
252
+ const title = name || 'New agent';
253
+ const main = html`${appHeader(a, 'shared')}<div class="title-row"><h1 class="ai-name">${title}</h1></div>${region('AI agent', await agentForm(a, s, x, name, []))}`;
254
+ return send(reply, s, shell(s, title, crumbs(a, title), main));
255
+ });
256
+
257
+ app.post(`${BASE}/apps/:id(^\\d+$)/ai-agents/agent`, async (req: Req, reply) => {
258
+ const ok = await load(req, reply);
259
+ if (!ok) return;
260
+ const { s, a } = ok;
261
+ const b = (req.body ?? {}) as Body;
262
+ const original = one(b.original).toUpperCase();
263
+ const errors: string[] = [];
264
+ const x = agentFrom(b, errors);
265
+ const problems = errors.length ? errors : await update(a.id, (agents, tools) => {
266
+ const out = agentProblems(x, new Set(tools.map((t) => t.name)));
267
+ const at = original ? agents.findIndex((g) => g.name === original) : -1;
268
+ if (original && at < 0) out.push(`Agent ${original} no longer exists.`);
269
+ if (agents.some((g, i) => g.name === x.name && i !== at)) out.push(`An agent ${x.name} exists already.`);
270
+ if (out.length) return out;
271
+ if (at >= 0) agents[at] = x;
272
+ else agents.push(x);
273
+ return [];
274
+ });
275
+ if (problems.length) {
276
+ const main = html`${appHeader(a, 'shared')}<div class="title-row"><h1 class="ai-name">${original || 'New agent'}</h1></div>${region('AI agent', await agentForm(a, s, x, original, problems))}`;
277
+ return send(reply.code(422), s, shell(s, original || 'New agent', crumbs(a, original || 'New agent'), main));
278
+ }
279
+ if (original && original !== x.name)
280
+ await owner.query(`update meta.region r set config = jsonb_set(r.config, '{agent}', to_jsonb($3::text)) from meta.page p
281
+ where p.id = r.page_id and p.app_id = $1 and r.type = 'ai_assistant' and upper(r.config->>'agent') = $2`, [a.id, original, x.name]);
282
+ flash(s, 'AI agent saved.');
283
+ return back(reply, s, `${BASE}/apps/${a.id}/ai-agents/agent?name=${encodeURIComponent(x.name)}`);
284
+ });
285
+
286
+ app.post(`${BASE}/apps/:id(^\\d+$)/ai-agents/agent/delete`, async (req: Req, reply) => {
287
+ const ok = await load(req, reply);
288
+ if (!ok) return;
289
+ const { s, a } = ok;
290
+ const name = one((req.body as Body)?.name).toUpperCase();
291
+ await update(a.id, (agents) => {
292
+ const at = agents.findIndex((g) => g.name === name);
293
+ if (at >= 0) agents.splice(at, 1);
294
+ return [];
295
+ });
296
+ flash(s, `AI agent ${name} deleted.`);
297
+ return back(reply, s, `${BASE}/apps/${a.id}/ai-agents`);
298
+ });
299
+
300
+ app.get(`${BASE}/apps/:id(^\\d+$)/ai-agents/tool`, async (req: Req, reply) => {
301
+ const ok = await load(req, reply);
302
+ if (!ok) return;
303
+ const { s, a } = ok;
304
+ const name = String((req.query as Record<string, unknown>)?.name ?? '');
305
+ const x = name ? ((a.ai_tools ?? []) as AiTool[]).find((t) => t.name === name) : {};
306
+ if (!x) return reply.code(404).send('Not found');
307
+ const title = name || 'New tool';
308
+ const main = html`${appHeader(a, 'shared')}<div class="title-row"><h1 class="ai-name">${title}</h1></div>${region('AI tool', await toolForm(a, s, x, name, []))}`;
309
+ return send(reply, s, shell(s, title, crumbs(a, title), main));
310
+ });
311
+
312
+ app.post(`${BASE}/apps/:id(^\\d+$)/ai-agents/tool`, async (req: Req, reply) => {
313
+ const ok = await load(req, reply);
314
+ if (!ok) return;
315
+ const { s, a } = ok;
316
+ const b = (req.body ?? {}) as Body;
317
+ const original = one(b.original);
318
+ const errors: string[] = [];
319
+ const x = toolFrom(b, errors);
320
+ const { parameters_text: _, ...tool } = x;
321
+ const problems = errors.length ? errors : await update(a.id, async (agents, tools) => {
322
+ const out = aiToolProblems(tool);
323
+ if (tool.type === 'rest' && tool.source && !(await owner.one('select 1 from meta.rest_source where app_id = $1 and name = $2', [a.id, tool.source])))
324
+ out.push(`There is no REST data source ${tool.source}.`);
325
+ if (tool.type === 'navigate') {
326
+ const known = new Set((await owner.query<{ page_no: number }>('select page_no from meta.page where app_id = $1', [a.id])).rows.map((p) => p.page_no));
327
+ for (const p of tool.pages ?? []) if (!known.has(p)) out.push(`There is no page ${p}.`);
328
+ }
329
+ const at = original ? tools.findIndex((t) => t.name === original) : -1;
330
+ if (original && at < 0) out.push(`Tool ${original} no longer exists.`);
331
+ if (tools.some((t, i) => t.name === tool.name && i !== at)) out.push(`A tool ${tool.name} exists already.`);
332
+ if (out.length) return out;
333
+ if (at >= 0) tools[at] = tool;
334
+ else tools.push(tool);
335
+ // a renamed tool keeps its agents
336
+ if (original && original !== tool.name) for (const g of agents) if (g.tools) g.tools = g.tools.map((n) => (n === original ? tool.name : n));
337
+ return [];
338
+ });
339
+ if (problems.length) {
340
+ const main = html`${appHeader(a, 'shared')}<div class="title-row"><h1 class="ai-name">${original || 'New tool'}</h1></div>${region('AI tool', await toolForm(a, s, x, original, problems))}`;
341
+ return send(reply.code(422), s, shell(s, original || 'New tool', crumbs(a, original || 'New tool'), main));
342
+ }
343
+ flash(s, 'AI tool saved.');
344
+ return back(reply, s, `${BASE}/apps/${a.id}/ai-agents/tool?name=${encodeURIComponent(tool.name)}`);
345
+ });
346
+
347
+ app.post(`${BASE}/apps/:id(^\\d+$)/ai-agents/tool/delete`, async (req: Req, reply) => {
348
+ const ok = await load(req, reply);
349
+ if (!ok) return;
350
+ const { s, a } = ok;
351
+ const name = one((req.body as Body)?.name);
352
+ const problems = await update(a.id, (agents, tools) => {
353
+ const users = agents.filter((g) => (g.tools ?? []).includes(name)).map((g) => g.name);
354
+ if (users.length) return [`Tool ${name} is used by ${users.join(', ')}: take it out of those agents first.`];
355
+ const at = tools.findIndex((t) => t.name === name);
356
+ if (at >= 0) tools.splice(at, 1);
357
+ return [];
358
+ });
359
+ if (problems.length) {
360
+ flash(s, problems.join(' '), 'error');
361
+ return back(reply, s, `${BASE}/apps/${a.id}/ai-agents/tool?name=${encodeURIComponent(name)}`);
362
+ }
363
+ flash(s, `AI tool ${name} deleted.`);
364
+ return back(reply, s, `${BASE}/apps/${a.id}/ai-agents`);
365
+ });
366
+ }
367
+
368
+ /** The agents an AI assistant region may name (the region settings form). */
369
+ export async function agentOptions(appId: number) {
370
+ return ((await owner.one<{ ai_agents: AiAgent[] }>('select ai_agents from meta.app where id = $1', [appId]))?.ai_agents ?? []).map((g) => g.name);
371
+ }
372
+
@@ -1,6 +1,6 @@
1
1
  import type { FastifyInstance, FastifyReply } from 'fastify';
2
2
  import pg from 'pg';
3
- import { generate } from '../ai/service.ts';
3
+ import { generate, providerLabel } from '../ai/service.ts';
4
4
  import { AiError } from '../ai/types.ts';
5
5
  import { owner } from '../db.ts';
6
6
  import { html, raw, type Raw } from '../html.ts';
@@ -116,12 +116,12 @@ async function serviceBox(s: Session) {
116
116
  const admin = await isAdmin(s.username);
117
117
  if (!admin)
118
118
  return svc
119
- ? html`<p class="muted">AI service: <b>${svc.name}</b> (${svc.provider === 'anthropic' ? 'Claude' : 'OpenAI'}, ${svc.model}). Table and column names, types and descriptions, and what you type, are sent to it.</p>`
119
+ ? html`<p class="muted">AI service: <b>${svc.name}</b> (${providerLabel(svc.provider)}, ${svc.model}). Table and column names, types and descriptions, and what you type, are sent to it.</p>`
120
120
  : html`<div class="alert alert-info">The App Builder has no AI service yet: ask an administrator to choose one here.</div>`;
121
121
  const all = (await owner.query<{ name: string; provider: string; model: string }>('select name, provider, model from meta.ai_service where enabled order by name')).rows;
122
122
  return html`<form method="post" action="${BASE}/sql/ai/service" class="component-form">${csrf(s)}
123
123
  <div class="form-grid"><div class="field"><label class="label" for="f_ai_builder_service">AI service of the App Builder (administrators)</label>
124
- <select id="f_ai_builder_service" name="service">${opt('', '- none: no AI in the App Builder -', svc?.name ?? '')}${all.map((x) => opt(x.name, `${x.name} (${x.provider === 'anthropic' ? 'Claude' : 'OpenAI'}, ${x.model})`, svc?.name ?? ''))}</select>
124
+ <select id="f_ai_builder_service" name="service">${opt('', '- none: no AI in the App Builder -', svc?.name ?? '')}${all.map((x) => opt(x.name, `${x.name} (${providerLabel(x.provider)}, ${x.model})`, svc?.name ?? ''))}</select>
125
125
  <small class="help">Table and column names, types and descriptions (never rows), and what developers type, are sent to it. Its calls are logged without an application.</small></div></div>
126
126
  <div class="buttons"><button class="btn">Save</button></div></form>`;
127
127
  }