@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.
- package/LICENSE +202 -0
- package/README.md +1189 -2
- package/dist/body.d.ts +9 -0
- package/dist/body.js +28 -0
- package/dist/chat-history.d.ts +53 -0
- package/dist/chat-history.js +11 -0
- package/dist/client.d.ts +217 -0
- package/dist/client.js +367 -0
- package/dist/core.d.ts +47 -0
- package/dist/core.js +1 -0
- package/dist/crypto.d.ts +25 -0
- package/dist/crypto.js +81 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +134 -0
- package/dist/events.d.ts +131 -0
- package/dist/events.js +5 -0
- package/dist/generated/api.d.ts +5505 -0
- package/dist/generated/api.js +446 -0
- package/dist/handler.d.ts +201 -0
- package/dist/handler.js +682 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +12 -0
- package/dist/kit-agents.d.ts +95 -0
- package/dist/kit-agents.js +280 -0
- package/dist/kit-codes.d.ts +279 -0
- package/dist/kit-codes.js +44 -0
- package/dist/kit-connections.d.ts +21 -0
- package/dist/kit-connections.js +189 -0
- package/dist/kit-files.d.ts +8 -0
- package/dist/kit-files.js +117 -0
- package/dist/kit-model-keys.d.ts +8 -0
- package/dist/kit-model-keys.js +86 -0
- package/dist/kit-threads.d.ts +3 -0
- package/dist/kit-threads.js +92 -0
- package/dist/kit-tools.d.ts +21 -0
- package/dist/kit-tools.js +47 -0
- package/dist/kit.d.ts +130 -0
- package/dist/kit.js +155 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.js +166 -0
- package/dist/next.d.ts +24 -0
- package/dist/next.js +6 -0
- package/dist/ol-signed.d.ts +82 -0
- package/dist/ol-signed.js +115 -0
- package/dist/react/add-server.d.ts +90 -0
- package/dist/react/add-server.js +183 -0
- package/dist/react/agent-chat.d.ts +925 -0
- package/dist/react/agent-chat.js +1917 -0
- package/dist/react/agent-editor.d.ts +133 -0
- package/dist/react/agent-editor.js +222 -0
- package/dist/react/agent-kit.d.ts +160 -0
- package/dist/react/agent-kit.js +279 -0
- package/dist/react/agent-list.d.ts +247 -0
- package/dist/react/agent-list.js +248 -0
- package/dist/react/approval-card.d.ts +272 -0
- package/dist/react/approval-card.js +372 -0
- package/dist/react/artifact-card.d.ts +38 -0
- package/dist/react/artifact-card.js +39 -0
- package/dist/react/avatar.d.ts +21 -0
- package/dist/react/avatar.js +33 -0
- package/dist/react/card-ledger.d.ts +30 -0
- package/dist/react/card-ledger.js +49 -0
- package/dist/react/chat-timing.d.ts +121 -0
- package/dist/react/chat-timing.js +229 -0
- package/dist/react/computer-view.d.ts +309 -0
- package/dist/react/computer-view.js +543 -0
- package/dist/react/computer-viewer.d.ts +175 -0
- package/dist/react/computer-viewer.js +555 -0
- package/dist/react/computer-wire.d.ts +178 -0
- package/dist/react/computer-wire.js +244 -0
- package/dist/react/connect-button.d.ts +145 -0
- package/dist/react/connect-button.js +212 -0
- package/dist/react/connection-request.d.ts +16 -0
- package/dist/react/connection-request.js +18 -0
- package/dist/react/connections-list.d.ts +90 -0
- package/dist/react/connections-list.js +76 -0
- package/dist/react/errors.d.ts +250 -0
- package/dist/react/errors.js +146 -0
- package/dist/react/files.d.ts +41 -0
- package/dist/react/files.js +78 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.js +26 -0
- package/dist/react/limit-words.d.ts +80 -0
- package/dist/react/limit-words.js +53 -0
- package/dist/react/markdown.d.ts +42 -0
- package/dist/react/markdown.js +56 -0
- package/dist/react/md-parse.d.ts +106 -0
- package/dist/react/md-parse.js +638 -0
- package/dist/react/model-keys-card.d.ts +94 -0
- package/dist/react/model-keys-card.js +123 -0
- package/dist/react/reload.d.ts +211 -0
- package/dist/react/reload.js +339 -0
- package/dist/react/reveal.d.ts +17 -0
- package/dist/react/reveal.js +22 -0
- package/dist/react/schedule-editor.d.ts +107 -0
- package/dist/react/schedule-editor.js +172 -0
- package/dist/react/schedule-time.d.ts +66 -0
- package/dist/react/schedule-time.js +90 -0
- package/dist/react/styles.css +1274 -0
- package/dist/react/transport.d.ts +49 -0
- package/dist/react/transport.js +380 -0
- package/dist/react/use-connections.d.ts +155 -0
- package/dist/react/use-connections.js +231 -0
- package/dist/react/use-model-keys.d.ts +47 -0
- package/dist/react/use-model-keys.js +57 -0
- package/dist/react/use-threads.d.ts +63 -0
- package/dist/react/use-threads.js +73 -0
- package/dist/react/vault-form.d.ts +92 -0
- package/dist/react/vault-form.js +151 -0
- package/dist/schema.d.ts +47 -0
- package/dist/schema.js +16 -0
- package/dist/secrets.d.ts +15 -0
- package/dist/secrets.js +18 -0
- package/dist/stream.d.ts +129 -0
- package/dist/stream.js +192 -0
- package/dist/testing.d.ts +33 -0
- package/dist/testing.js +40 -0
- package/dist/webhooks.d.ts +31 -0
- package/dist/webhooks.js +72 -0
- package/package.json +85 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
3
|
+
import { AgentKitError, announce, DEFAULT_KIT_BASE, kitRequest, useHears } from "./agent-kit.js";
|
|
4
|
+
export function isSetupRefusal(err) {
|
|
5
|
+
if (!(err instanceof AgentKitError))
|
|
6
|
+
return false;
|
|
7
|
+
if (err.status === 403)
|
|
8
|
+
return err.code === "cross_origin";
|
|
9
|
+
if (err.status !== 400)
|
|
10
|
+
return false;
|
|
11
|
+
return err.code !== "invalid_server" && !(err.param ?? "").startsWith("server.");
|
|
12
|
+
}
|
|
13
|
+
export function setupRefusalMessage(err, pageOrigin) {
|
|
14
|
+
const head = `OpenLegion Connect: starting a connect session was refused (${err.status ?? "?"} ${err.code}${err.param ? `, on ${err.param}` : ""}), and no retry will fix it. The person was told it isn't set up yet. The API said: ${err.message}`;
|
|
15
|
+
let fix;
|
|
16
|
+
if (err.param === "return_url") {
|
|
17
|
+
const origin = /origin (\S+) is not/.exec(err.message)?.[1] ?? pageOrigin ?? "this page's origin";
|
|
18
|
+
fix = `Fix: add ${origin} to your organization's return origins, from your server with your secret key: POST /connect_settings {"return_origins": [..., "${origin}"]}. (createHandler's connectReturnUrl, when you set it, must be on one of them too.)`;
|
|
19
|
+
}
|
|
20
|
+
else if (err.code === "app_not_enabled") {
|
|
21
|
+
fix = "Fix: list the app in the apps of the workspace's template version, and apply that version to the workspace.";
|
|
22
|
+
}
|
|
23
|
+
else if (err.code === "app_needs_oauth_app") {
|
|
24
|
+
fix = "Fix: register an OAuth app for it, or don't offer it.";
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
fix = "Fix: check what your page passes to <ConnectButton> or <AddServerForm>, and createHandler's connect options.";
|
|
28
|
+
}
|
|
29
|
+
return `${head}\n${fix}`;
|
|
30
|
+
}
|
|
31
|
+
export function reportSetupRefusal(err) {
|
|
32
|
+
if (!isSetupRefusal(err))
|
|
33
|
+
return false;
|
|
34
|
+
console.error(setupRefusalMessage(err, typeof window !== "undefined" ? window.location.origin : null));
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
export function connectChannelName(api = DEFAULT_KIT_BASE) {
|
|
38
|
+
return `openlegion-connect:${api.replace(/\/+$/, "")}`;
|
|
39
|
+
}
|
|
40
|
+
export function watchConnectSession(id, opts) {
|
|
41
|
+
let stopped = false;
|
|
42
|
+
let inFlight = false;
|
|
43
|
+
const channel = opts.channel !== undefined ? opts.channel : typeof BroadcastChannel === "function" ? new BroadcastChannel(connectChannelName(opts.api)) : null;
|
|
44
|
+
const focus = opts.focus !== undefined ? opts.focus : typeof window !== "undefined" ? window : null;
|
|
45
|
+
const check = async () => {
|
|
46
|
+
if (stopped || inFlight)
|
|
47
|
+
return;
|
|
48
|
+
inFlight = true;
|
|
49
|
+
try {
|
|
50
|
+
const s = await kitRequest(`/connect_sessions/${encodeURIComponent(id)}`, opts);
|
|
51
|
+
if (stopped)
|
|
52
|
+
return;
|
|
53
|
+
opts.onChange(s);
|
|
54
|
+
if (s.status !== "open")
|
|
55
|
+
stop();
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
opts.onError?.(err);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
inFlight = false;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const onMessage = (e) => {
|
|
65
|
+
const d = e.data;
|
|
66
|
+
if (d && d.connect_session === id)
|
|
67
|
+
void check();
|
|
68
|
+
};
|
|
69
|
+
const onFocus = () => void check();
|
|
70
|
+
const timer = setInterval(() => void check(), opts.intervalMs ?? 3000);
|
|
71
|
+
channel?.addEventListener("message", onMessage);
|
|
72
|
+
focus?.addEventListener("focus", onFocus);
|
|
73
|
+
if (opts.immediate)
|
|
74
|
+
void check();
|
|
75
|
+
function stop() {
|
|
76
|
+
if (stopped)
|
|
77
|
+
return;
|
|
78
|
+
stopped = true;
|
|
79
|
+
clearInterval(timer);
|
|
80
|
+
channel?.close();
|
|
81
|
+
focus?.removeEventListener("focus", onFocus);
|
|
82
|
+
}
|
|
83
|
+
return stop;
|
|
84
|
+
}
|
|
85
|
+
export function useConnections(opts = {}) {
|
|
86
|
+
const [connections, setConnections] = useState(null);
|
|
87
|
+
const [session, setSession] = useState(null);
|
|
88
|
+
const [error, setError] = useState(null);
|
|
89
|
+
const [loading, setLoading] = useState(opts.enabled !== false);
|
|
90
|
+
const optsRef = useRef(opts);
|
|
91
|
+
optsRef.current = opts;
|
|
92
|
+
const stopRef = useRef(null);
|
|
93
|
+
const key = opts.enabled === false ? null : `${opts.api ?? DEFAULT_KIT_BASE}|${opts.app ?? ""}`;
|
|
94
|
+
const self = useId();
|
|
95
|
+
const refresh = useCallback(async () => {
|
|
96
|
+
const o = optsRef.current;
|
|
97
|
+
hasRead.current = true;
|
|
98
|
+
setLoading(true);
|
|
99
|
+
try {
|
|
100
|
+
const page = await kitRequest(`/connections${o.app ? `?app=${encodeURIComponent(o.app)}` : ""}`, o);
|
|
101
|
+
setConnections(page.data);
|
|
102
|
+
setError(null);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
setError(err);
|
|
106
|
+
o.onError?.(err);
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
setLoading(false);
|
|
110
|
+
}
|
|
111
|
+
}, []);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (key === null)
|
|
114
|
+
return;
|
|
115
|
+
void refresh();
|
|
116
|
+
}, [key, refresh]);
|
|
117
|
+
const gone = useRef(false);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
gone.current = false;
|
|
120
|
+
return () => {
|
|
121
|
+
gone.current = true;
|
|
122
|
+
stopRef.current?.();
|
|
123
|
+
};
|
|
124
|
+
}, []);
|
|
125
|
+
const hasRead = useRef(false);
|
|
126
|
+
const heard = useCallback(() => {
|
|
127
|
+
setSession((s) => (s && s.status !== "open" ? null : s));
|
|
128
|
+
if (optsRef.current.enabled !== false || hasRead.current)
|
|
129
|
+
void refresh();
|
|
130
|
+
}, [refresh]);
|
|
131
|
+
useHears("connections", self, heard);
|
|
132
|
+
const follow = useCallback((s) => {
|
|
133
|
+
if (gone.current)
|
|
134
|
+
return;
|
|
135
|
+
stopRef.current?.();
|
|
136
|
+
setSession(s.app === "" ? null : s);
|
|
137
|
+
if (s.status !== "open")
|
|
138
|
+
return;
|
|
139
|
+
const o = optsRef.current;
|
|
140
|
+
const stop = watchConnectSession(s.id, {
|
|
141
|
+
api: o.api,
|
|
142
|
+
fetch: o.fetch,
|
|
143
|
+
...(o.onError ? { onError: o.onError } : {}),
|
|
144
|
+
immediate: s.app === "",
|
|
145
|
+
onChange: (next) => {
|
|
146
|
+
setSession(next);
|
|
147
|
+
if (next.status !== "completed")
|
|
148
|
+
return;
|
|
149
|
+
void refresh();
|
|
150
|
+
announce("connections", self);
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
stopRef.current = stop;
|
|
154
|
+
}, [refresh, self]);
|
|
155
|
+
return {
|
|
156
|
+
connections,
|
|
157
|
+
session,
|
|
158
|
+
error,
|
|
159
|
+
loading,
|
|
160
|
+
refresh,
|
|
161
|
+
connect: async (app, o = {}) => {
|
|
162
|
+
const body = { app, ...(o.agent ? { agent: o.agent } : {}), ...(o.returnPath ? { return_path: o.returnPath } : {}) };
|
|
163
|
+
const s = await kitRequest("/connect_sessions", { ...optsRef.current, method: "POST", body });
|
|
164
|
+
follow(s);
|
|
165
|
+
if (s.status === "completed") {
|
|
166
|
+
void refresh();
|
|
167
|
+
announce("connections", self);
|
|
168
|
+
}
|
|
169
|
+
return s;
|
|
170
|
+
},
|
|
171
|
+
connectServer: async (server, o = {}) => {
|
|
172
|
+
const body = { server: { url: server.url, ...(server.name ? { name: server.name } : {}) }, ...(o.agent ? { agent: o.agent } : {}) };
|
|
173
|
+
const s = await kitRequest("/connect_sessions", { ...optsRef.current, method: "POST", body });
|
|
174
|
+
follow(s);
|
|
175
|
+
return s;
|
|
176
|
+
},
|
|
177
|
+
resume: (id) => follow({ object: "connect_session", id, app: "", status: "open", failure_code: null, connection: null, expires_at: "" }),
|
|
178
|
+
disconnect: async (id) => {
|
|
179
|
+
await kitRequest(`/connections/${encodeURIComponent(id)}`, { ...optsRef.current, method: "DELETE" });
|
|
180
|
+
setConnections((list) => (list ? list.filter((c) => c.id !== id) : list));
|
|
181
|
+
announce("connections", self);
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
export function filterCatalog(apps, search) {
|
|
186
|
+
const q = (search ?? "").trim().toLowerCase();
|
|
187
|
+
return q === "" ? [...apps] : apps.filter((a) => a.name.toLowerCase().includes(q) || a.description.toLowerCase().includes(q));
|
|
188
|
+
}
|
|
189
|
+
export function useCatalog(opts = {}) {
|
|
190
|
+
const [apps, setApps] = useState(null);
|
|
191
|
+
const [customServers, setCustomServers] = useState(undefined);
|
|
192
|
+
const self = useId();
|
|
193
|
+
const [error, setError] = useState(null);
|
|
194
|
+
const [loading, setLoading] = useState(opts.enabled !== false);
|
|
195
|
+
const optsRef = useRef(opts);
|
|
196
|
+
optsRef.current = opts;
|
|
197
|
+
const key = `${opts.api ?? DEFAULT_KIT_BASE}|${opts.category ?? ""}`;
|
|
198
|
+
const refresh = useCallback(async () => {
|
|
199
|
+
const o = optsRef.current;
|
|
200
|
+
setLoading(true);
|
|
201
|
+
try {
|
|
202
|
+
const page = await kitRequest(`/catalog${o.category ? `?category=${encodeURIComponent(o.category)}` : ""}`, o);
|
|
203
|
+
setApps(page.data);
|
|
204
|
+
setCustomServers(page.custom_servers ?? null);
|
|
205
|
+
setError(null);
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
setError(err);
|
|
209
|
+
o.onError?.(err);
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
setLoading(false);
|
|
213
|
+
}
|
|
214
|
+
}, []);
|
|
215
|
+
const on = opts.enabled !== false;
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
if (on)
|
|
218
|
+
void refresh();
|
|
219
|
+
}, [key, refresh, on]);
|
|
220
|
+
useHears("connections", self, () => {
|
|
221
|
+
if (optsRef.current.enabled !== false)
|
|
222
|
+
void refresh();
|
|
223
|
+
});
|
|
224
|
+
return {
|
|
225
|
+
apps: apps === null ? null : filterCatalog(apps, opts.search),
|
|
226
|
+
customServers,
|
|
227
|
+
error,
|
|
228
|
+
loading,
|
|
229
|
+
refresh,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type UseAgentKitOptions } from "./agent-kit.ts";
|
|
2
|
+
export type ModelKeyVendor = "anthropic" | "openai";
|
|
3
|
+
export declare const MODEL_KEY_VENDORS: readonly ModelKeyVendor[];
|
|
4
|
+
/** A saved key as the kit's route answers it: never the key, only its last four characters. */
|
|
5
|
+
export interface KitModelKey {
|
|
6
|
+
object: "model_key";
|
|
7
|
+
vendor: ModelKeyVendor;
|
|
8
|
+
/** e.g. "…x9Qa". */
|
|
9
|
+
tail: string | null;
|
|
10
|
+
/** When the vendor last accepted it. */
|
|
11
|
+
checked_at: string | null;
|
|
12
|
+
/** The workspace's assistants run on it now. */
|
|
13
|
+
in_use: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** What `GET {basePath}/model_keys` answers. */
|
|
16
|
+
export interface KitModelKeys {
|
|
17
|
+
object: "model_keys";
|
|
18
|
+
/** Whether this workspace may use a person's own key (`model_keys_allowed`, which your server sets). */
|
|
19
|
+
allowed: boolean;
|
|
20
|
+
/** At most one. */
|
|
21
|
+
keys: KitModelKey[];
|
|
22
|
+
}
|
|
23
|
+
/** What `save` answers: the key saved, and the other vendor's it replaced, if any. */
|
|
24
|
+
export type KitSavedModelKey = KitModelKey & {
|
|
25
|
+
replaced: {
|
|
26
|
+
vendor: ModelKeyVendor;
|
|
27
|
+
tail: string | null;
|
|
28
|
+
} | null;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The workspace's model key: `allowed`, the `key` saved (or null), `save(vendor, key)` and `remove(vendor)`.
|
|
32
|
+
* `save` asks the vendor first, so it takes a few seconds; `saving` is true meanwhile. A save or remove that fails
|
|
33
|
+
* throws an `AgentKitError` (its `code` says why: `model_key_malformed`, `model_key_invalid`, `model_key_unchecked`, …) and leaves the
|
|
34
|
+
* state as it was.
|
|
35
|
+
*/
|
|
36
|
+
export declare function useModelKeys(opts?: UseAgentKitOptions & {
|
|
37
|
+
enabled?: boolean;
|
|
38
|
+
}): {
|
|
39
|
+
allowed: boolean | null;
|
|
40
|
+
key: KitModelKey | null;
|
|
41
|
+
error: Error | null;
|
|
42
|
+
loading: boolean;
|
|
43
|
+
saving: boolean;
|
|
44
|
+
refresh: () => Promise<void>;
|
|
45
|
+
save: (vendor: ModelKeyVendor, apiKey: string) => Promise<KitSavedModelKey>;
|
|
46
|
+
remove: (vendor: ModelKeyVendor) => Promise<void>;
|
|
47
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { DEFAULT_KIT_BASE, kitRequest } from "./agent-kit.js";
|
|
4
|
+
export const MODEL_KEY_VENDORS = ["anthropic", "openai"];
|
|
5
|
+
export function useModelKeys(opts = {}) {
|
|
6
|
+
const [state, setState] = useState(null);
|
|
7
|
+
const [error, setError] = useState(null);
|
|
8
|
+
const [loading, setLoading] = useState(opts.enabled !== false);
|
|
9
|
+
const [saving, setSaving] = useState(false);
|
|
10
|
+
const optsRef = useRef(opts);
|
|
11
|
+
optsRef.current = opts;
|
|
12
|
+
const key = opts.enabled === false ? null : (opts.api ?? DEFAULT_KIT_BASE);
|
|
13
|
+
const refresh = useCallback(async () => {
|
|
14
|
+
const o = optsRef.current;
|
|
15
|
+
setLoading(true);
|
|
16
|
+
try {
|
|
17
|
+
setState(await kitRequest("/model_keys", o));
|
|
18
|
+
setError(null);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
setError(err);
|
|
22
|
+
o.onError?.(err);
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
setLoading(false);
|
|
26
|
+
}
|
|
27
|
+
}, []);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (key === null)
|
|
30
|
+
return;
|
|
31
|
+
void refresh();
|
|
32
|
+
}, [key, refresh]);
|
|
33
|
+
return {
|
|
34
|
+
allowed: state?.allowed ?? null,
|
|
35
|
+
key: state?.keys[0] ?? null,
|
|
36
|
+
error,
|
|
37
|
+
loading,
|
|
38
|
+
saving,
|
|
39
|
+
refresh,
|
|
40
|
+
save: async (vendor, apiKey) => {
|
|
41
|
+
setSaving(true);
|
|
42
|
+
try {
|
|
43
|
+
const saved = await kitRequest("/model_keys", { ...optsRef.current, method: "POST", body: { vendor, key: apiKey } });
|
|
44
|
+
const { replaced: _r, ...k } = saved;
|
|
45
|
+
setState((s) => (s ? { ...s, keys: [k] } : s));
|
|
46
|
+
return saved;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
setSaving(false);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
remove: async (vendor) => {
|
|
53
|
+
await kitRequest(`/model_keys/${encodeURIComponent(vendor)}`, { ...optsRef.current, method: "DELETE" });
|
|
54
|
+
setState((s) => (s ? { ...s, keys: s.keys.filter((k) => k.vendor !== vendor) } : s));
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type UseAgentKitOptions } from "./agent-kit.ts";
|
|
2
|
+
/** A conversation as the kit's route answers it (your `metadata` is never sent to the page). */
|
|
3
|
+
export interface KitThread {
|
|
4
|
+
object: "thread";
|
|
5
|
+
/** thr_… */
|
|
6
|
+
id: string;
|
|
7
|
+
created: string;
|
|
8
|
+
/** agt_…: the agent it is with. */
|
|
9
|
+
agent: string;
|
|
10
|
+
/** run_…: the run answering in it now, or null. */
|
|
11
|
+
active_run: string | null;
|
|
12
|
+
/** The first line of its first message (at most 80 characters), or the name it was given; null before any. */
|
|
13
|
+
title: string | null;
|
|
14
|
+
/** When its newest message was written; null before the first. */
|
|
15
|
+
last_message_at: string | null;
|
|
16
|
+
archived: boolean;
|
|
17
|
+
/** The agent's home conversation, where its work in the background reports. */
|
|
18
|
+
home: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* How far this person has seen it: the time of the newest message they have seen here (their `seen`), or of their
|
|
21
|
+
* own newest message if that is later. Null when the call names no person.
|
|
22
|
+
*/
|
|
23
|
+
seen_through: string | null;
|
|
24
|
+
/**
|
|
25
|
+
* The messages here this person has not seen (newer than their mark, not their own), counted exactly; 100 means
|
|
26
|
+
* 100 or more. Ask `unseen > 0`, never a time against `seen_through`. Null when the call names no person, or the API
|
|
27
|
+
* doesn't send it yet: no badge.
|
|
28
|
+
*/
|
|
29
|
+
unseen: number | null;
|
|
30
|
+
}
|
|
31
|
+
/** A next page after what is shown: a conversation already shown (it moved up while paging) is not drawn twice. */
|
|
32
|
+
export declare function mergeThreadPage(shown: readonly KitThread[], page: readonly KitThread[]): KitThread[];
|
|
33
|
+
/** A conversation just started or just written in: first, and nowhere else in the list. */
|
|
34
|
+
export declare function putThreadFirst(shown: readonly KitThread[], t: KitThread): KitThread[];
|
|
35
|
+
export interface UseThreadsOptions extends UseAgentKitOptions {
|
|
36
|
+
/** Only this agent's conversations (agt_…). */
|
|
37
|
+
agent?: string | undefined;
|
|
38
|
+
/** How many a page reads, 1–100. Default 20. */
|
|
39
|
+
pageSize?: number | undefined;
|
|
40
|
+
/** false: read nothing (yet). */
|
|
41
|
+
enabled?: boolean | undefined;
|
|
42
|
+
}
|
|
43
|
+
export declare function useThreads(opts?: UseThreadsOptions): {
|
|
44
|
+
/** Newest message first; null before the first read. */
|
|
45
|
+
threads: KitThread[] | null;
|
|
46
|
+
hasMore: boolean;
|
|
47
|
+
error: Error | null;
|
|
48
|
+
loading: boolean;
|
|
49
|
+
/** Read the first page again (after the person sent in a conversation, say). */
|
|
50
|
+
refresh: () => Promise<void>;
|
|
51
|
+
/** Read the next page and add it below what is shown. */
|
|
52
|
+
loadMore: () => Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Start a conversation with `agent` (`home: true`: the agent's home conversation, the same one every time), put
|
|
55
|
+
* it first, and answer it. A failure throws an `AgentKitError` and leaves the list as it was.
|
|
56
|
+
*/
|
|
57
|
+
create: (agent: string, o?: {
|
|
58
|
+
home?: boolean;
|
|
59
|
+
idempotencyKey?: string;
|
|
60
|
+
}) => Promise<KitThread>;
|
|
61
|
+
/** A conversation the person just wrote in, as your page knows it: first. */
|
|
62
|
+
touched: (t: KitThread) => void;
|
|
63
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { DEFAULT_KIT_BASE, kitRequest, newIdempotencyKey } from "./agent-kit.js";
|
|
4
|
+
export function mergeThreadPage(shown, page) {
|
|
5
|
+
const have = new Set(shown.map((t) => t.id));
|
|
6
|
+
return [...shown, ...page.filter((t) => !have.has(t.id))];
|
|
7
|
+
}
|
|
8
|
+
export function putThreadFirst(shown, t) {
|
|
9
|
+
return [t, ...shown.filter((x) => x.id !== t.id)];
|
|
10
|
+
}
|
|
11
|
+
export function useThreads(opts = {}) {
|
|
12
|
+
const [threads, setThreads] = useState(null);
|
|
13
|
+
const [hasMore, setHasMore] = useState(false);
|
|
14
|
+
const [error, setError] = useState(null);
|
|
15
|
+
const [loading, setLoading] = useState(opts.enabled !== false);
|
|
16
|
+
const optsRef = useRef(opts);
|
|
17
|
+
optsRef.current = opts;
|
|
18
|
+
const key = opts.enabled === false ? null : `${opts.api ?? DEFAULT_KIT_BASE}|${opts.agent ?? ""}|${opts.pageSize ?? 20}`;
|
|
19
|
+
const current = useRef(key);
|
|
20
|
+
const read = useCallback(async (after) => {
|
|
21
|
+
const o = optsRef.current;
|
|
22
|
+
const k = current.current;
|
|
23
|
+
const q = new URLSearchParams({ limit: String(o.pageSize ?? 20), ...(o.agent ? { agent: o.agent } : {}), ...(after ? { starting_after: after } : {}) });
|
|
24
|
+
setLoading(true);
|
|
25
|
+
try {
|
|
26
|
+
const page = await kitRequest(`/threads?${q.toString()}`, o);
|
|
27
|
+
if (current.current !== k)
|
|
28
|
+
return;
|
|
29
|
+
setThreads((shown) => (after === null || shown === null ? page.data : mergeThreadPage(shown, page.data)));
|
|
30
|
+
setHasMore(page.has_more);
|
|
31
|
+
setError(null);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
if (current.current !== k)
|
|
35
|
+
return;
|
|
36
|
+
setError(err);
|
|
37
|
+
o.onError?.(err);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
if (current.current === k)
|
|
41
|
+
setLoading(false);
|
|
42
|
+
}
|
|
43
|
+
}, []);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
current.current = key;
|
|
46
|
+
setThreads(null);
|
|
47
|
+
setHasMore(false);
|
|
48
|
+
setError(null);
|
|
49
|
+
if (key !== null)
|
|
50
|
+
void read(null);
|
|
51
|
+
else
|
|
52
|
+
setLoading(false);
|
|
53
|
+
}, [key, read]);
|
|
54
|
+
return {
|
|
55
|
+
threads,
|
|
56
|
+
hasMore,
|
|
57
|
+
error,
|
|
58
|
+
loading,
|
|
59
|
+
refresh: () => read(null),
|
|
60
|
+
loadMore: async () => {
|
|
61
|
+
const last = threads?.at(-1);
|
|
62
|
+
if (!hasMore || !last || loading)
|
|
63
|
+
return;
|
|
64
|
+
await read(last.id);
|
|
65
|
+
},
|
|
66
|
+
create: async (agent, o = {}) => {
|
|
67
|
+
const t = await kitRequest("/threads", { ...optsRef.current, method: "POST", body: { agent, ...(o.home !== undefined ? { home: o.home } : {}) }, idempotencyKey: o.idempotencyKey ?? newIdempotencyKey() });
|
|
68
|
+
setThreads((shown) => putThreadFirst(shown ?? [], t));
|
|
69
|
+
return t;
|
|
70
|
+
},
|
|
71
|
+
touched: (t) => setThreads((shown) => (shown ? putThreadFirst(shown, t) : shown)),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** One `vault_ask_user` answer, as the form reads it. */
|
|
2
|
+
export interface VaultRequest {
|
|
3
|
+
name: string;
|
|
4
|
+
handle: string;
|
|
5
|
+
kind: "login" | "api_key" | "secret";
|
|
6
|
+
label: string;
|
|
7
|
+
origins: string[];
|
|
8
|
+
hosts: string[];
|
|
9
|
+
existing: boolean;
|
|
10
|
+
addsNewSite: boolean;
|
|
11
|
+
added: string[];
|
|
12
|
+
/** The saved login has an authenticator key, and a new site for it needs the key entered again (VAULT-1b). */
|
|
13
|
+
needsTotp: boolean;
|
|
14
|
+
tainted: boolean;
|
|
15
|
+
reason: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The request in a `data-vault-request` part's `data`, or null. Anything that is not exactly a vault request is
|
|
19
|
+
* null, so a malformed part draws no form.
|
|
20
|
+
*/
|
|
21
|
+
export declare function vaultRequestOf(data: unknown): VaultRequest | null;
|
|
22
|
+
/**
|
|
23
|
+
* Every string the form shows. `{label}`, `{handle}` and `{sites}` are replaced. The defaults name the item by its
|
|
24
|
+
* label, never by its handle (`vault://…`), which is for the agent, not for the person.
|
|
25
|
+
*/
|
|
26
|
+
export interface VaultFormLabels {
|
|
27
|
+
title: string;
|
|
28
|
+
usedOn: string;
|
|
29
|
+
reason: string;
|
|
30
|
+
tainted: string;
|
|
31
|
+
addsSite: string;
|
|
32
|
+
username: string;
|
|
33
|
+
password: string;
|
|
34
|
+
totp: string;
|
|
35
|
+
totpHint: string;
|
|
36
|
+
/** The authenticator field when the saved login has a key and this save adds a site: required. */
|
|
37
|
+
totpRequired: string;
|
|
38
|
+
totpRequiredHint: string;
|
|
39
|
+
value: string;
|
|
40
|
+
save: string;
|
|
41
|
+
saving: string;
|
|
42
|
+
saved: string;
|
|
43
|
+
failed: string;
|
|
44
|
+
/** The save would replace an item someone else saved (the API's 403): only an owner or admin may. */
|
|
45
|
+
notYours: string;
|
|
46
|
+
/** The agent is still working on the reply that asked, and the form waited 20 s for it. */
|
|
47
|
+
notYet: string;
|
|
48
|
+
}
|
|
49
|
+
export declare const DEFAULT_VAULT_LABELS: VaultFormLabels;
|
|
50
|
+
export interface VaultSecretFormProps {
|
|
51
|
+
request: VaultRequest;
|
|
52
|
+
/** The kit's vault route. Default "/api/openlegion/vault". */
|
|
53
|
+
api?: string;
|
|
54
|
+
labels?: Partial<VaultFormLabels>;
|
|
55
|
+
/** After a save, with the handle and the request it answered: `<AgentChat>` tells the agent, so it carries on. */
|
|
56
|
+
onSaved?: (handle: string, request: VaultRequest) => void;
|
|
57
|
+
/** Every error, for YOUR logs. The form shows only `labels.failed`. */
|
|
58
|
+
onError?: (err: Error) => void;
|
|
59
|
+
fetch?: typeof fetch;
|
|
60
|
+
className?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The ask this form answers: the conversation (`thr_…`) and the id of the agent's message the form is in.
|
|
63
|
+
* `<AgentChat>` passes it. The kit's vault route refuses a save without it, as a member may save only what their
|
|
64
|
+
* own conversation asked for.
|
|
65
|
+
*/
|
|
66
|
+
ask?: {
|
|
67
|
+
thread: string;
|
|
68
|
+
message: string;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** What the form posts: the item as the agent asked for it, the value typed, and the ask it answers. */
|
|
72
|
+
export declare function vaultSaveBody(r: VaultRequest, secret: Readonly<Record<string, string | undefined>>, ask: {
|
|
73
|
+
thread: string;
|
|
74
|
+
message: string;
|
|
75
|
+
} | undefined): string;
|
|
76
|
+
/**
|
|
77
|
+
* What the form says after the route answered `status` with error `code`: saved; someone else's item (403); the ask
|
|
78
|
+
* not stored yet, after the form waited for it (`vault_ask_missing`); or the usual failure.
|
|
79
|
+
*/
|
|
80
|
+
export declare function vaultSaveState(status: number, code: string | null): "saved" | "notYours" | "notYet" | "failed";
|
|
81
|
+
/** How often, and for how long, a save made while the agent is still working is tried again. */
|
|
82
|
+
export declare const ASK_WAIT: {
|
|
83
|
+
readonly every: 2000;
|
|
84
|
+
readonly upTo: 20000;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Send the save, and while the route answers `409 vault_ask_missing` (a reply drawn live has no `msg_` until its run
|
|
88
|
+
* ends, and the person saved while the agent was still working) send it again every 2 s for up to 20 s. Any other
|
|
89
|
+
* answer is final at once. Resolves with the last answer. `send` holds the value in its closure only.
|
|
90
|
+
*/
|
|
91
|
+
export declare function saveUntilAsked(send: () => Promise<Response>, wait?: (ms: number) => Promise<void>): Promise<Response>;
|
|
92
|
+
export declare function VaultSecretForm(props: VaultSecretFormProps): import("react").JSX.Element;
|