@copilotkit/runtime 2.0.0-rc.30 → 2.0.0-rc.32
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/README.md +1 -1
- package/dist/agent/converters/aisdk.cjs +17 -0
- package/dist/agent/converters/aisdk.cjs.map +1 -1
- package/dist/agent/converters/aisdk.d.cts.map +1 -1
- package/dist/agent/converters/aisdk.d.mts.map +1 -1
- package/dist/agent/converters/aisdk.mjs +18 -2
- package/dist/agent/converters/aisdk.mjs.map +1 -1
- package/dist/agent/index.cjs +15 -4
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +2 -2
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts +2 -2
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +16 -5
- package/dist/agent/index.mjs.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/runtime/core/channel-manager.cjs +10 -1
- package/dist/runtime/core/channel-manager.cjs.map +1 -1
- package/dist/runtime/core/channel-manager.mjs +10 -1
- package/dist/runtime/core/channel-manager.mjs.map +1 -1
- package/dist/runtime/core/package-info.cjs +1 -1
- package/dist/runtime/core/package-info.mjs +1 -1
- package/dist/runtime/handlers/handle-run.cjs +5 -2
- package/dist/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/runtime/handlers/handle-run.mjs +5 -2
- package/dist/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/runtime/handlers/handle-suggest.cjs +4 -2
- package/dist/runtime/handlers/handle-suggest.cjs.map +1 -1
- package/dist/runtime/handlers/handle-suggest.mjs +4 -2
- package/dist/runtime/handlers/handle-suggest.mjs.map +1 -1
- package/dist/runtime/handlers/intelligence/run.cjs +24 -17
- package/dist/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/runtime/handlers/intelligence/run.mjs +24 -17
- package/dist/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/runtime/index.d.cts +1 -1
- package/dist/runtime/index.d.mts +1 -1
- package/dist/runtime/intelligence-platform/analytics-context.cjs +84 -0
- package/dist/runtime/intelligence-platform/analytics-context.cjs.map +1 -0
- package/dist/runtime/intelligence-platform/analytics-context.mjs +83 -0
- package/dist/runtime/intelligence-platform/analytics-context.mjs.map +1 -0
- package/dist/runtime/intelligence-platform/client.cjs +27 -0
- package/dist/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/runtime/intelligence-platform/client.d.cts +23 -0
- package/dist/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/runtime/intelligence-platform/client.d.mts +23 -0
- package/dist/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/runtime/intelligence-platform/client.mjs +27 -0
- package/dist/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/runtime/intelligence-platform/stop-run.cjs +50 -0
- package/dist/runtime/intelligence-platform/stop-run.cjs.map +1 -0
- package/dist/runtime/intelligence-platform/stop-run.mjs +50 -0
- package/dist/runtime/intelligence-platform/stop-run.mjs.map +1 -0
- package/dist/runtime/runner/agent-runner.cjs.map +1 -1
- package/dist/runtime/runner/agent-runner.d.cts +9 -0
- package/dist/runtime/runner/agent-runner.d.cts.map +1 -1
- package/dist/runtime/runner/agent-runner.d.mts +9 -0
- package/dist/runtime/runner/agent-runner.d.mts.map +1 -1
- package/dist/runtime/runner/agent-runner.mjs.map +1 -1
- package/dist/runtime/runner/index.d.cts +1 -1
- package/dist/runtime/runner/index.d.mts +1 -1
- package/dist/runtime/runner/index.mjs +1 -1
- package/dist/runtime/runner/intelligence.cjs +50 -5
- package/dist/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/runtime/runner/intelligence.d.cts +8 -1
- package/dist/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/runtime/runner/intelligence.d.mts +8 -1
- package/dist/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/runtime/runner/intelligence.mjs +50 -6
- package/dist/runtime/runner/intelligence.mjs.map +1 -1
- package/package.json +2 -2
package/dist/runtime/index.d.cts
CHANGED
|
@@ -14,7 +14,7 @@ import { ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMi
|
|
|
14
14
|
import { RecordedEventTransformOptions, transformRecordedEvents } from "./event-transforms/recorded-events.cjs";
|
|
15
15
|
import { A2UIMiddleware, A2UIMiddlewareConfig } from "./event-transforms/index.cjs";
|
|
16
16
|
import { InMemoryAgentRunner, InMemoryAgentRunnerOptions, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵOWNER_RETENTION_DEFAULTS, ɵOwnerRetention, ɵestimateBytes, ɵnormalizeLimits } from "./runner/in-memory.cjs";
|
|
17
|
-
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary } from "./runner/intelligence.cjs";
|
|
17
|
+
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS } from "./runner/intelligence.cjs";
|
|
18
18
|
import { FinalizeRunOptions, RunEventFinalizer, createRunEventFinalizer, finalizeRunEvents } from "./runner/index.cjs";
|
|
19
19
|
import { ChannelStatus, ChannelsControl } from "./core/channel-manager.cjs";
|
|
20
20
|
import { CopilotRuntimeHooks, ErrorHookContext, HandlerHookContext, HookContext, ResponseHookContext, RouteInfo } from "./core/hooks.cjs";
|
package/dist/runtime/index.d.mts
CHANGED
|
@@ -14,7 +14,7 @@ import { ArgsParser, GenerateSandboxedUIParams, OnParamEvent, OpenGenerativeUIMi
|
|
|
14
14
|
import { RecordedEventTransformOptions, transformRecordedEvents } from "./event-transforms/recorded-events.mjs";
|
|
15
15
|
import { A2UIMiddleware, A2UIMiddlewareConfig } from "./event-transforms/index.mjs";
|
|
16
16
|
import { InMemoryAgentRunner, InMemoryAgentRunnerOptions, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵOWNER_RETENTION_DEFAULTS, ɵOwnerRetention, ɵestimateBytes, ɵnormalizeLimits } from "./runner/in-memory.mjs";
|
|
17
|
-
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary } from "./runner/intelligence.mjs";
|
|
17
|
+
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS } from "./runner/intelligence.mjs";
|
|
18
18
|
import { FinalizeRunOptions, RunEventFinalizer, createRunEventFinalizer, finalizeRunEvents } from "./runner/index.mjs";
|
|
19
19
|
import { ChannelStatus, ChannelsControl } from "./core/channel-manager.mjs";
|
|
20
20
|
import { CopilotRuntimeHooks, ErrorHookContext, HandlerHookContext, HookContext, ResponseHookContext, RouteInfo } from "./core/hooks.mjs";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
let _copilotkit_shared = require("@copilotkit/shared");
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/intelligence-platform/analytics-context.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `forwardedProps` key an app uses to hand the runtime its analytics
|
|
6
|
+
* context for one run.
|
|
7
|
+
*/
|
|
8
|
+
const ANALYTICS_CONTEXT_FORWARDED_PROP = "__copilotkitAnalyticsContext";
|
|
9
|
+
/** The largest analytics context the runtime forwards, in UTF-8 bytes of JSON. */
|
|
10
|
+
const MAX_ANALYTICS_CONTEXT_BYTES = 4096;
|
|
11
|
+
/**
|
|
12
|
+
* Splits an app's analytics context off a run's input.
|
|
13
|
+
*
|
|
14
|
+
* The returned input no longer carries the context, so it never reaches the
|
|
15
|
+
* agent or the run's stored events. The caller forwards it on the thread lock
|
|
16
|
+
* request alone. The context is returned as the app sent it; check it with
|
|
17
|
+
* {@link normalizeAnalyticsContext} before sending it anywhere.
|
|
18
|
+
*/
|
|
19
|
+
function takeAnalyticsContext(input) {
|
|
20
|
+
const forwardedProps = input.forwardedProps;
|
|
21
|
+
if (typeof forwardedProps !== "object" || forwardedProps === null || !Object.prototype.hasOwnProperty.call(forwardedProps, ANALYTICS_CONTEXT_FORWARDED_PROP)) return {
|
|
22
|
+
input,
|
|
23
|
+
analyticsContext: void 0
|
|
24
|
+
};
|
|
25
|
+
const { [ANALYTICS_CONTEXT_FORWARDED_PROP]: analyticsContext, ...agentForwardedProps } = forwardedProps;
|
|
26
|
+
return {
|
|
27
|
+
input: {
|
|
28
|
+
...input,
|
|
29
|
+
forwardedProps: agentForwardedProps
|
|
30
|
+
},
|
|
31
|
+
analyticsContext
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns a JSON copy of an app's analytics context when it has the shape the
|
|
36
|
+
* lock request carries, and `undefined` otherwise.
|
|
37
|
+
*
|
|
38
|
+
* The runtime does not know what the context means. It checks only that the
|
|
39
|
+
* value is a plain object that serializes to JSON without losing anything (no
|
|
40
|
+
* functions, symbols, BigInts, cycles or non-finite numbers) in at most
|
|
41
|
+
* {@link MAX_ANALYTICS_CONTEXT_BYTES}. Intelligence validates the fields.
|
|
42
|
+
*
|
|
43
|
+
* A missing context (`undefined` or `null`) is dropped silently. A malformed
|
|
44
|
+
* one is dropped with a warning that names the problem but never the content.
|
|
45
|
+
* Neither ever fails the run.
|
|
46
|
+
*/
|
|
47
|
+
function normalizeAnalyticsContext(value) {
|
|
48
|
+
if (value === void 0 || value === null) return void 0;
|
|
49
|
+
const result = serializeAnalyticsContext(value);
|
|
50
|
+
if ("problem" in result) {
|
|
51
|
+
_copilotkit_shared.logger.warn({ problem: result.problem }, "Dropped the run's analytics context; the run continues without it");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
return JSON.parse(result.json);
|
|
55
|
+
}
|
|
56
|
+
function serializeAnalyticsContext(value) {
|
|
57
|
+
if (!isPlainObject(value)) return { problem: "not a plain object" };
|
|
58
|
+
let lossy;
|
|
59
|
+
let json;
|
|
60
|
+
try {
|
|
61
|
+
json = JSON.stringify(value, (_key, entry) => {
|
|
62
|
+
if (typeof entry === "function") lossy ??= "contains a function";
|
|
63
|
+
else if (typeof entry === "symbol") lossy ??= "contains a symbol";
|
|
64
|
+
else if (typeof entry === "number" && !Number.isFinite(entry)) lossy ??= "contains a non-finite number";
|
|
65
|
+
return entry;
|
|
66
|
+
});
|
|
67
|
+
} catch {
|
|
68
|
+
return { problem: "not serializable as JSON" };
|
|
69
|
+
}
|
|
70
|
+
if (lossy) return { problem: lossy };
|
|
71
|
+
const bytes = new TextEncoder().encode(json).byteLength;
|
|
72
|
+
if (bytes > MAX_ANALYTICS_CONTEXT_BYTES) return { problem: `larger than ${MAX_ANALYTICS_CONTEXT_BYTES} bytes (${bytes})` };
|
|
73
|
+
return { json };
|
|
74
|
+
}
|
|
75
|
+
function isPlainObject(value) {
|
|
76
|
+
if (typeof value !== "object" || value === null) return false;
|
|
77
|
+
const prototype = Object.getPrototypeOf(value);
|
|
78
|
+
return prototype === Object.prototype || prototype === null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
exports.normalizeAnalyticsContext = normalizeAnalyticsContext;
|
|
83
|
+
exports.takeAnalyticsContext = takeAnalyticsContext;
|
|
84
|
+
//# sourceMappingURL=analytics-context.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-context.cjs","names":[],"sources":["../../../src/runtime/intelligence-platform/analytics-context.ts"],"sourcesContent":["import type { RunAgentInput } from \"@ag-ui/client\";\nimport { logger } from \"@copilotkit/shared\";\n\n/**\n * The `forwardedProps` key an app uses to hand the runtime its analytics\n * context for one run.\n */\nexport const ANALYTICS_CONTEXT_FORWARDED_PROP = \"__copilotkitAnalyticsContext\";\n\n/** The largest analytics context the runtime forwards, in UTF-8 bytes of JSON. */\nexport const MAX_ANALYTICS_CONTEXT_BYTES = 4096;\n\n/**\n * Splits an app's analytics context off a run's input.\n *\n * The returned input no longer carries the context, so it never reaches the\n * agent or the run's stored events. The caller forwards it on the thread lock\n * request alone. The context is returned as the app sent it; check it with\n * {@link normalizeAnalyticsContext} before sending it anywhere.\n */\nexport function takeAnalyticsContext(input: RunAgentInput): {\n input: RunAgentInput;\n analyticsContext: unknown;\n} {\n const forwardedProps: unknown = input.forwardedProps;\n if (\n typeof forwardedProps !== \"object\" ||\n forwardedProps === null ||\n !Object.prototype.hasOwnProperty.call(\n forwardedProps,\n ANALYTICS_CONTEXT_FORWARDED_PROP,\n )\n ) {\n return { input, analyticsContext: undefined };\n }\n const {\n [ANALYTICS_CONTEXT_FORWARDED_PROP]: analyticsContext,\n ...agentForwardedProps\n } = forwardedProps as Record<string, unknown>;\n return {\n input: { ...input, forwardedProps: agentForwardedProps },\n analyticsContext,\n };\n}\n\n/**\n * Returns a JSON copy of an app's analytics context when it has the shape the\n * lock request carries, and `undefined` otherwise.\n *\n * The runtime does not know what the context means. It checks only that the\n * value is a plain object that serializes to JSON without losing anything (no\n * functions, symbols, BigInts, cycles or non-finite numbers) in at most\n * {@link MAX_ANALYTICS_CONTEXT_BYTES}. Intelligence validates the fields.\n *\n * A missing context (`undefined` or `null`) is dropped silently. A malformed\n * one is dropped with a warning that names the problem but never the content.\n * Neither ever fails the run.\n */\nexport function normalizeAnalyticsContext(\n value: unknown,\n): Record<string, unknown> | undefined {\n if (value === undefined || value === null) return undefined;\n const result = serializeAnalyticsContext(value);\n if (\"problem\" in result) {\n logger.warn(\n { problem: result.problem },\n \"Dropped the run's analytics context; the run continues without it\",\n );\n return undefined;\n }\n return JSON.parse(result.json) as Record<string, unknown>;\n}\n\nfunction serializeAnalyticsContext(\n value: unknown,\n): { json: string } | { problem: string } {\n if (!isPlainObject(value)) {\n return { problem: \"not a plain object\" };\n }\n let lossy: string | undefined;\n let json: string;\n try {\n json = JSON.stringify(value, (_key, entry: unknown) => {\n if (typeof entry === \"function\") lossy ??= \"contains a function\";\n else if (typeof entry === \"symbol\") lossy ??= \"contains a symbol\";\n else if (typeof entry === \"number\" && !Number.isFinite(entry))\n lossy ??= \"contains a non-finite number\";\n return entry;\n });\n } catch {\n // JSON.stringify throws on cycles and BigInts.\n return { problem: \"not serializable as JSON\" };\n }\n if (lossy) return { problem: lossy };\n const bytes = new TextEncoder().encode(json).byteLength;\n if (bytes > MAX_ANALYTICS_CONTEXT_BYTES) {\n return {\n problem: `larger than ${MAX_ANALYTICS_CONTEXT_BYTES} bytes (${bytes})`,\n };\n }\n return { json };\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n"],"mappings":";;;;;;;AAOA,MAAa,mCAAmC;;AAGhD,MAAa,8BAA8B;;;;;;;;;AAU3C,SAAgB,qBAAqB,OAGnC;CACA,MAAM,iBAA0B,MAAM;AACtC,KACE,OAAO,mBAAmB,YAC1B,mBAAmB,QACnB,CAAC,OAAO,UAAU,eAAe,KAC/B,gBACA,iCACD,CAED,QAAO;EAAE;EAAO,kBAAkB;EAAW;CAE/C,MAAM,GACH,mCAAmC,kBACpC,GAAG,wBACD;AACJ,QAAO;EACL,OAAO;GAAE,GAAG;GAAO,gBAAgB;GAAqB;EACxD;EACD;;;;;;;;;;;;;;;AAgBH,SAAgB,0BACd,OACqC;AACrC,KAAI,UAAU,UAAa,UAAU,KAAM,QAAO;CAClD,MAAM,SAAS,0BAA0B,MAAM;AAC/C,KAAI,aAAa,QAAQ;AACvB,4BAAO,KACL,EAAE,SAAS,OAAO,SAAS,EAC3B,oEACD;AACD;;AAEF,QAAO,KAAK,MAAM,OAAO,KAAK;;AAGhC,SAAS,0BACP,OACwC;AACxC,KAAI,CAAC,cAAc,MAAM,CACvB,QAAO,EAAE,SAAS,sBAAsB;CAE1C,IAAI;CACJ,IAAI;AACJ,KAAI;AACF,SAAO,KAAK,UAAU,QAAQ,MAAM,UAAmB;AACrD,OAAI,OAAO,UAAU,WAAY,WAAU;YAClC,OAAO,UAAU,SAAU,WAAU;YACrC,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,MAAM,CAC3D,WAAU;AACZ,UAAO;IACP;SACI;AAEN,SAAO,EAAE,SAAS,4BAA4B;;AAEhD,KAAI,MAAO,QAAO,EAAE,SAAS,OAAO;CACpC,MAAM,QAAQ,IAAI,aAAa,CAAC,OAAO,KAAK,CAAC;AAC7C,KAAI,QAAQ,4BACV,QAAO,EACL,SAAS,eAAe,4BAA4B,UAAU,MAAM,IACrE;AAEH,QAAO,EAAE,MAAM;;AAGjB,SAAS,cAAc,OAAkD;AACvE,KAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;CACxD,MAAM,YAAqB,OAAO,eAAe,MAAM;AACvD,QAAO,cAAc,OAAO,aAAa,cAAc"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { logger } from "@copilotkit/shared";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/intelligence-platform/analytics-context.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `forwardedProps` key an app uses to hand the runtime its analytics
|
|
6
|
+
* context for one run.
|
|
7
|
+
*/
|
|
8
|
+
const ANALYTICS_CONTEXT_FORWARDED_PROP = "__copilotkitAnalyticsContext";
|
|
9
|
+
/** The largest analytics context the runtime forwards, in UTF-8 bytes of JSON. */
|
|
10
|
+
const MAX_ANALYTICS_CONTEXT_BYTES = 4096;
|
|
11
|
+
/**
|
|
12
|
+
* Splits an app's analytics context off a run's input.
|
|
13
|
+
*
|
|
14
|
+
* The returned input no longer carries the context, so it never reaches the
|
|
15
|
+
* agent or the run's stored events. The caller forwards it on the thread lock
|
|
16
|
+
* request alone. The context is returned as the app sent it; check it with
|
|
17
|
+
* {@link normalizeAnalyticsContext} before sending it anywhere.
|
|
18
|
+
*/
|
|
19
|
+
function takeAnalyticsContext(input) {
|
|
20
|
+
const forwardedProps = input.forwardedProps;
|
|
21
|
+
if (typeof forwardedProps !== "object" || forwardedProps === null || !Object.prototype.hasOwnProperty.call(forwardedProps, ANALYTICS_CONTEXT_FORWARDED_PROP)) return {
|
|
22
|
+
input,
|
|
23
|
+
analyticsContext: void 0
|
|
24
|
+
};
|
|
25
|
+
const { [ANALYTICS_CONTEXT_FORWARDED_PROP]: analyticsContext, ...agentForwardedProps } = forwardedProps;
|
|
26
|
+
return {
|
|
27
|
+
input: {
|
|
28
|
+
...input,
|
|
29
|
+
forwardedProps: agentForwardedProps
|
|
30
|
+
},
|
|
31
|
+
analyticsContext
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns a JSON copy of an app's analytics context when it has the shape the
|
|
36
|
+
* lock request carries, and `undefined` otherwise.
|
|
37
|
+
*
|
|
38
|
+
* The runtime does not know what the context means. It checks only that the
|
|
39
|
+
* value is a plain object that serializes to JSON without losing anything (no
|
|
40
|
+
* functions, symbols, BigInts, cycles or non-finite numbers) in at most
|
|
41
|
+
* {@link MAX_ANALYTICS_CONTEXT_BYTES}. Intelligence validates the fields.
|
|
42
|
+
*
|
|
43
|
+
* A missing context (`undefined` or `null`) is dropped silently. A malformed
|
|
44
|
+
* one is dropped with a warning that names the problem but never the content.
|
|
45
|
+
* Neither ever fails the run.
|
|
46
|
+
*/
|
|
47
|
+
function normalizeAnalyticsContext(value) {
|
|
48
|
+
if (value === void 0 || value === null) return void 0;
|
|
49
|
+
const result = serializeAnalyticsContext(value);
|
|
50
|
+
if ("problem" in result) {
|
|
51
|
+
logger.warn({ problem: result.problem }, "Dropped the run's analytics context; the run continues without it");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
return JSON.parse(result.json);
|
|
55
|
+
}
|
|
56
|
+
function serializeAnalyticsContext(value) {
|
|
57
|
+
if (!isPlainObject(value)) return { problem: "not a plain object" };
|
|
58
|
+
let lossy;
|
|
59
|
+
let json;
|
|
60
|
+
try {
|
|
61
|
+
json = JSON.stringify(value, (_key, entry) => {
|
|
62
|
+
if (typeof entry === "function") lossy ??= "contains a function";
|
|
63
|
+
else if (typeof entry === "symbol") lossy ??= "contains a symbol";
|
|
64
|
+
else if (typeof entry === "number" && !Number.isFinite(entry)) lossy ??= "contains a non-finite number";
|
|
65
|
+
return entry;
|
|
66
|
+
});
|
|
67
|
+
} catch {
|
|
68
|
+
return { problem: "not serializable as JSON" };
|
|
69
|
+
}
|
|
70
|
+
if (lossy) return { problem: lossy };
|
|
71
|
+
const bytes = new TextEncoder().encode(json).byteLength;
|
|
72
|
+
if (bytes > MAX_ANALYTICS_CONTEXT_BYTES) return { problem: `larger than ${MAX_ANALYTICS_CONTEXT_BYTES} bytes (${bytes})` };
|
|
73
|
+
return { json };
|
|
74
|
+
}
|
|
75
|
+
function isPlainObject(value) {
|
|
76
|
+
if (typeof value !== "object" || value === null) return false;
|
|
77
|
+
const prototype = Object.getPrototypeOf(value);
|
|
78
|
+
return prototype === Object.prototype || prototype === null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
export { normalizeAnalyticsContext, takeAnalyticsContext };
|
|
83
|
+
//# sourceMappingURL=analytics-context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics-context.mjs","names":[],"sources":["../../../src/runtime/intelligence-platform/analytics-context.ts"],"sourcesContent":["import type { RunAgentInput } from \"@ag-ui/client\";\nimport { logger } from \"@copilotkit/shared\";\n\n/**\n * The `forwardedProps` key an app uses to hand the runtime its analytics\n * context for one run.\n */\nexport const ANALYTICS_CONTEXT_FORWARDED_PROP = \"__copilotkitAnalyticsContext\";\n\n/** The largest analytics context the runtime forwards, in UTF-8 bytes of JSON. */\nexport const MAX_ANALYTICS_CONTEXT_BYTES = 4096;\n\n/**\n * Splits an app's analytics context off a run's input.\n *\n * The returned input no longer carries the context, so it never reaches the\n * agent or the run's stored events. The caller forwards it on the thread lock\n * request alone. The context is returned as the app sent it; check it with\n * {@link normalizeAnalyticsContext} before sending it anywhere.\n */\nexport function takeAnalyticsContext(input: RunAgentInput): {\n input: RunAgentInput;\n analyticsContext: unknown;\n} {\n const forwardedProps: unknown = input.forwardedProps;\n if (\n typeof forwardedProps !== \"object\" ||\n forwardedProps === null ||\n !Object.prototype.hasOwnProperty.call(\n forwardedProps,\n ANALYTICS_CONTEXT_FORWARDED_PROP,\n )\n ) {\n return { input, analyticsContext: undefined };\n }\n const {\n [ANALYTICS_CONTEXT_FORWARDED_PROP]: analyticsContext,\n ...agentForwardedProps\n } = forwardedProps as Record<string, unknown>;\n return {\n input: { ...input, forwardedProps: agentForwardedProps },\n analyticsContext,\n };\n}\n\n/**\n * Returns a JSON copy of an app's analytics context when it has the shape the\n * lock request carries, and `undefined` otherwise.\n *\n * The runtime does not know what the context means. It checks only that the\n * value is a plain object that serializes to JSON without losing anything (no\n * functions, symbols, BigInts, cycles or non-finite numbers) in at most\n * {@link MAX_ANALYTICS_CONTEXT_BYTES}. Intelligence validates the fields.\n *\n * A missing context (`undefined` or `null`) is dropped silently. A malformed\n * one is dropped with a warning that names the problem but never the content.\n * Neither ever fails the run.\n */\nexport function normalizeAnalyticsContext(\n value: unknown,\n): Record<string, unknown> | undefined {\n if (value === undefined || value === null) return undefined;\n const result = serializeAnalyticsContext(value);\n if (\"problem\" in result) {\n logger.warn(\n { problem: result.problem },\n \"Dropped the run's analytics context; the run continues without it\",\n );\n return undefined;\n }\n return JSON.parse(result.json) as Record<string, unknown>;\n}\n\nfunction serializeAnalyticsContext(\n value: unknown,\n): { json: string } | { problem: string } {\n if (!isPlainObject(value)) {\n return { problem: \"not a plain object\" };\n }\n let lossy: string | undefined;\n let json: string;\n try {\n json = JSON.stringify(value, (_key, entry: unknown) => {\n if (typeof entry === \"function\") lossy ??= \"contains a function\";\n else if (typeof entry === \"symbol\") lossy ??= \"contains a symbol\";\n else if (typeof entry === \"number\" && !Number.isFinite(entry))\n lossy ??= \"contains a non-finite number\";\n return entry;\n });\n } catch {\n // JSON.stringify throws on cycles and BigInts.\n return { problem: \"not serializable as JSON\" };\n }\n if (lossy) return { problem: lossy };\n const bytes = new TextEncoder().encode(json).byteLength;\n if (bytes > MAX_ANALYTICS_CONTEXT_BYTES) {\n return {\n problem: `larger than ${MAX_ANALYTICS_CONTEXT_BYTES} bytes (${bytes})`,\n };\n }\n return { json };\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n"],"mappings":";;;;;;;AAOA,MAAa,mCAAmC;;AAGhD,MAAa,8BAA8B;;;;;;;;;AAU3C,SAAgB,qBAAqB,OAGnC;CACA,MAAM,iBAA0B,MAAM;AACtC,KACE,OAAO,mBAAmB,YAC1B,mBAAmB,QACnB,CAAC,OAAO,UAAU,eAAe,KAC/B,gBACA,iCACD,CAED,QAAO;EAAE;EAAO,kBAAkB;EAAW;CAE/C,MAAM,GACH,mCAAmC,kBACpC,GAAG,wBACD;AACJ,QAAO;EACL,OAAO;GAAE,GAAG;GAAO,gBAAgB;GAAqB;EACxD;EACD;;;;;;;;;;;;;;;AAgBH,SAAgB,0BACd,OACqC;AACrC,KAAI,UAAU,UAAa,UAAU,KAAM,QAAO;CAClD,MAAM,SAAS,0BAA0B,MAAM;AAC/C,KAAI,aAAa,QAAQ;AACvB,SAAO,KACL,EAAE,SAAS,OAAO,SAAS,EAC3B,oEACD;AACD;;AAEF,QAAO,KAAK,MAAM,OAAO,KAAK;;AAGhC,SAAS,0BACP,OACwC;AACxC,KAAI,CAAC,cAAc,MAAM,CACvB,QAAO,EAAE,SAAS,sBAAsB;CAE1C,IAAI;CACJ,IAAI;AACJ,KAAI;AACF,SAAO,KAAK,UAAU,QAAQ,MAAM,UAAmB;AACrD,OAAI,OAAO,UAAU,WAAY,WAAU;YAClC,OAAO,UAAU,SAAU,WAAU;YACrC,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,MAAM,CAC3D,WAAU;AACZ,UAAO;IACP;SACI;AAEN,SAAO,EAAE,SAAS,4BAA4B;;AAEhD,KAAI,MAAO,QAAO,EAAE,SAAS,OAAO;CACpC,MAAM,QAAQ,IAAI,aAAa,CAAC,OAAO,KAAK,CAAC;AAC7C,KAAI,QAAQ,4BACV,QAAO,EACL,SAAS,eAAe,4BAA4B,UAAU,MAAM,IACrE;AAEH,QAAO,EAAE,MAAM;;AAGjB,SAAS,cAAc,OAAkD;AACvE,KAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;CACxD,MAAM,YAAqB,OAAO,eAAe,MAAM;AACvD,QAAO,cAAc,OAAO,aAAa,cAAc"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const require_learning_management = require('./learning-management.cjs');
|
|
2
2
|
const require_learning = require('../core/learning.cjs');
|
|
3
|
+
const require_analytics_context = require('./analytics-context.cjs');
|
|
4
|
+
const require_stop_run = require('./stop-run.cjs');
|
|
3
5
|
const require_trajectories = require('./trajectories.cjs');
|
|
4
6
|
const require_learned_skills = require('./learned-skills.cjs');
|
|
5
7
|
let _copilotkit_shared = require("@copilotkit/shared");
|
|
@@ -1218,6 +1220,7 @@ var CopilotKitIntelligence = class {
|
|
|
1218
1220
|
return response;
|
|
1219
1221
|
}
|
|
1220
1222
|
async ɵacquireThreadLock(params) {
|
|
1223
|
+
const analyticsContext = require_analytics_context.normalizeAnalyticsContext(params.analyticsContext);
|
|
1221
1224
|
return this.#request("POST", `/api/threads/${encodeURIComponent(params.threadId)}/lock`, {
|
|
1222
1225
|
runId: params.runId,
|
|
1223
1226
|
userId: params.userId,
|
|
@@ -1225,6 +1228,7 @@ var CopilotKitIntelligence = class {
|
|
|
1225
1228
|
...params.supportsBackendThreadId === void 0 ? {} : { supportsBackendThreadId: params.supportsBackendThreadId },
|
|
1226
1229
|
...params.learningContainerId !== void 0 ? { learningContainerId: params.learningContainerId } : {},
|
|
1227
1230
|
...params.requestId !== void 0 ? { requestId: params.requestId } : {},
|
|
1231
|
+
...analyticsContext !== void 0 ? { analyticsContext } : {},
|
|
1228
1232
|
...params.lockKeyPrefix !== void 0 ? { lockKeyPrefix: params.lockKeyPrefix } : {},
|
|
1229
1233
|
...params.ttlSeconds !== void 0 ? { ttlSeconds: params.ttlSeconds } : {}
|
|
1230
1234
|
}, params.channelDeliveryId ? { "X-Cpki-Channel-Delivery-Id": params.channelDeliveryId } : void 0);
|
|
@@ -1249,6 +1253,29 @@ var CopilotKitIntelligence = class {
|
|
|
1249
1253
|
agentId: params.agentId
|
|
1250
1254
|
}) ?? null;
|
|
1251
1255
|
}
|
|
1256
|
+
/**
|
|
1257
|
+
* @internal Records that the runtime stopped a run, by the route a
|
|
1258
|
+
* browser's Stop takes: mint a thread join token, join the thread's client
|
|
1259
|
+
* channel and send `stop_run`. Intelligence stores the stop for the run, so
|
|
1260
|
+
* it counts the run's terminal event as cancelled rather than finished.
|
|
1261
|
+
*
|
|
1262
|
+
* @throws {@link PlatformRequestError} when no join token is issued, or an
|
|
1263
|
+
* error when the channel or the stop is refused or `signal` aborts.
|
|
1264
|
+
*/
|
|
1265
|
+
async ɵrecordRunStop(params) {
|
|
1266
|
+
const connection = await this.#request("POST", `/api/threads/${encodeURIComponent(params.threadId)}/connect`, {
|
|
1267
|
+
userId: params.userId,
|
|
1268
|
+
agentId: params.agentId
|
|
1269
|
+
}, void 0, params.signal);
|
|
1270
|
+
if (!connection?.joinToken) throw new PlatformRequestError("Intelligence issued no join token to record the stop", 502);
|
|
1271
|
+
await require_stop_run.sendStopRun({
|
|
1272
|
+
url: this.#clientWsUrl,
|
|
1273
|
+
joinToken: connection.joinToken,
|
|
1274
|
+
threadId: params.threadId,
|
|
1275
|
+
runId: params.runId,
|
|
1276
|
+
...params.signal ? { signal: params.signal } : {}
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1252
1279
|
};
|
|
1253
1280
|
/**
|
|
1254
1281
|
* Normalize a configured URL to "provided" or "not provided". A blank string
|