@webknox/ai-sdk 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -31,7 +31,19 @@ const element = document.createElement('webknox-answer');
31
31
  element.answer = toolResult.output;
32
32
  ```
33
33
 
34
- The model reads only the answer's text and sources, and is told the user already sees the card. That keeps a large card out of its context, and it won't read the card back. [The Next.js chat](https://github.com/webknox/webknox-examples/tree/main/examples/nextjs-chat) shows the whole loop.
34
+ The model reads only the answer's text and sources, and is told the user already sees the card. That keeps a large card out of its context, and it won't read the card back.
35
+
36
+ ## Filters on a card
37
+
38
+ Search cards have filters, sort and "Show more". A change fires `webknox-refine`. Answer it from your server with `refineAnswer`, so the card updates in place without a model turn:
39
+
40
+ ```js
41
+ import {refineAnswer} from '@webknox/ai-sdk';
42
+
43
+ const answer = await refineAnswer({question, locale, refine});
44
+ ```
45
+
46
+ [The Next.js chat](https://github.com/webknox/webknox-examples/tree/main/examples/nextjs-chat) shows the whole loop.
35
47
 
36
48
  ## Options
37
49
 
@@ -43,7 +55,7 @@ askWebknox({
43
55
  });
44
56
  ```
45
57
 
46
- `apiKey`, `baseUrl`, `render` (hosted embeds and images, on Pro and Scale) and `fetch` work the same way. A question WebKnox can't answer comes back with `matched: false`, and the model answers it itself. A refused request throws a `WebKnoxError` with `status`, `code` and `retryAfterS`.
58
+ `apiKey`, `baseUrl`, `render` (hosted embeds and images, on Pro and Scale) and `fetch` work the same way. Some models answer from memory rather than call a tool, so tell yours in the system prompt: "Use askWebknox for anything it covers, even in a follow-up." A question WebKnox can't answer comes back with `matched: false`, and the model answers it itself. A refused request throws a `WebKnoxError` with `status`, `code` and `retryAfterS`.
47
59
 
48
60
  ## License
49
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webknox/ai-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "WebKnox for the AI SDK: a tool that answers everyday questions with sourced UI cards.",
5
5
  "keywords": ["webknox", "ai-sdk", "vercel-ai-sdk", "tool", "generative-ui", "cards"],
6
6
  "homepage": "https://webknox.com/docs/ai-sdk",
package/src/index.d.ts CHANGED
@@ -57,6 +57,17 @@ export interface AskWebknoxOptions {
57
57
 
58
58
  export function askWebknox(options?: AskWebknoxOptions): Tool<{question: string}, WebKnoxAnswer>;
59
59
 
60
+ export interface RefineRequest {
61
+ /** What the card answered: its search's query, or the answer's text. */
62
+ question: string;
63
+ locale?: string;
64
+ /** `detail.refine` from the card's `webknox-refine` event. */
65
+ refine: Record<string, unknown>;
66
+ }
67
+
68
+ /** Answers a change to a search card straight from the API, without a model turn. Call it from your server. */
69
+ export function refineAnswer(request: RefineRequest, connection?: Pick<AskWebknoxOptions, 'apiKey' | 'baseUrl' | 'fetch'>): Promise<WebKnoxAnswer>;
70
+
60
71
  /** What the model reads of an answer: its text and sources, not the card. */
61
72
  export function forModel(answer: WebKnoxAnswer): string;
62
73
 
package/src/index.js CHANGED
@@ -4,10 +4,11 @@ import {z} from 'zod';
4
4
  const API = 'https://api.webknox.com';
5
5
 
6
6
  const DESCRIPTION = [
7
- 'Answers everyday questions with a sourced card the user sees in the chat: weather, nutrition, recipes, recipe, game and',
8
- 'artwork searches, comparisons with photos, translations, definitions, conversions, maths, time and timers, sunrise and',
9
- 'sunset, videos, music and radio, pictures, jokes, quotes and this day in history.',
10
- 'Call it when the user asks for one of these, and answer anything else yourself.',
7
+ 'Shows the user a sourced card in the chat, from live data: weather, nutrition, single recipes and recipe searches,',
8
+ 'game and artwork searches, comparisons with photos, translations, definitions, conversions, maths, time and timers,',
9
+ 'sunrise and sunset, videos, music and radio, pictures, jokes, quotes and this day in history.',
10
+ 'Call it whenever the user asks for one of these, even if you could answer from memory: real recipes with photos and',
11
+ 'filters beat a recipe you write yourself. Answer anything else yourself.',
11
12
  ].join(' ');
12
13
 
13
14
  const QUESTION = 'The question as one self-contained sentence in the user\'s language that names the place, food or things it is about; a follow-up such as "and tomorrow?" written out as a full question.';
@@ -43,26 +44,38 @@ async function refused(response) {
43
44
  });
44
45
  }
45
46
 
47
+ /** One POST to the Answers API. */
48
+ async function post(body, {apiKey, baseUrl = API, fetch: fetchImpl = globalThis.fetch} = {}, signal) {
49
+ const key = apiKey ?? globalThis.process?.env?.WEBKNOX_API_KEY;
50
+ if (!key) throw new WebKnoxError('No API key: set WEBKNOX_API_KEY or pass apiKey. Get one at https://webknox.com/console');
51
+ const response = await fetchImpl(`${baseUrl}/v2/answers`, {
52
+ method: 'POST',
53
+ headers: {Authorization: `Bearer ${key}`, 'Content-Type': 'application/json'},
54
+ body: JSON.stringify(body),
55
+ signal,
56
+ });
57
+ if (!response.ok) throw await refused(response);
58
+ return response.json();
59
+ }
60
+
46
61
  /**
47
62
  * The WebKnox tool for `generateText`, `streamText` and agents. Its result is the Answers API's answer: hand it to
48
63
  * `<webknox-answer>` to show the card.
49
64
  */
50
- export function askWebknox({apiKey, baseUrl = API, locale, context, accept, render, fetch: fetchImpl = globalThis.fetch} = {}) {
65
+ export function askWebknox({locale, context, accept, render, ...connection} = {}) {
51
66
  return tool({
52
67
  description: DESCRIPTION,
53
68
  inputSchema: z.object({question: z.string().trim().min(1).max(1000).describe(QUESTION)}),
54
- execute: async ({question}, {abortSignal} = {}) => {
55
- const key = apiKey ?? globalThis.process?.env?.WEBKNOX_API_KEY;
56
- if (!key) throw new WebKnoxError('No API key: set WEBKNOX_API_KEY or pass apiKey. Get one at https://webknox.com/console');
57
- const response = await fetchImpl(`${baseUrl}/v2/answers`, {
58
- method: 'POST',
59
- headers: {Authorization: `Bearer ${key}`, 'Content-Type': 'application/json'},
60
- body: JSON.stringify({messages: [{role: 'user', content: question}], locale, context, accept, render}),
61
- signal: abortSignal,
62
- });
63
- if (!response.ok) throw await refused(response);
64
- return response.json();
65
- },
69
+ execute: ({question}, {abortSignal} = {}) =>
70
+ post({messages: [{role: 'user', content: question}], locale, context, accept, render}, connection, abortSignal),
66
71
  toModelOutput: ({output}) => ({type: 'text', value: forModel(output)}),
67
72
  });
68
73
  }
74
+
75
+ /**
76
+ * A change to a search card: `refine` from its `webknox-refine` event, for its filters, sort, more results or one of its
77
+ * recipes. It goes straight to the API, so the card updates without a model turn. `question` is what the card answered.
78
+ */
79
+ export function refineAnswer({question, locale, refine}, connection) {
80
+ return post({messages: [{role: 'user', content: question}], locale, refine}, connection);
81
+ }