@copilotkit/shared 1.80.0 → 1.82.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.
- package/dist/a2ui-envelope.cjs +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +139 -0
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +60 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +60 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +134 -1
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +177 -0
- package/dist/generative-ui/basic-functions.cjs.map +1 -0
- package/dist/generative-ui/basic-functions.d.cts +108 -0
- package/dist/generative-ui/basic-functions.d.cts.map +1 -0
- package/dist/generative-ui/basic-functions.d.mts +108 -0
- package/dist/generative-ui/basic-functions.d.mts.map +1 -0
- package/dist/generative-ui/basic-functions.mjs +167 -0
- package/dist/generative-ui/basic-functions.mjs.map +1 -0
- package/dist/generative-ui/child-refs.cjs +161 -0
- package/dist/generative-ui/child-refs.cjs.map +1 -0
- package/dist/generative-ui/child-refs.d.cts +68 -0
- package/dist/generative-ui/child-refs.d.cts.map +1 -0
- package/dist/generative-ui/child-refs.d.mts +68 -0
- package/dist/generative-ui/child-refs.d.mts.map +1 -0
- package/dist/generative-ui/child-refs.mjs +152 -0
- package/dist/generative-ui/child-refs.mjs.map +1 -0
- package/dist/generative-ui/is-record.cjs +10 -0
- package/dist/generative-ui/is-record.cjs.map +1 -0
- package/dist/generative-ui/is-record.mjs +9 -0
- package/dist/generative-ui/is-record.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +91 -0
- package/dist/generative-ui/prompt.cjs.map +1 -0
- package/dist/generative-ui/prompt.d.cts +13 -0
- package/dist/generative-ui/prompt.d.cts.map +1 -0
- package/dist/generative-ui/prompt.d.mts +13 -0
- package/dist/generative-ui/prompt.d.mts.map +1 -0
- package/dist/generative-ui/prompt.mjs +90 -0
- package/dist/generative-ui/prompt.mjs.map +1 -0
- package/dist/generative-ui/render-ui-args.cjs +160 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -0
- package/dist/generative-ui/render-ui-args.d.cts +40 -0
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
- package/dist/generative-ui/render-ui-args.d.mts +40 -0
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
- package/dist/generative-ui/render-ui-args.mjs +159 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +101 -0
- package/dist/generative-ui/types.cjs.map +1 -0
- package/dist/generative-ui/types.d.cts +270 -0
- package/dist/generative-ui/types.d.cts.map +1 -0
- package/dist/generative-ui/types.d.mts +270 -0
- package/dist/generative-ui/types.d.mts.map +1 -0
- package/dist/generative-ui/types.mjs +86 -0
- package/dist/generative-ui/types.mjs.map +1 -0
- package/dist/generative-ui/validate-operation.cjs +405 -0
- package/dist/generative-ui/validate-operation.cjs.map +1 -0
- package/dist/generative-ui/validate-operation.d.cts +86 -0
- package/dist/generative-ui/validate-operation.d.cts.map +1 -0
- package/dist/generative-ui/validate-operation.d.mts +86 -0
- package/dist/generative-ui/validate-operation.d.mts.map +1 -0
- package/dist/generative-ui/validate-operation.mjs +399 -0
- package/dist/generative-ui/validate-operation.mjs.map +1 -0
- package/dist/index.cjs +63 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +9 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1421 -20
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/telemetry-client.cjs +1 -1
- package/dist/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/telemetry/telemetry-client.d.cts.map +1 -1
- package/dist/telemetry/telemetry-client.d.mts.map +1 -1
- package/dist/telemetry/telemetry-client.mjs +1 -1
- package/dist/telemetry/telemetry-client.mjs.map +1 -1
- package/dist/utils/dashboard-url.cjs +19 -0
- package/dist/utils/dashboard-url.cjs.map +1 -0
- package/dist/utils/dashboard-url.mjs +18 -0
- package/dist/utils/dashboard-url.mjs.map +1 -0
- package/dist/utils/index.cjs +2 -1
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +2 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-learning.cjs +10 -7
- package/dist/utils/inspector-learning.cjs.map +1 -1
- package/dist/utils/inspector-learning.d.cts.map +1 -1
- package/dist/utils/inspector-learning.d.mts.map +1 -1
- package/dist/utils/inspector-learning.mjs +11 -7
- package/dist/utils/inspector-learning.mjs.map +1 -1
- package/dist/utils/inspector-metadata.cjs +2 -3
- package/dist/utils/inspector-metadata.cjs.map +1 -1
- package/dist/utils/inspector-metadata.d.cts.map +1 -1
- package/dist/utils/inspector-metadata.d.mts.map +1 -1
- package/dist/utils/inspector-metadata.mjs +3 -3
- package/dist/utils/inspector-metadata.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/a2ui-envelope.test.ts +434 -0
- package/src/__tests__/a2ui-prompts.test.ts +362 -0
- package/src/a2ui-envelope.ts +309 -0
- package/src/a2ui-prompts.ts +207 -0
- package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
- package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
- package/src/generative-ui/basic-functions.ts +282 -0
- package/src/generative-ui/child-refs.ts +210 -0
- package/src/generative-ui/index.ts +6 -0
- package/src/generative-ui/is-record.ts +4 -0
- package/src/generative-ui/prompt.ts +174 -0
- package/src/generative-ui/render-ui-args.ts +199 -0
- package/src/generative-ui/types.ts +314 -0
- package/src/generative-ui/validate-operation.ts +723 -0
- package/src/index.ts +15 -0
- package/src/telemetry/telemetry-client.test.ts +18 -0
- package/src/telemetry/telemetry-client.ts +3 -1
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/inspector-learning.test.ts +58 -0
- package/src/utils/inspector-learning.ts +26 -5
- package/src/utils/inspector-metadata.test.ts +2 -1
- package/src/utils/inspector-metadata.ts +3 -13
- package/src/utils.test.ts +71 -0
package/src/index.ts
CHANGED
|
@@ -174,8 +174,23 @@ export function createLicenseContextValue(
|
|
|
174
174
|
export {
|
|
175
175
|
A2UI_DEFAULT_GENERATION_GUIDELINES,
|
|
176
176
|
A2UI_DEFAULT_DESIGN_GUIDELINES,
|
|
177
|
+
ALL_GENERATIVE_UI_GUIDE_FEATURES,
|
|
178
|
+
buildGenerativeUIMarkdownGuidelines,
|
|
179
|
+
generativeUIGuideFeatures,
|
|
180
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES,
|
|
181
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
|
|
182
|
+
GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION,
|
|
177
183
|
} from "./a2ui-prompts";
|
|
184
|
+
export type { GenerativeUIGuideFeatures } from "./a2ui-prompts";
|
|
185
|
+
export {
|
|
186
|
+
COPILOTKIT_FORWARDED_PROPS_KEY,
|
|
187
|
+
COPILOTKIT_UI_ENVELOPE_FORMAT,
|
|
188
|
+
envelopeToManifest,
|
|
189
|
+
readCopilotKitUIEnvelope,
|
|
190
|
+
} from "./a2ui-envelope";
|
|
191
|
+
export type { CopilotKitUIEnvelope } from "./a2ui-envelope";
|
|
178
192
|
|
|
179
193
|
export type { DebugEventEnvelope } from "./debug-event-envelope";
|
|
180
194
|
|
|
181
195
|
export { GENERATE_SANDBOXED_UI_DESCRIPTION } from "./open-generative-ui-prompts";
|
|
196
|
+
export * from "./generative-ui";
|
|
@@ -214,6 +214,24 @@ describe("v1 TelemetryClient", () => {
|
|
|
214
214
|
});
|
|
215
215
|
});
|
|
216
216
|
|
|
217
|
+
test("an opt-out set after the client is created stops both sinks", async () => {
|
|
218
|
+
// e.g. dotenv.config() placed below the import that creates the client.
|
|
219
|
+
vi.stubEnv("COPILOTKIT_TELEMETRY_DISABLED", "");
|
|
220
|
+
vi.stubEnv("DO_NOT_TRACK", "");
|
|
221
|
+
try {
|
|
222
|
+
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
223
|
+
const client = makeClient({ sampleRate: 0.05 });
|
|
224
|
+
vi.stubEnv("DO_NOT_TRACK", "1");
|
|
225
|
+
|
|
226
|
+
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
|
|
227
|
+
|
|
228
|
+
expect(lambdaSpy).not.toHaveBeenCalled();
|
|
229
|
+
expect(segmentTrackMock).not.toHaveBeenCalled();
|
|
230
|
+
} finally {
|
|
231
|
+
vi.unstubAllEnvs();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
217
235
|
test("an anonymous event sampled out of Segment still reaches the lambda sink", async () => {
|
|
218
236
|
// Math.random=0.99 vs sampleRate=0.05. The gate is Segment's alone now:
|
|
219
237
|
// the lambda sink is ours and takes every event, so a rate that drops
|
|
@@ -125,7 +125,9 @@ export class TelemetryClient {
|
|
|
125
125
|
properties: AnalyticsEvents[K],
|
|
126
126
|
identity: ResolvedTelemetryIdentity,
|
|
127
127
|
): Promise<void> {
|
|
128
|
-
|
|
128
|
+
// Re-read the environment: an opt-out set after import (for example a
|
|
129
|
+
// dotenv call below the imports) must still stop events.
|
|
130
|
+
if (this.telemetryDisabled || isTelemetryDisabled()) {
|
|
129
131
|
return;
|
|
130
132
|
}
|
|
131
133
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks dashboard navigation transport and credentials only.
|
|
3
|
+
* Callers must separately enforce the configured origin and allowed paths.
|
|
4
|
+
* This policy does not authorize server-side requests.
|
|
5
|
+
*/
|
|
6
|
+
export function isSafeDashboardUrl(url: URL): boolean {
|
|
7
|
+
const local =
|
|
8
|
+
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname) ||
|
|
9
|
+
/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+localhost$/u.test(
|
|
10
|
+
url.hostname,
|
|
11
|
+
);
|
|
12
|
+
return (
|
|
13
|
+
!url.username &&
|
|
14
|
+
!url.password &&
|
|
15
|
+
(url.protocol === "https:" || (url.protocol === "http:" && local))
|
|
16
|
+
);
|
|
17
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -27,13 +27,19 @@ export function parseJson(json: string, fallback: any = "unset") {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// Allow partial values of every type except unterminated numbers. A cut-off
|
|
31
|
+
// number is a *different* number (`{"max":1` is about to become 100, not 1),
|
|
32
|
+
// while a cut-off string is still the beginning of the final string. Numbers
|
|
33
|
+
// therefore only appear once terminated by `,`, `}` or `]`.
|
|
34
|
+
const PARTIAL_JSON_ALLOW = PartialJSON.Allow.ALL & ~PartialJSON.Allow.NUM;
|
|
35
|
+
|
|
30
36
|
/**
|
|
31
37
|
* Parses a partial/incomplete JSON string, returning as much valid data as possible.
|
|
32
38
|
* Falls back to an empty object if parsing fails entirely.
|
|
33
39
|
*/
|
|
34
40
|
export function partialJSONParse(json: string) {
|
|
35
41
|
try {
|
|
36
|
-
const parsed = PartialJSON.parse(json);
|
|
42
|
+
const parsed = PartialJSON.parse(json, PARTIAL_JSON_ALLOW);
|
|
37
43
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
38
44
|
return parsed;
|
|
39
45
|
}
|
|
@@ -363,3 +363,61 @@ describe("Inspector Learning wire validation", () => {
|
|
|
363
363
|
expect(parseInspectorLearningRequestV1({ skillsPage: 0 })).toBeUndefined();
|
|
364
364
|
});
|
|
365
365
|
});
|
|
366
|
+
|
|
367
|
+
it.each([
|
|
368
|
+
"http://localhost:7066",
|
|
369
|
+
"http://127.0.0.1:7066",
|
|
370
|
+
"http://[::1]:7066",
|
|
371
|
+
"http://cpk.localhost:7066",
|
|
372
|
+
"http://team.cpk.localhost:7066",
|
|
373
|
+
"https://dashboard.example.com",
|
|
374
|
+
])("accepts Learning links on configured dashboard origin %s", (origin) => {
|
|
375
|
+
const url = `${origin}/o/acme/support/learning`;
|
|
376
|
+
|
|
377
|
+
expect(parseInspectorLearningUrl(url, origin)).toBe(url);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it.each([
|
|
381
|
+
"http://remote.example.com",
|
|
382
|
+
"http://cpk.localhost.example.com",
|
|
383
|
+
"http://-bad.localhost",
|
|
384
|
+
"http://bad_.localhost",
|
|
385
|
+
"http://bad..localhost",
|
|
386
|
+
"http://user:password@cpk.localhost",
|
|
387
|
+
"ftp://cpk.localhost",
|
|
388
|
+
"javascript:alert(1)",
|
|
389
|
+
])("rejects unsafe dashboard origin %s", (origin) => {
|
|
390
|
+
expect(
|
|
391
|
+
parseInspectorLearningUrl(`${origin}/o/acme/support/learning`, origin),
|
|
392
|
+
).toBeUndefined();
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it.each([
|
|
396
|
+
"http://localhost:7066",
|
|
397
|
+
"http://cpk.localhost:7067",
|
|
398
|
+
"https://cpk.localhost:7066",
|
|
399
|
+
"https://other.example.com",
|
|
400
|
+
])("rejects a Learning link to another origin %s", (origin) => {
|
|
401
|
+
expect(
|
|
402
|
+
parseInspectorLearningUrl(
|
|
403
|
+
`${origin}/o/acme/support/learning`,
|
|
404
|
+
"http://cpk.localhost:7066",
|
|
405
|
+
),
|
|
406
|
+
).toBeUndefined();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it.each([
|
|
410
|
+
["learning", "/account"],
|
|
411
|
+
["learning", "/o/acme/support/learning?redirect=elsewhere"],
|
|
412
|
+
["candidates", "/o/acme/support/learning/quality/analysis-results"],
|
|
413
|
+
["runs", "/o/acme/support/learning/quality/skills"],
|
|
414
|
+
["runs", "/o/acme/support/learning/quality/analysis-results#fragment"],
|
|
415
|
+
])("rejects invalid %s Learning path %s", (field, path) => {
|
|
416
|
+
const snapshot = emptySnapshot();
|
|
417
|
+
expect(
|
|
418
|
+
parseInspectorLearningSnapshotV1({
|
|
419
|
+
...snapshot,
|
|
420
|
+
links: { ...snapshot.links, [field]: `${snapshot.webAppOrigin}${path}` },
|
|
421
|
+
}),
|
|
422
|
+
).toBeUndefined();
|
|
423
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isSafeDashboardUrl } from "./dashboard-url";
|
|
2
|
+
|
|
1
3
|
/** A bounded page in the Inspector Learning snapshot. */
|
|
2
4
|
export interface InspectorLearningPage<T> {
|
|
3
5
|
readonly page: number;
|
|
@@ -132,9 +134,6 @@ export function parseInspectorLearningUrl(
|
|
|
132
134
|
try {
|
|
133
135
|
const parsed = new URL(value);
|
|
134
136
|
const configured = new URL(trustedOrigin);
|
|
135
|
-
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(
|
|
136
|
-
configured.hostname,
|
|
137
|
-
);
|
|
138
137
|
if (
|
|
139
138
|
parsed.username ||
|
|
140
139
|
parsed.password ||
|
|
@@ -143,8 +142,7 @@ export function parseInspectorLearningUrl(
|
|
|
143
142
|
configured.pathname !== "/" ||
|
|
144
143
|
configured.search ||
|
|
145
144
|
configured.hash ||
|
|
146
|
-
(configured
|
|
147
|
-
!(configured.protocol === "http:" && loopback)) ||
|
|
145
|
+
!isSafeDashboardUrl(configured) ||
|
|
148
146
|
parsed.origin !== configured.origin
|
|
149
147
|
) {
|
|
150
148
|
return undefined;
|
|
@@ -155,6 +153,26 @@ export function parseInspectorLearningUrl(
|
|
|
155
153
|
}
|
|
156
154
|
}
|
|
157
155
|
|
|
156
|
+
/** Validates the canonical dashboard route after the origin check. */
|
|
157
|
+
function validLearningPath(
|
|
158
|
+
value: string,
|
|
159
|
+
kind: "learning" | "candidates" | "runs",
|
|
160
|
+
): boolean {
|
|
161
|
+
const url = new URL(value);
|
|
162
|
+
if (url.search || url.hash) return false;
|
|
163
|
+
if (kind === "learning") {
|
|
164
|
+
return (
|
|
165
|
+
url.pathname === "/learning" ||
|
|
166
|
+
/^\/o\/[^/]+\/[^/]+\/learning(?:\/[^/]+)?$/u.test(url.pathname)
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return kind === "candidates"
|
|
170
|
+
? /^\/o\/[^/]+\/[^/]+\/learning\/[^/]+\/skills$/u.test(url.pathname)
|
|
171
|
+
: /^\/o\/[^/]+\/[^/]+\/learning\/[^/]+\/analysis-results$/u.test(
|
|
172
|
+
url.pathname,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
158
176
|
function parseEvidence(value: unknown): InspectorLearningEvidence | undefined {
|
|
159
177
|
if (record(value) && value.status === "unavailable") {
|
|
160
178
|
return Object.keys(value).length === 1
|
|
@@ -453,6 +471,9 @@ export function parseInspectorLearningSnapshotV1(
|
|
|
453
471
|
!learning ||
|
|
454
472
|
candidates === undefined ||
|
|
455
473
|
runs === undefined ||
|
|
474
|
+
!validLearningPath(learning, "learning") ||
|
|
475
|
+
(candidates !== null && !validLearningPath(candidates, "candidates")) ||
|
|
476
|
+
(runs !== null && !validLearningPath(runs, "runs")) ||
|
|
456
477
|
(value.pendingThreadCount > 0 && runs === null) ||
|
|
457
478
|
(value.pendingCandidateCount > 0 && candidates === null)
|
|
458
479
|
)
|
|
@@ -374,6 +374,8 @@ test.each([
|
|
|
374
374
|
"https://cloud.copilotkit.ai/manage",
|
|
375
375
|
"https://cloud.copilotkit.ai/manage/plan",
|
|
376
376
|
"http://localhost/manage",
|
|
377
|
+
"http://sub.localhost/manage",
|
|
378
|
+
"http://cpk.localhost:7066/manage",
|
|
377
379
|
"http://localhost:3000/manage",
|
|
378
380
|
"http://127.0.0.1:3000/manage",
|
|
379
381
|
"http://[::1]:3000/manage",
|
|
@@ -402,7 +404,6 @@ test.each([
|
|
|
402
404
|
"ftp://cloud.copilotkit.ai/manage",
|
|
403
405
|
"http://cloud.copilotkit.ai/manage",
|
|
404
406
|
"http://localhost.example.com/manage",
|
|
405
|
-
"http://sub.localhost/manage",
|
|
406
407
|
"http://127.0.0.2/manage",
|
|
407
408
|
"http://[::2]/manage",
|
|
408
409
|
"http://0.0.0.0/manage",
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isSafeDashboardUrl } from "./dashboard-url";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Inspector metadata supplied by a trusted CopilotKit runtime.
|
|
3
5
|
*
|
|
@@ -157,19 +159,7 @@ function parseSafeActionUrl(value: unknown): string | undefined {
|
|
|
157
159
|
return undefined;
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
|
|
161
|
-
return url;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const isLoopbackHost =
|
|
165
|
-
parsed.hostname === "localhost" ||
|
|
166
|
-
parsed.hostname === "127.0.0.1" ||
|
|
167
|
-
parsed.hostname === "[::1]";
|
|
168
|
-
if (parsed.protocol === "http:" && isLoopbackHost) {
|
|
169
|
-
return url;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return undefined;
|
|
162
|
+
return isSafeDashboardUrl(parsed) ? url : undefined;
|
|
173
163
|
}
|
|
174
164
|
|
|
175
165
|
function parseAction(value: unknown): InspectorAction | undefined {
|
package/src/utils.test.ts
CHANGED
|
@@ -33,6 +33,77 @@ describe("partialJSONParse", () => {
|
|
|
33
33
|
test("should return {} for an empty string", () => {
|
|
34
34
|
expect(partialJSONParse("")).toEqual({});
|
|
35
35
|
});
|
|
36
|
+
|
|
37
|
+
test("should withhold an unterminated trailing number", () => {
|
|
38
|
+
expect(partialJSONParse('{"value":72,"max":1')).toEqual({ value: 72 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("should withhold a number that is still unterminated at end of input", () => {
|
|
42
|
+
expect(partialJSONParse('{"value":72,"max":100')).toEqual({ value: 72 });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("should reveal a number once terminated", () => {
|
|
46
|
+
expect(partialJSONParse('{"value":72,"max":100}')).toEqual({
|
|
47
|
+
value: 72,
|
|
48
|
+
max: 100,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("should withhold an unterminated trailing array number", () => {
|
|
53
|
+
expect(partialJSONParse('{"data":[10,20,3')).toEqual({ data: [10, 20] });
|
|
54
|
+
expect(partialJSONParse('{"data":[10,20,30]')).toEqual({
|
|
55
|
+
data: [10, 20, 30],
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("should withhold unterminated numbers at any depth", () => {
|
|
60
|
+
expect(partialJSONParse('{"nested":{"a":1,"b":2')).toEqual({
|
|
61
|
+
nested: { a: 1 },
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("should withhold unterminated negatives, decimals and exponents", () => {
|
|
66
|
+
for (const input of [
|
|
67
|
+
'{"x":-',
|
|
68
|
+
'{"x":-4',
|
|
69
|
+
'{"x":3.',
|
|
70
|
+
'{"x":3.1',
|
|
71
|
+
'{"x":1e',
|
|
72
|
+
'{"x":1e-',
|
|
73
|
+
]) {
|
|
74
|
+
expect(partialJSONParse(input)).toEqual({});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("should keep negative, decimal and exponent numbers once terminated", () => {
|
|
79
|
+
expect(partialJSONParse('{"x":-4,"y":2')).toEqual({ x: -4 });
|
|
80
|
+
expect(partialJSONParse('{"x":3.1,"y":2')).toEqual({ x: 3.1 });
|
|
81
|
+
expect(partialJSONParse('{"x":1e-3,"y":2')).toEqual({ x: 1e-3 });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("should withhold a number followed only by whitespace", () => {
|
|
85
|
+
expect(partialJSONParse('{"max":100\n')).toEqual({});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("should still allow partial strings, booleans and null", () => {
|
|
89
|
+
expect(partialJSONParse('{"name":"jo')).toEqual({ name: "jo" });
|
|
90
|
+
expect(partialJSONParse('{"value":72,"max":tru')).toEqual({
|
|
91
|
+
value: 72,
|
|
92
|
+
max: true,
|
|
93
|
+
});
|
|
94
|
+
expect(partialJSONParse('{"value":72,"max":nul')).toEqual({
|
|
95
|
+
value: 72,
|
|
96
|
+
max: null,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("should still parse complete numbers", () => {
|
|
101
|
+
expect(partialJSONParse('{"value":72,"max":100}')).toEqual({
|
|
102
|
+
value: 72,
|
|
103
|
+
max: 100,
|
|
104
|
+
});
|
|
105
|
+
expect(partialJSONParse('{"pi":3.14}')).toEqual({ pi: 3.14 });
|
|
106
|
+
});
|
|
36
107
|
});
|
|
37
108
|
|
|
38
109
|
describe("safeParseToolArgs", () => {
|