@copilotkit/shared 1.54.1 → 1.55.0-next.7
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/CHANGELOG.md +5 -1
- package/dist/constants/index.cjs +5 -0
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +4 -1
- package/dist/constants/index.d.cts.map +1 -1
- package/dist/constants/index.d.mts +4 -1
- package/dist/constants/index.d.mts.map +1 -1
- package/dist/constants/index.mjs +4 -1
- package/dist/constants/index.mjs.map +1 -1
- package/dist/finalize-events.cjs +106 -0
- package/dist/finalize-events.cjs.map +1 -0
- package/dist/finalize-events.d.cts +11 -0
- package/dist/finalize-events.d.cts.map +1 -0
- package/dist/finalize-events.d.mts +11 -0
- package/dist/finalize-events.d.mts.map +1 -0
- package/dist/finalize-events.mjs +105 -0
- package/dist/finalize-events.mjs.map +1 -0
- package/dist/index.cjs +25 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +9 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +10 -3
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +286 -5
- package/dist/index.umd.js.map +1 -1
- package/dist/logger.cjs +7 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +6 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/standard-schema.cjs +29 -0
- package/dist/standard-schema.cjs.map +1 -0
- package/dist/standard-schema.d.cts +32 -0
- package/dist/standard-schema.d.cts.map +1 -0
- package/dist/standard-schema.d.mts +32 -0
- package/dist/standard-schema.d.mts.map +1 -0
- package/dist/standard-schema.mjs +28 -0
- package/dist/standard-schema.mjs.map +1 -0
- package/dist/transcription-errors.cjs +84 -0
- package/dist/transcription-errors.cjs.map +1 -0
- package/dist/transcription-errors.d.cts +52 -0
- package/dist/transcription-errors.d.cts.map +1 -0
- package/dist/transcription-errors.d.mts +52 -0
- package/dist/transcription-errors.d.mts.map +1 -0
- package/dist/transcription-errors.mjs +82 -0
- package/dist/transcription-errors.mjs.map +1 -0
- package/dist/types/message.d.cts +1 -1
- package/dist/types/message.d.mts +1 -1
- package/dist/utils/index.cjs +49 -0
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +24 -2
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +24 -2
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +45 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/types.cjs +9 -0
- package/dist/utils/types.cjs.map +1 -0
- package/dist/utils/types.d.cts +12 -1
- package/dist/utils/types.d.cts.map +1 -1
- package/dist/utils/types.d.mts +12 -1
- package/dist/utils/types.d.mts.map +1 -1
- package/dist/utils/types.mjs +7 -0
- package/dist/utils/types.mjs.map +1 -0
- package/package.json +11 -4
- package/src/__tests__/standard-schema-types.test.ts +144 -0
- package/src/__tests__/standard-schema.test.ts +267 -0
- package/src/__tests__/zod-regression.test.ts +319 -0
- package/src/constants/index.ts +5 -0
- package/src/finalize-events.ts +154 -0
- package/src/index.ts +12 -0
- package/src/logger.ts +1 -0
- package/src/standard-schema.ts +76 -0
- package/src/transcription-errors.ts +99 -0
- package/src/types/message.ts +1 -1
- package/src/utils/index.ts +58 -0
- package/src/utils/types.ts +21 -0
- package/src/utils.test.ts +66 -0
- package/tsconfig.json +9 -1
- package/tsdown.config.ts +10 -2
package/src/utils/index.ts
CHANGED
|
@@ -6,6 +6,8 @@ export * from "./types";
|
|
|
6
6
|
export * from "./random-id";
|
|
7
7
|
export * from "./requests";
|
|
8
8
|
|
|
9
|
+
import * as PartialJSON from "partial-json";
|
|
10
|
+
|
|
9
11
|
/**
|
|
10
12
|
* Safely parses a JSON string into an object
|
|
11
13
|
* @param json The JSON string to parse
|
|
@@ -20,6 +22,39 @@ export function parseJson(json: string, fallback: any = "unset") {
|
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Parses a partial/incomplete JSON string, returning as much valid data as possible.
|
|
27
|
+
* Falls back to an empty object if parsing fails entirely.
|
|
28
|
+
*/
|
|
29
|
+
export function partialJSONParse(json: string) {
|
|
30
|
+
try {
|
|
31
|
+
const parsed = PartialJSON.parse(json);
|
|
32
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
33
|
+
return parsed;
|
|
34
|
+
}
|
|
35
|
+
return {};
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns an exponential backoff function suitable for Phoenix.js
|
|
43
|
+
* `reconnectAfterMs` and `rejoinAfterMs` options.
|
|
44
|
+
*
|
|
45
|
+
* @param baseMs - Initial delay for the first retry attempt.
|
|
46
|
+
* @param maxMs - Upper bound — delays are capped at this value.
|
|
47
|
+
*
|
|
48
|
+
* Phoenix calls the returned function with a 1-based `tries` count.
|
|
49
|
+
* The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
|
|
50
|
+
*/
|
|
51
|
+
export function phoenixExponentialBackoff(
|
|
52
|
+
baseMs: number,
|
|
53
|
+
maxMs: number,
|
|
54
|
+
): (tries: number) => number {
|
|
55
|
+
return (tries: number) => Math.min(baseMs * 2 ** (tries - 1), maxMs);
|
|
56
|
+
}
|
|
57
|
+
|
|
23
58
|
/**
|
|
24
59
|
* Maps an array of items to a new array, skipping items that throw errors during mapping
|
|
25
60
|
* @param items The array to map
|
|
@@ -47,3 +82,26 @@ export function tryMap<TItem, TMapped>(
|
|
|
47
82
|
export function isMacOS(): boolean {
|
|
48
83
|
return /Mac|iMac|Macintosh/i.test(navigator.userAgent);
|
|
49
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Safely parses a JSON string into a tool arguments object.
|
|
88
|
+
* Returns the parsed object only if it's a plain object (not an array, null, etc.).
|
|
89
|
+
* Falls back to an empty object for any non-object JSON value or parse failure.
|
|
90
|
+
*/
|
|
91
|
+
export function safeParseToolArgs(raw: string): Record<string, unknown> {
|
|
92
|
+
try {
|
|
93
|
+
const parsed = JSON.parse(raw);
|
|
94
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
console.warn(
|
|
98
|
+
`[CopilotKit] Tool arguments parsed to non-object (${typeof parsed}), falling back to empty object`,
|
|
99
|
+
);
|
|
100
|
+
return {};
|
|
101
|
+
} catch {
|
|
102
|
+
console.warn(
|
|
103
|
+
"[CopilotKit] Failed to parse tool arguments, falling back to empty object",
|
|
104
|
+
);
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/utils/types.ts
CHANGED
|
@@ -19,8 +19,29 @@ export interface AgentDescription {
|
|
|
19
19
|
description: string;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export type RuntimeMode = "sse" | "intelligence";
|
|
23
|
+
|
|
24
|
+
export const RUNTIME_MODE_SSE = "sse" as const;
|
|
25
|
+
export const RUNTIME_MODE_INTELLIGENCE = "intelligence" as const;
|
|
26
|
+
|
|
27
|
+
export interface IntelligenceRuntimeInfo {
|
|
28
|
+
wsUrl: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type RuntimeLicenseStatus =
|
|
32
|
+
| "valid"
|
|
33
|
+
| "none"
|
|
34
|
+
| "expired"
|
|
35
|
+
| "expiring"
|
|
36
|
+
| "invalid"
|
|
37
|
+
| "unknown";
|
|
38
|
+
|
|
22
39
|
export interface RuntimeInfo {
|
|
23
40
|
version: string;
|
|
24
41
|
agents: Record<string, AgentDescription>;
|
|
25
42
|
audioFileTranscriptionEnabled: boolean;
|
|
43
|
+
mode: RuntimeMode;
|
|
44
|
+
intelligence?: IntelligenceRuntimeInfo;
|
|
45
|
+
a2uiEnabled?: boolean;
|
|
46
|
+
licenseStatus?: RuntimeLicenseStatus;
|
|
26
47
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, test, expect, vi } from "vitest";
|
|
2
|
+
import { partialJSONParse, safeParseToolArgs } from "./utils";
|
|
3
|
+
|
|
4
|
+
describe("partialJSONParse", () => {
|
|
5
|
+
test("should parse a valid object", () => {
|
|
6
|
+
expect(partialJSONParse('{"key": "value"}')).toEqual({ key: "value" });
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("should return {} for a JSON string value", () => {
|
|
10
|
+
expect(partialJSONParse('"hello"')).toEqual({});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("should return {} for a JSON number", () => {
|
|
14
|
+
expect(partialJSONParse("42")).toEqual({});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("should return {} for a JSON boolean", () => {
|
|
18
|
+
expect(partialJSONParse("true")).toEqual({});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("should return {} for a JSON null", () => {
|
|
22
|
+
expect(partialJSONParse("null")).toEqual({});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("should return {} for a JSON array", () => {
|
|
26
|
+
expect(partialJSONParse("[1, 2, 3]")).toEqual({});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("should return {} for unparseable input", () => {
|
|
30
|
+
expect(partialJSONParse("not-json")).toEqual({});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("should return {} for an empty string", () => {
|
|
34
|
+
expect(partialJSONParse("")).toEqual({});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("safeParseToolArgs", () => {
|
|
39
|
+
test("should parse a valid JSON object string", () => {
|
|
40
|
+
expect(safeParseToolArgs('{"key": "value"}')).toEqual({ key: "value" });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("should return {} for a JSON string value", () => {
|
|
44
|
+
expect(safeParseToolArgs('"hello"')).toEqual({});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("should return {} for a JSON number", () => {
|
|
48
|
+
expect(safeParseToolArgs("42")).toEqual({});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("should return {} for a JSON array", () => {
|
|
52
|
+
expect(safeParseToolArgs("[1, 2, 3]")).toEqual({});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("should return {} for malformed JSON", () => {
|
|
56
|
+
expect(safeParseToolArgs("{broken")).toEqual({});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("should return {} for a JSON null", () => {
|
|
60
|
+
expect(safeParseToolArgs("null")).toEqual({});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("should return {} for a JSON boolean", () => {
|
|
64
|
+
expect(safeParseToolArgs("true")).toEqual({});
|
|
65
|
+
});
|
|
66
|
+
});
|
package/tsconfig.json
CHANGED
|
@@ -2,8 +2,16 @@
|
|
|
2
2
|
"extends": "../tsconfig/base.json",
|
|
3
3
|
"include": ["."],
|
|
4
4
|
"compilerOptions": {
|
|
5
|
+
"target": "es2017",
|
|
5
6
|
"lib": ["es2017", "dom"],
|
|
6
7
|
"resolveJsonModule": true
|
|
7
8
|
},
|
|
8
|
-
"exclude": [
|
|
9
|
+
"exclude": [
|
|
10
|
+
"dist",
|
|
11
|
+
"build",
|
|
12
|
+
"node_modules",
|
|
13
|
+
"**/*.test.ts",
|
|
14
|
+
"**/*.test.tsx",
|
|
15
|
+
"**/__tests__/*"
|
|
16
|
+
]
|
|
9
17
|
}
|
package/tsdown.config.ts
CHANGED
|
@@ -18,8 +18,14 @@ export default defineConfig([
|
|
|
18
18
|
sourcemap: true,
|
|
19
19
|
target: "es2018",
|
|
20
20
|
outDir: "dist",
|
|
21
|
-
external: [
|
|
22
|
-
|
|
21
|
+
external: [
|
|
22
|
+
"zod",
|
|
23
|
+
"graphql",
|
|
24
|
+
"uuid",
|
|
25
|
+
"@ag-ui/core",
|
|
26
|
+
"@ag-ui/client",
|
|
27
|
+
"partial-json",
|
|
28
|
+
],
|
|
23
29
|
outputOptions(options) {
|
|
24
30
|
options.entryFileNames = "[name].umd.js";
|
|
25
31
|
options.globals = {
|
|
@@ -27,8 +33,10 @@ export default defineConfig([
|
|
|
27
33
|
graphql: "GraphQL",
|
|
28
34
|
uuid: "UUID",
|
|
29
35
|
"@ag-ui/core": "AgUICore",
|
|
36
|
+
"@ag-ui/client": "AgUIClient",
|
|
30
37
|
"@segment/analytics-node": "SegmentAnalyticsNode",
|
|
31
38
|
chalk: "chalk",
|
|
39
|
+
"partial-json": "PartialJSON",
|
|
32
40
|
};
|
|
33
41
|
return options;
|
|
34
42
|
},
|