assistant-stream 0.2.47 → 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.
@@ -0,0 +1,2 @@
1
+ export { toGenericMessages, type GenericMessage, type GenericSystemMessage, type GenericUserMessage, type GenericAssistantMessage, type GenericToolMessage, type GenericTextPart, type GenericFilePart, type GenericToolCallPart, type GenericToolResultPart, } from "./toGenericMessages.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/converters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,kBAAkB,EACvB,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,GAC3B,+BAA4B"}
@@ -0,0 +1,2 @@
1
+ export { toGenericMessages, } from "./toGenericMessages.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/converters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAUlB,+BAA4B"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Generic message types for framework-agnostic LLM message interchange.
3
+ * These types represent a common format that can be converted to/from
4
+ * various LLM provider formats (AI SDK, LangChain, etc.).
5
+ */
6
+ export type GenericTextPart = {
7
+ type: "text";
8
+ text: string;
9
+ };
10
+ export type GenericFilePart = {
11
+ type: "file";
12
+ data: string | URL;
13
+ mediaType: string;
14
+ };
15
+ export type GenericToolCallPart = {
16
+ type: "tool-call";
17
+ toolCallId: string;
18
+ toolName: string;
19
+ args: Record<string, unknown>;
20
+ };
21
+ export type GenericToolResultPart = {
22
+ type: "tool-result";
23
+ toolCallId: string;
24
+ toolName: string;
25
+ result: unknown;
26
+ isError?: boolean;
27
+ };
28
+ export type GenericSystemMessage = {
29
+ role: "system";
30
+ content: string;
31
+ };
32
+ export type GenericUserMessage = {
33
+ role: "user";
34
+ content: (GenericTextPart | GenericFilePart)[];
35
+ };
36
+ export type GenericAssistantMessage = {
37
+ role: "assistant";
38
+ content: (GenericTextPart | GenericToolCallPart)[];
39
+ };
40
+ export type GenericToolMessage = {
41
+ role: "tool";
42
+ content: GenericToolResultPart[];
43
+ };
44
+ export type GenericMessage = GenericSystemMessage | GenericUserMessage | GenericAssistantMessage | GenericToolMessage;
45
+ type MessagePartLike = {
46
+ type: string;
47
+ text?: string;
48
+ image?: string;
49
+ data?: string;
50
+ mimeType?: string;
51
+ toolCallId?: string;
52
+ toolName?: string;
53
+ args?: Record<string, unknown>;
54
+ result?: unknown;
55
+ isError?: boolean;
56
+ };
57
+ type AttachmentLike = {
58
+ content: readonly MessagePartLike[];
59
+ };
60
+ type ThreadMessageLike = {
61
+ role: "system" | "user" | "assistant";
62
+ content: readonly MessagePartLike[];
63
+ attachments?: readonly AttachmentLike[];
64
+ };
65
+ /**
66
+ * Converts thread messages to generic LLM messages.
67
+ * This format can then be easily converted to provider-specific formats.
68
+ */
69
+ export declare function toGenericMessages(messages: readonly ThreadMessageLike[]): GenericMessage[];
70
+ export {};
71
+ //# sourceMappingURL=toGenericMessages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toGenericMessages.d.ts","sourceRoot":"","sources":["../../../src/core/converters/toGenericMessages.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,m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,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,OAAO,EAAE,SAAS,eAAe,EAAE,CAAC;CACrC,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;IACtC,OAAO,EAAE,SAAS,eAAe,EAAE,CAAC;IACpC,WAAW,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;CACzC,CAAC;AAgKF;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,SAAS,iBAAiB,EAAE,GACrC,cAAc,EAAE,CAkBlB"}
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Generic message types for framework-agnostic LLM message interchange.
3
+ * These types represent a common format that can be converted to/from
4
+ * various LLM provider formats (AI SDK, LangChain, etc.).
5
+ */
6
+ const IMAGE_MEDIA_TYPES = {
7
+ jpg: "image/jpeg",
8
+ jpeg: "image/jpeg",
9
+ png: "image/png",
10
+ gif: "image/gif",
11
+ webp: "image/webp",
12
+ svg: "image/svg+xml",
13
+ avif: "image/avif",
14
+ bmp: "image/bmp",
15
+ ico: "image/x-icon",
16
+ tiff: "image/tiff",
17
+ tif: "image/tiff",
18
+ heic: "image/heic",
19
+ heif: "image/heif",
20
+ };
21
+ function inferImageMediaType(url) {
22
+ // Handle data URLs: data:[<mediatype>][;base64],<data>
23
+ if (url.startsWith("data:")) {
24
+ const match = url.match(/^data:([^;,]+)/);
25
+ if (match?.[1])
26
+ return match[1];
27
+ }
28
+ // Extract extension from URL path, ignoring query string and hash
29
+ const [pathWithoutParams = ""] = url.split(/[?#]/);
30
+ const ext = pathWithoutParams.split(".").pop()?.toLowerCase() ?? "";
31
+ return IMAGE_MEDIA_TYPES[ext] ?? "image/png";
32
+ }
33
+ function toUrlOrString(value) {
34
+ try {
35
+ return new URL(value);
36
+ }
37
+ catch {
38
+ return value;
39
+ }
40
+ }
41
+ function processToolCall(part, accumulator) {
42
+ if (!part.toolCallId || !part.toolName)
43
+ return false;
44
+ accumulator.textParts.push({
45
+ type: "tool-call",
46
+ toolCallId: part.toolCallId,
47
+ toolName: part.toolName,
48
+ args: part.args ?? {},
49
+ });
50
+ if (part.result !== undefined) {
51
+ const toolResult = {
52
+ type: "tool-result",
53
+ toolCallId: part.toolCallId,
54
+ toolName: part.toolName,
55
+ result: part.result,
56
+ };
57
+ if (part.isError) {
58
+ toolResult.isError = true;
59
+ }
60
+ accumulator.toolResults.push(toolResult);
61
+ return true;
62
+ }
63
+ return false;
64
+ }
65
+ function flushAccumulator(accumulator, result) {
66
+ if (accumulator.textParts.length > 0) {
67
+ result.push({ role: "assistant", content: accumulator.textParts });
68
+ accumulator.textParts = [];
69
+ }
70
+ if (accumulator.toolResults.length > 0) {
71
+ result.push({ role: "tool", content: accumulator.toolResults });
72
+ accumulator.toolResults = [];
73
+ }
74
+ }
75
+ function convertSystemMessage(message, result) {
76
+ const textPart = message.content.find((p) => p.type === "text");
77
+ if (textPart?.text) {
78
+ result.push({ role: "system", content: textPart.text });
79
+ }
80
+ }
81
+ function convertUserMessage(message, result) {
82
+ const attachments = message.attachments ?? [];
83
+ const allContent = [
84
+ ...message.content,
85
+ ...attachments.flatMap((a) => a.content),
86
+ ];
87
+ const content = [];
88
+ for (const part of allContent) {
89
+ if (part.type === "text" && part.text) {
90
+ content.push({ type: "text", text: part.text });
91
+ }
92
+ else if (part.type === "image" && part.image) {
93
+ content.push({
94
+ type: "file",
95
+ data: toUrlOrString(part.image),
96
+ mediaType: inferImageMediaType(part.image),
97
+ });
98
+ }
99
+ else if (part.type === "file" && part.data && part.mimeType) {
100
+ content.push({
101
+ type: "file",
102
+ data: toUrlOrString(part.data),
103
+ mediaType: part.mimeType,
104
+ });
105
+ }
106
+ }
107
+ if (content.length > 0) {
108
+ result.push({ role: "user", content });
109
+ }
110
+ }
111
+ function convertAssistantMessage(message, result) {
112
+ const accumulator = {
113
+ textParts: [],
114
+ toolResults: [],
115
+ };
116
+ let hasPendingToolResults = false;
117
+ for (const part of message.content) {
118
+ if (part.type === "text" && part.text) {
119
+ // Flush pending tool results before adding more text
120
+ if (hasPendingToolResults) {
121
+ flushAccumulator(accumulator, result);
122
+ hasPendingToolResults = false;
123
+ }
124
+ accumulator.textParts.push({ type: "text", text: part.text });
125
+ }
126
+ else if (part.type === "tool-call") {
127
+ if (processToolCall(part, accumulator)) {
128
+ hasPendingToolResults = true;
129
+ }
130
+ }
131
+ }
132
+ flushAccumulator(accumulator, result);
133
+ }
134
+ /**
135
+ * Converts thread messages to generic LLM messages.
136
+ * This format can then be easily converted to provider-specific formats.
137
+ */
138
+ export function toGenericMessages(messages) {
139
+ const result = [];
140
+ for (const message of messages) {
141
+ switch (message.role) {
142
+ case "system":
143
+ convertSystemMessage(message, result);
144
+ break;
145
+ case "user":
146
+ convertUserMessage(message, result);
147
+ break;
148
+ case "assistant":
149
+ convertAssistantMessage(message, result);
150
+ break;
151
+ }
152
+ }
153
+ return result;
154
+ }
155
+ //# sourceMappingURL=toGenericMessages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toGenericMessages.js","sourceRoot":"","sources":["../../../src/core/converters/toGenericMessages.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA6EH,MAAM,iBAAiB,GAA2B;IAChD,GAAG,EAAE,YAAY;IACjB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,WAAW;IAChB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,eAAe;IACpB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,cAAc;IACnB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,YAAY;IACjB,IAAI,EAAE,YAAY;IAClB,IAAI,EAAE,YAAY;CACnB,CAAC;AAEF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AC;gBACzC,MAAM;QACV,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -15,4 +15,5 @@ export type { ToolCallStreamController } from "./modules/tool-call.js";
15
15
  export { createObjectStream } from "./object/createObjectStream.js";
16
16
  export { ObjectStreamResponse, fromObjectStreamResponse, } from "./object/ObjectStreamResponse.js";
17
17
  export type { ObjectStreamChunk } from "./object/types.js";
18
+ export * from "./converters/index.js";
18
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,6BAA6B,EAC7B,+BAA+B,GAChC,sCAAmC;AACpC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,IAAI,6BAA6B,GACtD,wDAAqD;AACtD,OAAO,EAAE,eAAe,EAAE,6BAA0B;AACpD,YAAY,EAAE,yBAAyB,EAAE,sCAAmC;AAC5E,YAAY,EAAE,oBAAoB,EAAE,kCAA+B;AACnE,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAClB,kDAA+C;AAChD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,qCAAkC;AAC/E,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,kEAA+D;AAChE,OAAO,EACL,sBAAsB,EACtB,KAAK,oBAAoB,EACzB,KAAK,wBAAwB,EAC7B,KAAK,6BAA6B,GACnC,6DAA0D;AAC3D,OAAO,EAAE,sBAAsB,EAAE,iDAA8C;AAC/E,YAAY,EAAE,gBAAgB,EAAE,yBAAsB;AAEtD,gCAAuB;AACvB,YAAY,EAAE,oBAAoB,EAAE,0BAAuB;AAC3D,YAAY,EAAE,wBAAwB,EAAE,+BAA4B;AAEpE,OAAO,EAAE,kBAAkB,EAAE,uCAAoC;AACjE,OAAO,EACL,oBAAoB,EACpB,wBAAwB,GACzB,yCAAsC;AACvC,YAAY,EAAE,iBAAiB,EAAE,0BAAuB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,6BAA6B,EAC7B,+BAA+B,GAChC,sCAAmC;AACpC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,IAAI,6BAA6B,GACtD,wDAAqD;AACtD,OAAO,EAAE,eAAe,EAAE,6BAA0B;AACpD,YAAY,EAAE,yBAAyB,EAAE,sCAAmC;AAC5E,YAAY,EAAE,oBAAoB,EAAE,kCAA+B;AACnE,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAClB,kDAA+C;AAChD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,qCAAkC;AAC/E,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,kEAA+D;AAChE,OAAO,EACL,sBAAsB,EACtB,KAAK,oBAAoB,EACzB,KAAK,wBAAwB,EAC7B,KAAK,6BAA6B,GACnC,6DAA0D;AAC3D,OAAO,EAAE,sBAAsB,EAAE,iDAA8C;AAC/E,YAAY,EAAE,gBAAgB,EAAE,yBAAsB;AAEtD,gCAAuB;AACvB,YAAY,EAAE,oBAAoB,EAAE,0BAAuB;AAC3D,YAAY,EAAE,wBAAwB,EAAE,+BAA4B;AAEpE,OAAO,EAAE,kBAAkB,EAAE,uCAAoC;AACjE,OAAO,EACL,oBAAoB,EACpB,wBAAwB,GACzB,yCAAsC;AACvC,YAAY,EAAE,iBAAiB,EAAE,0BAAuB;AAExD,sCAA6B"}
@@ -9,4 +9,5 @@ export { AssistantMessageStream } from "./accumulators/AssistantMessageStream.js
9
9
  export * from "./tool/index.js";
10
10
  export { createObjectStream } from "./object/createObjectStream.js";
11
11
  export { ObjectStreamResponse, fromObjectStreamResponse, } from "./object/ObjectStreamResponse.js";
12
+ export * from "./converters/index.js";
12
13
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,6BAA6B,EAC7B,+BAA+B,GAChC,sCAAmC;AACpC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,IAAI,6BAA6B,GACtD,wDAAqD;AACtD,OAAO,EAAE,eAAe,EAAE,6BAA0B;AAGpD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAClB,kDAA+C;AAChD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,qCAAkC;AAC/E,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,kEAA+D;AAChE,OAAO,EACL,sBAAsB,GAIvB,6DAA0D;AAC3D,OAAO,EAAE,sBAAsB,EAAE,iDAA8C;AAG/E,gCAAuB;AAIvB,OAAO,EAAE,kBAAkB,EAAE,uCAAoC;AACjE,OAAO,EACL,oBAAoB,EACpB,wBAAwB,GACzB,yCAAsC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,6BAA6B,EAC7B,+BAA+B,GAChC,sCAAmC;AACpC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,IAAI,6BAA6B,GACtD,wDAAqD;AACtD,OAAO,EAAE,eAAe,EAAE,6BAA0B;AAGpD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAClB,kDAA+C;AAChD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,qCAAkC;AAC/E,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,kEAA+D;AAChE,OAAO,EACL,sBAAsB,GAIvB,6DAA0D;AAC3D,OAAO,EAAE,sBAAsB,EAAE,iDAA8C;AAG/E,gCAAuB;AAIvB,OAAO,EAAE,kBAAkB,EAAE,uCAAoC;AACjE,OAAO,EACL,oBAAoB,EACpB,wBAAwB,GACzB,yCAAsC;AAGvC,sCAA6B"}
@@ -3,4 +3,5 @@ export { ToolResponse, type ToolResponseLike } from "./ToolResponse.js";
3
3
  export { ToolExecutionStream } from "./ToolExecutionStream.js";
4
4
  export type { ToolCallReader } from "./tool-types.js";
5
5
  export { toolResultStream as unstable_toolResultStream, unstable_runPendingTools, type ToolResultStreamOptions, } from "./toolResultStream.js";
6
+ export { toJSONSchema, toToolsJSONSchema, type ToolJSONSchema, type ToToolsJSONSchemaOptions, } from "./schema-utils.js";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/tool/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,IAAI,EAAE,wBAAqB;AACzC,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,0BAAuB;AACrE,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,YAAY,EAAE,cAAc,EAAE,wBAAqB;AACnD,OAAO,EACL,gBAAgB,IAAI,yBAAyB,EAC7C,wBAAwB,EACxB,KAAK,uBAAuB,GAC7B,8BAA2B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/tool/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,IAAI,EAAE,wBAAqB;AACzC,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,0BAAuB;AACrE,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,YAAY,EAAE,cAAc,EAAE,wBAAqB;AACnD,OAAO,EACL,gBAAgB,IAAI,yBAAyB,EAC7C,wBAAwB,EACxB,KAAK,uBAAuB,GAC7B,8BAA2B;AAC5B,OAAO,EACL,YAAY,EACZ,iBAAiB,EACjB,KAAK,cAAc,EACnB,KAAK,wBAAwB,GAC9B,0BAAuB"}
@@ -1,4 +1,5 @@
1
1
  export { ToolResponse } from "./ToolResponse.js";
2
2
  export { ToolExecutionStream } from "./ToolExecutionStream.js";
3
3
  export { toolResultStream as unstable_toolResultStream, unstable_runPendingTools, } from "./toolResultStream.js";
4
+ export { toJSONSchema, toToolsJSONSchema, } from "./schema-utils.js";
4
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/tool/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAyB,0BAAuB;AACrE,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAE5D,OAAO,EACL,gBAAgB,IAAI,yBAAyB,EAC7C,wBAAwB,GAEzB,8BAA2B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/tool/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAyB,0BAAuB;AACrE,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAE5D,OAAO,EACL,gBAAgB,IAAI,yBAAyB,EAC7C,wBAAwB,GAEzB,8BAA2B;AAC5B,OAAO,EACL,YAAY,EACZ,iBAAiB,GAGlB,0BAAuB"}
@@ -0,0 +1,32 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import type { Tool } from "./tool-types.js";
4
+ /**
5
+ * Type for a tool definition with JSON Schema parameters.
6
+ */
7
+ export type ToolJSONSchema = {
8
+ description?: string;
9
+ parameters: JSONSchema7;
10
+ };
11
+ export type ToToolsJSONSchemaOptions = {
12
+ /**
13
+ * Filter to determine which tools to include.
14
+ * Defaults to excluding disabled tools and backend tools.
15
+ */
16
+ filter?: (name: string, tool: Tool) => boolean;
17
+ };
18
+ /**
19
+ * Converts a schema to JSONSchema7.
20
+ * Supports:
21
+ * - StandardSchemaV1 with ~standard.toJSONSchema (e.g., Zod v4)
22
+ * - Objects with toJSONSchema() method
23
+ * - Objects with toJSON() method
24
+ * - Plain JSONSchema7 objects
25
+ */
26
+ export declare function toJSONSchema(schema: StandardSchemaV1 | JSONSchema7): JSONSchema7;
27
+ /**
28
+ * Converts a record of tools to a record of tool definitions with JSON Schema parameters.
29
+ * By default, filters out disabled tools and backend tools.
30
+ */
31
+ export declare function toToolsJSONSchema(tools: Record<string, Tool> | undefined, options?: ToToolsJSONSchemaOptions): Record<string, ToolJSONSchema>;
32
+ //# sourceMappingURL=schema-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-utils.d.ts","sourceRoot":"","sources":["../../../src/core/tool/schema-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAqB;AAEzC;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,WAAW,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CAChD,CAAC;AAiCF;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,gBAAgB,GAAG,WAAW,GACrC,WAAW,CAqBb;AAMD;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,EACvC,OAAO,GAAE,wBAA6B,GACrC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAgBhC"}
@@ -0,0 +1,67 @@
1
+ function isStandardSchema(schema) {
2
+ return (typeof schema === "object" &&
3
+ schema !== null &&
4
+ "~standard" in schema &&
5
+ typeof schema["~standard"] === "object");
6
+ }
7
+ function hasToJSONSchemaMethod(schema) {
8
+ return (typeof schema === "object" &&
9
+ schema !== null &&
10
+ "toJSONSchema" in schema &&
11
+ typeof schema.toJSONSchema === "function");
12
+ }
13
+ function hasToJSONMethod(schema) {
14
+ return (typeof schema === "object" &&
15
+ schema !== null &&
16
+ "toJSON" in schema &&
17
+ typeof schema.toJSON === "function");
18
+ }
19
+ /**
20
+ * Converts a schema to JSONSchema7.
21
+ * Supports:
22
+ * - StandardSchemaV1 with ~standard.toJSONSchema (e.g., Zod v4)
23
+ * - Objects with toJSONSchema() method
24
+ * - Objects with toJSON() method
25
+ * - Plain JSONSchema7 objects
26
+ */
27
+ export function toJSONSchema(schema) {
28
+ // StandardSchemaV1 with ~standard.toJSONSchema (e.g., Zod v4)
29
+ if (isStandardSchema(schema)) {
30
+ const toJSONSchemaMethod = schema["~standard"].toJSONSchema;
31
+ if (typeof toJSONSchemaMethod === "function") {
32
+ return toJSONSchemaMethod();
33
+ }
34
+ }
35
+ // toJSONSchema method on the schema itself
36
+ if (hasToJSONSchemaMethod(schema)) {
37
+ return schema.toJSONSchema();
38
+ }
39
+ // toJSON method on the schema
40
+ if (hasToJSONMethod(schema)) {
41
+ return schema.toJSON();
42
+ }
43
+ // Already a plain JSONSchema7
44
+ return schema;
45
+ }
46
+ function defaultToolFilter(_name, tool) {
47
+ return !tool.disabled && tool.type !== "backend";
48
+ }
49
+ /**
50
+ * Converts a record of tools to a record of tool definitions with JSON Schema parameters.
51
+ * By default, filters out disabled tools and backend tools.
52
+ */
53
+ export function toToolsJSONSchema(tools, options = {}) {
54
+ if (!tools)
55
+ return {};
56
+ const filter = options.filter ?? defaultToolFilter;
57
+ return Object.fromEntries(Object.entries(tools)
58
+ .filter(([name, tool]) => filter(name, tool) && tool.parameters)
59
+ .map(([name, tool]) => [
60
+ name,
61
+ {
62
+ ...(tool.description && { description: tool.description }),
63
+ parameters: toJSONSchema(tool.parameters),
64
+ },
65
+ ]));
66
+ }
67
+ //# sourceMappingURL=schema-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-utils.js","sourceRoot":"","sources":["../../../src/core/tool/schema-utils.ts"],"names":[],"mappings":"AAoBA,SAAS,gBAAgB,CAAC,MAAe;IAGvC,OAAO,CACL,OAAO,MAAM,KAAK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C;AACnD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,KAAuC,EACvC,UAAoC,EAAE;IAEtC,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IAEtB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,iBAAiB,CAAC;IAEnD,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;SAClB,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC;SAC/D,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC;QACrB,IAAI;QACJ;YACE,GAAG,CAAC,IAAI,CAAC,WAAW,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC;YAC1D,UAAU,EAAE,YAAY,CAAC,IAAI,CAAC,UAAW,CAAC;SAC3C;KACF,CAAC,CACL,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assistant-stream",
3
- "version": "0.2.47",
3
+ "version": "0.3.0",
4
4
  "description": "Streaming utilities for AI assistants",
5
5
  "keywords": [
6
6
  "ai",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/json-schema": "^7.0.15",
45
- "vitest": "^4.0.17",
45
+ "vitest": "^4.0.18",
46
46
  "@assistant-ui/x-buildutils": "0.0.1"
47
47
  },
48
48
  "publishConfig": {
@@ -0,0 +1,12 @@
1
+ export {
2
+ toGenericMessages,
3
+ type GenericMessage,
4
+ type GenericSystemMessage,
5
+ type GenericUserMessage,
6
+ type GenericAssistantMessage,
7
+ type GenericToolMessage,
8
+ type GenericTextPart,
9
+ type GenericFilePart,
10
+ type GenericToolCallPart,
11
+ type GenericToolResultPart,
12
+ } from "./toGenericMessages";