@jigs.design/ai-sdk 0.3.3 → 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 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 whose first part, `prefix`, a provider may cache:
8
- * two system messages, the first marked as a cache breakpoint for providers
9
- * that need one (Anthropic). Providers that cache a repeated prefix on their
10
- * own (OpenAI, DeepSeek, Google) read the two as one. Without a prefix that
11
- * `system` starts with, `system` alone.
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 is marked for the provider's
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 whose first part, `prefix`, a provider may cache:
5
- * two system messages, the first marked as a cache breakpoint for providers
6
- * that need one (Anthropic). Providers that cache a repeated prefix on their
7
- * own (OpenAI, DeepSeek, Google) read the two as one. Without a prefix that
8
- * `system` starts with, `system` alone.
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
- return [{
13
- content: prefix,
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 is marked for the provider's
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) {
@@ -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. A tool called twice in one step leaves the last 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
- /** What a finished tool part does to a snapshot: sets its tool's value, or removes it. */
80
- function apply(snapshot, part, value) {
81
- const name = toolNameOf(part) ?? "";
82
- if (value === void 0) snapshot.delete(name);
83
- else snapshot.set(name, {
84
- id: `${part.toolCallId}:${part.state}`,
85
- value
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. A tool called twice in one step leaves the last 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
- if (settles(part)) {
140
- const value = valueOf === void 0 ? toolValue(part) : valueOf(toolNameOf(part) ?? "", part);
141
- settled.push({
142
- part,
143
- value
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 entry of settled) apply(snapshot, entry.part, entry.value);
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 entry of settled) apply(conversation, entry.part, entry.value);
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",
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.3",
33
- "@jigs.design/server": "0.3.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.3",
44
- "@jigs.design/host": "0.3.3"
43
+ "@jigs.design/catalog": "0.3.4",
44
+ "@jigs.design/host": "0.3.4"
45
45
  }
46
46
  }