@jigs.design/ai-sdk 0.3.4 → 0.3.5
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/changes.d.ts +46 -0
- package/dist/changes.js +72 -0
- package/dist/chat.d.ts +2 -1
- package/dist/chat.js +9 -1
- package/dist/client.d.ts +2 -1
- package/dist/client.js +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/schemas.d.ts +19 -0
- package/dist/schemas.js +13 -2
- package/dist/stored.d.ts +6 -2
- package/package.json +5 -5
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { JigsErrorCode, JsonValue, MutationResult } from "@jigs.design/protocol";
|
|
2
|
+
//#region src/changes.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* What the user changed in a jig since the model's last reply: a
|
|
5
|
+
* `data-jigs-change` part on their next message, which `jigsChat`'s
|
|
6
|
+
* `convertDataPart` writes as one sentence for the model.
|
|
7
|
+
*/
|
|
8
|
+
export interface JigsChange {
|
|
9
|
+
/** How many calls it stands for: `addJigsChange` keeps the latest of a run. */
|
|
10
|
+
count: number;
|
|
11
|
+
/** Why the last call failed, `DECLINED` when the user said no; null on a success. */
|
|
12
|
+
error: JigsErrorCode | null;
|
|
13
|
+
/** What the last call passed, as JSON. */
|
|
14
|
+
input: JsonValue;
|
|
15
|
+
/** The block's key, `id@version`. */
|
|
16
|
+
key: string;
|
|
17
|
+
/** The mutation. */
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* `changes` with one more settled mutation from the block `key`, as
|
|
22
|
+
* `JigsView`'s `onMutation` gives it. It replaces an earlier change to the
|
|
23
|
+
* same block, mutation and input once its number fields are left out, and
|
|
24
|
+
* counts it, so a field that mutates on every change sends one line. At
|
|
25
|
+
* most the latest ten are kept.
|
|
26
|
+
*/
|
|
27
|
+
export declare function addJigsChange(changes: ReadonlyArray<JigsChange>, key: string, mutation: {
|
|
28
|
+
input: unknown;
|
|
29
|
+
name: string;
|
|
30
|
+
result: MutationResult;
|
|
31
|
+
}): Array<JigsChange>;
|
|
32
|
+
/**
|
|
33
|
+
* A change as the model reads it: one sentence for the user's message,
|
|
34
|
+
* before their text. `jigsChat`'s `convertDataPart` writes each
|
|
35
|
+
* `data-jigs-change` part with it; a chat on another stack can call it.
|
|
36
|
+
*/
|
|
37
|
+
export declare function jigsChangeText(change: JigsChange): string;
|
|
38
|
+
/**
|
|
39
|
+
* The parts to send `changes` with, before the message's text:
|
|
40
|
+
* `sendMessage({ parts: [...jigsChangeParts(changes), { text, type: "text" }] })`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function jigsChangeParts(changes: ReadonlyArray<JigsChange>): Array<{
|
|
43
|
+
data: JigsChange;
|
|
44
|
+
type: "data-jigs-change";
|
|
45
|
+
}>;
|
|
46
|
+
//#endregion
|
package/dist/changes.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/changes.ts
|
|
2
|
+
function isObject(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function isNumber(value) {
|
|
6
|
+
return typeof value === "number";
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* What makes two calls the same change: the input with its number fields
|
|
10
|
+
* left out, so a field stepped from 300 to 340 is one change and another
|
|
11
|
+
* category or row is its own.
|
|
12
|
+
*/
|
|
13
|
+
function identity(input) {
|
|
14
|
+
if (isNumber(input)) return "";
|
|
15
|
+
if (!isObject(input)) return JSON.stringify(input);
|
|
16
|
+
return JSON.stringify(Object.keys(input).toSorted().flatMap((name) => {
|
|
17
|
+
const value = input[name];
|
|
18
|
+
return value === void 0 || isNumber(value) ? [] : [[name, value]];
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `changes` with one more settled mutation from the block `key`, as
|
|
23
|
+
* `JigsView`'s `onMutation` gives it. It replaces an earlier change to the
|
|
24
|
+
* same block, mutation and input once its number fields are left out, and
|
|
25
|
+
* counts it, so a field that mutates on every change sends one line. At
|
|
26
|
+
* most the latest ten are kept.
|
|
27
|
+
*/
|
|
28
|
+
function addJigsChange(changes, key, mutation) {
|
|
29
|
+
let input = null;
|
|
30
|
+
try {
|
|
31
|
+
const text = JSON.stringify(mutation.input ?? null);
|
|
32
|
+
if (text !== void 0) input = JSON.parse(text);
|
|
33
|
+
} catch {
|
|
34
|
+
input = null;
|
|
35
|
+
}
|
|
36
|
+
const same = (change) => change.key === key && change.name === mutation.name && identity(change.input) === identity(input);
|
|
37
|
+
const change = {
|
|
38
|
+
count: (changes.find(same)?.count ?? 0) + 1,
|
|
39
|
+
error: mutation.result.status === "success" ? null : mutation.result.error.code,
|
|
40
|
+
input,
|
|
41
|
+
key,
|
|
42
|
+
name: mutation.name
|
|
43
|
+
};
|
|
44
|
+
return [...changes.filter((entry) => !same(entry)), change].slice(-10);
|
|
45
|
+
}
|
|
46
|
+
/** The most of a change's input the model reads, in characters of JSON. */
|
|
47
|
+
const MAX_CHANGE_INPUT_CHARS = 500;
|
|
48
|
+
/**
|
|
49
|
+
* A change as the model reads it: one sentence for the user's message,
|
|
50
|
+
* before their text. `jigsChat`'s `convertDataPart` writes each
|
|
51
|
+
* `data-jigs-change` part with it; a chat on another stack can call it.
|
|
52
|
+
*/
|
|
53
|
+
function jigsChangeText(change) {
|
|
54
|
+
const json = JSON.stringify(change.input);
|
|
55
|
+
const input = json.length > MAX_CHANGE_INPUT_CHARS ? `${json.slice(0, MAX_CHANGE_INPUT_CHARS)}… (cut at ${MAX_CHANGE_INPUT_CHARS} characters)` : json;
|
|
56
|
+
const ran = change.count > 1 ? `ran \`${change.name}\` ${change.count} times, the last with ${input}` : `ran \`${change.name}\` with ${input}`;
|
|
57
|
+
const call = change.count > 1 ? "the last call" : "it";
|
|
58
|
+
const outcome = change.error === null ? `${call} succeeded` : change.error === "DECLINED" ? `they declined ${call} when asked to confirm` : `${call} failed with ${change.error}`;
|
|
59
|
+
return `Since your last reply, in the \`${change.key}\` UI the user ${ran}: ${outcome}.`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The parts to send `changes` with, before the message's text:
|
|
63
|
+
* `sendMessage({ parts: [...jigsChangeParts(changes), { text, type: "text" }] })`.
|
|
64
|
+
*/
|
|
65
|
+
function jigsChangeParts(changes) {
|
|
66
|
+
return changes.map((data) => ({
|
|
67
|
+
data,
|
|
68
|
+
type: "data-jigs-change"
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
export { addJigsChange, jigsChangeParts, jigsChangeText };
|
package/dist/chat.d.ts
CHANGED
|
@@ -5,7 +5,8 @@ import { InferUIMessageChunk, UIMessage, UIMessageChunk } from "ai";
|
|
|
5
5
|
export interface JigsChat {
|
|
6
6
|
/**
|
|
7
7
|
* For `convertToModelMessages(messages, { convertDataPart })`: writes each
|
|
8
|
-
* jigs back as the fenced block the model wrote, so it sees its UIs
|
|
8
|
+
* jigs back as the fenced block the model wrote, so it sees its UIs, and
|
|
9
|
+
* each `data-jigs-change` as one sentence saying what the user changed.
|
|
9
10
|
* Returns `undefined` for other data parts; chain your own after it.
|
|
10
11
|
*/
|
|
11
12
|
convertDataPart: (part: {
|
package/dist/chat.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jigsChangeText } from "./changes.js";
|
|
2
|
+
import { storedBlock, storedChange } from "./schemas.js";
|
|
2
3
|
import { chatBlockText } from "@jigs.design/server";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
//#region src/chat.ts
|
|
@@ -40,6 +41,13 @@ async function jigsChat(jigs, input) {
|
|
|
40
41
|
});
|
|
41
42
|
return {
|
|
42
43
|
convertDataPart: (part) => {
|
|
44
|
+
if (part.type === "data-jigs-change") {
|
|
45
|
+
const change = storedChange.safeParse(part.data);
|
|
46
|
+
return change.success ? {
|
|
47
|
+
text: jigsChangeText(change.data),
|
|
48
|
+
type: "text"
|
|
49
|
+
} : void 0;
|
|
50
|
+
}
|
|
43
51
|
if (part.type !== "data-jigs") return;
|
|
44
52
|
const parsed = blockSource.safeParse(part.data);
|
|
45
53
|
return parsed.success ? {
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JigsChange, addJigsChange, jigsChangeParts, jigsChangeText } from "./changes.js";
|
|
1
2
|
import { JigsBlockData, JigsDataTypes, JigsUIMessage, StoredJigsBlock, StoredJigsUIMessage, recentMessages, withoutPrograms } from "./stored.js";
|
|
2
3
|
import { ToolDataOptions, ToolPart, toolData, toolValue } from "./tool-data.js";
|
|
3
|
-
export { type JigsBlockData, type JigsDataTypes, type JigsUIMessage, type StoredJigsBlock, type StoredJigsUIMessage, type ToolDataOptions, type ToolPart, recentMessages, toolData, toolValue, withoutPrograms };
|
|
4
|
+
export { type JigsBlockData, type JigsChange, type JigsDataTypes, type JigsUIMessage, type StoredJigsBlock, type StoredJigsUIMessage, type ToolDataOptions, type ToolPart, addJigsChange, jigsChangeParts, jigsChangeText, recentMessages, toolData, toolValue, withoutPrograms };
|
package/dist/client.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { addJigsChange, jigsChangeParts, jigsChangeText } from "./changes.js";
|
|
1
2
|
import { recentMessages, withoutPrograms } from "./stored.js";
|
|
2
3
|
import { toolData, toolValue } from "./tool-data.js";
|
|
3
|
-
export { recentMessages, toolData, toolValue, withoutPrograms };
|
|
4
|
+
export { addJigsChange, jigsChangeParts, jigsChangeText, recentMessages, toolData, toolValue, withoutPrograms };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { JigsChange, addJigsChange, jigsChangeParts, jigsChangeText } from "./changes.js";
|
|
1
2
|
import { JigsBlockData, JigsDataTypes, JigsUIMessage, StoredJigsBlock, StoredJigsUIMessage, recentMessages, withoutPrograms } from "./stored.js";
|
|
2
3
|
import { ToolDataOptions, ToolPart, toolData, toolValue } from "./tool-data.js";
|
|
3
4
|
import { JigsChat, jigsChat, reviveJigs } from "./chat.js";
|
|
4
5
|
import { ModelSettings, aiSdkModel, cachedInstructions } from "./model.js";
|
|
5
6
|
import { jigsDataSchemas } from "./schemas.js";
|
|
6
|
-
export { type JigsBlockData, type JigsChat, type JigsDataTypes, type JigsUIMessage, type ModelSettings, type StoredJigsBlock, type StoredJigsUIMessage, type ToolDataOptions, type ToolPart, aiSdkModel, cachedInstructions, jigsChat, jigsDataSchemas, recentMessages, reviveJigs, toolData, toolValue, withoutPrograms };
|
|
7
|
+
export { type JigsBlockData, type JigsChange, type JigsChat, type JigsDataTypes, type JigsUIMessage, type ModelSettings, type StoredJigsBlock, type StoredJigsUIMessage, type ToolDataOptions, type ToolPart, addJigsChange, aiSdkModel, cachedInstructions, jigsChangeParts, jigsChangeText, jigsChat, jigsDataSchemas, recentMessages, reviveJigs, toolData, toolValue, withoutPrograms };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { addJigsChange, jigsChangeParts, jigsChangeText } from "./changes.js";
|
|
1
2
|
import { jigsDataSchemas } from "./schemas.js";
|
|
2
3
|
import { jigsChat, reviveJigs } from "./chat.js";
|
|
3
4
|
import { aiSdkModel, cachedInstructions } from "./model.js";
|
|
4
5
|
import { recentMessages, withoutPrograms } from "./stored.js";
|
|
5
6
|
import { toolData, toolValue } from "./tool-data.js";
|
|
6
|
-
export { aiSdkModel, cachedInstructions, jigsChat, jigsDataSchemas, recentMessages, reviveJigs, toolData, toolValue, withoutPrograms };
|
|
7
|
+
export { addJigsChange, aiSdkModel, cachedInstructions, jigsChangeParts, jigsChangeText, jigsChat, jigsDataSchemas, recentMessages, reviveJigs, toolData, toolValue, withoutPrograms };
|
package/dist/schemas.d.ts
CHANGED
|
@@ -66,5 +66,24 @@ export declare const jigsDataSchemas: {
|
|
|
66
66
|
toolKeys: z.ZodExactOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodLiteral<"all">]>>;
|
|
67
67
|
version: z.ZodNumber;
|
|
68
68
|
}, z.core.$strip>;
|
|
69
|
+
"jigs-change": z.ZodObject<{
|
|
70
|
+
count: z.ZodNumber;
|
|
71
|
+
error: z.ZodNullable<z.ZodEnum<{
|
|
72
|
+
BAD_INPUT: "BAD_INPUT";
|
|
73
|
+
CONFLICT: "CONFLICT";
|
|
74
|
+
DECLINED: "DECLINED";
|
|
75
|
+
FORBIDDEN: "FORBIDDEN";
|
|
76
|
+
INTERNAL: "INTERNAL";
|
|
77
|
+
NETWORK: "NETWORK";
|
|
78
|
+
NOT_FOUND: "NOT_FOUND";
|
|
79
|
+
RATE_LIMITED: "RATE_LIMITED";
|
|
80
|
+
UNAUTHORIZED: "UNAUTHORIZED";
|
|
81
|
+
UNAVAILABLE: "UNAVAILABLE";
|
|
82
|
+
UNKNOWN_PROCEDURE: "UNKNOWN_PROCEDURE";
|
|
83
|
+
}>>;
|
|
84
|
+
input: z.ZodJSONSchema;
|
|
85
|
+
key: z.ZodString;
|
|
86
|
+
name: z.ZodString;
|
|
87
|
+
}, z.core.$strip>;
|
|
69
88
|
};
|
|
70
89
|
//#endregion
|
package/dist/schemas.js
CHANGED
|
@@ -44,12 +44,23 @@ const storedBlock = z.object({
|
|
|
44
44
|
toolKeys: z.union([z.array(z.string()), z.literal("all")]).exactOptional(),
|
|
45
45
|
version: z.number()
|
|
46
46
|
});
|
|
47
|
+
/** A `data-jigs-change` part: what the user changed in a jig. */
|
|
48
|
+
const storedChange = z.object({
|
|
49
|
+
count: z.number().int().min(1),
|
|
50
|
+
error: z.enum(jigsErrorCodes).nullable(),
|
|
51
|
+
input: z.json(),
|
|
52
|
+
key: z.string(),
|
|
53
|
+
name: z.string()
|
|
54
|
+
});
|
|
47
55
|
/**
|
|
48
56
|
* Schemas for `validateUIMessages<StoredJigsUIMessage>({ messages,
|
|
49
57
|
* dataSchemas })`, to check a conversation the browser sent or one read
|
|
50
58
|
* from storage. A jig's program is dropped, never checked: the server
|
|
51
59
|
* reads only `name` and `source`, and a source is only ever compiled.
|
|
52
60
|
*/
|
|
53
|
-
const jigsDataSchemas = {
|
|
61
|
+
const jigsDataSchemas = {
|
|
62
|
+
jigs: storedBlock,
|
|
63
|
+
"jigs-change": storedChange
|
|
64
|
+
};
|
|
54
65
|
//#endregion
|
|
55
|
-
export { jigsDataSchemas, storedBlock };
|
|
66
|
+
export { jigsDataSchemas, storedBlock, storedChange };
|
package/dist/stored.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JigsChange } from "./changes.js";
|
|
1
2
|
import { ChatBlock } from "@jigs.design/protocol";
|
|
2
3
|
import { UIMessage } from "ai";
|
|
3
4
|
//#region src/stored.d.ts
|
|
@@ -10,11 +11,13 @@ export type JigsBlockData = ChatBlock;
|
|
|
10
11
|
*/
|
|
11
12
|
export type StoredJigsBlock = Omit<JigsBlockData, "program">;
|
|
12
13
|
/**
|
|
13
|
-
* The data parts chat mode adds to a message
|
|
14
|
-
*
|
|
14
|
+
* The data parts chat mode adds to a message: jigs on a reply, and what the
|
|
15
|
+
* user changed in them on the next user message. A type, not an interface:
|
|
16
|
+
* the AI SDK's data types need an implicit index signature.
|
|
15
17
|
*/
|
|
16
18
|
export type JigsDataTypes = {
|
|
17
19
|
jigs: JigsBlockData;
|
|
20
|
+
"jigs-change": JigsChange;
|
|
18
21
|
};
|
|
19
22
|
/** A `useChat` message whose replies may hold jigs. */
|
|
20
23
|
export type JigsUIMessage<TMetadata = unknown> = UIMessage<TMetadata, JigsDataTypes>;
|
|
@@ -25,6 +28,7 @@ export type JigsUIMessage<TMetadata = unknown> = UIMessage<TMetadata, JigsDataTy
|
|
|
25
28
|
*/
|
|
26
29
|
export type StoredJigsUIMessage<TMetadata = unknown> = UIMessage<TMetadata, {
|
|
27
30
|
jigs: StoredJigsBlock;
|
|
31
|
+
"jigs-change": JigsChange;
|
|
28
32
|
}>;
|
|
29
33
|
/** A message part, with a `data-jigs` part's data replaced by `TBlock`. */
|
|
30
34
|
type WithBlock<TPart, TBlock> = TPart extends {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jigs.design/ai-sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "Jigs on the AI SDK: a model function for createJigsServer, and chat mode for useChat.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@jigs.design/protocol": "0.3.
|
|
33
|
-
"@jigs.design/server": "0.3.
|
|
32
|
+
"@jigs.design/protocol": "0.3.5",
|
|
33
|
+
"@jigs.design/server": "0.3.5"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"ai": "^7.0.0",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@ai-sdk/provider": "4.0.27",
|
|
41
41
|
"ai": "7.0.140",
|
|
42
42
|
"zod": "4.6.7",
|
|
43
|
-
"@jigs.design/catalog": "0.3.
|
|
44
|
-
"@jigs.design/host": "0.3.
|
|
43
|
+
"@jigs.design/catalog": "0.3.5",
|
|
44
|
+
"@jigs.design/host": "0.3.5"
|
|
45
45
|
}
|
|
46
46
|
}
|