@jigs.design/ai-sdk 0.3.2 → 0.3.4
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/model.d.ts +11 -7
- package/dist/model.js +19 -15
- package/dist/tool-data.d.ts +3 -1
- package/dist/tool-data.js +31 -17
- package/package.json +5 -5
package/dist/model.d.ts
CHANGED
|
@@ -4,18 +4,22 @@ import { Instructions, LanguageModel, streamText } from "ai";
|
|
|
4
4
|
/** `streamText` settings that apply to every call, such as `temperature`. */
|
|
5
5
|
export type ModelSettings = Omit<Parameters<typeof streamText>[0], "abortSignal" | "instructions" | "messages" | "model" | "prompt" | "system">;
|
|
6
6
|
/**
|
|
7
|
-
* `system` as instructions
|
|
8
|
-
*
|
|
9
|
-
* that need one (Anthropic).
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* `system` as instructions a provider may cache: two system messages, its
|
|
8
|
+
* first part `prefix` and the rest, each marked as a cache breakpoint for
|
|
9
|
+
* providers that need one (Anthropic). The first mark keeps the prefix
|
|
10
|
+
* cached when the rest changes; the second caches the rest too while it
|
|
11
|
+
* does not, as it does not for a host whose procedures and instructions
|
|
12
|
+
* are fixed, so a turn reads the whole prompt from the cache. Two of
|
|
13
|
+
* Anthropic's four breakpoints. Providers that cache a repeated prefix on
|
|
14
|
+
* their own (OpenAI, DeepSeek, Google) read the two as one. Without a
|
|
15
|
+
* prefix that `system` starts with, `system` alone.
|
|
12
16
|
*/
|
|
13
17
|
export declare function cachedInstructions(system: string, prefix: string): Instructions;
|
|
14
18
|
/**
|
|
15
19
|
* A `model` for `withModel` from an AI SDK model: document-mode
|
|
16
20
|
* sessions and chat repairs stream through `streamText`, and token usage
|
|
17
|
-
* reaches `onTurn`. The prompt's stable start
|
|
18
|
-
* cache (`cachedInstructions`).
|
|
21
|
+
* reaches `onTurn`. The prompt's stable start and its end are marked for
|
|
22
|
+
* the provider's cache (`cachedInstructions`).
|
|
19
23
|
*/
|
|
20
24
|
export declare function aiSdkModel(model: LanguageModel, settings?: ModelSettings): ModelFunction<unknown, unknown>;
|
|
21
25
|
//#endregion
|
package/dist/model.js
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
import { streamText } from "ai";
|
|
2
2
|
//#region src/model.ts
|
|
3
|
+
/** A system message marked as an Anthropic cache breakpoint. */
|
|
4
|
+
const cachedMessage = (content) => ({
|
|
5
|
+
content,
|
|
6
|
+
providerOptions: { anthropic: { cacheControl: { type: "ephemeral" } } },
|
|
7
|
+
role: "system"
|
|
8
|
+
});
|
|
3
9
|
/**
|
|
4
|
-
* `system` as instructions
|
|
5
|
-
*
|
|
6
|
-
* that need one (Anthropic).
|
|
7
|
-
*
|
|
8
|
-
*
|
|
10
|
+
* `system` as instructions a provider may cache: two system messages, its
|
|
11
|
+
* first part `prefix` and the rest, each marked as a cache breakpoint for
|
|
12
|
+
* providers that need one (Anthropic). The first mark keeps the prefix
|
|
13
|
+
* cached when the rest changes; the second caches the rest too while it
|
|
14
|
+
* does not, as it does not for a host whose procedures and instructions
|
|
15
|
+
* are fixed, so a turn reads the whole prompt from the cache. Two of
|
|
16
|
+
* Anthropic's four breakpoints. Providers that cache a repeated prefix on
|
|
17
|
+
* their own (OpenAI, DeepSeek, Google) read the two as one. Without a
|
|
18
|
+
* prefix that `system` starts with, `system` alone.
|
|
9
19
|
*/
|
|
10
20
|
function cachedInstructions(system, prefix) {
|
|
11
21
|
if (prefix === "" || !system.startsWith(prefix)) return system;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
providerOptions: { anthropic: { cacheControl: { type: "ephemeral" } } },
|
|
15
|
-
role: "system"
|
|
16
|
-
}, {
|
|
17
|
-
content: system.slice(prefix.length),
|
|
18
|
-
role: "system"
|
|
19
|
-
}];
|
|
22
|
+
const rest = system.slice(prefix.length);
|
|
23
|
+
return rest === "" ? [cachedMessage(prefix)] : [cachedMessage(prefix), cachedMessage(rest)];
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
* A `model` for `withModel` from an AI SDK model: document-mode
|
|
23
27
|
* sessions and chat repairs stream through `streamText`, and token usage
|
|
24
|
-
* reaches `onTurn`. The prompt's stable start
|
|
25
|
-
* cache (`cachedInstructions`).
|
|
28
|
+
* reaches `onTurn`. The prompt's stable start and its end are marked for
|
|
29
|
+
* the provider's cache (`cachedInstructions`).
|
|
26
30
|
*/
|
|
27
31
|
function aiSdkModel(model, settings = {}) {
|
|
28
32
|
return async function* (request) {
|
package/dist/tool-data.d.ts
CHANGED
|
@@ -37,7 +37,9 @@ export interface ToolDataOptions {
|
|
|
37
37
|
* reply that wrote it; else its previous version's value; else the latest
|
|
38
38
|
* value earlier in the conversation. A failed or refused call in that reply
|
|
39
39
|
* removes the tool's entry, so the block shows its empty state rather than
|
|
40
|
-
* an older result.
|
|
40
|
+
* an older result. Each call is also under its number among that tool's
|
|
41
|
+
* calls in the latest reply that called it, `searchFlights#2` for the
|
|
42
|
+
* second, so blocks of a reply that called a tool twice read one each.
|
|
41
43
|
* While a block's results are the same, it gets the same object, so the view
|
|
42
44
|
* does not compare them again.
|
|
43
45
|
*/
|
package/dist/tool-data.js
CHANGED
|
@@ -76,13 +76,20 @@ function toolValue(part) {
|
|
|
76
76
|
values.set(part, value ?? null);
|
|
77
77
|
return value;
|
|
78
78
|
}
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
/**
|
|
80
|
+
* What a settled call does to a snapshot: sets or removes its tool's
|
|
81
|
+
* latest value and that of its numbered key, `name#n`. The first to settle
|
|
82
|
+
* of a message's calls of a tool drops the numbered keys an earlier reply
|
|
83
|
+
* left, so `name#n` is always the latest reply's.
|
|
84
|
+
*/
|
|
85
|
+
function apply(snapshot, call) {
|
|
86
|
+
if (call.first) {
|
|
87
|
+
for (const key of snapshot.keys()) if (key.startsWith(`${call.name}#`)) snapshot.delete(key);
|
|
88
|
+
}
|
|
89
|
+
for (const key of [call.name, `${call.name}#${call.ordinal}`]) if (call.value === void 0) snapshot.delete(key);
|
|
90
|
+
else snapshot.set(key, {
|
|
91
|
+
id: `${call.part.toolCallId}:${call.part.state}`,
|
|
92
|
+
value: call.value
|
|
86
93
|
});
|
|
87
94
|
}
|
|
88
95
|
/** Whether a part settles its tool's value: a final output, an error or a refusal. */
|
|
@@ -118,7 +125,9 @@ const DEFAULT_VALUE = {};
|
|
|
118
125
|
* reply that wrote it; else its previous version's value; else the latest
|
|
119
126
|
* value earlier in the conversation. A failed or refused call in that reply
|
|
120
127
|
* removes the tool's entry, so the block shows its empty state rather than
|
|
121
|
-
* an older result.
|
|
128
|
+
* an older result. Each call is also under its number among that tool's
|
|
129
|
+
* calls in the latest reply that called it, `searchFlights#2` for the
|
|
130
|
+
* second, so blocks of a reply that called a tool twice read one each.
|
|
122
131
|
* While a block's results are the same, it gets the same object, so the view
|
|
123
132
|
* does not compare them again.
|
|
124
133
|
*/
|
|
@@ -134,21 +143,26 @@ function toolData(messages, options = {}) {
|
|
|
134
143
|
const before = new Map(conversation);
|
|
135
144
|
/** The settled tool calls of this message so far, in order. */
|
|
136
145
|
const settled = [];
|
|
146
|
+
/** The calls of each tool in this message so far, settled or not. */
|
|
147
|
+
const counts = /* @__PURE__ */ new Map();
|
|
137
148
|
for (const part of message.parts) {
|
|
138
149
|
if (isToolPart(part)) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
150
|
+
const name = toolNameOf(part) ?? "";
|
|
151
|
+
const ordinal = (counts.get(name) ?? 0) + 1;
|
|
152
|
+
counts.set(name, ordinal);
|
|
153
|
+
if (settles(part)) settled.push({
|
|
154
|
+
first: !settled.some((call) => call.name === name),
|
|
155
|
+
name,
|
|
156
|
+
ordinal,
|
|
157
|
+
part,
|
|
158
|
+
value: valueOf === void 0 ? toolValue(part) : valueOf(name, part)
|
|
159
|
+
});
|
|
146
160
|
continue;
|
|
147
161
|
}
|
|
148
162
|
if (!isBlockPart(part)) continue;
|
|
149
163
|
const block = part.data;
|
|
150
164
|
const snapshot = new Map(versions.get(block.name) ?? before);
|
|
151
|
-
for (const
|
|
165
|
+
for (const call of settled) apply(snapshot, call);
|
|
152
166
|
versions.set(block.name, snapshot);
|
|
153
167
|
if (block.toolKeys === void 0) continue;
|
|
154
168
|
const projected = project(snapshot, block.toolKeys);
|
|
@@ -164,7 +178,7 @@ function toolData(messages, options = {}) {
|
|
|
164
178
|
});
|
|
165
179
|
result.set(block.key, data);
|
|
166
180
|
}
|
|
167
|
-
for (const
|
|
181
|
+
for (const call of settled) apply(conversation, call);
|
|
168
182
|
}
|
|
169
183
|
return result;
|
|
170
184
|
}
|
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.4",
|
|
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.4",
|
|
33
|
+
"@jigs.design/server": "0.3.4"
|
|
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.4",
|
|
44
|
+
"@jigs.design/host": "0.3.4"
|
|
45
45
|
}
|
|
46
46
|
}
|