@boldare/askdare 0.2.0 → 0.3.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/CHANGELOG.md +34 -0
- package/README.md +1 -1
- package/dist/{version.js → define-tool.js} +162 -15
- package/dist/index.js +4 -33
- package/dist/next.js +317 -179
- package/dist/react.d.ts +28 -4
- package/dist/react.js +124 -36
- package/dist/store.d.ts +31 -3
- package/dist/ui/askdare.css +67 -1
- package/dist/ui/avatar.tsx +1 -1
- package/dist/ui/chat.tsx +1 -1
- package/dist/ui/confirm.tsx +1 -1
- package/dist/ui/context.ts +1 -1
- package/dist/ui/feedback.tsx +1 -1
- package/dist/ui/footer.tsx +7 -2
- package/dist/ui/header.tsx +1 -1
- package/dist/ui/icons.tsx +1 -1
- package/dist/ui/index.ts +1 -1
- package/dist/ui/message.tsx +17 -3
- package/dist/ui/panel.tsx +1 -1
- package/dist/ui/welcome.tsx +1 -1
- package/docs/configuration.md +1 -0
- package/docs/costs.md +10 -1
- package/docs/embedding.md +4 -3
- package/docs/events.md +2 -2
- package/docs/feedback.md +116 -0
- package/docs/privacy.md +3 -3
- package/docs/protocol.md +13 -1
- package/docs/react.md +5 -2
- package/docs/reply-options.md +240 -0
- package/docs/styling.md +6 -3
- package/docs/texts.md +97 -0
- package/docs/themes.md +2 -1
- package/docs/tools.md +24 -0
- package/llms.txt +3 -1
- package/package.json +1 -1
- package/skills/askdare/SKILL.md +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @boldare/askdare
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- bca8d48: The chat can show clickable reply options under the latest answer ("Yes, book it", "Show prices"), in the widget and inside a page. A click sends the option as the visitor's own message. It adds a `replies` protocol event (additive in protocol version 1) with `replies` on the latest answer in the history, `ChatMessage.replies`, the `QuickReplies` primitive with the `replies` and `reply` parts, the theme variant `replies` (`answer` puts the options under the answer, `composer` puts them in one row above the message field), the `message.replies` text, and `source: 'reply'` on the `message.sent` chat event. Options are plain text: at most four, up to 80 characters each, without links. The model suggests options with `widget.replies: 'suggest'`, and a tool defines them with `defineTool({ replies })` (ADR-0015).
|
|
8
|
+
- 9fed4d3: A tool can offer clickable reply options derived from its result: `defineTool({ replies: (result) => ['Track the parcel', 'Talk to a person'] })`. `replies` gets the successful result as the model received it (parsed back from JSON) and is typed from `execute`; the options come back after a reload, from the stored call. The same limits as every reply option apply: at most four, up to 80 characters, plain text without links. Only the options reach the browser, never the tool's result (ADR-0015).
|
|
9
|
+
- 35ee326: The chat can switch language without a page load (ADR-0014, step 1). On a page with a bot set to `locale: 'auto'`, `askdare('configure', { locale: 'pl' })` or `data-follow-lang` on the script tag (which follows the page's `<html lang>`) switches the launcher's texts at once and reloads the chat in the new language. The visitor keeps their conversation; new answers follow the new language, as `context.locale` in your `instructions`. An answer still streaming during the switch is reported as `message.received` with `outcome: 'stopped'`, and the server still finishes and stores it. A bot with a fixed locale ignores the switch and says so once in the console. A `configure` call with only `locale` keeps the greeting, starters and context set before. In React, pass the new `locale` to `getChatConfig` as before.
|
|
10
|
+
- 7357c6f: Reply options suggested by the model (ADR-0015). With `widget.replies: 'suggest'`, a bot gets the built-in tool `suggest_replies`: the model can offer 2–4 short replies under an answer, which visitors click instead of typing. The server answers the call itself (no bot code runs), cleans the options up and sends them in the `replies` event; after a reload, the history brings back the options of the latest answer. When the model suggests them after its answer, the message ends without another model call; otherwise it takes one more. `suggest_replies` is now a reserved tool name. Off by default: it costs about 250 input tokens per model call, and sometimes a model call (docs/costs.md).
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 91e6826: The bundled docs no longer say that compaction comes in 0.3, or that `embedPath` and `toolRenderers` come in 0.3: compaction has no release yet, and the other two are planned for 0.4.
|
|
15
|
+
- d08bd22: The README's "What you get" picture now shows what 0.3 adds: clickable answers suggested by the model, reply options from your tools, switching the chat's language from the page, and the one-click deploy.
|
|
16
|
+
- 9cb8282: Reply options with characters the visitor can't see are dropped: zero-width and tag characters, direction marks, private-use and unassigned code points. A click sends an option as the visitor's own message, so it now always says exactly what they read. Emoji built with the zero-width joiner still work.
|
|
17
|
+
|
|
18
|
+
## 0.3.0-rc.1
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- d08bd22: The README's "What you get" picture now shows what 0.3 adds: clickable answers suggested by the model, reply options from your tools, switching the chat's language from the page, and the one-click deploy.
|
|
23
|
+
|
|
24
|
+
## 0.3.0-rc.0
|
|
25
|
+
|
|
26
|
+
### Minor Changes
|
|
27
|
+
|
|
28
|
+
- bca8d48: The chat can show clickable reply options under the latest answer ("Yes, book it", "Show prices"), in the widget and inside a page. A click sends the option as the visitor's own message. This release adds the groundwork: a `replies` protocol event (additive in protocol version 1) with `replies` on the latest answer in the history, `ChatMessage.replies`, the `QuickReplies` primitive with the `replies` and `reply` parts, the theme variant `replies` (`answer` puts the options under the answer, `composer` puts them in one row above the message field), the `message.replies` text, and `source: 'reply'` on the `message.sent` chat event. Options are plain text: at most four, up to 80 characters each, without links. Model-suggested and tool-defined options, which fill them, come in later releases (ADR-0015).
|
|
29
|
+
- 9fed4d3: A tool can offer clickable reply options derived from its result: `defineTool({ replies: (result) => ['Track the parcel', 'Talk to a person'] })`. `replies` gets the successful result as the model received it (parsed back from JSON) and is typed from `execute`; the options come back after a reload, from the stored call. The same limits as every reply option apply: at most four, up to 80 characters, plain text without links. Only the options reach the browser, never the tool's result (ADR-0015).
|
|
30
|
+
- 35ee326: The chat can switch language without a page load (ADR-0014, step 1). On a page with a bot set to `locale: 'auto'`, `askdare('configure', { locale: 'pl' })` or `data-follow-lang` on the script tag (which follows the page's `<html lang>`) switches the launcher's texts at once and reloads the chat in the new language. The visitor keeps their conversation; new answers follow the new language, as `context.locale` in your `instructions`. An answer still streaming during the switch is reported as `message.received` with `outcome: 'stopped'`, and the server still finishes and stores it. A bot with a fixed locale ignores the switch and says so once in the console. A `configure` call with only `locale` keeps the greeting, starters and context set before. In React, pass the new `locale` to `getChatConfig` as before.
|
|
31
|
+
- 7357c6f: Reply options suggested by the model (ADR-0015). With `widget.replies: 'suggest'`, a bot gets the built-in tool `suggest_replies`: the model can offer 2–4 short replies under an answer, which visitors click instead of typing. The server answers the call itself (no bot code runs), cleans the options up and sends them in the `replies` event; after a reload, the history brings back the options of the latest answer. When the model suggests them after its answer, the message ends without another model call; otherwise it takes one more. `suggest_replies` is now a reserved tool name. Off by default: it costs about 250 input tokens per model call, and sometimes a model call (docs/costs.md).
|
|
32
|
+
|
|
33
|
+
### Patch Changes
|
|
34
|
+
|
|
35
|
+
- 9cb8282: Reply options with characters the visitor can't see are dropped: zero-width and tag characters, direction marks, private-use and unassigned code points. A click sends an option as the visitor's own message, so it now always says exactly what they read. Emoji built with the zero-width joiner still work.
|
|
36
|
+
|
|
3
37
|
## 0.2.0
|
|
4
38
|
|
|
5
39
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ export const bots = [
|
|
|
38
38
|
|
|
39
39
|
## What you get
|
|
40
40
|
|
|
41
|
-
[](https://askdare.vercel.app/#features)
|
|
42
42
|
|
|
43
43
|
[Knowledge base](https://askdare.vercel.app/docs/knowledge-base) · [Tools](https://askdare.vercel.app/docs/tools) · [Attachments](https://askdare.vercel.app/docs/attachments) · [Voice input](https://askdare.vercel.app/docs/voice-input) · [The chat inside your page (React)](https://askdare.vercel.app/docs/react) · [Answer ratings](https://askdare.vercel.app/docs/feedback) · [Security](https://github.com/boldare/askdare/blob/main/THREAT_MODEL.md)
|
|
44
44
|
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
// src/i18n/locale.ts
|
|
3
|
+
/** The canonical form of a BCP 47 tag that Intl accepts (plural rules need it), else undefined. */
|
|
4
|
+
function canonicalLocale(tag) {
|
|
5
|
+
if (tag.length > 35) return void 0;
|
|
6
|
+
try {
|
|
7
|
+
return Intl.getCanonicalLocales(tag)[0];
|
|
8
|
+
} catch {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
// src/i18n/texts.ts
|
|
3
13
|
/**
|
|
4
14
|
* Every text the widget shows. The keys are public API (ADR-0008): override any of them with
|
|
@@ -25,6 +35,7 @@ const EN = {
|
|
|
25
35
|
"status.tool": "Using {name}…",
|
|
26
36
|
"status.typing": "The assistant is typing",
|
|
27
37
|
"message.sources": "Sources",
|
|
38
|
+
"message.replies": "Suggested replies",
|
|
28
39
|
"feedback.label": "Rate this answer",
|
|
29
40
|
"feedback.up": "Helpful",
|
|
30
41
|
"feedback.down": "Not helpful",
|
|
@@ -118,6 +129,7 @@ const PL = {
|
|
|
118
129
|
"status.tool": "Korzystam z narzędzia {name}…",
|
|
119
130
|
"status.typing": "Asystent pisze",
|
|
120
131
|
"message.sources": "Źródła",
|
|
132
|
+
"message.replies": "Proponowane odpowiedzi",
|
|
121
133
|
"feedback.label": "Oceń tę odpowiedź",
|
|
122
134
|
"feedback.up": "Pomocna",
|
|
123
135
|
"feedback.down": "Niepomocna",
|
|
@@ -182,15 +194,6 @@ const PL = {
|
|
|
182
194
|
"error.dictationBusy": "Poprzednie nagranie jest jeszcze przepisywane. Spróbuj za chwilę.",
|
|
183
195
|
"error.dictationBudget": "Dyktowanie jest niedostępne do końca dnia. Wiadomość możesz nadal wpisać."
|
|
184
196
|
};
|
|
185
|
-
/** The canonical form of a BCP 47 tag that Intl accepts (plural rules need it), else undefined. */
|
|
186
|
-
function canonicalLocale(tag) {
|
|
187
|
-
if (tag.length > 35) return void 0;
|
|
188
|
-
try {
|
|
189
|
-
return Intl.getCanonicalLocales(tag)[0];
|
|
190
|
-
} catch {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
197
|
/** Languages with a built-in dictionary; any other locale shows English texts. */
|
|
195
198
|
const TEXT_LOCALES = ["en", "pl"];
|
|
196
199
|
const DICTIONARIES = {
|
|
@@ -278,7 +281,8 @@ const PRESETS = {
|
|
|
278
281
|
bubbles: "filled",
|
|
279
282
|
surface: "glass",
|
|
280
283
|
density: "comfortable",
|
|
281
|
-
composer: "outline"
|
|
284
|
+
composer: "outline",
|
|
285
|
+
replies: "answer"
|
|
282
286
|
}
|
|
283
287
|
},
|
|
284
288
|
/**
|
|
@@ -331,7 +335,8 @@ const PRESETS = {
|
|
|
331
335
|
bubbles: "filled",
|
|
332
336
|
surface: "glass",
|
|
333
337
|
density: "comfortable",
|
|
334
|
-
composer: "gradient"
|
|
338
|
+
composer: "gradient",
|
|
339
|
+
replies: "answer"
|
|
335
340
|
}
|
|
336
341
|
},
|
|
337
342
|
/** A calm companion: sage and mint, light type, a persona card, black pill buttons. */
|
|
@@ -381,7 +386,8 @@ const PRESETS = {
|
|
|
381
386
|
bubbles: "filled",
|
|
382
387
|
surface: "solid",
|
|
383
388
|
density: "comfortable",
|
|
384
|
-
composer: "outline"
|
|
389
|
+
composer: "outline",
|
|
390
|
+
replies: "answer"
|
|
385
391
|
}
|
|
386
392
|
},
|
|
387
393
|
/** Trust first: indigo and lavender, a capability checklist, hyperlegible type, AAA text. */
|
|
@@ -431,7 +437,8 @@ const PRESETS = {
|
|
|
431
437
|
bubbles: "filled",
|
|
432
438
|
surface: "solid",
|
|
433
439
|
density: "comfortable",
|
|
434
|
-
composer: "outline"
|
|
440
|
+
composer: "outline",
|
|
441
|
+
replies: "answer"
|
|
435
442
|
}
|
|
436
443
|
}
|
|
437
444
|
};
|
|
@@ -584,7 +591,8 @@ const variantsSchema = z.object({
|
|
|
584
591
|
bubbles: z.enum(["filled", "plain"]),
|
|
585
592
|
surface: z.enum(["solid", "glass"]),
|
|
586
593
|
density: z.enum(["comfortable", "compact"]),
|
|
587
|
-
composer: z.enum(["outline", "gradient"])
|
|
594
|
+
composer: z.enum(["outline", "gradient"]),
|
|
595
|
+
replies: z.enum(["answer", "composer"])
|
|
588
596
|
}).partial().strict();
|
|
589
597
|
const themeDefinitionSchema = z.object({
|
|
590
598
|
extends: z.enum(PRESET_NAMES).optional(),
|
|
@@ -642,6 +650,56 @@ function resolveTheme(theme, colorScheme) {
|
|
|
642
650
|
function themeWarnings(theme) {
|
|
643
651
|
return (theme.colorScheme === "auto" ? ["light", "dark"] : [theme.colorScheme]).flatMap((scheme) => contrastFailures(theme[scheme]).map((failure) => `${scheme}: ${failure.name} has contrast ${failure.ratio}:1, needs ${failure.required}:1`));
|
|
644
652
|
}
|
|
653
|
+
// src/tools/suggest-replies.ts
|
|
654
|
+
/** The built-in tool's name, reserved: `defineBot` refuses a bot tool with it. */
|
|
655
|
+
const SUGGEST_REPLIES = "suggest_replies";
|
|
656
|
+
/** Tells the model when options help and what makes a good one. */
|
|
657
|
+
const SUGGEST_REPLIES_DESCRIPTION = [
|
|
658
|
+
"Show the visitor 2 to 4 short replies they can click instead of typing, under your answer.",
|
|
659
|
+
"Call it at most once per answer, in the same response, after you have written your full answer; never instead of answering.",
|
|
660
|
+
"Call it only when a few clear next steps would help: a choice you asked the visitor to make, a natural follow-up question, or the next step of a task.",
|
|
661
|
+
"Skip it for greetings, small talk, and answers that need no follow-up.",
|
|
662
|
+
"Write each reply as the visitor would say it, in the language of your answer: a few words, plain text, no links, emoji or formatting.",
|
|
663
|
+
"Offer only things you can actually answer or do in this chat.",
|
|
664
|
+
"When the visitor seems stuck, one reply may be a way out, such as asking for a person, if your instructions say how to reach one."
|
|
665
|
+
].join(" ");
|
|
666
|
+
/** The call's arguments as JSON Schema, for strict mode. Limits are enforced on the server. */
|
|
667
|
+
const SUGGEST_REPLIES_PARAMETERS = {
|
|
668
|
+
type: "object",
|
|
669
|
+
properties: { items: {
|
|
670
|
+
type: "array",
|
|
671
|
+
description: "2 to 4 replies in the visitor’s words, each under 80 characters.",
|
|
672
|
+
items: { type: "string" }
|
|
673
|
+
} },
|
|
674
|
+
required: ["items"],
|
|
675
|
+
additionalProperties: false
|
|
676
|
+
};
|
|
677
|
+
/**
|
|
678
|
+
* What the model gets back. It is the same for every call, so nothing the model wrote reaches it
|
|
679
|
+
* again, and it keeps the model from listing the options in its answer.
|
|
680
|
+
*/
|
|
681
|
+
const SUGGEST_REPLIES_OUTPUT = JSON.stringify("The visitor sees these replies under your answer. Do not list them, and do not repeat anything you have already written.");
|
|
682
|
+
const args = z.object({ items: z.array(z.unknown()) });
|
|
683
|
+
/**
|
|
684
|
+
* The options in one call's arguments, as the model wrote them; `normalizeReplies` cleans them up.
|
|
685
|
+
* Undefined when the arguments don't parse.
|
|
686
|
+
*/
|
|
687
|
+
function suggestedReplies(json) {
|
|
688
|
+
try {
|
|
689
|
+
const parsed = args.safeParse(JSON.parse(json));
|
|
690
|
+
return parsed.success ? parsed.data.items.filter((item) => typeof item === "string") : void 0;
|
|
691
|
+
} catch {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Finds the latest answer's suggested options in its turn's calls, for the history. Only an
|
|
697
|
+
* answered call counts, as only a turn that went on after it sent its options.
|
|
698
|
+
*/
|
|
699
|
+
const readSuggestedReplies = (calls) => {
|
|
700
|
+
const call = calls.findLast((stored) => stored.name === "suggest_replies" && stored.output !== void 0);
|
|
701
|
+
return call && suggestedReplies(call.arguments);
|
|
702
|
+
};
|
|
645
703
|
// src/protocol/version.ts
|
|
646
704
|
/**
|
|
647
705
|
* Version of the SSE wire protocol between the askdare server and its clients.
|
|
@@ -650,4 +708,93 @@ function themeWarnings(theme) {
|
|
|
650
708
|
const PROTOCOL_VERSION = 1;
|
|
651
709
|
/** Response header carrying {@link PROTOCOL_VERSION} on every streaming response. */
|
|
652
710
|
const PROTOCOL_HEADER = "x-askdare-protocol";
|
|
653
|
-
|
|
711
|
+
/**
|
|
712
|
+
* A URL or something a browser or a chat app would turn into a link: a scheme (`https://`,
|
|
713
|
+
* `mailto:`, `javascript:` …), `www.`, an IP address or a domain name such as `example.com`.
|
|
714
|
+
* Strict on purpose: a few odd names (`Node.js`) are dropped with them.
|
|
715
|
+
*/
|
|
716
|
+
const LINK = /[a-z][a-z\d+.-]*:\/\/|\b(?:mailto|tel|sms|javascript|vbscript|data|file):|\bwww\.|\b\d{1,3}(?:\.\d{1,3}){3}\b|[\p{L}\d-]\.[a-z]{2,}(?![\p{L}\d])/iu;
|
|
717
|
+
/**
|
|
718
|
+
* Characters the visitor can't see or read: line breaks and other control characters, format
|
|
719
|
+
* characters (bidirectional overrides that reorder text, zero-width characters, tag characters
|
|
720
|
+
* that can spell hidden words), private-use and unassigned code points. A click sends the option
|
|
721
|
+
* as the visitor's own message, so it must say exactly what they read. The zero-width joiner stays
|
|
722
|
+
* for emoji built from several, such as "woman technologist".
|
|
723
|
+
*/
|
|
724
|
+
const CONTROL = /[\p{Cc}\p{Co}\p{Cn}\u2028\u2029]|(?!\u200d)\p{Cf}/u;
|
|
725
|
+
/** One option as it goes over the wire: trimmed, 1 to 80 characters, one line, no links. */
|
|
726
|
+
function isReplyText(value) {
|
|
727
|
+
return typeof value === "string" && value.length > 0 && value.length <= 80 && value === value.trim() && !CONTROL.test(value) && !LINK.test(value);
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* The options worth sending from what a source proposed: whitespace collapsed and trimmed, then
|
|
731
|
+
* empty, too long, linked and repeated ones dropped, at most four. Undefined when none is left,
|
|
732
|
+
* so no event is sent.
|
|
733
|
+
*/
|
|
734
|
+
function normalizeReplies(values) {
|
|
735
|
+
const replies = [];
|
|
736
|
+
for (const value of values) {
|
|
737
|
+
if (typeof value !== "string") continue;
|
|
738
|
+
const reply = value.replace(/\s+/gu, " ").trim();
|
|
739
|
+
if (isReplyText(reply) && !replies.includes(reply)) replies.push(reply);
|
|
740
|
+
if (replies.length === 4) break;
|
|
741
|
+
}
|
|
742
|
+
return replies.length > 0 ? replies : void 0;
|
|
743
|
+
}
|
|
744
|
+
// src/tools/define-tool.ts
|
|
745
|
+
const TOOL_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
746
|
+
/** Finds fields OpenAI strict mode would reject, so they fail at startup instead of mid-chat. */
|
|
747
|
+
function strictModeProblems(schema, path) {
|
|
748
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodDefault) return [`${path || "(root)"} is optional; use .nullable() so the field is always present`];
|
|
749
|
+
if (schema instanceof z.ZodNullable) return strictModeProblems(schema.unwrap(), path);
|
|
750
|
+
if (schema instanceof z.ZodArray) return strictModeProblems(schema.element, `${path}[]`);
|
|
751
|
+
if (schema instanceof z.ZodObject) return Object.entries(schema.shape).flatMap(([key, value]) => strictModeProblems(value, path ? `${path}.${key}` : key));
|
|
752
|
+
return [];
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Declares a server-side function the model may call. Arguments are validated with the schema
|
|
756
|
+
* before `execute` runs; failures are reported to the model, never to the visitor.
|
|
757
|
+
*/
|
|
758
|
+
function defineTool(definition) {
|
|
759
|
+
const problems = [];
|
|
760
|
+
if (!TOOL_NAME.test(definition.name)) problems.push("name must be 1-64 letters, digits, \"_\" or \"-\"");
|
|
761
|
+
if (definition.description.trim() === "") problems.push("description must not be empty");
|
|
762
|
+
if (!(definition.input instanceof z.ZodObject)) problems.push("input must be a z.object schema");
|
|
763
|
+
else problems.push(...strictModeProblems(definition.input, ""));
|
|
764
|
+
const timeoutMs = definition.timeoutMs ?? 1e4;
|
|
765
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 12e4) problems.push("timeoutMs must be an integer between 100 and 120000");
|
|
766
|
+
if (definition.replies !== void 0 && typeof definition.replies !== "function") problems.push("replies must be a function of the result");
|
|
767
|
+
if (problems.length > 0) throw new TypeError(`[askdare] Invalid tool "${definition.name}": ${problems.join("; ")}`);
|
|
768
|
+
return Object.freeze({
|
|
769
|
+
name: definition.name,
|
|
770
|
+
description: definition.description.trim(),
|
|
771
|
+
input: definition.input,
|
|
772
|
+
execute: definition.execute,
|
|
773
|
+
timeoutMs,
|
|
774
|
+
...definition.replies ? { replies: definition.replies } : {}
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* The options a turn's calls define, oldest call first (ADR-0015): the last call that
|
|
779
|
+
* defines any wins. The live turn and the history both pick them here, from the stored JSON, so a
|
|
780
|
+
* reload shows what the turn showed. Arguments that don't fit the tool's schema mean the call
|
|
781
|
+
* failed, and a tool the bot no longer has defines nothing. A `replies` function that throws
|
|
782
|
+
* defines none; `onError` hears about it.
|
|
783
|
+
*/
|
|
784
|
+
function readToolReplies(tools, calls, onError) {
|
|
785
|
+
let replies;
|
|
786
|
+
for (const call of calls) {
|
|
787
|
+
const tool = tools.find((candidate) => candidate.name === call.name);
|
|
788
|
+
if (!tool?.replies || call.output === void 0) continue;
|
|
789
|
+
try {
|
|
790
|
+
if (!tool.input.safeParse(JSON.parse(call.arguments)).success) continue;
|
|
791
|
+
const items = tool.replies(JSON.parse(call.output));
|
|
792
|
+
const offered = Array.isArray(items) ? normalizeReplies(items) : void 0;
|
|
793
|
+
if (offered) replies = offered;
|
|
794
|
+
} catch (error) {
|
|
795
|
+
onError?.(tool, error);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return replies;
|
|
799
|
+
}
|
|
800
|
+
export { TEXT_KEYS as _, PROTOCOL_VERSION as a, textsFor as b, SUGGEST_REPLIES_OUTPUT as c, suggestedReplies as d, defineTheme as f, PRESET_NAMES as g, themeWarnings as h, PROTOCOL_HEADER as i, SUGGEST_REPLIES_PARAMETERS as l, themeDefinitionSchema as m, readToolReplies as n, SUGGEST_REPLIES as o, resolveTheme as p, normalizeReplies as r, SUGGEST_REPLIES_DESCRIPTION as s, defineTool as t, readSuggestedReplies as u, TEXT_LOCALES as v, canonicalLocale as x, textLocale as y };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { _ as TEXT_KEYS, a as PROTOCOL_VERSION, b as textsFor, f as defineTheme, g as PRESET_NAMES, h as themeWarnings, m as themeDefinitionSchema, o as SUGGEST_REPLIES, p as resolveTheme, t as defineTool, x as canonicalLocale } from "./define-tool.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
// src/bot/define-bot.ts
|
|
4
4
|
const DEFAULT_LIMITS = {
|
|
@@ -55,7 +55,7 @@ const definitionSchema = z.object({
|
|
|
55
55
|
voiceSecondsPerDay: z.number().int().min(60).optional(),
|
|
56
56
|
voiceSecondsPerIpPerDay: z.number().int().min(60).optional()
|
|
57
57
|
}).optional(),
|
|
58
|
-
tools: z.array(z.custom((value) => typeof value === "object" && value !== null && "execute" in value, "must be created with defineTool()")).max(64).refine((tools) => new Set(tools.map((tool) => tool.name)).size === tools.length, "must have unique names").refine((tools) => tools.every((tool) => tool.name !== "file_search"), "must not use the reserved name file_search").optional(),
|
|
58
|
+
tools: z.array(z.custom((value) => typeof value === "object" && value !== null && "execute" in value, "must be created with defineTool()")).max(64).refine((tools) => new Set(tools.map((tool) => tool.name)).size === tools.length, "must have unique names").refine((tools) => tools.every((tool) => tool.name !== "file_search"), "must not use the reserved name file_search").refine((tools) => tools.every((tool) => tool.name !== SUGGEST_REPLIES), `must not use the reserved name ${SUGGEST_REPLIES}`).optional(),
|
|
59
59
|
pageContext: z.boolean().optional(),
|
|
60
60
|
knowledge: z.object({
|
|
61
61
|
vectorStoreId: z.string().regex(/^vs_[A-Za-z0-9_-]+$/, "must be a vector store ID like vs_…"),
|
|
@@ -84,6 +84,7 @@ const definitionSchema = z.object({
|
|
|
84
84
|
subtitle: z.string().trim().min(1).max(120).optional(),
|
|
85
85
|
starters: z.array(z.string().trim().min(1).max(80)).max(4).optional(),
|
|
86
86
|
feedback: z.boolean().optional(),
|
|
87
|
+
replies: z.enum(["suggest", "off"]).optional(),
|
|
87
88
|
avatar: z.string().regex(AVATAR, "must be a path like /avatar.png or a PNG, JPEG, WebP or GIF data URL").optional(),
|
|
88
89
|
launcherLabel: z.string().trim().min(1).max(40).optional(),
|
|
89
90
|
theme: z.union([z.enum(PRESET_NAMES), themeDefinitionSchema]).optional(),
|
|
@@ -152,6 +153,7 @@ function defineBot(definition) {
|
|
|
152
153
|
locale,
|
|
153
154
|
starters: Object.freeze([...widget.starters ?? []]),
|
|
154
155
|
feedback: widget.feedback ?? true,
|
|
156
|
+
replies: widget.replies ?? "off",
|
|
155
157
|
avatar: widget.avatar,
|
|
156
158
|
launcherLabel: texts["launcher.label"] ?? "",
|
|
157
159
|
texts: overrides,
|
|
@@ -161,35 +163,4 @@ function defineBot(definition) {
|
|
|
161
163
|
})
|
|
162
164
|
});
|
|
163
165
|
}
|
|
164
|
-
// src/tools/define-tool.ts
|
|
165
|
-
const TOOL_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
166
|
-
/** Finds fields OpenAI strict mode would reject, so they fail at startup instead of mid-chat. */
|
|
167
|
-
function strictModeProblems(schema, path) {
|
|
168
|
-
if (schema instanceof z.ZodOptional || schema instanceof z.ZodDefault) return [`${path || "(root)"} is optional; use .nullable() so the field is always present`];
|
|
169
|
-
if (schema instanceof z.ZodNullable) return strictModeProblems(schema.unwrap(), path);
|
|
170
|
-
if (schema instanceof z.ZodArray) return strictModeProblems(schema.element, `${path}[]`);
|
|
171
|
-
if (schema instanceof z.ZodObject) return Object.entries(schema.shape).flatMap(([key, value]) => strictModeProblems(value, path ? `${path}.${key}` : key));
|
|
172
|
-
return [];
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Declares a server-side function the model may call. Arguments are validated with the schema
|
|
176
|
-
* before `execute` runs; failures are reported to the model, never to the visitor.
|
|
177
|
-
*/
|
|
178
|
-
function defineTool(definition) {
|
|
179
|
-
const problems = [];
|
|
180
|
-
if (!TOOL_NAME.test(definition.name)) problems.push("name must be 1-64 letters, digits, \"_\" or \"-\"");
|
|
181
|
-
if (definition.description.trim() === "") problems.push("description must not be empty");
|
|
182
|
-
if (!(definition.input instanceof z.ZodObject)) problems.push("input must be a z.object schema");
|
|
183
|
-
else problems.push(...strictModeProblems(definition.input, ""));
|
|
184
|
-
const timeoutMs = definition.timeoutMs ?? 1e4;
|
|
185
|
-
if (!Number.isInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 12e4) problems.push("timeoutMs must be an integer between 100 and 120000");
|
|
186
|
-
if (problems.length > 0) throw new TypeError(`[askdare] Invalid tool "${definition.name}": ${problems.join("; ")}`);
|
|
187
|
-
return Object.freeze({
|
|
188
|
-
name: definition.name,
|
|
189
|
-
description: definition.description.trim(),
|
|
190
|
-
input: definition.input,
|
|
191
|
-
execute: definition.execute,
|
|
192
|
-
timeoutMs
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
166
|
export { PROTOCOL_VERSION, defineBot, defineTheme, defineTool };
|