@useago/sdk 0.1.6 → 0.2.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/dist/AgoClient-CNT-8sh_.js +905 -0
- package/dist/AgoClient-CNT-8sh_.js.map +1 -0
- package/dist/AgoClient-D0l1GTRs.cjs +976 -0
- package/dist/AgoClient-D0l1GTRs.cjs.map +1 -0
- package/dist/angular/ago.service.d.ts +98 -0
- package/dist/angular/index.d.ts +4 -0
- package/dist/angular/provide.d.ts +27 -0
- package/dist/angular.cjs +143 -0
- package/dist/angular.cjs.map +1 -0
- package/dist/angular.d.ts +2 -0
- package/dist/angular.js +141 -0
- package/dist/angular.js.map +1 -0
- package/dist/auto/createAgo.d.ts +39 -0
- package/dist/auto/index.d.ts +1 -0
- package/dist/client/AgoClient.d.ts +56 -0
- package/dist/client/types.d.ts +9 -7
- package/dist/createMockClient-Ci_N2tH8.cjs +104 -0
- package/dist/createMockClient-Ci_N2tH8.cjs.map +1 -0
- package/dist/createMockClient-U0ae_AYy.js +99 -0
- package/dist/createMockClient-U0ae_AYy.js.map +1 -0
- package/dist/functions--c6lx3ic.cjs +480 -0
- package/dist/functions--c6lx3ic.cjs.map +1 -0
- package/dist/functions-C9F1dnmh.js +398 -0
- package/dist/functions-C9F1dnmh.js.map +1 -0
- package/dist/helpers/factory.d.ts +20 -0
- package/dist/helpers/functions.d.ts +62 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers.cjs +15 -0
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.js +2 -0
- package/dist/index.cjs +312 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +277 -19
- package/dist/index.js.map +1 -1
- package/dist/react/components/ChatWidget.d.ts +2 -0
- package/dist/react/components/Markdown.d.ts +12 -0
- package/dist/react/components/Message.d.ts +2 -0
- package/dist/react/components/index.d.ts +2 -0
- package/dist/react/context/AgoContext.d.ts +30 -4
- package/dist/react/context/index.d.ts +1 -1
- package/dist/react/hooks/index.d.ts +1 -0
- package/dist/react/hooks/useAgoContext.d.ts +40 -0
- package/dist/react/hooks/useAgoFunction.d.ts +14 -2
- package/dist/react/index.d.ts +4 -1
- package/dist/react.cjs +22104 -10273
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.ts +1 -0
- package/dist/react.js +22103 -10289
- package/dist/react.js.map +1 -1
- package/dist/rolldown-runtime-BqCkTl7Q.cjs +50 -0
- package/dist/rolldown-runtime-CNZpIYwj.js +33 -0
- package/dist/state/ClientContextRegistry.d.ts +64 -0
- package/dist/streaming/helpers.d.ts +67 -0
- package/dist/vue/composables/useAgo.d.ts +17 -0
- package/dist/vue/composables/useAgoEvents.d.ts +11 -0
- package/dist/vue/composables/useAgoFunction.d.ts +34 -0
- package/dist/vue/composables/useChat.d.ts +251 -0
- package/dist/vue/composables/useConversation.d.ts +178 -0
- package/dist/vue/composables/useMessages.d.ts +89 -0
- package/dist/vue/index.d.ts +10 -0
- package/dist/vue/plugin.d.ts +16 -0
- package/dist/vue/symbols.d.ts +3 -0
- package/dist/vue.cjs +311 -0
- package/dist/vue.cjs.map +1 -0
- package/dist/vue.d.ts +2 -0
- package/dist/vue.js +301 -0
- package/dist/vue.js.map +1 -0
- package/dist/widget/types.d.ts +1 -0
- package/dist/widget.cjs +0 -2
- package/dist/widget.d.ts +1 -0
- package/dist/widget.js +0 -2
- package/package.json +29 -7
- package/dist/createMockClient-BZKh_1em.cjs +0 -941
- package/dist/createMockClient-BZKh_1em.cjs.map +0 -1
- package/dist/createMockClient-uGlVyjbL.js +0 -942
- package/dist/createMockClient-uGlVyjbL.js.map +0 -1
- package/dist/widget.cjs.map +0 -1
- package/dist/widget.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,20 +1,315 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
2
|
+
const require_AgoClient = require("./AgoClient-D0l1GTRs.cjs");
|
|
3
|
+
const require_createMockClient = require("./createMockClient-Ci_N2tH8.cjs");
|
|
4
|
+
const require_functions = require("./functions--c6lx3ic.cjs");
|
|
5
|
+
//#region src/functions/defineFunction.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a reusable function definition.
|
|
8
|
+
* Returns the same object — this is an identity function for discoverability and type-checking.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* // Define once, reuse anywhere
|
|
12
|
+
* export const lookupOrder = defineFunction({
|
|
13
|
+
* name: "lookupOrder",
|
|
14
|
+
* description: "Look up an order by ID",
|
|
15
|
+
* parameters: { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
|
|
16
|
+
* handler: async (args) => fetchOrder(args.id as string),
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Register with client
|
|
20
|
+
* client.registerFunction(lookupOrder);
|
|
21
|
+
*
|
|
22
|
+
* // Or use in React
|
|
23
|
+
* useAgoFunction(lookupOrder.name, lookupOrder);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
4
26
|
function defineFunction(definition) {
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
27
|
+
return definition;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/streaming/helpers.ts
|
|
31
|
+
/**
|
|
32
|
+
* Subscribe to complete messages with a simple callback. Returns an unsubscribe function.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const unsub = onMessage(client, (msg) => console.log(msg.content));
|
|
36
|
+
* // later: unsub();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function onMessage(client, callback) {
|
|
40
|
+
client.on("message:complete", callback);
|
|
41
|
+
return () => client.off("message:complete", callback);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Subscribe to streaming text chunks.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* onMessageChunk(client, ({ content }) => {
|
|
48
|
+
* outputEl.textContent += content;
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function onMessageChunk(client, callback) {
|
|
53
|
+
client.on("message:chunk", callback);
|
|
54
|
+
return () => client.off("message:chunk", callback);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to message start events.
|
|
58
|
+
*/
|
|
59
|
+
function onMessageStart(client, callback) {
|
|
60
|
+
client.on("message:start", callback);
|
|
61
|
+
return () => client.off("message:start", callback);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Subscribe to message errors.
|
|
65
|
+
*/
|
|
66
|
+
function onMessageError(client, callback) {
|
|
67
|
+
client.on("message:error", callback);
|
|
68
|
+
return () => client.off("message:error", callback);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Subscribe to tool call events.
|
|
72
|
+
*/
|
|
73
|
+
function onToolCall(client, callback) {
|
|
74
|
+
client.on("toolCall:received", callback);
|
|
75
|
+
return () => client.off("toolCall:received", callback);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Subscribe to client function invocations.
|
|
79
|
+
*/
|
|
80
|
+
function onFunctionInvoke(client, callback) {
|
|
81
|
+
client.on("function:invoke", callback);
|
|
82
|
+
return () => client.off("function:invoke", callback);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Send a message and iterate over chunks as an async generator.
|
|
86
|
+
* Gives you full control over how you process the stream.
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* for await (const chunk of createMessageStream(client, "Hello!")) {
|
|
90
|
+
* console.log(chunk.type, chunk.data);
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
async function* createMessageStream(client, content, options) {
|
|
95
|
+
const queue = [];
|
|
96
|
+
let resolve = null;
|
|
97
|
+
let done = false;
|
|
98
|
+
const push = (event) => {
|
|
99
|
+
queue.push(event);
|
|
100
|
+
resolve?.();
|
|
101
|
+
};
|
|
102
|
+
const onStart = (data) => push({
|
|
103
|
+
type: "start",
|
|
104
|
+
data
|
|
105
|
+
});
|
|
106
|
+
const onChunk = (data) => push({
|
|
107
|
+
type: "chunk",
|
|
108
|
+
data
|
|
109
|
+
});
|
|
110
|
+
const onComplete = (data) => {
|
|
111
|
+
push({
|
|
112
|
+
type: "complete",
|
|
113
|
+
data
|
|
114
|
+
});
|
|
115
|
+
done = true;
|
|
116
|
+
resolve?.();
|
|
117
|
+
};
|
|
118
|
+
const onError = (data) => {
|
|
119
|
+
push({
|
|
120
|
+
type: "error",
|
|
121
|
+
data
|
|
122
|
+
});
|
|
123
|
+
done = true;
|
|
124
|
+
resolve?.();
|
|
125
|
+
};
|
|
126
|
+
const onTool = (data) => push({
|
|
127
|
+
type: "toolCall",
|
|
128
|
+
data
|
|
129
|
+
});
|
|
130
|
+
client.on("message:start", onStart);
|
|
131
|
+
client.on("message:chunk", onChunk);
|
|
132
|
+
client.on("message:complete", onComplete);
|
|
133
|
+
client.on("message:error", onError);
|
|
134
|
+
client.on("toolCall:received", onTool);
|
|
135
|
+
client.sendMessage(content, options).catch(() => {
|
|
136
|
+
done = true;
|
|
137
|
+
resolve?.();
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
while (!done || queue.length > 0) if (queue.length > 0) yield queue.shift();
|
|
141
|
+
else {
|
|
142
|
+
await new Promise((r) => {
|
|
143
|
+
resolve = r;
|
|
144
|
+
});
|
|
145
|
+
resolve = null;
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
client.off("message:start", onStart);
|
|
149
|
+
client.off("message:chunk", onChunk);
|
|
150
|
+
client.off("message:complete", onComplete);
|
|
151
|
+
client.off("message:error", onError);
|
|
152
|
+
client.off("toolCall:received", onTool);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/auto/createAgo.ts
|
|
157
|
+
/**
|
|
158
|
+
* Auto-detect AGO configuration from the DOM environment.
|
|
159
|
+
*
|
|
160
|
+
* Checks (in order of priority):
|
|
161
|
+
* 1. `window.AGO` global object (widget config)
|
|
162
|
+
* 2. `<meta>` tags: `<meta name="ago-api-key" content="...">`
|
|
163
|
+
* 3. `data-ago-*` attributes on `<body>` or `<script data-ago-api-key="...">`
|
|
164
|
+
* 4. Explicit overrides passed as argument
|
|
165
|
+
*
|
|
166
|
+
* Returns null if no API key can be found.
|
|
167
|
+
*/
|
|
168
|
+
function autoDetectConfig(overrides) {
|
|
169
|
+
let baseUrl;
|
|
170
|
+
let widgetId;
|
|
171
|
+
let agent;
|
|
172
|
+
let permission;
|
|
173
|
+
let userEmail;
|
|
174
|
+
let userJwt;
|
|
175
|
+
if (typeof window !== "undefined" && window.AGO) {
|
|
176
|
+
const ago = window.AGO;
|
|
177
|
+
baseUrl = ago.basepath;
|
|
178
|
+
widgetId = ago.widgetId;
|
|
179
|
+
agent = ago.agent || ago.defaultAgent;
|
|
180
|
+
permission = ago.permission;
|
|
181
|
+
userEmail = ago.email;
|
|
182
|
+
userJwt = ago.jwt;
|
|
183
|
+
}
|
|
184
|
+
if (typeof document !== "undefined") {
|
|
185
|
+
const getMeta = (name) => document.querySelector(`meta[name="${name}"]`)?.getAttribute("content") || void 0;
|
|
186
|
+
baseUrl = baseUrl || getMeta("ago-base-url");
|
|
187
|
+
widgetId = widgetId || getMeta("ago-widget-id");
|
|
188
|
+
agent = agent || getMeta("ago-agent") || getMeta("ago-agent-id");
|
|
189
|
+
permission = permission || getMeta("ago-permission");
|
|
190
|
+
userEmail = userEmail || getMeta("ago-user-email");
|
|
191
|
+
}
|
|
192
|
+
if (typeof document !== "undefined") {
|
|
193
|
+
const sources = [document.body, ...Array.from(document.querySelectorAll("script[data-ago-base-url]"))].filter(Boolean);
|
|
194
|
+
for (const el of sources) {
|
|
195
|
+
if (!el) continue;
|
|
196
|
+
baseUrl = baseUrl || el.getAttribute("data-ago-base-url") || void 0;
|
|
197
|
+
widgetId = widgetId || el.getAttribute("data-ago-widget-id") || void 0;
|
|
198
|
+
agent = agent || el.getAttribute("data-ago-agent") || el.getAttribute("data-ago-agent-id") || void 0;
|
|
199
|
+
permission = permission || el.getAttribute("data-ago-permission") || void 0;
|
|
200
|
+
userEmail = userEmail || el.getAttribute("data-ago-user-email") || void 0;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
baseUrl = overrides?.baseUrl || baseUrl;
|
|
204
|
+
widgetId = overrides?.widgetId || widgetId;
|
|
205
|
+
agent = overrides?.agent || overrides?.defaultAgentId || agent;
|
|
206
|
+
permission = overrides?.permission || permission;
|
|
207
|
+
userEmail = overrides?.userEmail || userEmail;
|
|
208
|
+
userJwt = overrides?.userJwt || userJwt;
|
|
209
|
+
const debug = overrides?.debug;
|
|
210
|
+
if (!baseUrl) return null;
|
|
211
|
+
return {
|
|
212
|
+
baseUrl,
|
|
213
|
+
widgetId,
|
|
214
|
+
agent,
|
|
215
|
+
permission,
|
|
216
|
+
userEmail,
|
|
217
|
+
userJwt,
|
|
218
|
+
debug
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Create an AgoClient with zero configuration.
|
|
223
|
+
* Auto-detects config from window.AGO, meta tags, and data-ago-* attributes.
|
|
224
|
+
*
|
|
225
|
+
* ```html
|
|
226
|
+
* <!-- In your HTML -->
|
|
227
|
+
* <meta name="ago-base-url" content="https://YOUR-DOMAIN.useago.com">
|
|
228
|
+
*
|
|
229
|
+
* <!-- Or on a script tag -->
|
|
230
|
+
* <script src="app.js" data-ago-base-url="https://YOUR-DOMAIN.useago.com"><\/script>
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { createAgo } from "@useago/sdk";
|
|
235
|
+
*
|
|
236
|
+
* // Zero config — picks up from DOM
|
|
237
|
+
* const client = createAgo();
|
|
238
|
+
*
|
|
239
|
+
* // Or with partial overrides
|
|
240
|
+
* const client = createAgo({ debug: true });
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* Throws if no base URL can be detected.
|
|
244
|
+
*/
|
|
245
|
+
function createAgo(overrides) {
|
|
246
|
+
const config = autoDetectConfig(overrides);
|
|
247
|
+
if (!config) throw new Error("createAgo(): could not detect AGO configuration. Set window.AGO, add <meta name=\"ago-base-url\">, or pass { baseUrl } explicitly.");
|
|
248
|
+
return new require_AgoClient.AgoClient(config);
|
|
249
|
+
}
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/helpers/factory.ts
|
|
252
|
+
/**
|
|
253
|
+
* Attach a handler to a pre-built function definition.
|
|
254
|
+
*
|
|
255
|
+
* Pre-built helpers (`showToast`, `trackEvent`, ...) ship with a no-op or
|
|
256
|
+
* console-logging default handler. Use `withHandler` to wire them up to
|
|
257
|
+
* your actual implementation without mutating the original definition.
|
|
258
|
+
*
|
|
259
|
+
* ```ts
|
|
260
|
+
* import { showToast, withHandler } from "@useago/sdk";
|
|
261
|
+
*
|
|
262
|
+
* client.register(
|
|
263
|
+
* withHandler(showToast, (args) => {
|
|
264
|
+
* myToast(args.message as string);
|
|
265
|
+
* return { shown: true };
|
|
266
|
+
* })
|
|
267
|
+
* );
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
function withHandler(definition, handler) {
|
|
271
|
+
return {
|
|
272
|
+
...definition,
|
|
273
|
+
handler
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
exports.AgoApiError = require_AgoClient.AgoApiError;
|
|
278
|
+
exports.AgoClient = require_AgoClient.AgoClient;
|
|
279
|
+
exports.AgoError = require_AgoClient.AgoError;
|
|
280
|
+
exports.AgoFunctionError = require_AgoClient.AgoFunctionError;
|
|
281
|
+
exports.AgoNetworkError = require_AgoClient.AgoNetworkError;
|
|
282
|
+
exports.AgoStreamError = require_AgoClient.AgoStreamError;
|
|
283
|
+
exports.ClientContextRegistry = require_AgoClient.ClientContextRegistry;
|
|
284
|
+
exports.EventEmitter = require_AgoClient.EventEmitter;
|
|
285
|
+
exports.FunctionRegistry = require_AgoClient.FunctionRegistry;
|
|
286
|
+
exports.SSEHandler = require_AgoClient.SSEHandler;
|
|
287
|
+
exports.autoDetectConfig = autoDetectConfig;
|
|
288
|
+
exports.copyToClipboard = require_functions.copyToClipboard;
|
|
289
|
+
exports.createAgo = createAgo;
|
|
290
|
+
exports.createMessageStream = createMessageStream;
|
|
291
|
+
exports.createMockClient = require_createMockClient.createMockClient;
|
|
19
292
|
exports.defineFunction = defineFunction;
|
|
20
|
-
|
|
293
|
+
exports.getLocalStorage = require_functions.getLocalStorage;
|
|
294
|
+
exports.getUserLocation = require_functions.getUserLocation;
|
|
295
|
+
exports.highlightElement = require_functions.highlightElement;
|
|
296
|
+
exports.isStreamNetworkError = require_AgoClient.isStreamNetworkError;
|
|
297
|
+
exports.logger = require_AgoClient.logger;
|
|
298
|
+
exports.onFunctionInvoke = onFunctionInvoke;
|
|
299
|
+
exports.onMessage = onMessage;
|
|
300
|
+
exports.onMessageChunk = onMessageChunk;
|
|
301
|
+
exports.onMessageError = onMessageError;
|
|
302
|
+
exports.onMessageStart = onMessageStart;
|
|
303
|
+
exports.onToolCall = onToolCall;
|
|
304
|
+
exports.openUrl = require_functions.openUrl;
|
|
305
|
+
exports.scrollToElement = require_functions.scrollToElement;
|
|
306
|
+
exports.setLocalStorage = require_functions.setLocalStorage;
|
|
307
|
+
exports.setTheme = require_functions.setTheme;
|
|
308
|
+
exports.showConfirmDialog = require_functions.showConfirmDialog;
|
|
309
|
+
exports.showNotification = require_functions.showNotification;
|
|
310
|
+
exports.showToast = require_functions.showToast;
|
|
311
|
+
exports.submitForm = require_functions.submitForm;
|
|
312
|
+
exports.trackEvent = require_functions.trackEvent;
|
|
313
|
+
exports.withHandler = withHandler;
|
|
314
|
+
|
|
315
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/functions/defineFunction.ts"],"sourcesContent":["import type { ClientFunctionDefinition } from \"./types\";\n\n/**\n * Create a reusable function definition.\n * Returns the same object — this is an identity function for discoverability and type-checking.\n *\n * ```ts\n * // Define once, reuse anywhere\n * export const lookupOrder = defineFunction({\n * name: \"lookupOrder\",\n * description: \"Look up an order by ID\",\n * parameters: { type: \"object\", properties: { id: { type: \"string\" } }, required: [\"id\"] },\n * handler: async (args) => fetchOrder(args.id as string),\n * });\n *\n * // Register with client\n * client.registerFunction(lookupOrder);\n *\n * // Or use in React\n * useAgoFunction(lookupOrder.name, lookupOrder);\n * ```\n */\nexport function defineFunction(\n definition: ClientFunctionDefinition\n): ClientFunctionDefinition {\n return definition;\n}\n"],"names":[],"mappings":";;;AAsBO,SAAS,eACd,YAC0B;AAC1B,SAAO;AACT;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/functions/defineFunction.ts","../src/streaming/helpers.ts","../src/auto/createAgo.ts","../src/helpers/factory.ts"],"sourcesContent":["import type { ClientFunctionDefinition } from \"./types\";\n\n/**\n * Create a reusable function definition.\n * Returns the same object — this is an identity function for discoverability and type-checking.\n *\n * ```ts\n * // Define once, reuse anywhere\n * export const lookupOrder = defineFunction({\n * name: \"lookupOrder\",\n * description: \"Look up an order by ID\",\n * parameters: { type: \"object\", properties: { id: { type: \"string\" } }, required: [\"id\"] },\n * handler: async (args) => fetchOrder(args.id as string),\n * });\n *\n * // Register with client\n * client.registerFunction(lookupOrder);\n *\n * // Or use in React\n * useAgoFunction(lookupOrder.name, lookupOrder);\n * ```\n */\nexport function defineFunction(\n definition: ClientFunctionDefinition\n): ClientFunctionDefinition {\n return definition;\n}\n","import type { AgoClient } from \"../client/AgoClient\";\nimport type {\n AgoMessage,\n AgoClientEvents,\n ToolCallData,\n ClientFunctionInvocation,\n} from \"../client/types\";\n\n/**\n * Subscribe to complete messages with a simple callback. Returns an unsubscribe function.\n *\n * ```ts\n * const unsub = onMessage(client, (msg) => console.log(msg.content));\n * // later: unsub();\n * ```\n */\nexport function onMessage(\n client: AgoClient,\n callback: (message: AgoMessage) => void\n): () => void {\n client.on(\"message:complete\", callback);\n return () => client.off(\"message:complete\", callback);\n}\n\n/**\n * Subscribe to streaming text chunks.\n *\n * ```ts\n * onMessageChunk(client, ({ content }) => {\n * outputEl.textContent += content;\n * });\n * ```\n */\nexport function onMessageChunk(\n client: AgoClient,\n callback: (data: AgoClientEvents[\"message:chunk\"]) => void\n): () => void {\n client.on(\"message:chunk\", callback);\n return () => client.off(\"message:chunk\", callback);\n}\n\n/**\n * Subscribe to message start events.\n */\nexport function onMessageStart(\n client: AgoClient,\n callback: (data: AgoClientEvents[\"message:start\"]) => void\n): () => void {\n client.on(\"message:start\", callback);\n return () => client.off(\"message:start\", callback);\n}\n\n/**\n * Subscribe to message errors.\n */\nexport function onMessageError(\n client: AgoClient,\n callback: (data: AgoClientEvents[\"message:error\"]) => void\n): () => void {\n client.on(\"message:error\", callback);\n return () => client.off(\"message:error\", callback);\n}\n\n/**\n * Subscribe to tool call events.\n */\nexport function onToolCall(\n client: AgoClient,\n callback: (toolCall: ToolCallData) => void\n): () => void {\n client.on(\"toolCall:received\", callback);\n return () => client.off(\"toolCall:received\", callback);\n}\n\n/**\n * Subscribe to client function invocations.\n */\nexport function onFunctionInvoke(\n client: AgoClient,\n callback: (data: ClientFunctionInvocation) => void\n): () => void {\n client.on(\"function:invoke\", callback);\n return () => client.off(\"function:invoke\", callback);\n}\n\n/**\n * Send a message and iterate over chunks as an async generator.\n * Gives you full control over how you process the stream.\n *\n * ```ts\n * for await (const chunk of createMessageStream(client, \"Hello!\")) {\n * console.log(chunk.type, chunk.data);\n * }\n * ```\n */\nexport async function* createMessageStream(\n client: AgoClient,\n content: string,\n options?: { conversationId?: string; agentId?: string; files?: File[] }\n): AsyncGenerator<\n | { type: \"start\"; data: AgoClientEvents[\"message:start\"] }\n | { type: \"chunk\"; data: AgoClientEvents[\"message:chunk\"] }\n | { type: \"complete\"; data: AgoMessage }\n | { type: \"error\"; data: AgoClientEvents[\"message:error\"] }\n | { type: \"toolCall\"; data: ToolCallData }\n> {\n type StreamEvent =\n | { type: \"start\"; data: AgoClientEvents[\"message:start\"] }\n | { type: \"chunk\"; data: AgoClientEvents[\"message:chunk\"] }\n | { type: \"complete\"; data: AgoMessage }\n | { type: \"error\"; data: AgoClientEvents[\"message:error\"] }\n | { type: \"toolCall\"; data: ToolCallData };\n\n const queue: StreamEvent[] = [];\n let resolve: (() => void) | null = null;\n let done = false;\n\n const push = (event: StreamEvent) => {\n queue.push(event);\n resolve?.();\n };\n\n const onStart = (data: AgoClientEvents[\"message:start\"]) => push({ type: \"start\", data });\n const onChunk = (data: AgoClientEvents[\"message:chunk\"]) => push({ type: \"chunk\", data });\n const onComplete = (data: AgoMessage) => {\n push({ type: \"complete\", data });\n done = true;\n resolve?.();\n };\n const onError = (data: AgoClientEvents[\"message:error\"]) => {\n push({ type: \"error\", data });\n done = true;\n resolve?.();\n };\n const onTool = (data: ToolCallData) => push({ type: \"toolCall\", data });\n\n client.on(\"message:start\", onStart);\n client.on(\"message:chunk\", onChunk);\n client.on(\"message:complete\", onComplete);\n client.on(\"message:error\", onError);\n client.on(\"toolCall:received\", onTool);\n\n // Fire the request (don't await — we'll consume events via the generator)\n client.sendMessage(content, options).catch(() => {\n done = true;\n resolve?.();\n });\n\n try {\n while (!done || queue.length > 0) {\n if (queue.length > 0) {\n yield queue.shift()!;\n } else {\n await new Promise<void>((r) => { resolve = r; });\n resolve = null;\n }\n }\n } finally {\n client.off(\"message:start\", onStart);\n client.off(\"message:chunk\", onChunk);\n client.off(\"message:complete\", onComplete);\n client.off(\"message:error\", onError);\n client.off(\"toolCall:received\", onTool);\n }\n}\n","import { AgoClient } from \"../client/AgoClient\";\nimport type { AgoConfig } from \"../client/types\";\n\n/**\n * Auto-detect AGO configuration from the DOM environment.\n *\n * Checks (in order of priority):\n * 1. `window.AGO` global object (widget config)\n * 2. `<meta>` tags: `<meta name=\"ago-api-key\" content=\"...\">`\n * 3. `data-ago-*` attributes on `<body>` or `<script data-ago-api-key=\"...\">`\n * 4. Explicit overrides passed as argument\n *\n * Returns null if no API key can be found.\n */\nexport function autoDetectConfig(\n overrides?: Partial<AgoConfig>\n): AgoConfig | null {\n let baseUrl: string | undefined;\n let widgetId: string | undefined;\n let agent: string | undefined;\n let permission: string | undefined;\n let userEmail: string | undefined;\n let userJwt: string | undefined;\n\n // 1. window.AGO (widget config format)\n if (typeof window !== \"undefined\" && (window as unknown as Record<string, unknown>).AGO) {\n const ago = (window as unknown as Record<string, unknown>).AGO as Record<string, unknown>;\n baseUrl = ago.basepath as string | undefined;\n widgetId = ago.widgetId as string | undefined;\n agent = (ago.agent as string | undefined) || (ago.defaultAgent as string | undefined);\n permission = ago.permission as string | undefined;\n userEmail = ago.email as string | undefined;\n userJwt = ago.jwt as string | undefined;\n }\n\n // 2. Meta tags\n if (typeof document !== \"undefined\") {\n const getMeta = (name: string) =>\n document.querySelector(`meta[name=\"${name}\"]`)?.getAttribute(\"content\") || undefined;\n\n baseUrl = baseUrl || getMeta(\"ago-base-url\");\n widgetId = widgetId || getMeta(\"ago-widget-id\");\n agent = agent || getMeta(\"ago-agent\") || getMeta(\"ago-agent-id\");\n permission = permission || getMeta(\"ago-permission\");\n userEmail = userEmail || getMeta(\"ago-user-email\");\n }\n\n // 3. data-ago-* attributes on body or script tags\n if (typeof document !== \"undefined\") {\n const sources = [\n document.body,\n ...Array.from(document.querySelectorAll(\"script[data-ago-base-url]\")),\n ].filter(Boolean);\n\n for (const el of sources) {\n if (!el) continue;\n baseUrl = baseUrl || el.getAttribute(\"data-ago-base-url\") || undefined;\n widgetId = widgetId || el.getAttribute(\"data-ago-widget-id\") || undefined;\n agent = agent || el.getAttribute(\"data-ago-agent\") || el.getAttribute(\"data-ago-agent-id\") || undefined;\n permission = permission || el.getAttribute(\"data-ago-permission\") || undefined;\n userEmail = userEmail || el.getAttribute(\"data-ago-user-email\") || undefined;\n }\n }\n\n // 4. Apply overrides\n baseUrl = overrides?.baseUrl || baseUrl;\n widgetId = overrides?.widgetId || widgetId;\n agent = overrides?.agent || overrides?.defaultAgentId || agent;\n permission = overrides?.permission || permission;\n userEmail = overrides?.userEmail || userEmail;\n userJwt = overrides?.userJwt || userJwt;\n const debug = overrides?.debug;\n\n if (!baseUrl) {\n return null;\n }\n\n return {\n baseUrl,\n widgetId,\n agent,\n permission,\n userEmail,\n userJwt,\n debug,\n };\n}\n\n/**\n * Create an AgoClient with zero configuration.\n * Auto-detects config from window.AGO, meta tags, and data-ago-* attributes.\n *\n * ```html\n * <!-- In your HTML -->\n * <meta name=\"ago-base-url\" content=\"https://YOUR-DOMAIN.useago.com\">\n *\n * <!-- Or on a script tag -->\n * <script src=\"app.js\" data-ago-base-url=\"https://YOUR-DOMAIN.useago.com\"></script>\n * ```\n *\n * ```ts\n * import { createAgo } from \"@useago/sdk\";\n *\n * // Zero config — picks up from DOM\n * const client = createAgo();\n *\n * // Or with partial overrides\n * const client = createAgo({ debug: true });\n * ```\n *\n * Throws if no base URL can be detected.\n */\nexport function createAgo(overrides?: Partial<AgoConfig>): AgoClient {\n const config = autoDetectConfig(overrides);\n\n if (!config) {\n throw new Error(\n \"createAgo(): could not detect AGO configuration. \" +\n \"Set window.AGO, add <meta name=\\\"ago-base-url\\\">, or pass { baseUrl } explicitly.\"\n );\n }\n\n return new AgoClient(config);\n}\n","import type {\n ClientFunctionDefinition,\n ClientFunctionHandler,\n} from \"../functions/types\";\n\n/**\n * Attach a handler to a pre-built function definition.\n *\n * Pre-built helpers (`showToast`, `trackEvent`, ...) ship with a no-op or\n * console-logging default handler. Use `withHandler` to wire them up to\n * your actual implementation without mutating the original definition.\n *\n * ```ts\n * import { showToast, withHandler } from \"@useago/sdk\";\n *\n * client.register(\n * withHandler(showToast, (args) => {\n * myToast(args.message as string);\n * return { shown: true };\n * })\n * );\n * ```\n */\nexport function withHandler<T extends ClientFunctionDefinition>(\n definition: T,\n handler: ClientFunctionHandler\n): T {\n return { ...definition, handler };\n}\n"],"mappings":"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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { AgoClient } from "./client/AgoClient";
|
|
2
2
|
export type { AgoConfig, SendMessageOptions, AgoMessage, Conversation, AgoAgent, AgoSource, ToolCallData, ToolCallType, FormSchema, FormField, FunctionSchema, ClientFunction, FunctionDefinition, ClientFunctionInvocation, AgoClientEvents, AgoEventName, AgoEventHandler, MessageStatus, SSEChunkData, } from "./client/types";
|
|
3
3
|
export { AgoError, AgoApiError, AgoNetworkError, AgoStreamError, AgoFunctionError, } from "./client/errors";
|
|
4
|
+
export { ClientContextRegistry } from "./state/ClientContextRegistry";
|
|
5
|
+
export type { ContextEntry, ContextSnapshot, DynamicContextProvider, } from "./state/ClientContextRegistry";
|
|
4
6
|
export { FunctionRegistry } from "./functions/FunctionRegistry";
|
|
5
7
|
export { defineFunction } from "./functions/defineFunction";
|
|
6
8
|
export type { ClientFunctionSchema, ClientFunctionHandler, ClientFunctionDefinition, RegisteredFunction, } from "./functions/types";
|
|
@@ -8,5 +10,9 @@ export { createMockClient } from "./testing";
|
|
|
8
10
|
export type { MockAgoClient, MockAgoClientOptions } from "./testing";
|
|
9
11
|
export { SSEHandler, isStreamNetworkError } from "./streaming/SSEHandler";
|
|
10
12
|
export type { AgoWidgetConfig, AgoWidgetColors } from "./widget/types";
|
|
13
|
+
export { onMessage, onMessageChunk, onMessageStart, onMessageError, onToolCall, onFunctionInvoke, createMessageStream, } from "./streaming/helpers";
|
|
14
|
+
export { createAgo, autoDetectConfig } from "./auto/createAgo";
|
|
15
|
+
export { showToast, showNotification, openUrl, copyToClipboard, setTheme, showConfirmDialog, getUserLocation, scrollToElement, setLocalStorage, getLocalStorage, highlightElement, submitForm, trackEvent, } from "./helpers/functions";
|
|
16
|
+
export { withHandler } from "./helpers/factory";
|
|
11
17
|
export { EventEmitter } from "./utils/eventEmitter";
|
|
12
18
|
export { logger } from "./utils/logger";
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,278 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as ClientContextRegistry, c as AgoApiError, d as AgoNetworkError, f as AgoStreamError, i as isStreamNetworkError, l as AgoError, n as EventEmitter, o as FunctionRegistry, r as SSEHandler, s as logger, t as AgoClient, u as AgoFunctionError } from "./AgoClient-CNT-8sh_.js";
|
|
2
|
+
import { t as createMockClient } from "./createMockClient-U0ae_AYy.js";
|
|
3
|
+
import { a as highlightElement, c as setLocalStorage, d as showNotification, f as showToast, i as getUserLocation, l as setTheme, m as trackEvent, o as openUrl, p as submitForm, r as getLocalStorage, s as scrollToElement, t as copyToClipboard, u as showConfirmDialog } from "./functions-C9F1dnmh.js";
|
|
4
|
+
//#region src/functions/defineFunction.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a reusable function definition.
|
|
7
|
+
* Returns the same object — this is an identity function for discoverability and type-checking.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* // Define once, reuse anywhere
|
|
11
|
+
* export const lookupOrder = defineFunction({
|
|
12
|
+
* name: "lookupOrder",
|
|
13
|
+
* description: "Look up an order by ID",
|
|
14
|
+
* parameters: { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
|
|
15
|
+
* handler: async (args) => fetchOrder(args.id as string),
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Register with client
|
|
19
|
+
* client.registerFunction(lookupOrder);
|
|
20
|
+
*
|
|
21
|
+
* // Or use in React
|
|
22
|
+
* useAgoFunction(lookupOrder.name, lookupOrder);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
2
25
|
function defineFunction(definition) {
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
return definition;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/streaming/helpers.ts
|
|
30
|
+
/**
|
|
31
|
+
* Subscribe to complete messages with a simple callback. Returns an unsubscribe function.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const unsub = onMessage(client, (msg) => console.log(msg.content));
|
|
35
|
+
* // later: unsub();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function onMessage(client, callback) {
|
|
39
|
+
client.on("message:complete", callback);
|
|
40
|
+
return () => client.off("message:complete", callback);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Subscribe to streaming text chunks.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* onMessageChunk(client, ({ content }) => {
|
|
47
|
+
* outputEl.textContent += content;
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function onMessageChunk(client, callback) {
|
|
52
|
+
client.on("message:chunk", callback);
|
|
53
|
+
return () => client.off("message:chunk", callback);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Subscribe to message start events.
|
|
57
|
+
*/
|
|
58
|
+
function onMessageStart(client, callback) {
|
|
59
|
+
client.on("message:start", callback);
|
|
60
|
+
return () => client.off("message:start", callback);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Subscribe to message errors.
|
|
64
|
+
*/
|
|
65
|
+
function onMessageError(client, callback) {
|
|
66
|
+
client.on("message:error", callback);
|
|
67
|
+
return () => client.off("message:error", callback);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Subscribe to tool call events.
|
|
71
|
+
*/
|
|
72
|
+
function onToolCall(client, callback) {
|
|
73
|
+
client.on("toolCall:received", callback);
|
|
74
|
+
return () => client.off("toolCall:received", callback);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Subscribe to client function invocations.
|
|
78
|
+
*/
|
|
79
|
+
function onFunctionInvoke(client, callback) {
|
|
80
|
+
client.on("function:invoke", callback);
|
|
81
|
+
return () => client.off("function:invoke", callback);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Send a message and iterate over chunks as an async generator.
|
|
85
|
+
* Gives you full control over how you process the stream.
|
|
86
|
+
*
|
|
87
|
+
* ```ts
|
|
88
|
+
* for await (const chunk of createMessageStream(client, "Hello!")) {
|
|
89
|
+
* console.log(chunk.type, chunk.data);
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
async function* createMessageStream(client, content, options) {
|
|
94
|
+
const queue = [];
|
|
95
|
+
let resolve = null;
|
|
96
|
+
let done = false;
|
|
97
|
+
const push = (event) => {
|
|
98
|
+
queue.push(event);
|
|
99
|
+
resolve?.();
|
|
100
|
+
};
|
|
101
|
+
const onStart = (data) => push({
|
|
102
|
+
type: "start",
|
|
103
|
+
data
|
|
104
|
+
});
|
|
105
|
+
const onChunk = (data) => push({
|
|
106
|
+
type: "chunk",
|
|
107
|
+
data
|
|
108
|
+
});
|
|
109
|
+
const onComplete = (data) => {
|
|
110
|
+
push({
|
|
111
|
+
type: "complete",
|
|
112
|
+
data
|
|
113
|
+
});
|
|
114
|
+
done = true;
|
|
115
|
+
resolve?.();
|
|
116
|
+
};
|
|
117
|
+
const onError = (data) => {
|
|
118
|
+
push({
|
|
119
|
+
type: "error",
|
|
120
|
+
data
|
|
121
|
+
});
|
|
122
|
+
done = true;
|
|
123
|
+
resolve?.();
|
|
124
|
+
};
|
|
125
|
+
const onTool = (data) => push({
|
|
126
|
+
type: "toolCall",
|
|
127
|
+
data
|
|
128
|
+
});
|
|
129
|
+
client.on("message:start", onStart);
|
|
130
|
+
client.on("message:chunk", onChunk);
|
|
131
|
+
client.on("message:complete", onComplete);
|
|
132
|
+
client.on("message:error", onError);
|
|
133
|
+
client.on("toolCall:received", onTool);
|
|
134
|
+
client.sendMessage(content, options).catch(() => {
|
|
135
|
+
done = true;
|
|
136
|
+
resolve?.();
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
while (!done || queue.length > 0) if (queue.length > 0) yield queue.shift();
|
|
140
|
+
else {
|
|
141
|
+
await new Promise((r) => {
|
|
142
|
+
resolve = r;
|
|
143
|
+
});
|
|
144
|
+
resolve = null;
|
|
145
|
+
}
|
|
146
|
+
} finally {
|
|
147
|
+
client.off("message:start", onStart);
|
|
148
|
+
client.off("message:chunk", onChunk);
|
|
149
|
+
client.off("message:complete", onComplete);
|
|
150
|
+
client.off("message:error", onError);
|
|
151
|
+
client.off("toolCall:received", onTool);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/auto/createAgo.ts
|
|
156
|
+
/**
|
|
157
|
+
* Auto-detect AGO configuration from the DOM environment.
|
|
158
|
+
*
|
|
159
|
+
* Checks (in order of priority):
|
|
160
|
+
* 1. `window.AGO` global object (widget config)
|
|
161
|
+
* 2. `<meta>` tags: `<meta name="ago-api-key" content="...">`
|
|
162
|
+
* 3. `data-ago-*` attributes on `<body>` or `<script data-ago-api-key="...">`
|
|
163
|
+
* 4. Explicit overrides passed as argument
|
|
164
|
+
*
|
|
165
|
+
* Returns null if no API key can be found.
|
|
166
|
+
*/
|
|
167
|
+
function autoDetectConfig(overrides) {
|
|
168
|
+
let baseUrl;
|
|
169
|
+
let widgetId;
|
|
170
|
+
let agent;
|
|
171
|
+
let permission;
|
|
172
|
+
let userEmail;
|
|
173
|
+
let userJwt;
|
|
174
|
+
if (typeof window !== "undefined" && window.AGO) {
|
|
175
|
+
const ago = window.AGO;
|
|
176
|
+
baseUrl = ago.basepath;
|
|
177
|
+
widgetId = ago.widgetId;
|
|
178
|
+
agent = ago.agent || ago.defaultAgent;
|
|
179
|
+
permission = ago.permission;
|
|
180
|
+
userEmail = ago.email;
|
|
181
|
+
userJwt = ago.jwt;
|
|
182
|
+
}
|
|
183
|
+
if (typeof document !== "undefined") {
|
|
184
|
+
const getMeta = (name) => document.querySelector(`meta[name="${name}"]`)?.getAttribute("content") || void 0;
|
|
185
|
+
baseUrl = baseUrl || getMeta("ago-base-url");
|
|
186
|
+
widgetId = widgetId || getMeta("ago-widget-id");
|
|
187
|
+
agent = agent || getMeta("ago-agent") || getMeta("ago-agent-id");
|
|
188
|
+
permission = permission || getMeta("ago-permission");
|
|
189
|
+
userEmail = userEmail || getMeta("ago-user-email");
|
|
190
|
+
}
|
|
191
|
+
if (typeof document !== "undefined") {
|
|
192
|
+
const sources = [document.body, ...Array.from(document.querySelectorAll("script[data-ago-base-url]"))].filter(Boolean);
|
|
193
|
+
for (const el of sources) {
|
|
194
|
+
if (!el) continue;
|
|
195
|
+
baseUrl = baseUrl || el.getAttribute("data-ago-base-url") || void 0;
|
|
196
|
+
widgetId = widgetId || el.getAttribute("data-ago-widget-id") || void 0;
|
|
197
|
+
agent = agent || el.getAttribute("data-ago-agent") || el.getAttribute("data-ago-agent-id") || void 0;
|
|
198
|
+
permission = permission || el.getAttribute("data-ago-permission") || void 0;
|
|
199
|
+
userEmail = userEmail || el.getAttribute("data-ago-user-email") || void 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
baseUrl = overrides?.baseUrl || baseUrl;
|
|
203
|
+
widgetId = overrides?.widgetId || widgetId;
|
|
204
|
+
agent = overrides?.agent || overrides?.defaultAgentId || agent;
|
|
205
|
+
permission = overrides?.permission || permission;
|
|
206
|
+
userEmail = overrides?.userEmail || userEmail;
|
|
207
|
+
userJwt = overrides?.userJwt || userJwt;
|
|
208
|
+
const debug = overrides?.debug;
|
|
209
|
+
if (!baseUrl) return null;
|
|
210
|
+
return {
|
|
211
|
+
baseUrl,
|
|
212
|
+
widgetId,
|
|
213
|
+
agent,
|
|
214
|
+
permission,
|
|
215
|
+
userEmail,
|
|
216
|
+
userJwt,
|
|
217
|
+
debug
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Create an AgoClient with zero configuration.
|
|
222
|
+
* Auto-detects config from window.AGO, meta tags, and data-ago-* attributes.
|
|
223
|
+
*
|
|
224
|
+
* ```html
|
|
225
|
+
* <!-- In your HTML -->
|
|
226
|
+
* <meta name="ago-base-url" content="https://YOUR-DOMAIN.useago.com">
|
|
227
|
+
*
|
|
228
|
+
* <!-- Or on a script tag -->
|
|
229
|
+
* <script src="app.js" data-ago-base-url="https://YOUR-DOMAIN.useago.com"><\/script>
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* ```ts
|
|
233
|
+
* import { createAgo } from "@useago/sdk";
|
|
234
|
+
*
|
|
235
|
+
* // Zero config — picks up from DOM
|
|
236
|
+
* const client = createAgo();
|
|
237
|
+
*
|
|
238
|
+
* // Or with partial overrides
|
|
239
|
+
* const client = createAgo({ debug: true });
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* Throws if no base URL can be detected.
|
|
243
|
+
*/
|
|
244
|
+
function createAgo(overrides) {
|
|
245
|
+
const config = autoDetectConfig(overrides);
|
|
246
|
+
if (!config) throw new Error("createAgo(): could not detect AGO configuration. Set window.AGO, add <meta name=\"ago-base-url\">, or pass { baseUrl } explicitly.");
|
|
247
|
+
return new AgoClient(config);
|
|
248
|
+
}
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/helpers/factory.ts
|
|
251
|
+
/**
|
|
252
|
+
* Attach a handler to a pre-built function definition.
|
|
253
|
+
*
|
|
254
|
+
* Pre-built helpers (`showToast`, `trackEvent`, ...) ship with a no-op or
|
|
255
|
+
* console-logging default handler. Use `withHandler` to wire them up to
|
|
256
|
+
* your actual implementation without mutating the original definition.
|
|
257
|
+
*
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { showToast, withHandler } from "@useago/sdk";
|
|
260
|
+
*
|
|
261
|
+
* client.register(
|
|
262
|
+
* withHandler(showToast, (args) => {
|
|
263
|
+
* myToast(args.message as string);
|
|
264
|
+
* return { shown: true };
|
|
265
|
+
* })
|
|
266
|
+
* );
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
function withHandler(definition, handler) {
|
|
270
|
+
return {
|
|
271
|
+
...definition,
|
|
272
|
+
handler
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
export { AgoApiError, AgoClient, AgoError, AgoFunctionError, AgoNetworkError, AgoStreamError, ClientContextRegistry, EventEmitter, FunctionRegistry, SSEHandler, autoDetectConfig, copyToClipboard, createAgo, createMessageStream, createMockClient, defineFunction, getLocalStorage, getUserLocation, highlightElement, isStreamNetworkError, logger, onFunctionInvoke, onMessage, onMessageChunk, onMessageError, onMessageStart, onToolCall, openUrl, scrollToElement, setLocalStorage, setTheme, showConfirmDialog, showNotification, showToast, submitForm, trackEvent, withHandler };
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=index.js.map
|