@openlegion/platform 0.0.0-stage → 0.1.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 (120) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1189 -2
  3. package/dist/body.d.ts +9 -0
  4. package/dist/body.js +28 -0
  5. package/dist/chat-history.d.ts +53 -0
  6. package/dist/chat-history.js +11 -0
  7. package/dist/client.d.ts +217 -0
  8. package/dist/client.js +367 -0
  9. package/dist/core.d.ts +47 -0
  10. package/dist/core.js +1 -0
  11. package/dist/crypto.d.ts +25 -0
  12. package/dist/crypto.js +81 -0
  13. package/dist/errors.d.ts +111 -0
  14. package/dist/errors.js +134 -0
  15. package/dist/events.d.ts +131 -0
  16. package/dist/events.js +5 -0
  17. package/dist/generated/api.d.ts +5505 -0
  18. package/dist/generated/api.js +446 -0
  19. package/dist/handler.d.ts +201 -0
  20. package/dist/handler.js +682 -0
  21. package/dist/index.d.ts +24 -0
  22. package/dist/index.js +12 -0
  23. package/dist/kit-agents.d.ts +95 -0
  24. package/dist/kit-agents.js +280 -0
  25. package/dist/kit-codes.d.ts +279 -0
  26. package/dist/kit-codes.js +44 -0
  27. package/dist/kit-connections.d.ts +21 -0
  28. package/dist/kit-connections.js +189 -0
  29. package/dist/kit-files.d.ts +8 -0
  30. package/dist/kit-files.js +117 -0
  31. package/dist/kit-model-keys.d.ts +8 -0
  32. package/dist/kit-model-keys.js +86 -0
  33. package/dist/kit-threads.d.ts +3 -0
  34. package/dist/kit-threads.js +92 -0
  35. package/dist/kit-tools.d.ts +21 -0
  36. package/dist/kit-tools.js +47 -0
  37. package/dist/kit.d.ts +130 -0
  38. package/dist/kit.js +155 -0
  39. package/dist/mcp-server.d.ts +27 -0
  40. package/dist/mcp-server.js +166 -0
  41. package/dist/next.d.ts +24 -0
  42. package/dist/next.js +6 -0
  43. package/dist/ol-signed.d.ts +82 -0
  44. package/dist/ol-signed.js +115 -0
  45. package/dist/react/add-server.d.ts +90 -0
  46. package/dist/react/add-server.js +183 -0
  47. package/dist/react/agent-chat.d.ts +925 -0
  48. package/dist/react/agent-chat.js +1917 -0
  49. package/dist/react/agent-editor.d.ts +133 -0
  50. package/dist/react/agent-editor.js +222 -0
  51. package/dist/react/agent-kit.d.ts +160 -0
  52. package/dist/react/agent-kit.js +279 -0
  53. package/dist/react/agent-list.d.ts +247 -0
  54. package/dist/react/agent-list.js +248 -0
  55. package/dist/react/approval-card.d.ts +272 -0
  56. package/dist/react/approval-card.js +372 -0
  57. package/dist/react/artifact-card.d.ts +38 -0
  58. package/dist/react/artifact-card.js +39 -0
  59. package/dist/react/avatar.d.ts +21 -0
  60. package/dist/react/avatar.js +33 -0
  61. package/dist/react/card-ledger.d.ts +30 -0
  62. package/dist/react/card-ledger.js +49 -0
  63. package/dist/react/chat-timing.d.ts +121 -0
  64. package/dist/react/chat-timing.js +229 -0
  65. package/dist/react/computer-view.d.ts +309 -0
  66. package/dist/react/computer-view.js +543 -0
  67. package/dist/react/computer-viewer.d.ts +175 -0
  68. package/dist/react/computer-viewer.js +555 -0
  69. package/dist/react/computer-wire.d.ts +178 -0
  70. package/dist/react/computer-wire.js +244 -0
  71. package/dist/react/connect-button.d.ts +145 -0
  72. package/dist/react/connect-button.js +212 -0
  73. package/dist/react/connection-request.d.ts +16 -0
  74. package/dist/react/connection-request.js +18 -0
  75. package/dist/react/connections-list.d.ts +90 -0
  76. package/dist/react/connections-list.js +76 -0
  77. package/dist/react/errors.d.ts +250 -0
  78. package/dist/react/errors.js +146 -0
  79. package/dist/react/files.d.ts +41 -0
  80. package/dist/react/files.js +78 -0
  81. package/dist/react/index.d.ts +35 -0
  82. package/dist/react/index.js +26 -0
  83. package/dist/react/limit-words.d.ts +80 -0
  84. package/dist/react/limit-words.js +53 -0
  85. package/dist/react/markdown.d.ts +42 -0
  86. package/dist/react/markdown.js +56 -0
  87. package/dist/react/md-parse.d.ts +106 -0
  88. package/dist/react/md-parse.js +638 -0
  89. package/dist/react/model-keys-card.d.ts +94 -0
  90. package/dist/react/model-keys-card.js +123 -0
  91. package/dist/react/reload.d.ts +211 -0
  92. package/dist/react/reload.js +339 -0
  93. package/dist/react/reveal.d.ts +17 -0
  94. package/dist/react/reveal.js +22 -0
  95. package/dist/react/schedule-editor.d.ts +107 -0
  96. package/dist/react/schedule-editor.js +172 -0
  97. package/dist/react/schedule-time.d.ts +66 -0
  98. package/dist/react/schedule-time.js +90 -0
  99. package/dist/react/styles.css +1274 -0
  100. package/dist/react/transport.d.ts +49 -0
  101. package/dist/react/transport.js +380 -0
  102. package/dist/react/use-connections.d.ts +155 -0
  103. package/dist/react/use-connections.js +231 -0
  104. package/dist/react/use-model-keys.d.ts +47 -0
  105. package/dist/react/use-model-keys.js +57 -0
  106. package/dist/react/use-threads.d.ts +63 -0
  107. package/dist/react/use-threads.js +73 -0
  108. package/dist/react/vault-form.d.ts +92 -0
  109. package/dist/react/vault-form.js +151 -0
  110. package/dist/schema.d.ts +47 -0
  111. package/dist/schema.js +16 -0
  112. package/dist/secrets.d.ts +15 -0
  113. package/dist/secrets.js +18 -0
  114. package/dist/stream.d.ts +129 -0
  115. package/dist/stream.js +192 -0
  116. package/dist/testing.d.ts +33 -0
  117. package/dist/testing.js +40 -0
  118. package/dist/webhooks.d.ts +31 -0
  119. package/dist/webhooks.js +72 -0
  120. package/package.json +85 -3
@@ -0,0 +1,133 @@
1
+ import { type AgentInput, type AgentKitErrorLabels, type AgentLimit, type KitAgent, type KitModelPreset, type TemplateAgent, type UseAgentKitOptions } from "./agent-kit.ts";
2
+ type Tier = "background" | "fast" | "reasoning" | "frontier";
3
+ /** A model choice: a preset, Auto, or "" (follow the template's preset). */
4
+ type ModelValue = Tier | "auto" | "";
5
+ export interface AgentEditorLabels {
6
+ kind: string;
7
+ name: string;
8
+ role: string;
9
+ instructions: string;
10
+ /** Under the instructions box. */
11
+ instructionsHint: string;
12
+ /** The model preset's field: a choice when there are two or more, the one preset as plain text when there is one. */
13
+ model: string;
14
+ /** The model list's choice that follows the template: its preset, by name in `modelDefaultKnown` (`{tier}`) when known. */
15
+ modelDefault: string;
16
+ modelDefaultKnown: string;
17
+ /** With `showModelNames`: a preset's word and its model's name as the read answers it. `{tier}` and `{model}` are replaced. */
18
+ modelNamed: string;
19
+ /** With `showModelNames`: the one line under the choice, on the preset chosen. */
20
+ modelHints: Record<Tier, string>;
21
+ /** With `showModelNames`, after that line, when the workspace's assistants run on a person's own model key. */
22
+ modelOwnKey: string;
23
+ /** The choice that lets the platform pick a model for each run, among the presets this person may choose. */
24
+ modelAuto: string;
25
+ /** Under the choice, when it is Auto. */
26
+ modelAutoHint: string;
27
+ tools: string;
28
+ /** Beside a tool that waits for a person's approval. */
29
+ toolHeld: string;
30
+ create: string;
31
+ save: string;
32
+ saving: string;
33
+ saved: string;
34
+ /** `{used}` and `{max}` are replaced. */
35
+ counter: string;
36
+ /** An agent nothing about may be changed. */
37
+ nothingToChange: string;
38
+ /** No template agent may be made more of. */
39
+ nothingToCreate: string;
40
+ /** In place of the create form when the workspace has made the most its template allows. `{count}` and `{max}` are replaced. */
41
+ limit: string;
42
+ loading: string;
43
+ models: Record<Tier, string>;
44
+ errors?: Partial<AgentKitErrorLabels>;
45
+ }
46
+ export declare const DEFAULT_AGENT_EDITOR_LABELS: AgentEditorLabels;
47
+ /** What the form holds. */
48
+ export interface AgentDraft {
49
+ templateAgent: string | null;
50
+ name: string;
51
+ role: string;
52
+ instructions: string;
53
+ modelTier: ModelValue;
54
+ /** Tool id → on. Only the optional ones. */
55
+ tools: Record<string, boolean>;
56
+ }
57
+ /**
58
+ * An agent's own model choice: "auto" when it is on Auto (`overrides.model_tier` "auto", or `model.mode` "auto"; its
59
+ * `model.tier` is then Auto's fallback, not a choice), else its own preset, else "" (the template's).
60
+ */
61
+ export declare function agentModelValue(agent: Pick<KitAgent, "model" | "overrides">): ModelValue;
62
+ /** The draft an agent (or a new one from `ta`) starts from. */
63
+ export declare function draftOf(agent: KitAgent | null | undefined, ta: TemplateAgent | null | undefined): AgentDraft;
64
+ type Customizable = NonNullable<TemplateAgent["customizable"]>;
65
+ /** One choice in the model list: `""` follows the template's preset (`tier`, when it is known). */
66
+ export interface ModelChoice {
67
+ value: ModelValue;
68
+ tier: Tier | null;
69
+ }
70
+ /**
71
+ * The presets a person chooses between: the template's own (`template`, the empty value: following it is always
72
+ * admitted) and the ones `customizable.model_tier.allowed` lists, once each, in order of depth. The agent's own
73
+ * preset (`current`) stays a choice even when it is no longer allowed (a plan that changed), so the form never shows
74
+ * a preset it isn't on. Two or more: a choice. One: nothing to choose.
75
+ */
76
+ export declare function modelChoices(allowed: readonly string[], template: string | null, current: string | null, auto?: boolean): ModelChoice[];
77
+ /**
78
+ * The body a save sends: only fields `customizable` admits, and only what differs from the starting draft on
79
+ * an edit (so a save that changes nothing writes nothing). Empty text clears a change back to the template's.
80
+ */
81
+ export declare function bodyOf(d: AgentDraft, start: AgentDraft, c: Customizable | null, creating: boolean): AgentInput;
82
+ /** The form field a refused `param` names, if it is one of ours. */
83
+ export declare function fieldOfParam(param: string | null): keyof AgentDraft | null;
84
+ export interface AgentEditorViewProps {
85
+ /** Null: making a new one. */
86
+ agent: KitAgent | null;
87
+ /** The template agents a new one may be made from (creatable), or the edited agent's own. */
88
+ templateAgents: TemplateAgent[] | null;
89
+ /** Making one: how many are made and the most allowed. At the most, the sentence takes the form's place. */
90
+ agentLimit?: AgentLimit | null | undefined;
91
+ draft: AgentDraft;
92
+ onChange: (d: AgentDraft) => void;
93
+ onSubmit: () => void;
94
+ busy: boolean;
95
+ saved: boolean;
96
+ error: Error | null;
97
+ labels?: Partial<AgentEditorLabels> | undefined;
98
+ /** A tool's name for a person. Default: the API's label (`customizable.tools.labels`), else the id in words. */
99
+ toolLabel?: ((id: string) => string | null) | undefined;
100
+ /** The model each preset runs on (`useModelPresets`): its name beside each preset, and a line under the choice. Null: neither. */
101
+ modelPresets?: readonly KitModelPreset[] | null | undefined;
102
+ /**
103
+ * Whose model key the workspace runs on (`GET /model_presets`' `key`); read from `modelPresets` when absent. Auto is
104
+ * offered only on "openlegion": a workspace's own key makes no routing call, so the control never promises one.
105
+ * Unknown (null): not offered.
106
+ */
107
+ modelKey?: "openlegion" | "own" | null | undefined;
108
+ className?: string | undefined;
109
+ }
110
+ /** "web.search" → "Web search", "mcp_shop_create_item" → "Create item". */
111
+ export declare function humanizeTool(id: string): string;
112
+ /** The form's markup alone, from its state (what `<AgentEditor>` draws). */
113
+ export declare function AgentEditorView(p: AgentEditorViewProps): import("react").JSX.Element;
114
+ export interface AgentEditorProps extends UseAgentKitOptions {
115
+ /** The agent to change. Absent: make a new one from a template agent the template marks creatable. */
116
+ agent?: KitAgent | null;
117
+ /** The template agents (from `useTemplateAgents`), when the page already has them. Pass its `agentLimit` too. */
118
+ templateAgents?: TemplateAgent[] | null;
119
+ /** With `templateAgents`: `useTemplateAgents().agentLimit`. Otherwise the editor loads it itself. */
120
+ agentLimit?: AgentLimit | null;
121
+ labels?: Partial<AgentEditorLabels>;
122
+ toolLabel?: (id: string) => string | null;
123
+ /**
124
+ * Beside each preset in "Speed and depth", the model it runs on now ("Thorough · Claude Sonnet 5.5"), and one line
125
+ * under the choice on the preset chosen. Off by default. The names come from one read per page (GET
126
+ * {basePath}/model_presets); until it answers, or when it fails, the choice is as it is without this.
127
+ */
128
+ showModelNames?: boolean;
129
+ onSaved?: (agent: KitAgent) => void;
130
+ className?: string;
131
+ }
132
+ export declare function AgentEditor(props: AgentEditorProps): import("react").JSX.Element;
133
+ export {};
@@ -0,0 +1,222 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useId, useRef, useState } from "react";
4
+ import { agentKitErrorText, AgentKitError, DEFAULT_MODEL_TIER_LABELS, newIdempotencyKey, useAgents, useModelPresets, useTemplateAgents } from "./agent-kit.js";
5
+ import { useNoteInView } from "./reveal.js";
6
+ export const DEFAULT_AGENT_EDITOR_LABELS = {
7
+ kind: "Kind of assistant",
8
+ name: "Name",
9
+ role: "What it does",
10
+ instructions: "Your instructions",
11
+ instructionsHint: "Added after the assistant's own. Never put passwords or keys here.",
12
+ model: "Speed and depth",
13
+ modelDefault: "Default",
14
+ modelDefaultKnown: "Default ({tier})",
15
+ modelNamed: "{tier} · {model}",
16
+ modelHints: {
17
+ background: "For small, routine jobs.",
18
+ fast: "Quick answers for everyday work.",
19
+ reasoning: "Takes more care over harder work.",
20
+ frontier: "For the hardest work.",
21
+ },
22
+ modelOwnKey: "Runs on your own model key.",
23
+ modelAuto: "Auto",
24
+ modelAutoHint: "Chooses a model for each task from the ones available to you.",
25
+ tools: "What it may use",
26
+ toolHeld: "asks you first",
27
+ create: "Create assistant",
28
+ save: "Save changes",
29
+ saving: "Saving…",
30
+ saved: "Saved.",
31
+ counter: "{used} of {max}",
32
+ nothingToChange: "This assistant can't be changed here.",
33
+ nothingToCreate: "There's no assistant you can add here.",
34
+ limit: "You have {count} of {max} assistants. Remove one to add another.",
35
+ loading: "Loading…",
36
+ models: DEFAULT_MODEL_TIER_LABELS,
37
+ };
38
+ export function agentModelValue(agent) {
39
+ const own = agent.overrides.model_tier;
40
+ if (own === "auto" || agent.model.mode === "auto")
41
+ return "auto";
42
+ return (own ?? "");
43
+ }
44
+ export function draftOf(agent, ta) {
45
+ if (agent) {
46
+ return {
47
+ templateAgent: agent.template_agent,
48
+ name: agent.name,
49
+ role: agent.overrides.role ?? "",
50
+ instructions: agent.overrides.instructions ?? "",
51
+ modelTier: agentModelValue(agent),
52
+ tools: Object.fromEntries(agent.tools.filter((t) => t.optional).map((t) => [t.id, t.enabled])),
53
+ };
54
+ }
55
+ return {
56
+ templateAgent: ta?.key ?? null,
57
+ name: "",
58
+ role: "",
59
+ instructions: "",
60
+ modelTier: "",
61
+ tools: Object.fromEntries((ta?.tools ?? []).filter((t) => t.optional).map((t) => [t.id, true])),
62
+ };
63
+ }
64
+ const DEPTH = ["background", "fast", "reasoning", "frontier"];
65
+ const isTier = (t) => typeof t === "string" && DEPTH.includes(t);
66
+ export function modelChoices(allowed, template, current, auto = false) {
67
+ const named = new Set([...allowed, ...(current ? [current] : [])].filter(isTier));
68
+ if (auto || current === "auto")
69
+ return [{ value: "auto", tier: null }, ...modelChoices(allowed, template, current === "auto" ? null : current)];
70
+ if (isTier(template))
71
+ named.add(template);
72
+ const out = DEPTH.filter((t) => named.has(t)).map((t) => (t === template ? { value: "", tier: t } : { value: t, tier: t }));
73
+ return isTier(template) ? out : [{ value: "", tier: null }, ...out];
74
+ }
75
+ export function bodyOf(d, start, c, creating) {
76
+ const out = {};
77
+ const changed = (a, b) => creating || a !== b;
78
+ if (c?.name && changed(d.name, start.name) && (d.name.trim() !== "" || !creating))
79
+ out.name = d.name.trim() === "" ? null : d.name.trim();
80
+ if (c?.role && changed(d.role, start.role) && (d.role.trim() !== "" || !creating))
81
+ out.role = d.role.trim() === "" ? null : d.role.trim();
82
+ if (c?.instructions && changed(d.instructions, start.instructions) && (d.instructions.trim() !== "" || !creating))
83
+ out.instructions = d.instructions.trim() === "" ? null : d.instructions;
84
+ if (c?.model_tier && changed(d.modelTier, start.modelTier) && (d.modelTier !== "" || !creating))
85
+ out.model_tier = d.modelTier === "" ? null : d.modelTier;
86
+ if (c?.tools) {
87
+ const tools = {};
88
+ for (const id of c.tools.optional)
89
+ if (d.tools[id] !== undefined && (creating ? d.tools[id] === false : d.tools[id] !== start.tools[id]))
90
+ tools[id] = d.tools[id];
91
+ if (Object.keys(tools).length > 0)
92
+ out.tools = tools;
93
+ }
94
+ return out;
95
+ }
96
+ export function fieldOfParam(param) {
97
+ if (!param)
98
+ return null;
99
+ const head = param.split(/[.[]/)[0];
100
+ return head === "name" ? "name" : head === "role" ? "role" : head === "instructions" ? "instructions" : head === "model_tier" || head === "auto_presets" ? "modelTier" : head === "tools" ? "tools" : head === "template_agent" ? "templateAgent" : null;
101
+ }
102
+ export function humanizeTool(id) {
103
+ const bare = id.replace(/^mcp_[a-z0-9]+_/, "").replace(/^mcp\.[a-z0-9]+\./, "");
104
+ const words = bare.replace(/[._-]+/g, " ").trim();
105
+ return words ? words[0].toUpperCase() + words.slice(1) : id;
106
+ }
107
+ const col = { display: "flex", flexDirection: "column", gap: "var(--ol-gap, 0.75rem)" };
108
+ const field = { display: "flex", flexDirection: "column", gap: "var(--ol-field-gap, 0.25rem)" };
109
+ export function AgentEditorView(p) {
110
+ const l = { ...DEFAULT_AGENT_EDITOR_LABELS, ...p.labels, models: { ...DEFAULT_AGENT_EDITOR_LABELS.models, ...p.labels?.models }, modelHints: { ...DEFAULT_AGENT_EDITOR_LABELS.modelHints, ...p.labels?.modelHints } };
111
+ const id = useId();
112
+ const creating = p.agent === null;
113
+ const ta = (p.templateAgents ?? []).find((t) => t.key === p.draft.templateAgent) ?? null;
114
+ const c = (creating ? ta?.customizable : p.agent?.customizable) ?? null;
115
+ const bad = p.error instanceof AgentKitError ? fieldOfParam(p.error.param) : null;
116
+ const message = p.error ? agentKitErrorText(p.error, l.errors) : null;
117
+ const errorInView = useNoteInView(message);
118
+ const savedInView = useNoteInView();
119
+ const set = (patch) => p.onChange({ ...p.draft, ...patch });
120
+ const fieldProps = (name) => ({
121
+ id: `${id}-${name}`,
122
+ "aria-invalid": bad === name ? true : undefined,
123
+ "aria-describedby": bad === name ? `${id}-error` : undefined,
124
+ });
125
+ const counter = (used, max) => max ? (_jsx("span", { "data-ol-agent-counter": true, "aria-live": "polite", style: { fontSize: "var(--ol-small, 0.875em)", opacity: 0.7 }, children: l.counter.replace("{used}", String(used)).replace("{max}", String(max)) })) : null;
126
+ if (creating && p.templateAgents !== null && p.templateAgents.length === 0)
127
+ return _jsx("p", { "data-ol-agent-empty": true, children: l.nothingToCreate });
128
+ if (creating && p.templateAgents === null)
129
+ return _jsx("p", { "data-ol-agent-loading": true, role: "status", children: l.loading });
130
+ if (creating && p.agentLimit && p.agentLimit.count >= p.agentLimit.max) {
131
+ return (_jsx("p", { className: p.className, "data-ol-agent-limit": true, role: "status", children: l.limit.replace("{count}", String(p.agentLimit.count)).replace("{max}", String(p.agentLimit.max)) }));
132
+ }
133
+ if (!creating && !c)
134
+ return _jsx("p", { "data-ol-agent-empty": true, children: l.nothingToChange });
135
+ const submit = (e) => {
136
+ e.preventDefault();
137
+ if (!p.busy)
138
+ p.onSubmit();
139
+ };
140
+ const optional = c?.tools?.optional ?? [];
141
+ const toolsOf = creating ? (ta?.tools ?? []) : (p.agent?.tools ?? []);
142
+ const toolName = (t) => p.toolLabel?.(t) ?? c?.tools?.labels?.[t] ?? humanizeTool(t);
143
+ const defaultTier = ta?.model_tier ?? (p.agent && agentModelValue(p.agent) === "" ? p.agent.model.tier : null);
144
+ const modelKey = p.modelKey ?? p.modelPresets?.[0]?.key ?? null;
145
+ const autoOffered = c?.model_tier?.auto === true && modelKey === "openlegion";
146
+ const choices = c?.model_tier ? modelChoices(c.model_tier.allowed, defaultTier, creating || !p.agent ? null : agentModelValue(p.agent) || null, autoOffered) : [];
147
+ const presetOf = (t) => (t ? (p.modelPresets?.find((x) => x.id === t) ?? null) : null);
148
+ const tierName = (t) => {
149
+ const named = presetOf(t);
150
+ const word = l.models[t] ?? t;
151
+ return named ? l.modelNamed.replace("{tier}", () => word).replace("{model}", () => named.model.name) : word;
152
+ };
153
+ const choiceName = (m) => (m.value === "auto" ? l.modelAuto : m.value !== "" ? tierName(m.value) : m.tier ? l.modelDefaultKnown.replace("{tier}", () => tierName(m.tier)) : l.modelDefault);
154
+ const modelValue = p.draft.modelTier !== "" && p.draft.modelTier === defaultTier ? "" : p.draft.modelTier;
155
+ const chosenTier = choices.length === 1 ? choices[0].tier : modelValue === "" ? (choices.find((m) => m.value === "")?.tier ?? null) : modelValue === "auto" ? null : modelValue;
156
+ const chosen = presetOf(chosenTier);
157
+ const modelHint = modelValue === "auto" && choices.length > 1
158
+ ? modelKey === "own"
159
+ ? l.modelOwnKey
160
+ : l.modelAutoHint
161
+ : chosen
162
+ ? [l.modelHints[chosen.id], chosen.key === "own" ? l.modelOwnKey : ""].filter((t) => t).join(" ")
163
+ : "";
164
+ const hintLine = modelHint ? (_jsx("span", { id: `${id}-modelHint`, "data-ol-agent-model-hint": true, style: { fontSize: "var(--ol-small, 0.875em)", opacity: 0.7 }, children: modelHint })) : null;
165
+ let row = null;
166
+ if (creating && (p.templateAgents?.length ?? 0) > 1) {
167
+ row = (_jsxs("div", { style: field, "data-ol-agent-field": "kind", children: [_jsx("label", { htmlFor: `${id}-templateAgent`, children: l.kind }), _jsx("select", { ...fieldProps("templateAgent"), "data-ol-agent-kind": true, value: p.draft.templateAgent ?? "", onChange: (e) => p.onChange(draftOf(null, p.templateAgents?.find((t) => t.key === e.target.value))), children: p.templateAgents.map((t) => (_jsx("option", { value: t.key, children: t.name }, t.key))) })] }));
168
+ }
169
+ return (_jsxs("form", { className: p.className, "data-ol-agent-editor": true, onSubmit: submit, style: col, "aria-busy": p.busy, children: [row, c?.name ? (_jsxs("div", { style: field, "data-ol-agent-field": "name", children: [_jsx("label", { htmlFor: `${id}-name`, children: l.name }), _jsx("input", { ...fieldProps("name"), "data-ol-agent-name": true, value: p.draft.name, maxLength: c.name.max_length, placeholder: ta?.name, onChange: (e) => set({ name: e.target.value }) })] })) : null, c?.role ? (_jsxs("div", { style: field, "data-ol-agent-field": "role", children: [_jsx("label", { htmlFor: `${id}-role`, children: l.role }), _jsx("input", { ...fieldProps("role"), "data-ol-agent-role": true, value: p.draft.role, maxLength: c.role.max_length, placeholder: ta?.role, onChange: (e) => set({ role: e.target.value }) })] })) : null, c?.instructions ? (_jsxs("div", { style: field, "data-ol-agent-field": "instructions", children: [_jsx("label", { htmlFor: `${id}-instructions`, children: l.instructions }), _jsx("textarea", { ...fieldProps("instructions"), "data-ol-agent-instructions": true, rows: 5, value: p.draft.instructions, maxLength: c.instructions.max_length, "aria-describedby": [`${id}-hint`, bad === "instructions" ? `${id}-error` : null].filter(Boolean).join(" "), onChange: (e) => set({ instructions: e.target.value }), style: { font: "inherit", resize: "vertical" } }), _jsx("span", { id: `${id}-hint`, "data-ol-agent-hint": true, style: { fontSize: "var(--ol-small, 0.875em)", opacity: 0.7 }, children: l.instructionsHint }), counter(p.draft.instructions.length, c.instructions.max_length)] })) : null, choices.length >= 2 ? (_jsxs("div", { style: field, "data-ol-agent-field": "model", children: [_jsx("label", { htmlFor: `${id}-modelTier`, children: l.model }), _jsx("select", { ...fieldProps("modelTier"), ...(hintLine ? { "aria-describedby": [`${id}-modelHint`, bad === "modelTier" ? `${id}-error` : null].filter(Boolean).join(" ") } : {}), "data-ol-agent-model": true, value: modelValue, onChange: (e) => set({ modelTier: e.target.value }), children: choices.map((m) => (_jsx("option", { value: m.value, children: choiceName(m) }, m.value))) }), hintLine] })) : choices.length === 1 ? (_jsxs("div", { style: field, "data-ol-agent-model-fixed": true, "data-ol-agent-field": "model", children: [_jsx("span", { "data-ol-agent-field-name": true, children: l.model }), _jsx("span", { children: choices[0].tier ? tierName(choices[0].tier) : l.modelDefault }), hintLine] })) : null, optional.length > 0 ? (_jsxs("fieldset", { "data-ol-agent-tools": true, "data-ol-agent-field": "tools", "aria-invalid": bad === "tools" ? true : undefined, style: { border: "none", margin: 0, padding: 0, ...col }, children: [_jsx("legend", { children: l.tools }), toolsOf
170
+ .filter((t) => optional.includes(t.id))
171
+ .map((t) => (_jsxs("label", { "data-ol-agent-tool": true, style: { display: "flex", gap: "0.5rem", alignItems: "center" }, children: [_jsx("input", { type: "checkbox", checked: p.draft.tools[t.id] !== false, onChange: (e) => set({ tools: { ...p.draft.tools, [t.id]: e.target.checked } }) }), _jsx("span", { children: toolName(t.id) }), t.held ? _jsxs("span", { "data-ol-agent-tool-held": true, style: { opacity: 0.7 }, children: ["(", l.toolHeld, ")"] }) : null] }, t.id)))] })) : null, message ? (_jsx("p", { id: `${id}-error`, role: "alert", "data-ol-agent-error": true, style: { margin: 0, color: "var(--ol-danger, inherit)" }, ref: errorInView, children: message })) : p.saved ? (_jsx("p", { role: "status", "data-ol-agent-saved": true, style: { margin: 0 }, ref: savedInView, children: l.saved })) : null, _jsx("div", { "data-ol-agent-actions": true, children: _jsx("button", { type: "submit", "data-ol-agent-submit": true, disabled: p.busy, children: p.busy ? l.saving : creating ? l.create : l.save }) })] }));
172
+ }
173
+ export function AgentEditor(props) {
174
+ const creating = !props.agent;
175
+ const loaded = useTemplateAgents({ ...props, enabled: props.templateAgents === undefined });
176
+ const all = props.templateAgents !== undefined ? props.templateAgents : loaded.templateAgents;
177
+ const agentLimit = props.agentLimit !== undefined ? props.agentLimit : loaded.agentLimit;
178
+ const creatable = creating ? (all ? all.filter((t) => t.creatable) : null) : null;
179
+ const { create, update } = useAgents(props);
180
+ const autoPossible = creating ? (creatable ?? []).some((t) => t.customizable?.model_tier?.auto === true) : props.agent?.customizable?.model_tier?.auto === true || (props.agent ? agentModelValue(props.agent) === "auto" : false);
181
+ const modelPresets = useModelPresets({ api: props.api, fetch: props.fetch, onError: props.onError, enabled: props.showModelNames === true || autoPossible });
182
+ const [start, setStart] = useState(() => draftOf(props.agent, creatable?.[0]));
183
+ const [draft, setDraft] = useState(start);
184
+ const [busy, setBusy] = useState(false);
185
+ const [saved, setSaved] = useState(false);
186
+ const [error, setError] = useState(null);
187
+ const key = useRef(newIdempotencyKey());
188
+ if (creating && draft.templateAgent === null && creatable && creatable.length > 0) {
189
+ const d = draftOf(null, creatable[0]);
190
+ setStart(d);
191
+ setDraft(d);
192
+ }
193
+ const onSubmit = () => {
194
+ const ta = creatable?.find((t) => t.key === draft.templateAgent) ?? null;
195
+ const c = (creating ? ta?.customizable : props.agent?.customizable) ?? null;
196
+ const body = bodyOf(draft, start, c, creating);
197
+ setBusy(true);
198
+ setSaved(false);
199
+ setError(null);
200
+ const done = creating ? create({ ...body, template_agent: draft.templateAgent ?? "" }, key.current) : update(props.agent.id, body, key.current);
201
+ done
202
+ .then((a) => {
203
+ key.current = newIdempotencyKey();
204
+ const next = draftOf(a, null);
205
+ setStart(next);
206
+ setDraft(creating ? draftOf(null, ta) : next);
207
+ setSaved(true);
208
+ props.onSaved?.(a);
209
+ })
210
+ .catch((err) => {
211
+ setError(err);
212
+ props.onError?.(err);
213
+ if (creating && err instanceof AgentKitError && err.code === "agent_limit_reached")
214
+ void loaded.refresh();
215
+ })
216
+ .finally(() => setBusy(false));
217
+ };
218
+ return (_jsx(AgentEditorView, { agent: props.agent ?? null, templateAgents: creating ? creatable : all, agentLimit: creating ? agentLimit : null, draft: draft, onChange: (d) => {
219
+ setDraft(d);
220
+ setSaved(false);
221
+ }, onSubmit: onSubmit, busy: busy, saved: saved, error: error, labels: props.labels, toolLabel: props.toolLabel, modelPresets: props.showModelNames === true ? modelPresets : null, modelKey: modelPresets?.[0]?.key ?? null, className: props.className }));
222
+ }
@@ -0,0 +1,160 @@
1
+ import type { Agent, ModelPreset, Schedule, TemplateAgent } from "../generated/api.ts";
2
+ /**
3
+ * An agent as the kit's route answers it: without your `metadata` and `external_ref`, and with its `role`, the one
4
+ * line on what it is for (the person's own when they changed it, else its template agent's, else null).
5
+ */
6
+ export type KitAgent = Omit<Agent, "metadata" | "external_ref"> & {
7
+ role: string | null;
8
+ };
9
+ /** A schedule as the kit's route answers it: without your `metadata`. */
10
+ export type KitSchedule = Omit<Schedule, "metadata">;
11
+ export type { TemplateAgent };
12
+ export declare const DEFAULT_KIT_BASE = "/api/openlegion";
13
+ /** A refused or failed call to the kit's routes. `message` is for your logs; a page shows `agentKitErrorText`. */
14
+ export declare class AgentKitError extends Error {
15
+ name: string;
16
+ readonly code: string;
17
+ readonly status: number | null;
18
+ readonly type: string | null;
19
+ /** The field the API named (`param`), so an editor can point at it. */
20
+ readonly param: string | null;
21
+ /** Only a refusal's message: the one server text written for an end user. */
22
+ readonly displayMessage: string | null;
23
+ constructor(code: string, message: string, o?: {
24
+ status?: number | null;
25
+ type?: string | null;
26
+ param?: string | null;
27
+ displayMessage?: string | null;
28
+ cause?: unknown;
29
+ });
30
+ }
31
+ export interface KitRequestOptions {
32
+ /** The kit's base path. Default "/api/openlegion". */
33
+ api?: string | undefined;
34
+ fetch?: typeof fetch | undefined;
35
+ }
36
+ /** One call to a kit route: its JSON, or an `AgentKitError`. */
37
+ export declare function kitRequest<T>(path: string, init?: {
38
+ method?: string;
39
+ body?: unknown;
40
+ idempotencyKey?: string;
41
+ } & KitRequestOptions): Promise<T>;
42
+ /**
43
+ * A model preset's name for a person, in the kit's words: they say the trade (speed against depth) without making
44
+ * the usual one sound lesser. `background` is OpenLegion's own; a template rarely allows it. Yours replace them.
45
+ */
46
+ export declare const DEFAULT_MODEL_TIER_LABELS: Record<"background" | "fast" | "reasoning" | "frontier", string>;
47
+ /** Every string the agent and schedule editors show for a failure. Pass any of them, in your product's voice. */
48
+ export interface AgentKitErrorLabels {
49
+ /** A member asked for a change only an owner or admin may make. */
50
+ notAllowed: string;
51
+ /** A schedule that runs more often than the assistant may run in a day. */
52
+ tooOften: string;
53
+ /** A value the API refused as invalid (the field is marked too). */
54
+ invalid: string;
55
+ /** A model preset the person may not choose: your `allowModelTier` said no, or the template no longer allows it. */
56
+ modelNotAllowed: string;
57
+ notFound: string;
58
+ signIn: string;
59
+ rateLimited: string;
60
+ unavailable: string;
61
+ generic: string;
62
+ /** Your words for a refusal, by its code, in place of OpenLegion's end-user sentence. */
63
+ refusals?: Partial<Record<string, string>>;
64
+ }
65
+ export declare const DEFAULT_KIT_ERROR_LABELS: AgentKitErrorLabels;
66
+ /** The words for a failure: a refusal's own sentence, or ours. Never `err.message`. */
67
+ export declare function agentKitErrorText(err: unknown, labels?: Partial<AgentKitErrorLabels>): string;
68
+ /** A fresh Idempotency-Key: one per submission, so a double submit is one write. */
69
+ export declare function newIdempotencyKey(): string;
70
+ export interface UseAgentKitOptions extends KitRequestOptions {
71
+ /** Every failure, in full, for YOUR logs. A page shows only `agentKitErrorText`. */
72
+ onError?: ((err: Error) => void) | undefined;
73
+ }
74
+ /** The workspace's assistants made from its template agents, and the most the template allows (GET /template_agents). */
75
+ export interface AgentLimit {
76
+ count: number;
77
+ max: number;
78
+ }
79
+ type KitTopic = "agents" | "connections";
80
+ /** What a write said beyond its topic: the assistant it removed (`agents`), so a list drops it at once. */
81
+ export interface KitNews {
82
+ removed: string | null;
83
+ }
84
+ export declare function announce(topic: KitTopic, from: string, news?: {
85
+ removed?: string;
86
+ }): void;
87
+ /** Hear every other hook's write on `topic` (never `self`'s own); answers the function that stops hearing. */
88
+ export declare function hearKit(topic: KitTopic, self: string, heard: (news: KitNews) => unknown): () => void;
89
+ export declare function useHears(topic: KitTopic, self: string, refresh: (news: KitNews) => unknown): void;
90
+ /** The template agents of the workspace's version: what may be changed on each, and which may be made more of. */
91
+ export declare function useTemplateAgents(opts?: UseAgentKitOptions & {
92
+ enabled?: boolean;
93
+ }): {
94
+ templateAgents: TemplateAgent[] | null;
95
+ /** How many assistants are made and the most allowed; null when none may be made. POST still decides. */
96
+ agentLimit: AgentLimit | null;
97
+ error: Error | null;
98
+ loading: boolean;
99
+ refresh: () => Promise<void>;
100
+ };
101
+ /**
102
+ * One model preset and the model a run on it uses now, on this workspace, as the kit's route answers it (GET
103
+ * /model_presets). The mapping is OpenLegion's and changes: read it, never write a name down. `key` "own": the
104
+ * workspace's assistants run on a person's own model key, so the model is that vendor's for the preset.
105
+ */
106
+ export type KitModelPreset = Pick<ModelPreset, "object" | "id" | "model" | "key">;
107
+ /**
108
+ * The workspace's model presets, read once per page however many editors ask (a failed read is not asked again:
109
+ * each keeps the control it had). In the browser only: the hooks call it from an effect, never on your server.
110
+ */
111
+ export declare function readModelPresets(opts?: KitRequestOptions): Promise<KitModelPreset[]>;
112
+ /**
113
+ * The model each preset runs on, for "Speed and depth" (`<AgentEditor showModelNames>`): null while off, while the
114
+ * one read is on its way, and when it failed (the control is then as it is without names).
115
+ */
116
+ export declare function useModelPresets(opts?: UseAgentKitOptions & {
117
+ enabled?: boolean;
118
+ }): KitModelPreset[] | null;
119
+ export type AgentInput = {
120
+ name?: string | null;
121
+ role?: string | null;
122
+ instructions?: string | null;
123
+ model_tier?: "background" | "fast" | "reasoning" | "frontier" | "auto" | null;
124
+ tools?: Record<string, boolean>;
125
+ };
126
+ /** The workspace's agents (the first 100), with create, update, pause, resume and remove. Each write answers the agent and updates the list. */
127
+ export declare function useAgents(opts?: UseAgentKitOptions): {
128
+ agents: KitAgent[] | null;
129
+ error: Error | null;
130
+ loading: boolean;
131
+ refresh: () => Promise<void>;
132
+ create: (body: AgentInput & {
133
+ template_agent: string;
134
+ }, idempotencyKey?: string) => Promise<KitAgent>;
135
+ update: (id: string, body: AgentInput, idempotencyKey?: string) => Promise<KitAgent>;
136
+ /** Pause an agent: it starts nothing (a message to it is refused, a schedule waits) until `resume`. */
137
+ pause: (id: string) => Promise<KitAgent>;
138
+ resume: (id: string) => Promise<KitAgent>;
139
+ remove: (id: string) => Promise<void>;
140
+ };
141
+ export type ScheduleInput = {
142
+ cron?: string;
143
+ run_at?: string;
144
+ instructions?: string;
145
+ description?: string | null;
146
+ };
147
+ /** One agent's schedules that haven't been cancelled, with create, update, pause, resume and cancel. */
148
+ export declare function useSchedules(agentId: string | null, opts?: UseAgentKitOptions): {
149
+ schedules: KitSchedule[] | null;
150
+ error: Error | null;
151
+ loading: boolean;
152
+ refresh: () => Promise<void>;
153
+ create: (body: ScheduleInput & {
154
+ instructions: string;
155
+ }, idempotencyKey?: string) => Promise<KitSchedule>;
156
+ update: (id: string, body: ScheduleInput, idempotencyKey?: string) => Promise<KitSchedule>;
157
+ pause: (id: string) => Promise<KitSchedule>;
158
+ resume: (id: string) => Promise<KitSchedule>;
159
+ cancel: (id: string) => Promise<KitSchedule>;
160
+ };