@dexto/core 1.6.26 → 1.6.27
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/agent/DextoAgent.cjs +75 -90
- package/dist/agent/DextoAgent.d.ts +4 -4
- package/dist/agent/DextoAgent.d.ts.map +1 -1
- package/dist/agent/DextoAgent.js +76 -91
- package/dist/agent/error-codes.cjs +1 -0
- package/dist/agent/error-codes.d.ts +1 -0
- package/dist/agent/error-codes.d.ts.map +1 -1
- package/dist/agent/error-codes.js +1 -0
- package/dist/agent/errors.cjs +13 -0
- package/dist/agent/errors.d.ts +6 -0
- package/dist/agent/errors.d.ts.map +1 -1
- package/dist/agent/errors.js +13 -0
- package/dist/agent/index.d.ts +1 -0
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/schemas.d.ts +2 -2
- package/dist/agent/types.d.ts +11 -0
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/approval/factory.cjs +1 -0
- package/dist/approval/factory.d.ts.map +1 -1
- package/dist/approval/factory.js +1 -0
- package/dist/approval/manager.cjs +19 -6
- package/dist/approval/manager.d.ts +6 -0
- package/dist/approval/manager.d.ts.map +1 -1
- package/dist/approval/manager.js +19 -6
- package/dist/approval/schemas.cjs +10 -0
- package/dist/approval/schemas.d.ts +305 -0
- package/dist/approval/schemas.d.ts.map +1 -1
- package/dist/approval/schemas.js +10 -0
- package/dist/events/index.cjs +210 -75
- package/dist/events/index.d.ts +44 -181
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +206 -74
- package/dist/hooks/manager.cjs +5 -2
- package/dist/hooks/manager.d.ts +2 -0
- package/dist/hooks/manager.d.ts.map +1 -1
- package/dist/hooks/manager.js +5 -2
- package/dist/hooks/types.d.ts +3 -0
- package/dist/hooks/types.d.ts.map +1 -1
- package/dist/index.browser.d.ts +1 -0
- package/dist/index.browser.d.ts.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/llm/executor/turn-executor.cjs +8 -4
- package/dist/llm/executor/turn-executor.d.ts +3 -1
- package/dist/llm/executor/turn-executor.d.ts.map +1 -1
- package/dist/llm/executor/turn-executor.js +8 -4
- package/dist/llm/services/vercel.cjs +29 -6
- package/dist/llm/services/vercel.d.ts +3 -0
- package/dist/llm/services/vercel.d.ts.map +1 -1
- package/dist/llm/services/vercel.js +27 -5
- package/dist/mcp/manager.cjs +7 -2
- package/dist/mcp/manager.d.ts +3 -1
- package/dist/mcp/manager.d.ts.map +1 -1
- package/dist/mcp/manager.js +7 -2
- package/dist/mcp/mcp-client.cjs +71 -62
- package/dist/mcp/mcp-client.d.ts +3 -2
- package/dist/mcp/mcp-client.d.ts.map +1 -1
- package/dist/mcp/mcp-client.js +71 -62
- package/dist/mcp/schemas.d.ts +10 -10
- package/dist/resources/handlers/filesystem-handler.cjs +22 -3
- package/dist/resources/handlers/filesystem-handler.d.ts.map +1 -1
- package/dist/resources/handlers/filesystem-handler.js +22 -3
- package/dist/runtime/host-runtime.cjs +163 -0
- package/dist/runtime/host-runtime.d.ts +23 -0
- package/dist/runtime/host-runtime.d.ts.map +1 -0
- package/dist/runtime/host-runtime.js +133 -0
- package/dist/runtime/index.cjs +42 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +21 -0
- package/dist/runtime/run-context.cjs +53 -0
- package/dist/runtime/run-context.d.ts +13 -0
- package/dist/runtime/run-context.d.ts.map +1 -0
- package/dist/runtime/run-context.js +34 -0
- package/dist/session/chat-session.cjs +30 -32
- package/dist/session/chat-session.d.ts +5 -15
- package/dist/session/chat-session.d.ts.map +1 -1
- package/dist/session/chat-session.js +31 -33
- package/dist/session/error-codes.cjs +1 -0
- package/dist/session/error-codes.d.ts +2 -1
- package/dist/session/error-codes.d.ts.map +1 -1
- package/dist/session/error-codes.js +1 -0
- package/dist/session/errors.cjs +13 -0
- package/dist/session/errors.d.ts +6 -0
- package/dist/session/errors.d.ts.map +1 -1
- package/dist/session/errors.js +13 -0
- package/dist/telemetry/decorators.cjs +75 -57
- package/dist/telemetry/decorators.d.ts +2 -0
- package/dist/telemetry/decorators.d.ts.map +1 -1
- package/dist/telemetry/decorators.js +75 -57
- package/dist/telemetry/utils.cjs +9 -6
- package/dist/telemetry/utils.d.ts +3 -0
- package/dist/telemetry/utils.d.ts.map +1 -1
- package/dist/telemetry/utils.js +9 -6
- package/dist/tools/tool-manager.cjs +92 -36
- package/dist/tools/tool-manager.d.ts +12 -3
- package/dist/tools/tool-manager.d.ts.map +1 -1
- package/dist/tools/tool-manager.js +92 -36
- package/dist/tools/types.d.ts +7 -1
- package/dist/tools/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import "../chunk-PTJYTZNU.js";
|
|
2
|
+
import { propagation } from "@opentelemetry/api";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
const HOST_RUNTIME_ENTRY_PREFIX = "hostRuntime.ids.";
|
|
5
|
+
const WELL_KNOWN_HOST_RUNTIME_ID_KEYS = ["runtimeId", "runId", "attemptId", "workspaceId"];
|
|
6
|
+
const WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET = new Set(WELL_KNOWN_HOST_RUNTIME_ID_KEYS);
|
|
7
|
+
const HostRuntimeIdKeySchema = z.string().min(1).regex(
|
|
8
|
+
/^[A-Za-z0-9._-]+$/,
|
|
9
|
+
"Runtime ID keys may only contain letters, numbers, dot, underscore, or hyphen."
|
|
10
|
+
);
|
|
11
|
+
const HostRuntimeIdValueSchema = z.string().trim().min(1);
|
|
12
|
+
const HostRuntimeIdsSchema = z.record(HostRuntimeIdKeySchema, HostRuntimeIdValueSchema).describe(
|
|
13
|
+
"Host-owned runtime IDs keyed by a stable identifier name such as runId, attemptId, or workspaceId."
|
|
14
|
+
);
|
|
15
|
+
const HostRuntimeContextSchema = z.object({
|
|
16
|
+
ids: HostRuntimeIdsSchema.optional().describe(
|
|
17
|
+
"Optional host-owned runtime IDs used for correlation across orchestration, telemetry, logs, and events."
|
|
18
|
+
)
|
|
19
|
+
}).strict().describe("Host-owned runtime context surfaced through core runtime flows.");
|
|
20
|
+
function freezeHostRuntimeContext(hostRuntime) {
|
|
21
|
+
return Object.freeze({
|
|
22
|
+
ids: Object.freeze({ ...hostRuntime.ids })
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function normalizeHostRuntimeContext(input) {
|
|
26
|
+
if (input === void 0) {
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
const parsed = HostRuntimeContextSchema.parse(input);
|
|
30
|
+
if (!parsed.ids || Object.keys(parsed.ids).length === 0) {
|
|
31
|
+
return void 0;
|
|
32
|
+
}
|
|
33
|
+
return freezeHostRuntimeContext({ ids: parsed.ids });
|
|
34
|
+
}
|
|
35
|
+
function resolveHostRuntimeContext({
|
|
36
|
+
inherited,
|
|
37
|
+
explicit,
|
|
38
|
+
runId
|
|
39
|
+
}) {
|
|
40
|
+
const ids = {
|
|
41
|
+
...inherited?.ids ?? {},
|
|
42
|
+
...explicit?.ids ?? {},
|
|
43
|
+
...runId !== void 0 ? { runId } : {}
|
|
44
|
+
};
|
|
45
|
+
if (Object.keys(ids).length === 0) {
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
return normalizeHostRuntimeContext({ ids });
|
|
49
|
+
}
|
|
50
|
+
function isWellKnownHostRuntimeIdKey(key) {
|
|
51
|
+
return WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET.has(key);
|
|
52
|
+
}
|
|
53
|
+
function isHostRuntimeBaggageKey(key) {
|
|
54
|
+
return key.startsWith(HOST_RUNTIME_ENTRY_PREFIX) || WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET.has(key);
|
|
55
|
+
}
|
|
56
|
+
function getValidHostRuntimeIdValue(value) {
|
|
57
|
+
const parsed = HostRuntimeIdValueSchema.safeParse(value);
|
|
58
|
+
return parsed.success ? parsed.data : void 0;
|
|
59
|
+
}
|
|
60
|
+
function getHostRuntimeBaggageEntries(hostRuntime) {
|
|
61
|
+
const ids = hostRuntime?.ids;
|
|
62
|
+
if (!ids) {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
const entries = {};
|
|
66
|
+
for (const [key, value] of Object.entries(ids)) {
|
|
67
|
+
entries[`${HOST_RUNTIME_ENTRY_PREFIX}${key}`] = { value };
|
|
68
|
+
if (isWellKnownHostRuntimeIdKey(key)) {
|
|
69
|
+
entries[key] = { value };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return entries;
|
|
73
|
+
}
|
|
74
|
+
function getHostRuntimeAttributes(hostRuntime) {
|
|
75
|
+
const ids = hostRuntime?.ids;
|
|
76
|
+
if (!ids) {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
const attributes = {};
|
|
80
|
+
for (const [key, value] of Object.entries(ids)) {
|
|
81
|
+
attributes[`${HOST_RUNTIME_ENTRY_PREFIX}${key}`] = value;
|
|
82
|
+
if (isWellKnownHostRuntimeIdKey(key)) {
|
|
83
|
+
attributes[key] = value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return attributes;
|
|
87
|
+
}
|
|
88
|
+
function getHostRuntimeContextFromBaggage(ctx) {
|
|
89
|
+
const baggage = propagation.getBaggage(ctx);
|
|
90
|
+
if (!baggage) {
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
const ids = {};
|
|
94
|
+
for (const [key, entry] of baggage.getAllEntries()) {
|
|
95
|
+
if (key.startsWith(HOST_RUNTIME_ENTRY_PREFIX)) {
|
|
96
|
+
const hostRuntimeKey = key.slice(HOST_RUNTIME_ENTRY_PREFIX.length);
|
|
97
|
+
if (!HostRuntimeIdKeySchema.safeParse(hostRuntimeKey).success) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const value = getValidHostRuntimeIdValue(entry.value);
|
|
101
|
+
if (value === void 0) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
ids[hostRuntimeKey] = value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const key of WELL_KNOWN_HOST_RUNTIME_ID_KEYS) {
|
|
108
|
+
if (ids[key] !== void 0) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const entry = baggage.getEntry(key);
|
|
112
|
+
if (entry) {
|
|
113
|
+
const value = getValidHostRuntimeIdValue(entry.value);
|
|
114
|
+
if (value !== void 0) {
|
|
115
|
+
ids[key] = value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (Object.keys(ids).length === 0) {
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
return normalizeHostRuntimeContext({ ids });
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
HostRuntimeContextSchema,
|
|
126
|
+
HostRuntimeIdsSchema,
|
|
127
|
+
getHostRuntimeAttributes,
|
|
128
|
+
getHostRuntimeBaggageEntries,
|
|
129
|
+
getHostRuntimeContextFromBaggage,
|
|
130
|
+
isHostRuntimeBaggageKey,
|
|
131
|
+
normalizeHostRuntimeContext,
|
|
132
|
+
resolveHostRuntimeContext
|
|
133
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var runtime_exports = {};
|
|
20
|
+
__export(runtime_exports, {
|
|
21
|
+
HostRuntimeContextSchema: () => import_host_runtime.HostRuntimeContextSchema,
|
|
22
|
+
HostRuntimeIdsSchema: () => import_host_runtime.HostRuntimeIdsSchema,
|
|
23
|
+
buildHostRuntimeBaggageEntries: () => import_host_runtime.getHostRuntimeBaggageEntries,
|
|
24
|
+
getHostRuntimeAttributes: () => import_host_runtime.getHostRuntimeAttributes,
|
|
25
|
+
getHostRuntimeBaggageEntries: () => import_host_runtime.getHostRuntimeBaggageEntries,
|
|
26
|
+
getHostRuntimeContextFromBaggage: () => import_host_runtime.getHostRuntimeContextFromBaggage,
|
|
27
|
+
normalizeHostRuntimeContext: () => import_host_runtime.normalizeHostRuntimeContext,
|
|
28
|
+
resolveHostRuntimeContext: () => import_host_runtime.resolveHostRuntimeContext
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
31
|
+
var import_host_runtime = require("./host-runtime.js");
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
HostRuntimeContextSchema,
|
|
35
|
+
HostRuntimeIdsSchema,
|
|
36
|
+
buildHostRuntimeBaggageEntries,
|
|
37
|
+
getHostRuntimeAttributes,
|
|
38
|
+
getHostRuntimeBaggageEntries,
|
|
39
|
+
getHostRuntimeContextFromBaggage,
|
|
40
|
+
normalizeHostRuntimeContext,
|
|
41
|
+
resolveHostRuntimeContext
|
|
42
|
+
});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { HostRuntimeContextSchema, HostRuntimeIdsSchema, getHostRuntimeBaggageEntries as buildHostRuntimeBaggageEntries, getHostRuntimeAttributes, getHostRuntimeBaggageEntries, getHostRuntimeContextFromBaggage, normalizeHostRuntimeContext, resolveHostRuntimeContext, type HostRuntimeContext, type HostRuntimeIds, } from './host-runtime.js';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,wBAAwB,EACxB,oBAAoB,EACpB,4BAA4B,IAAI,8BAA8B,EAC9D,wBAAwB,EACxB,4BAA4B,EAC5B,gCAAgC,EAChC,2BAA2B,EAC3B,yBAAyB,EACzB,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACtB,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import "../chunk-PTJYTZNU.js";
|
|
2
|
+
import {
|
|
3
|
+
HostRuntimeContextSchema,
|
|
4
|
+
HostRuntimeIdsSchema,
|
|
5
|
+
getHostRuntimeBaggageEntries,
|
|
6
|
+
getHostRuntimeAttributes,
|
|
7
|
+
getHostRuntimeBaggageEntries as getHostRuntimeBaggageEntries2,
|
|
8
|
+
getHostRuntimeContextFromBaggage,
|
|
9
|
+
normalizeHostRuntimeContext,
|
|
10
|
+
resolveHostRuntimeContext
|
|
11
|
+
} from "./host-runtime.js";
|
|
12
|
+
export {
|
|
13
|
+
HostRuntimeContextSchema,
|
|
14
|
+
HostRuntimeIdsSchema,
|
|
15
|
+
getHostRuntimeBaggageEntries as buildHostRuntimeBaggageEntries,
|
|
16
|
+
getHostRuntimeAttributes,
|
|
17
|
+
getHostRuntimeBaggageEntries2 as getHostRuntimeBaggageEntries,
|
|
18
|
+
getHostRuntimeContextFromBaggage,
|
|
19
|
+
normalizeHostRuntimeContext,
|
|
20
|
+
resolveHostRuntimeContext
|
|
21
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var run_context_exports = {};
|
|
20
|
+
__export(run_context_exports, {
|
|
21
|
+
createAgentRunContext: () => createAgentRunContext
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(run_context_exports);
|
|
24
|
+
var import_api = require("@opentelemetry/api");
|
|
25
|
+
var import_host_runtime = require("./host-runtime.js");
|
|
26
|
+
function createAgentRunContext(options) {
|
|
27
|
+
const hostRuntime = (0, import_host_runtime.normalizeHostRuntimeContext)(options.hostRuntime);
|
|
28
|
+
const parentContext = options.parentContext ?? import_api.context.active();
|
|
29
|
+
const existingBaggage = import_api.propagation.getBaggage(parentContext);
|
|
30
|
+
const baggageEntries = {};
|
|
31
|
+
if (existingBaggage) {
|
|
32
|
+
existingBaggage.getAllEntries().forEach(([key, entry]) => {
|
|
33
|
+
if ((0, import_host_runtime.isHostRuntimeBaggageKey)(key)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
baggageEntries[key] = { ...entry };
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
Object.assign(baggageEntries, (0, import_host_runtime.getHostRuntimeBaggageEntries)(hostRuntime));
|
|
40
|
+
baggageEntries.sessionId = { ...baggageEntries.sessionId, value: options.sessionId };
|
|
41
|
+
return Object.freeze({
|
|
42
|
+
sessionId: options.sessionId,
|
|
43
|
+
...hostRuntime !== void 0 ? { hostRuntime } : {},
|
|
44
|
+
telemetryContext: import_api.propagation.setBaggage(
|
|
45
|
+
parentContext,
|
|
46
|
+
import_api.propagation.createBaggage(baggageEntries)
|
|
47
|
+
)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
createAgentRunContext
|
|
53
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Context } from '@opentelemetry/api';
|
|
2
|
+
import { type HostRuntimeContext } from './host-runtime.js';
|
|
3
|
+
export interface AgentRunContext {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
hostRuntime?: HostRuntimeContext | undefined;
|
|
6
|
+
telemetryContext: Context;
|
|
7
|
+
}
|
|
8
|
+
export declare function createAgentRunContext(options: {
|
|
9
|
+
sessionId: string;
|
|
10
|
+
hostRuntime?: HostRuntimeContext | undefined;
|
|
11
|
+
parentContext?: Context | undefined;
|
|
12
|
+
}): AgentRunContext;
|
|
13
|
+
//# sourceMappingURL=run-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-context.d.ts","sourceRoot":"","sources":["../../src/runtime/run-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2C,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC3F,OAAO,EAIH,KAAK,kBAAkB,EAC1B,MAAM,mBAAmB,CAAC;AAE3B,MAAM,WAAW,eAAe;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;IAC7C,gBAAgB,EAAE,OAAO,CAAC;CAC7B;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;IAC7C,aAAa,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACvC,GAAG,eAAe,CA0BlB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "../chunk-PTJYTZNU.js";
|
|
2
|
+
import { context, propagation } from "@opentelemetry/api";
|
|
3
|
+
import {
|
|
4
|
+
getHostRuntimeBaggageEntries,
|
|
5
|
+
isHostRuntimeBaggageKey,
|
|
6
|
+
normalizeHostRuntimeContext
|
|
7
|
+
} from "./host-runtime.js";
|
|
8
|
+
function createAgentRunContext(options) {
|
|
9
|
+
const hostRuntime = normalizeHostRuntimeContext(options.hostRuntime);
|
|
10
|
+
const parentContext = options.parentContext ?? context.active();
|
|
11
|
+
const existingBaggage = propagation.getBaggage(parentContext);
|
|
12
|
+
const baggageEntries = {};
|
|
13
|
+
if (existingBaggage) {
|
|
14
|
+
existingBaggage.getAllEntries().forEach(([key, entry]) => {
|
|
15
|
+
if (isHostRuntimeBaggageKey(key)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
baggageEntries[key] = { ...entry };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
Object.assign(baggageEntries, getHostRuntimeBaggageEntries(hostRuntime));
|
|
22
|
+
baggageEntries.sessionId = { ...baggageEntries.sessionId, value: options.sessionId };
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
sessionId: options.sessionId,
|
|
25
|
+
...hostRuntime !== void 0 ? { hostRuntime } : {},
|
|
26
|
+
telemetryContext: propagation.setBaggage(
|
|
27
|
+
parentContext,
|
|
28
|
+
propagation.createBaggage(baggageEntries)
|
|
29
|
+
)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
createAgentRunContext
|
|
34
|
+
};
|
|
@@ -31,6 +31,7 @@ var import_error_codes = require("../hooks/error-codes.js");
|
|
|
31
31
|
var import_message_queue = require("./message-queue.js");
|
|
32
32
|
var import_usage_metadata = require("../llm/usage-metadata.js");
|
|
33
33
|
var import_codex_base_url = require("../llm/providers/codex-base-url.js");
|
|
34
|
+
var import_errors2 = require("./errors.js");
|
|
34
35
|
class ChatSession {
|
|
35
36
|
/**
|
|
36
37
|
* Creates a new ChatSession instance.
|
|
@@ -38,7 +39,7 @@ class ChatSession {
|
|
|
38
39
|
* Each session creates its own isolated services:
|
|
39
40
|
* - ConversationHistoryProvider (with session-specific storage, shared across LLM switches)
|
|
40
41
|
* - LLM service (creates its own properly-typed ContextManager internally)
|
|
41
|
-
* - SessionEventBus (session-local event handling
|
|
42
|
+
* - SessionEventBus (session-local event handling)
|
|
42
43
|
*
|
|
43
44
|
* @param services - The shared services from the agent (state manager, prompt, client managers, etc.)
|
|
44
45
|
* @param id - Unique identifier for this session
|
|
@@ -55,7 +56,7 @@ class ChatSession {
|
|
|
55
56
|
this.id,
|
|
56
57
|
this.services.messageQueueStore
|
|
57
58
|
);
|
|
58
|
-
this.
|
|
59
|
+
this.setupTokenAccumulation();
|
|
59
60
|
this.logger.debug(`ChatSession ${this.id}: Created, awaiting initialization`);
|
|
60
61
|
}
|
|
61
62
|
/**
|
|
@@ -87,11 +88,7 @@ class ChatSession {
|
|
|
87
88
|
* Reused across LLM switches so mid-task follow-ups survive service recreation.
|
|
88
89
|
*/
|
|
89
90
|
messageQueue;
|
|
90
|
-
|
|
91
|
-
* Map of event forwarder functions for cleanup.
|
|
92
|
-
* Stores the bound functions so they can be removed from the event bus.
|
|
93
|
-
*/
|
|
94
|
-
forwarders = /* @__PURE__ */ new Map();
|
|
91
|
+
activeForwarderCleanup = null;
|
|
95
92
|
/**
|
|
96
93
|
* Token accumulator listener for cleanup.
|
|
97
94
|
*/
|
|
@@ -109,27 +106,20 @@ class ChatSession {
|
|
|
109
106
|
async init() {
|
|
110
107
|
await this.initializeServices();
|
|
111
108
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
* information when needed.
|
|
119
|
-
*/
|
|
120
|
-
setupEventForwarding() {
|
|
121
|
-
import_events.SessionEventNames.forEach((eventName) => {
|
|
122
|
-
const forwarder = (payload) => {
|
|
123
|
-
const payloadWithSession = payload && typeof payload === "object" ? { ...payload, sessionId: this.id } : { sessionId: this.id };
|
|
124
|
-
this.services.agentEventBus.emit(eventName, payloadWithSession);
|
|
125
|
-
};
|
|
126
|
-
this.forwarders.set(eventName, forwarder);
|
|
127
|
-
this.eventBus.on(eventName, forwarder);
|
|
109
|
+
attachRunEventForwarders(runContext) {
|
|
110
|
+
const cleanup = (0, import_events.forwardSessionEventsToAgentBus)({
|
|
111
|
+
sessionEventBus: this.eventBus,
|
|
112
|
+
agentEventBus: this.services.agentEventBus,
|
|
113
|
+
sessionId: this.id,
|
|
114
|
+
...runContext?.hostRuntime !== void 0 ? { hostRuntime: runContext.hostRuntime } : {}
|
|
128
115
|
});
|
|
129
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
116
|
+
this.activeForwarderCleanup = cleanup;
|
|
117
|
+
return () => {
|
|
118
|
+
if (this.activeForwarderCleanup === cleanup) {
|
|
119
|
+
this.activeForwarderCleanup = null;
|
|
120
|
+
}
|
|
121
|
+
cleanup();
|
|
122
|
+
};
|
|
133
123
|
}
|
|
134
124
|
/**
|
|
135
125
|
* Sets up token usage accumulation by listening to llm:response events.
|
|
@@ -275,8 +265,12 @@ class ChatSession {
|
|
|
275
265
|
this.logger.debug(
|
|
276
266
|
`Streaming session ${this.id} | textParts=${textParts.length} | images=${imageParts.length} | files=${fileParts.length}`
|
|
277
267
|
);
|
|
268
|
+
if (this.isBusy()) {
|
|
269
|
+
throw import_errors2.SessionError.busy(this.id);
|
|
270
|
+
}
|
|
278
271
|
this.currentRunController = new AbortController();
|
|
279
272
|
const signal = options?.signal ? this.combineSignals(options.signal, this.currentRunController.signal) : this.currentRunController.signal;
|
|
273
|
+
const detachForwarders = this.attachRunEventForwarders(options?.runContext);
|
|
280
274
|
try {
|
|
281
275
|
const textContent = textParts.map((p) => p.text).join("\n");
|
|
282
276
|
const firstImage = imageParts[0];
|
|
@@ -306,6 +300,7 @@ class ChatSession {
|
|
|
306
300
|
mcpManager: this.services.mcpManager,
|
|
307
301
|
toolManager: this.services.toolManager,
|
|
308
302
|
stateManager: this.services.stateManager,
|
|
303
|
+
...options?.runContext !== void 0 && { runContext: options.runContext },
|
|
309
304
|
sessionId: this.id,
|
|
310
305
|
abortSignal: signal
|
|
311
306
|
}
|
|
@@ -315,7 +310,10 @@ class ChatSession {
|
|
|
315
310
|
modifiedParts = modifiedParts.filter((p) => p.type !== "text");
|
|
316
311
|
modifiedParts.unshift({ type: "text", text: modifiedBeforePayload.text });
|
|
317
312
|
}
|
|
318
|
-
const streamResult = await this.llmService.stream(modifiedParts, {
|
|
313
|
+
const streamResult = await this.llmService.stream(modifiedParts, {
|
|
314
|
+
signal,
|
|
315
|
+
...options?.runContext !== void 0 && { runContext: options.runContext }
|
|
316
|
+
});
|
|
319
317
|
const llmConfig = this.services.stateManager.getLLMConfig(this.id);
|
|
320
318
|
const beforeResponsePayload = {
|
|
321
319
|
content: streamResult.text,
|
|
@@ -331,6 +329,7 @@ class ChatSession {
|
|
|
331
329
|
mcpManager: this.services.mcpManager,
|
|
332
330
|
toolManager: this.services.toolManager,
|
|
333
331
|
stateManager: this.services.stateManager,
|
|
332
|
+
...options?.runContext !== void 0 && { runContext: options.runContext },
|
|
334
333
|
sessionId: this.id,
|
|
335
334
|
abortSignal: signal
|
|
336
335
|
}
|
|
@@ -386,6 +385,7 @@ class ChatSession {
|
|
|
386
385
|
);
|
|
387
386
|
throw error;
|
|
388
387
|
} finally {
|
|
388
|
+
detachForwarders();
|
|
389
389
|
this.currentRunController = null;
|
|
390
390
|
}
|
|
391
391
|
}
|
|
@@ -536,10 +536,8 @@ class ChatSession {
|
|
|
536
536
|
*/
|
|
537
537
|
dispose() {
|
|
538
538
|
this.logger.debug(`Disposing session ${this.id} - cleaning up event listeners`);
|
|
539
|
-
this.
|
|
540
|
-
|
|
541
|
-
});
|
|
542
|
-
this.forwarders.clear();
|
|
539
|
+
this.activeForwarderCleanup?.();
|
|
540
|
+
this.activeForwarderCleanup = null;
|
|
543
541
|
if (this.tokenAccumulatorListener) {
|
|
544
542
|
this.eventBus.off("llm:response", this.tokenAccumulatorListener);
|
|
545
543
|
this.tokenAccumulatorListener = null;
|
|
@@ -15,6 +15,7 @@ import type { MessageQueueStore } from './message-queue-store.js';
|
|
|
15
15
|
import type { ContentInput } from '../agent/types.js';
|
|
16
16
|
import type { CompactionStrategy } from '../context/compaction/types.js';
|
|
17
17
|
import type { VercelLLMService } from '../llm/services/vercel.js';
|
|
18
|
+
import type { AgentRunContext } from '../runtime/run-context.js';
|
|
18
19
|
/**
|
|
19
20
|
* Represents an isolated conversation session within a Dexto agent.
|
|
20
21
|
*
|
|
@@ -91,11 +92,7 @@ export declare class ChatSession {
|
|
|
91
92
|
* Reused across LLM switches so mid-task follow-ups survive service recreation.
|
|
92
93
|
*/
|
|
93
94
|
private messageQueue;
|
|
94
|
-
|
|
95
|
-
* Map of event forwarder functions for cleanup.
|
|
96
|
-
* Stores the bound functions so they can be removed from the event bus.
|
|
97
|
-
*/
|
|
98
|
-
private forwarders;
|
|
95
|
+
private activeForwarderCleanup;
|
|
99
96
|
/**
|
|
100
97
|
* Token accumulator listener for cleanup.
|
|
101
98
|
*/
|
|
@@ -112,7 +109,7 @@ export declare class ChatSession {
|
|
|
112
109
|
* Each session creates its own isolated services:
|
|
113
110
|
* - ConversationHistoryProvider (with session-specific storage, shared across LLM switches)
|
|
114
111
|
* - LLM service (creates its own properly-typed ContextManager internally)
|
|
115
|
-
* - SessionEventBus (session-local event handling
|
|
112
|
+
* - SessionEventBus (session-local event handling)
|
|
116
113
|
*
|
|
117
114
|
* @param services - The shared services from the agent (state manager, prompt, client managers, etc.)
|
|
118
115
|
* @param id - Unique identifier for this session
|
|
@@ -138,15 +135,7 @@ export declare class ChatSession {
|
|
|
138
135
|
* This must be called after construction to set up the storage-backed services.
|
|
139
136
|
*/
|
|
140
137
|
init(): Promise<void>;
|
|
141
|
-
|
|
142
|
-
* Sets up event forwarding from session bus to global agent bus.
|
|
143
|
-
*
|
|
144
|
-
* All session events are automatically forwarded to the global bus with the same
|
|
145
|
-
* event names, but with session context added to the payload. This allows the app
|
|
146
|
-
* layer to continue listening to standard events while having access to session
|
|
147
|
-
* information when needed.
|
|
148
|
-
*/
|
|
149
|
-
private setupEventForwarding;
|
|
138
|
+
private attachRunEventForwarders;
|
|
150
139
|
/**
|
|
151
140
|
* Sets up token usage accumulation by listening to llm:response events.
|
|
152
141
|
* Accumulates token usage and cost to session metadata for /stats tracking.
|
|
@@ -192,6 +181,7 @@ export declare class ChatSession {
|
|
|
192
181
|
*/
|
|
193
182
|
stream(content: ContentInput, options?: {
|
|
194
183
|
signal?: AbortSignal;
|
|
184
|
+
runContext?: AgentRunContext;
|
|
195
185
|
}): Promise<{
|
|
196
186
|
text: string;
|
|
197
187
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-session.d.ts","sourceRoot":"","sources":["../../src/session/chat-session.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,KAAK,EAA2B,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAC9F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAA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AAY,EAAE,aAAa,EAAE;IAIhE;;;;OAIG;IACU,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI9D;;;OAGG;IACU,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC;IAOjD;;;OAGG;IACI,MAAM,IAAI,OAAO;CAa3B"}
|
|
@@ -4,7 +4,7 @@ import { createDatabaseHistoryProvider } from "./history/factory.js";
|
|
|
4
4
|
import { createLLMService } from "../llm/services/factory.js";
|
|
5
5
|
import {
|
|
6
6
|
SessionEventBus,
|
|
7
|
-
|
|
7
|
+
forwardSessionEventsToAgentBus
|
|
8
8
|
} from "../events/index.js";
|
|
9
9
|
import { DextoLogComponent } from "../logger/v2/types.js";
|
|
10
10
|
import { DextoRuntimeError, ErrorScope, ErrorType } from "../errors/index.js";
|
|
@@ -12,6 +12,7 @@ import { HookErrorCode } from "../hooks/error-codes.js";
|
|
|
12
12
|
import { MessageQueueService } from "./message-queue.js";
|
|
13
13
|
import { getUsagePricingMetadata, hasMeaningfulTokenUsage } from "../llm/usage-metadata.js";
|
|
14
14
|
import { parseCodexBaseURL } from "../llm/providers/codex-base-url.js";
|
|
15
|
+
import { SessionError } from "./errors.js";
|
|
15
16
|
class ChatSession {
|
|
16
17
|
/**
|
|
17
18
|
* Creates a new ChatSession instance.
|
|
@@ -19,7 +20,7 @@ class ChatSession {
|
|
|
19
20
|
* Each session creates its own isolated services:
|
|
20
21
|
* - ConversationHistoryProvider (with session-specific storage, shared across LLM switches)
|
|
21
22
|
* - LLM service (creates its own properly-typed ContextManager internally)
|
|
22
|
-
* - SessionEventBus (session-local event handling
|
|
23
|
+
* - SessionEventBus (session-local event handling)
|
|
23
24
|
*
|
|
24
25
|
* @param services - The shared services from the agent (state manager, prompt, client managers, etc.)
|
|
25
26
|
* @param id - Unique identifier for this session
|
|
@@ -36,7 +37,7 @@ class ChatSession {
|
|
|
36
37
|
this.id,
|
|
37
38
|
this.services.messageQueueStore
|
|
38
39
|
);
|
|
39
|
-
this.
|
|
40
|
+
this.setupTokenAccumulation();
|
|
40
41
|
this.logger.debug(`ChatSession ${this.id}: Created, awaiting initialization`);
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
@@ -68,11 +69,7 @@ class ChatSession {
|
|
|
68
69
|
* Reused across LLM switches so mid-task follow-ups survive service recreation.
|
|
69
70
|
*/
|
|
70
71
|
messageQueue;
|
|
71
|
-
|
|
72
|
-
* Map of event forwarder functions for cleanup.
|
|
73
|
-
* Stores the bound functions so they can be removed from the event bus.
|
|
74
|
-
*/
|
|
75
|
-
forwarders = /* @__PURE__ */ new Map();
|
|
72
|
+
activeForwarderCleanup = null;
|
|
76
73
|
/**
|
|
77
74
|
* Token accumulator listener for cleanup.
|
|
78
75
|
*/
|
|
@@ -90,27 +87,20 @@ class ChatSession {
|
|
|
90
87
|
async init() {
|
|
91
88
|
await this.initializeServices();
|
|
92
89
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
* information when needed.
|
|
100
|
-
*/
|
|
101
|
-
setupEventForwarding() {
|
|
102
|
-
SessionEventNames.forEach((eventName) => {
|
|
103
|
-
const forwarder = (payload) => {
|
|
104
|
-
const payloadWithSession = payload && typeof payload === "object" ? { ...payload, sessionId: this.id } : { sessionId: this.id };
|
|
105
|
-
this.services.agentEventBus.emit(eventName, payloadWithSession);
|
|
106
|
-
};
|
|
107
|
-
this.forwarders.set(eventName, forwarder);
|
|
108
|
-
this.eventBus.on(eventName, forwarder);
|
|
90
|
+
attachRunEventForwarders(runContext) {
|
|
91
|
+
const cleanup = forwardSessionEventsToAgentBus({
|
|
92
|
+
sessionEventBus: this.eventBus,
|
|
93
|
+
agentEventBus: this.services.agentEventBus,
|
|
94
|
+
sessionId: this.id,
|
|
95
|
+
...runContext?.hostRuntime !== void 0 ? { hostRuntime: runContext.hostRuntime } : {}
|
|
109
96
|
});
|
|
110
|
-
this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
97
|
+
this.activeForwarderCleanup = cleanup;
|
|
98
|
+
return () => {
|
|
99
|
+
if (this.activeForwarderCleanup === cleanup) {
|
|
100
|
+
this.activeForwarderCleanup = null;
|
|
101
|
+
}
|
|
102
|
+
cleanup();
|
|
103
|
+
};
|
|
114
104
|
}
|
|
115
105
|
/**
|
|
116
106
|
* Sets up token usage accumulation by listening to llm:response events.
|
|
@@ -256,8 +246,12 @@ class ChatSession {
|
|
|
256
246
|
this.logger.debug(
|
|
257
247
|
`Streaming session ${this.id} | textParts=${textParts.length} | images=${imageParts.length} | files=${fileParts.length}`
|
|
258
248
|
);
|
|
249
|
+
if (this.isBusy()) {
|
|
250
|
+
throw SessionError.busy(this.id);
|
|
251
|
+
}
|
|
259
252
|
this.currentRunController = new AbortController();
|
|
260
253
|
const signal = options?.signal ? this.combineSignals(options.signal, this.currentRunController.signal) : this.currentRunController.signal;
|
|
254
|
+
const detachForwarders = this.attachRunEventForwarders(options?.runContext);
|
|
261
255
|
try {
|
|
262
256
|
const textContent = textParts.map((p) => p.text).join("\n");
|
|
263
257
|
const firstImage = imageParts[0];
|
|
@@ -287,6 +281,7 @@ class ChatSession {
|
|
|
287
281
|
mcpManager: this.services.mcpManager,
|
|
288
282
|
toolManager: this.services.toolManager,
|
|
289
283
|
stateManager: this.services.stateManager,
|
|
284
|
+
...options?.runContext !== void 0 && { runContext: options.runContext },
|
|
290
285
|
sessionId: this.id,
|
|
291
286
|
abortSignal: signal
|
|
292
287
|
}
|
|
@@ -296,7 +291,10 @@ class ChatSession {
|
|
|
296
291
|
modifiedParts = modifiedParts.filter((p) => p.type !== "text");
|
|
297
292
|
modifiedParts.unshift({ type: "text", text: modifiedBeforePayload.text });
|
|
298
293
|
}
|
|
299
|
-
const streamResult = await this.llmService.stream(modifiedParts, {
|
|
294
|
+
const streamResult = await this.llmService.stream(modifiedParts, {
|
|
295
|
+
signal,
|
|
296
|
+
...options?.runContext !== void 0 && { runContext: options.runContext }
|
|
297
|
+
});
|
|
300
298
|
const llmConfig = this.services.stateManager.getLLMConfig(this.id);
|
|
301
299
|
const beforeResponsePayload = {
|
|
302
300
|
content: streamResult.text,
|
|
@@ -312,6 +310,7 @@ class ChatSession {
|
|
|
312
310
|
mcpManager: this.services.mcpManager,
|
|
313
311
|
toolManager: this.services.toolManager,
|
|
314
312
|
stateManager: this.services.stateManager,
|
|
313
|
+
...options?.runContext !== void 0 && { runContext: options.runContext },
|
|
315
314
|
sessionId: this.id,
|
|
316
315
|
abortSignal: signal
|
|
317
316
|
}
|
|
@@ -367,6 +366,7 @@ class ChatSession {
|
|
|
367
366
|
);
|
|
368
367
|
throw error;
|
|
369
368
|
} finally {
|
|
369
|
+
detachForwarders();
|
|
370
370
|
this.currentRunController = null;
|
|
371
371
|
}
|
|
372
372
|
}
|
|
@@ -517,10 +517,8 @@ class ChatSession {
|
|
|
517
517
|
*/
|
|
518
518
|
dispose() {
|
|
519
519
|
this.logger.debug(`Disposing session ${this.id} - cleaning up event listeners`);
|
|
520
|
-
this.
|
|
521
|
-
|
|
522
|
-
});
|
|
523
|
-
this.forwarders.clear();
|
|
520
|
+
this.activeForwarderCleanup?.();
|
|
521
|
+
this.activeForwarderCleanup = null;
|
|
524
522
|
if (this.tokenAccumulatorListener) {
|
|
525
523
|
this.eventBus.off("llm:response", this.tokenAccumulatorListener);
|
|
526
524
|
this.tokenAccumulatorListener = null;
|