@useago/sdk 0.1.6 → 0.1.7
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/{createMockClient-BZKh_1em.cjs → AgoClient-BDO4avLq.cjs} +219 -124
- package/dist/AgoClient-BDO4avLq.cjs.map +1 -0
- package/dist/{createMockClient-uGlVyjbL.js → AgoClient-D-c91tx5.js} +221 -126
- package/dist/AgoClient-D-c91tx5.js.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 +105 -0
- package/dist/angular.cjs.map +1 -0
- package/dist/angular.d.ts +1 -0
- package/dist/angular.js +105 -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 +4 -6
- package/dist/createMockClient-B1DcBiIK.js +94 -0
- package/dist/createMockClient-B1DcBiIK.js.map +1 -0
- package/dist/createMockClient-BqNSJUu4.cjs +93 -0
- package/dist/createMockClient-BqNSJUu4.cjs.map +1 -0
- package/dist/functions-B0Z0rNQW.cjs +306 -0
- package/dist/functions-B0Z0rNQW.cjs.map +1 -0
- package/dist/functions-C-wLEc8b.js +306 -0
- package/dist/functions-C-wLEc8b.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 +17 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.js +17 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +179 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +173 -5
- package/dist/index.js.map +1 -1
- 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 +2 -1
- package/dist/react.cjs +76 -11
- package/dist/react.cjs.map +1 -1
- package/dist/react.js +81 -17
- package/dist/react.js.map +1 -1
- 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 +232 -0
- package/dist/vue.cjs.map +1 -0
- package/dist/vue.d.ts +1 -0
- package/dist/vue.js +232 -0
- package/dist/vue.js.map +1 -0
- package/dist/widget/types.d.ts +1 -0
- package/package.json +23 -3
- package/dist/createMockClient-BZKh_1em.cjs.map +0 -1
- package/dist/createMockClient-uGlVyjbL.js.map +0 -1
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","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 defaultAgentId: string | undefined;\n let userEmail: string | undefined;\n let userJwt: string | undefined;\n let debug: boolean | 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 defaultAgentId = ago.defaultAgent 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 defaultAgentId = defaultAgentId || getMeta(\"ago-agent-id\");\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 defaultAgentId = defaultAgentId || el.getAttribute(\"data-ago-agent-id\") || 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 defaultAgentId = overrides?.defaultAgentId || defaultAgentId;\n userEmail = overrides?.userEmail || userEmail;\n userJwt = overrides?.userJwt || userJwt;\n debug = overrides?.debug ?? debug;\n\n if (!baseUrl) {\n return null;\n }\n\n return {\n baseUrl,\n widgetId,\n defaultAgentId,\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"],"names":["AgoClient"],"mappings":";;;;;AAsBO,SAAS,eACd,YAC0B;AAC1B,SAAO;AACT;ACVO,SAAS,UACd,QACA,UACY;AACZ,SAAO,GAAG,oBAAoB,QAAQ;AACtC,SAAO,MAAM,OAAO,IAAI,oBAAoB,QAAQ;AACtD;AAWO,SAAS,eACd,QACA,UACY;AACZ,SAAO,GAAG,iBAAiB,QAAQ;AACnC,SAAO,MAAM,OAAO,IAAI,iBAAiB,QAAQ;AACnD;AAKO,SAAS,eACd,QACA,UACY;AACZ,SAAO,GAAG,iBAAiB,QAAQ;AACnC,SAAO,MAAM,OAAO,IAAI,iBAAiB,QAAQ;AACnD;AAKO,SAAS,eACd,QACA,UACY;AACZ,SAAO,GAAG,iBAAiB,QAAQ;AACnC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AGA,aAAU,uCAAW,YAAW;AAChC,cAAW,uCAAW,aAAY;AAClC,oBAAiB,uCAAW,mBAAkB;AAC9C,eAAY,uCAAW,cAAa;AACpC,aAAU,uCAAW,YAAW;AAChC,WAAQ,uCAAW,UAAS;AAE5B,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;AA0BO,SAAS,UAAU,WAA2C;AACnE,QAAM,SAAS,iBAAiB,SAAS;AAEzC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAGJ;AAEA,SAAO,IAAIA,UAAAA,UAAU,MAAM;AAC7B;AC/FO,SAAS,YACd,YACA,SACG;AACH,SAAO,EAAE,GAAG,YAAY,QAAA;AAC1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
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,188 @@
|
|
|
1
|
-
import { A
|
|
1
|
+
import { A as AgoClient } from "./AgoClient-D-c91tx5.js";
|
|
2
|
+
import { a, b, c, d, e, C, E, F, S, i, l } from "./AgoClient-D-c91tx5.js";
|
|
3
|
+
import { c as c2 } from "./createMockClient-B1DcBiIK.js";
|
|
4
|
+
import { copyToClipboard, getLocalStorage, getUserLocation, highlightElement, openUrl, scrollToElement, setLocalStorage, setTheme, showConfirmDialog, showNotification, showToast, submitForm, trackEvent } from "./functions-C-wLEc8b.js";
|
|
2
5
|
function defineFunction(definition) {
|
|
3
6
|
return definition;
|
|
4
7
|
}
|
|
8
|
+
function onMessage(client, callback) {
|
|
9
|
+
client.on("message:complete", callback);
|
|
10
|
+
return () => client.off("message:complete", callback);
|
|
11
|
+
}
|
|
12
|
+
function onMessageChunk(client, callback) {
|
|
13
|
+
client.on("message:chunk", callback);
|
|
14
|
+
return () => client.off("message:chunk", callback);
|
|
15
|
+
}
|
|
16
|
+
function onMessageStart(client, callback) {
|
|
17
|
+
client.on("message:start", callback);
|
|
18
|
+
return () => client.off("message:start", callback);
|
|
19
|
+
}
|
|
20
|
+
function onMessageError(client, callback) {
|
|
21
|
+
client.on("message:error", callback);
|
|
22
|
+
return () => client.off("message:error", callback);
|
|
23
|
+
}
|
|
24
|
+
function onToolCall(client, callback) {
|
|
25
|
+
client.on("toolCall:received", callback);
|
|
26
|
+
return () => client.off("toolCall:received", callback);
|
|
27
|
+
}
|
|
28
|
+
function onFunctionInvoke(client, callback) {
|
|
29
|
+
client.on("function:invoke", callback);
|
|
30
|
+
return () => client.off("function:invoke", callback);
|
|
31
|
+
}
|
|
32
|
+
async function* createMessageStream(client, content, options) {
|
|
33
|
+
const queue = [];
|
|
34
|
+
let resolve = null;
|
|
35
|
+
let done = false;
|
|
36
|
+
const push = (event) => {
|
|
37
|
+
queue.push(event);
|
|
38
|
+
resolve == null ? void 0 : resolve();
|
|
39
|
+
};
|
|
40
|
+
const onStart = (data) => push({ type: "start", data });
|
|
41
|
+
const onChunk = (data) => push({ type: "chunk", data });
|
|
42
|
+
const onComplete = (data) => {
|
|
43
|
+
push({ type: "complete", data });
|
|
44
|
+
done = true;
|
|
45
|
+
resolve == null ? void 0 : resolve();
|
|
46
|
+
};
|
|
47
|
+
const onError = (data) => {
|
|
48
|
+
push({ type: "error", data });
|
|
49
|
+
done = true;
|
|
50
|
+
resolve == null ? void 0 : resolve();
|
|
51
|
+
};
|
|
52
|
+
const onTool = (data) => push({ type: "toolCall", data });
|
|
53
|
+
client.on("message:start", onStart);
|
|
54
|
+
client.on("message:chunk", onChunk);
|
|
55
|
+
client.on("message:complete", onComplete);
|
|
56
|
+
client.on("message:error", onError);
|
|
57
|
+
client.on("toolCall:received", onTool);
|
|
58
|
+
client.sendMessage(content, options).catch(() => {
|
|
59
|
+
done = true;
|
|
60
|
+
resolve == null ? void 0 : resolve();
|
|
61
|
+
});
|
|
62
|
+
try {
|
|
63
|
+
while (!done || queue.length > 0) {
|
|
64
|
+
if (queue.length > 0) {
|
|
65
|
+
yield queue.shift();
|
|
66
|
+
} else {
|
|
67
|
+
await new Promise((r) => {
|
|
68
|
+
resolve = r;
|
|
69
|
+
});
|
|
70
|
+
resolve = null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
} finally {
|
|
74
|
+
client.off("message:start", onStart);
|
|
75
|
+
client.off("message:chunk", onChunk);
|
|
76
|
+
client.off("message:complete", onComplete);
|
|
77
|
+
client.off("message:error", onError);
|
|
78
|
+
client.off("toolCall:received", onTool);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function autoDetectConfig(overrides) {
|
|
82
|
+
let baseUrl;
|
|
83
|
+
let widgetId;
|
|
84
|
+
let defaultAgentId;
|
|
85
|
+
let userEmail;
|
|
86
|
+
let userJwt;
|
|
87
|
+
let debug;
|
|
88
|
+
if (typeof window !== "undefined" && window.AGO) {
|
|
89
|
+
const ago = window.AGO;
|
|
90
|
+
baseUrl = ago.basepath;
|
|
91
|
+
widgetId = ago.widgetId;
|
|
92
|
+
defaultAgentId = ago.defaultAgent;
|
|
93
|
+
userEmail = ago.email;
|
|
94
|
+
userJwt = ago.jwt;
|
|
95
|
+
}
|
|
96
|
+
if (typeof document !== "undefined") {
|
|
97
|
+
const getMeta = (name) => {
|
|
98
|
+
var _a;
|
|
99
|
+
return ((_a = document.querySelector(`meta[name="${name}"]`)) == null ? void 0 : _a.getAttribute("content")) || void 0;
|
|
100
|
+
};
|
|
101
|
+
baseUrl = baseUrl || getMeta("ago-base-url");
|
|
102
|
+
widgetId = widgetId || getMeta("ago-widget-id");
|
|
103
|
+
defaultAgentId = defaultAgentId || getMeta("ago-agent-id");
|
|
104
|
+
userEmail = userEmail || getMeta("ago-user-email");
|
|
105
|
+
}
|
|
106
|
+
if (typeof document !== "undefined") {
|
|
107
|
+
const sources = [
|
|
108
|
+
document.body,
|
|
109
|
+
...Array.from(document.querySelectorAll("script[data-ago-base-url]"))
|
|
110
|
+
].filter(Boolean);
|
|
111
|
+
for (const el of sources) {
|
|
112
|
+
if (!el) continue;
|
|
113
|
+
baseUrl = baseUrl || el.getAttribute("data-ago-base-url") || void 0;
|
|
114
|
+
widgetId = widgetId || el.getAttribute("data-ago-widget-id") || void 0;
|
|
115
|
+
defaultAgentId = defaultAgentId || el.getAttribute("data-ago-agent-id") || void 0;
|
|
116
|
+
userEmail = userEmail || el.getAttribute("data-ago-user-email") || void 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
baseUrl = (overrides == null ? void 0 : overrides.baseUrl) || baseUrl;
|
|
120
|
+
widgetId = (overrides == null ? void 0 : overrides.widgetId) || widgetId;
|
|
121
|
+
defaultAgentId = (overrides == null ? void 0 : overrides.defaultAgentId) || defaultAgentId;
|
|
122
|
+
userEmail = (overrides == null ? void 0 : overrides.userEmail) || userEmail;
|
|
123
|
+
userJwt = (overrides == null ? void 0 : overrides.userJwt) || userJwt;
|
|
124
|
+
debug = (overrides == null ? void 0 : overrides.debug) ?? debug;
|
|
125
|
+
if (!baseUrl) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
baseUrl,
|
|
130
|
+
widgetId,
|
|
131
|
+
defaultAgentId,
|
|
132
|
+
userEmail,
|
|
133
|
+
userJwt,
|
|
134
|
+
debug
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function createAgo(overrides) {
|
|
138
|
+
const config = autoDetectConfig(overrides);
|
|
139
|
+
if (!config) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
'createAgo(): could not detect AGO configuration. Set window.AGO, add <meta name="ago-base-url">, or pass { baseUrl } explicitly.'
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return new AgoClient(config);
|
|
145
|
+
}
|
|
146
|
+
function withHandler(definition, handler) {
|
|
147
|
+
return { ...definition, handler };
|
|
148
|
+
}
|
|
5
149
|
export {
|
|
6
|
-
|
|
7
|
-
|
|
150
|
+
a as AgoApiError,
|
|
151
|
+
AgoClient,
|
|
8
152
|
b as AgoError,
|
|
9
153
|
c as AgoFunctionError,
|
|
10
154
|
d as AgoNetworkError,
|
|
11
155
|
e as AgoStreamError,
|
|
156
|
+
C as ClientContextRegistry,
|
|
12
157
|
E as EventEmitter,
|
|
13
158
|
F as FunctionRegistry,
|
|
14
159
|
S as SSEHandler,
|
|
15
|
-
|
|
160
|
+
autoDetectConfig,
|
|
161
|
+
copyToClipboard,
|
|
162
|
+
createAgo,
|
|
163
|
+
createMessageStream,
|
|
164
|
+
c2 as createMockClient,
|
|
16
165
|
defineFunction,
|
|
166
|
+
getLocalStorage,
|
|
167
|
+
getUserLocation,
|
|
168
|
+
highlightElement,
|
|
17
169
|
i as isStreamNetworkError,
|
|
18
|
-
l as logger
|
|
170
|
+
l as logger,
|
|
171
|
+
onFunctionInvoke,
|
|
172
|
+
onMessage,
|
|
173
|
+
onMessageChunk,
|
|
174
|
+
onMessageError,
|
|
175
|
+
onMessageStart,
|
|
176
|
+
onToolCall,
|
|
177
|
+
openUrl,
|
|
178
|
+
scrollToElement,
|
|
179
|
+
setLocalStorage,
|
|
180
|
+
setTheme,
|
|
181
|
+
showConfirmDialog,
|
|
182
|
+
showNotification,
|
|
183
|
+
showToast,
|
|
184
|
+
submitForm,
|
|
185
|
+
trackEvent,
|
|
186
|
+
withHandler
|
|
19
187
|
};
|
|
20
188
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","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.js","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 defaultAgentId: string | undefined;\n let userEmail: string | undefined;\n let userJwt: string | undefined;\n let debug: boolean | 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 defaultAgentId = ago.defaultAgent 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 defaultAgentId = defaultAgentId || getMeta(\"ago-agent-id\");\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 defaultAgentId = defaultAgentId || el.getAttribute(\"data-ago-agent-id\") || 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 defaultAgentId = overrides?.defaultAgentId || defaultAgentId;\n userEmail = overrides?.userEmail || userEmail;\n userJwt = overrides?.userJwt || userJwt;\n debug = overrides?.debug ?? debug;\n\n if (!baseUrl) {\n return null;\n }\n\n return {\n baseUrl,\n widgetId,\n defaultAgentId,\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"],"names":[],"mappings":";;;;AAsBO,SAAS,eACd,YAC0B;AAC1B,SAAO;AACT;ACVO,SAAS,UACd,QACA,UACY;AACZ,SAAO,GAAG,oBAAoB,QAAQ;AACtC,SAAO,MAAM,OAAO,IAAI,oBAAoB,QAAQ;AACtD;AAWO,SAAS,eACd,QACA,UACY;AACZ,SAAO,GAAG,iBAAiB,QAAQ;AACnC,SAAO,MAAM,OAAO,IAAI,iBAAiB,QAAQ;AACnD;AAKO,SAAS,eACd,QACA,UACY;AACZ,SAAO,GAAG,iBAAiB,QAAQ;AACnC,SAAO,MAAM,OAAO,IAAI,iBAAiB,QAAQ;AACnD;AAKO,SAAS,eACd,QACA,UAC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a,qBAAqB,KAAK;AAAA,IACrE;AAAA,EACF;AAGA,aAAU,uCAAW,YAAW;AAChC,cAAW,uCAAW,aAAY;AAClC,oBAAiB,uCAAW,mBAAkB;AAC9C,eAAY,uCAAW,cAAa;AACpC,aAAU,uCAAW,YAAW;AAChC,WAAQ,uCAAW,UAAS;AAE5B,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;AA0BO,SAAS,UAAU,WAA2C;AACnE,QAAM,SAAS,iBAAiB,SAAS;AAEzC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAGJ;AAEA,SAAO,IAAI,UAAU,MAAM;AAC7B;AC/FO,SAAS,YACd,YACA,SACG;AACH,SAAO,EAAE,GAAG,YAAY,QAAA;AAC1B;"}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { AgoClient } from "../../client/AgoClient";
|
|
3
3
|
import type { AgoConfig } from "../../client/types";
|
|
4
|
+
import type { ClientFunctionDefinition, ClientFunctionHandler } from "../../functions/types";
|
|
5
|
+
/**
|
|
6
|
+
* Top-level declarative configuration for the `AgoProvider`.
|
|
7
|
+
*
|
|
8
|
+
* - `tools` — full function definitions to expose to the AI app-wide
|
|
9
|
+
* - `helpers` — map of pre-built helper name → handler override (or `true` for default)
|
|
10
|
+
* - `pageContext` — `"auto"` enables automatic URL/title capture
|
|
11
|
+
*/
|
|
12
|
+
export interface AgoDeclarativeConfig {
|
|
13
|
+
tools?: ClientFunctionDefinition[];
|
|
14
|
+
helpers?: Record<string, ClientFunctionHandler | true>;
|
|
15
|
+
pageContext?: "auto" | "manual";
|
|
16
|
+
}
|
|
4
17
|
export type AgoProviderProps = {
|
|
5
18
|
children: React.ReactNode;
|
|
6
|
-
} & (({
|
|
19
|
+
} & AgoDeclarativeConfig & (({
|
|
7
20
|
client: AgoClient;
|
|
8
21
|
} & Partial<Record<keyof AgoConfig, never>>) | ({
|
|
9
22
|
client?: never;
|
|
@@ -12,12 +25,25 @@ export type AgoProviderProps = {
|
|
|
12
25
|
* Provides an AgoClient to all descendant components via React context.
|
|
13
26
|
*
|
|
14
27
|
* ```tsx
|
|
15
|
-
* //
|
|
16
|
-
* <AgoProvider
|
|
28
|
+
* // Minimal setup
|
|
29
|
+
* <AgoProvider baseUrl="https://YOUR-DOMAIN.useago.com">
|
|
30
|
+
* <App />
|
|
31
|
+
* </AgoProvider>
|
|
32
|
+
*
|
|
33
|
+
* // Declarative config — tools, helpers, page context
|
|
34
|
+
* <AgoProvider
|
|
35
|
+
* baseUrl="https://YOUR-DOMAIN.useago.com"
|
|
36
|
+
* tools={[lookupOrder, cancelOrder]}
|
|
37
|
+
* helpers={{
|
|
38
|
+
* showToast: (args) => toast(args.message as string),
|
|
39
|
+
* copyToClipboard: true, // use default handler
|
|
40
|
+
* }}
|
|
41
|
+
* pageContext="auto"
|
|
42
|
+
* >
|
|
17
43
|
* <App />
|
|
18
44
|
* </AgoProvider>
|
|
19
45
|
*
|
|
20
|
-
* //
|
|
46
|
+
* // With a pre-built client (useful for testing)
|
|
21
47
|
* <AgoProvider client={myClient}>
|
|
22
48
|
* <App />
|
|
23
49
|
* </AgoProvider>
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { AgoProvider, useAgoClient, useOptionalAgoClient } from "./AgoContext";
|
|
2
|
-
export type { AgoProviderProps } from "./AgoContext";
|
|
2
|
+
export type { AgoProviderProps, AgoDeclarativeConfig } from "./AgoContext";
|
|
@@ -4,3 +4,4 @@ export { useMessages } from "./useMessages";
|
|
|
4
4
|
export type { UseMessagesOptions, UseMessagesResult } from "./useMessages";
|
|
5
5
|
export { useConversation } from "./useConversation";
|
|
6
6
|
export type { UseConversationOptions, UseConversationResult } from "./useConversation";
|
|
7
|
+
export { useAgoContext } from "./useAgoContext";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ContextEntry, DynamicContextProvider } from "../../state/ClientContextRegistry";
|
|
2
|
+
/**
|
|
3
|
+
* Declaratively expose a piece of context to the AI agent with every message.
|
|
4
|
+
*
|
|
5
|
+
* Accepts either:
|
|
6
|
+
* - An **object** — sent as-is on every message (re-registered when it changes).
|
|
7
|
+
* Use for data that already lives in React state.
|
|
8
|
+
* - A **function** — called lazily at each message send, so the AI always gets
|
|
9
|
+
* the freshest value. Use for data outside React state (global stores, refs,
|
|
10
|
+
* computed values).
|
|
11
|
+
*
|
|
12
|
+
* A unique key is auto-generated per component instance via `useId()`. Pass an
|
|
13
|
+
* explicit key as second argument to share context across components or
|
|
14
|
+
* reference it from elsewhere.
|
|
15
|
+
*
|
|
16
|
+
* ```tsx
|
|
17
|
+
* // Static object — captured at render time
|
|
18
|
+
* function OrderPage({ order }) {
|
|
19
|
+
* useAgoContext({
|
|
20
|
+
* name: "Order detail",
|
|
21
|
+
* description: "The user is viewing a specific order",
|
|
22
|
+
* data: { orderId: order.id, status: order.status },
|
|
23
|
+
* });
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* // Dynamic function — evaluated at each sendMessage
|
|
27
|
+
* function App() {
|
|
28
|
+
* const storeRef = useRef(reduxStore);
|
|
29
|
+
* useAgoContext(() => {
|
|
30
|
+
* const state = storeRef.current.getState();
|
|
31
|
+
* return { name: "App shell", data: { userId: state.auth.userId } };
|
|
32
|
+
* });
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // With an explicit key
|
|
36
|
+
* useAgoContext({ name: "Sidebar filter", data: { filter } }, "sidebar-filter");
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useAgoContext(entry: ContextEntry, key?: string): void;
|
|
40
|
+
export declare function useAgoContext(provider: DynamicContextProvider, key?: string): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClientFunctionHandler } from "../../functions/types";
|
|
1
|
+
import type { ClientFunctionDefinition, ClientFunctionHandler } from "../../functions/types";
|
|
2
2
|
export interface UseAgoFunctionOptions {
|
|
3
3
|
description: string;
|
|
4
4
|
parameters: {
|
|
@@ -17,14 +17,26 @@ export interface UseAgoFunctionOptions {
|
|
|
17
17
|
* Declaratively register a client-side function with the AGO agent.
|
|
18
18
|
* Automatically unregisters on unmount and re-registers when deps change.
|
|
19
19
|
*
|
|
20
|
+
* Accepts either a full definition object (preferred) or classic (name, options) args.
|
|
21
|
+
*
|
|
20
22
|
* ```tsx
|
|
21
|
-
*
|
|
23
|
+
* // Preferred — pass the whole definition
|
|
24
|
+
* useAgoFunction({
|
|
25
|
+
* name: "showToast",
|
|
22
26
|
* description: "Show a notification to the user",
|
|
23
27
|
* parameters: { type: "object", properties: { message: { type: "string" } } },
|
|
24
28
|
* handler: async (args) => { toast(args.message); return { shown: true }; },
|
|
25
29
|
* });
|
|
30
|
+
*
|
|
31
|
+
* // Or reuse a pre-defined function
|
|
32
|
+
* const lookupOrder = defineFunction({ name: "lookupOrder", ... });
|
|
33
|
+
* useAgoFunction(lookupOrder);
|
|
34
|
+
*
|
|
35
|
+
* // Classic form
|
|
36
|
+
* useAgoFunction("showToast", { description: "...", parameters: {...}, handler: async (args) => ... });
|
|
26
37
|
* ```
|
|
27
38
|
*/
|
|
39
|
+
export declare function useAgoFunction(definition: ClientFunctionDefinition): void;
|
|
28
40
|
export declare function useAgoFunction(name: string, options: UseAgoFunctionOptions): void;
|
|
29
41
|
export interface AgoRoute {
|
|
30
42
|
name: string;
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { AgoProvider, useAgoClient, useOptionalAgoClient } from "./context";
|
|
2
|
-
export type { AgoProviderProps } from "./context";
|
|
2
|
+
export type { AgoProviderProps, AgoDeclarativeConfig } from "./context/AgoContext";
|
|
3
3
|
export { ChatWidget } from "./components/ChatWidget";
|
|
4
4
|
export type { ChatWidgetProps } from "./components/ChatWidget";
|
|
5
5
|
export { Message } from "./components/Message";
|
|
@@ -16,5 +16,6 @@ export { useChat } from "./hooks/useChat";
|
|
|
16
16
|
export type { UseChatOptions, UseChatResult } from "./hooks/useChat";
|
|
17
17
|
export { useAgoFunction, useAgoNavigation } from "./hooks/useAgoFunction";
|
|
18
18
|
export type { UseAgoFunctionOptions, AgoRoute } from "./hooks/useAgoFunction";
|
|
19
|
+
export { useAgoContext } from "./hooks/useAgoContext";
|
|
19
20
|
export { createMockClient } from "./testing";
|
|
20
21
|
export type { AgoConfig, AgoMessage, Conversation, AgoAgent, AgoSource, ToolCallData, } from "../client/types";
|
package/dist/react.cjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const jsxRuntime = require("react/jsx-runtime");
|
|
4
4
|
const react = require("react");
|
|
5
|
-
const
|
|
5
|
+
const AgoClient = require("./AgoClient-BDO4avLq.cjs");
|
|
6
|
+
const createMockClient = require("./createMockClient-BqNSJUu4.cjs");
|
|
6
7
|
function useAgo(config) {
|
|
7
8
|
const [isReady, setIsReady] = react.useState(false);
|
|
8
9
|
const clientRef = react.useRef(null);
|
|
@@ -10,10 +11,10 @@ function useAgo(config) {
|
|
|
10
11
|
if (clientRef.current) {
|
|
11
12
|
clientRef.current.destroy();
|
|
12
13
|
}
|
|
13
|
-
const newClient = new
|
|
14
|
+
const newClient = new AgoClient.AgoClient(config);
|
|
14
15
|
clientRef.current = newClient;
|
|
15
16
|
return newClient;
|
|
16
|
-
}, [config.
|
|
17
|
+
}, [config.baseUrl, config.widgetId]);
|
|
17
18
|
react.useEffect(() => {
|
|
18
19
|
setIsReady(true);
|
|
19
20
|
return () => {
|
|
@@ -41,19 +42,59 @@ const AgoContext = react.createContext(null);
|
|
|
41
42
|
const AgoProvider = ({
|
|
42
43
|
children,
|
|
43
44
|
client: externalClient,
|
|
45
|
+
tools,
|
|
46
|
+
helpers,
|
|
47
|
+
pageContext,
|
|
44
48
|
...config
|
|
45
49
|
}) => {
|
|
46
50
|
const { client: internalClient } = useAgo(
|
|
47
|
-
externalClient ? {
|
|
51
|
+
externalClient ? { baseUrl: "" } : config
|
|
48
52
|
);
|
|
49
53
|
const client = externalClient || internalClient;
|
|
54
|
+
react.useEffect(() => {
|
|
55
|
+
if (!tools || tools.length === 0) return;
|
|
56
|
+
for (const tool of tools) {
|
|
57
|
+
client.register(tool);
|
|
58
|
+
}
|
|
59
|
+
return () => {
|
|
60
|
+
for (const tool of tools) {
|
|
61
|
+
client.unregisterFunction(tool.name);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}, [client, tools]);
|
|
65
|
+
react.useEffect(() => {
|
|
66
|
+
if (!helpers) return;
|
|
67
|
+
const registered = [];
|
|
68
|
+
Promise.resolve().then(() => require("./functions-B0Z0rNQW.cjs")).then((mod) => {
|
|
69
|
+
for (const [name2, handlerOrTrue] of Object.entries(helpers)) {
|
|
70
|
+
const helper = mod[name2];
|
|
71
|
+
if (!helper) {
|
|
72
|
+
console.warn(`[AGO] Unknown helper: "${name2}"`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const definition2 = handlerOrTrue === true ? helper : { ...helper, handler: handlerOrTrue };
|
|
76
|
+
client.register(definition2);
|
|
77
|
+
registered.push(definition2.name);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return () => {
|
|
81
|
+
for (const name2 of registered) {
|
|
82
|
+
client.unregisterFunction(name2);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}, [client, helpers]);
|
|
86
|
+
react.useEffect(() => {
|
|
87
|
+
if (pageContext === "auto") {
|
|
88
|
+
client.enableAutoPageContext();
|
|
89
|
+
}
|
|
90
|
+
}, [client, pageContext]);
|
|
50
91
|
return /* @__PURE__ */ jsxRuntime.jsx(AgoContext.Provider, { value: client, children });
|
|
51
92
|
};
|
|
52
93
|
function useAgoClient() {
|
|
53
94
|
const client = react.useContext(AgoContext);
|
|
54
95
|
if (!client) {
|
|
55
96
|
throw new Error(
|
|
56
|
-
'useAgoClient must be used within an <AgoProvider>. Wrap your component tree with <AgoProvider
|
|
97
|
+
'useAgoClient must be used within an <AgoProvider>. Wrap your component tree with <AgoProvider baseUrl="...">.'
|
|
57
98
|
);
|
|
58
99
|
}
|
|
59
100
|
return client;
|
|
@@ -10540,20 +10581,21 @@ function useChat(options = {}) {
|
|
|
10540
10581
|
refreshConversations: conversationResult.refreshConversations
|
|
10541
10582
|
};
|
|
10542
10583
|
}
|
|
10543
|
-
function useAgoFunction(
|
|
10584
|
+
function useAgoFunction(nameOrDef, options) {
|
|
10544
10585
|
const client = useAgoClient();
|
|
10545
|
-
const
|
|
10546
|
-
handlerRef
|
|
10586
|
+
const { name: name2, description, parameters, handler } = typeof nameOrDef === "string" ? { name: nameOrDef, ...options } : nameOrDef;
|
|
10587
|
+
const handlerRef = react.useRef(handler);
|
|
10588
|
+
handlerRef.current = handler;
|
|
10547
10589
|
react.useEffect(() => {
|
|
10548
10590
|
const stableHandler = (args) => handlerRef.current(args);
|
|
10549
10591
|
client.registerFunction(name2, stableHandler, {
|
|
10550
|
-
description
|
|
10551
|
-
parameters
|
|
10592
|
+
description,
|
|
10593
|
+
parameters
|
|
10552
10594
|
});
|
|
10553
10595
|
return () => {
|
|
10554
10596
|
client.unregisterFunction(name2);
|
|
10555
10597
|
};
|
|
10556
|
-
}, [client, name2,
|
|
10598
|
+
}, [client, name2, description, parameters]);
|
|
10557
10599
|
}
|
|
10558
10600
|
function useAgoNavigation(navigate, routes) {
|
|
10559
10601
|
const client = useAgoClient();
|
|
@@ -10569,6 +10611,28 @@ function useAgoNavigation(navigate, routes) {
|
|
|
10569
10611
|
};
|
|
10570
10612
|
}, [client, routes]);
|
|
10571
10613
|
}
|
|
10614
|
+
function useAgoContext(entryOrProvider, key) {
|
|
10615
|
+
const client = useAgoClient();
|
|
10616
|
+
const autoKey = react.useId();
|
|
10617
|
+
const effectiveKey = key ?? autoKey;
|
|
10618
|
+
const providerRef = react.useRef(entryOrProvider);
|
|
10619
|
+
providerRef.current = entryOrProvider;
|
|
10620
|
+
const isDynamic = typeof entryOrProvider === "function";
|
|
10621
|
+
const serialized = isDynamic ? "" : JSON.stringify(entryOrProvider);
|
|
10622
|
+
react.useEffect(() => {
|
|
10623
|
+
if (typeof providerRef.current === "function") {
|
|
10624
|
+
const stableProvider = () => providerRef.current();
|
|
10625
|
+
client.addDynamicContext(effectiveKey, stableProvider);
|
|
10626
|
+
return () => {
|
|
10627
|
+
client.removeDynamicContext(effectiveKey);
|
|
10628
|
+
};
|
|
10629
|
+
}
|
|
10630
|
+
client.setContext(effectiveKey, providerRef.current);
|
|
10631
|
+
return () => {
|
|
10632
|
+
client.removeContext(effectiveKey);
|
|
10633
|
+
};
|
|
10634
|
+
}, [client, effectiveKey, isDynamic, serialized]);
|
|
10635
|
+
}
|
|
10572
10636
|
exports.createMockClient = createMockClient.createMockClient;
|
|
10573
10637
|
exports.AgoProvider = AgoProvider;
|
|
10574
10638
|
exports.ChatInput = ChatInput;
|
|
@@ -10576,6 +10640,7 @@ exports.ChatWidget = ChatWidget;
|
|
|
10576
10640
|
exports.Message = Message;
|
|
10577
10641
|
exports.useAgo = useAgo;
|
|
10578
10642
|
exports.useAgoClient = useAgoClient;
|
|
10643
|
+
exports.useAgoContext = useAgoContext;
|
|
10579
10644
|
exports.useAgoFunction = useAgoFunction;
|
|
10580
10645
|
exports.useAgoNavigation = useAgoNavigation;
|
|
10581
10646
|
exports.useChat = useChat;
|