@dexto/core 1.6.26 → 1.7.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.
Files changed (160) hide show
  1. package/dist/agent/DextoAgent.cjs +75 -90
  2. package/dist/agent/DextoAgent.d.ts +7 -7
  3. package/dist/agent/DextoAgent.d.ts.map +1 -1
  4. package/dist/agent/DextoAgent.js +76 -91
  5. package/dist/agent/error-codes.cjs +1 -0
  6. package/dist/agent/error-codes.d.ts +1 -0
  7. package/dist/agent/error-codes.d.ts.map +1 -1
  8. package/dist/agent/error-codes.js +1 -0
  9. package/dist/agent/errors.cjs +13 -0
  10. package/dist/agent/errors.d.ts +6 -0
  11. package/dist/agent/errors.d.ts.map +1 -1
  12. package/dist/agent/errors.js +13 -0
  13. package/dist/agent/index.d.ts +1 -0
  14. package/dist/agent/index.d.ts.map +1 -1
  15. package/dist/agent/schemas.cjs +5 -5
  16. package/dist/agent/schemas.d.ts +69 -980
  17. package/dist/agent/schemas.d.ts.map +1 -1
  18. package/dist/agent/schemas.js +5 -5
  19. package/dist/agent/types.d.ts +11 -0
  20. package/dist/agent/types.d.ts.map +1 -1
  21. package/dist/approval/factory.cjs +1 -0
  22. package/dist/approval/factory.d.ts.map +1 -1
  23. package/dist/approval/factory.js +1 -0
  24. package/dist/approval/manager.cjs +19 -6
  25. package/dist/approval/manager.d.ts +6 -0
  26. package/dist/approval/manager.d.ts.map +1 -1
  27. package/dist/approval/manager.js +19 -6
  28. package/dist/approval/schemas.cjs +16 -6
  29. package/dist/approval/schemas.d.ts +373 -1044
  30. package/dist/approval/schemas.d.ts.map +1 -1
  31. package/dist/approval/schemas.js +16 -6
  32. package/dist/approval/session-approval-store.cjs +1 -1
  33. package/dist/approval/session-approval-store.d.ts +12 -30
  34. package/dist/approval/session-approval-store.d.ts.map +1 -1
  35. package/dist/approval/session-approval-store.js +1 -1
  36. package/dist/events/index.cjs +210 -75
  37. package/dist/events/index.d.ts +44 -181
  38. package/dist/events/index.d.ts.map +1 -1
  39. package/dist/events/index.js +206 -74
  40. package/dist/hooks/manager.cjs +5 -2
  41. package/dist/hooks/manager.d.ts +2 -0
  42. package/dist/hooks/manager.d.ts.map +1 -1
  43. package/dist/hooks/manager.js +5 -2
  44. package/dist/hooks/types.d.ts +3 -0
  45. package/dist/hooks/types.d.ts.map +1 -1
  46. package/dist/index.browser.d.ts +1 -0
  47. package/dist/index.browser.d.ts.map +1 -1
  48. package/dist/index.cjs +3 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +1 -0
  52. package/dist/llm/executor/turn-executor.cjs +8 -4
  53. package/dist/llm/executor/turn-executor.d.ts +3 -1
  54. package/dist/llm/executor/turn-executor.d.ts.map +1 -1
  55. package/dist/llm/executor/turn-executor.js +8 -4
  56. package/dist/llm/providers/local/schemas.d.ts +102 -262
  57. package/dist/llm/providers/local/schemas.d.ts.map +1 -1
  58. package/dist/llm/registry/index.d.ts +5 -3
  59. package/dist/llm/registry/index.d.ts.map +1 -1
  60. package/dist/llm/resolver.cjs +0 -1
  61. package/dist/llm/resolver.d.ts.map +1 -1
  62. package/dist/llm/resolver.js +0 -1
  63. package/dist/llm/schemas.d.ts +95 -199
  64. package/dist/llm/schemas.d.ts.map +1 -1
  65. package/dist/llm/services/vercel.cjs +29 -6
  66. package/dist/llm/services/vercel.d.ts +3 -0
  67. package/dist/llm/services/vercel.d.ts.map +1 -1
  68. package/dist/llm/services/vercel.js +27 -5
  69. package/dist/logger/default-logger-factory.d.ts +28 -256
  70. package/dist/logger/default-logger-factory.d.ts.map +1 -1
  71. package/dist/logger/v2/schemas.d.ts +18 -120
  72. package/dist/logger/v2/schemas.d.ts.map +1 -1
  73. package/dist/mcp/manager.cjs +7 -2
  74. package/dist/mcp/manager.d.ts +3 -1
  75. package/dist/mcp/manager.d.ts.map +1 -1
  76. package/dist/mcp/manager.js +7 -2
  77. package/dist/mcp/mcp-client.cjs +71 -62
  78. package/dist/mcp/mcp-client.d.ts +3 -2
  79. package/dist/mcp/mcp-client.d.ts.map +1 -1
  80. package/dist/mcp/mcp-client.js +71 -62
  81. package/dist/mcp/schemas.cjs +4 -4
  82. package/dist/mcp/schemas.d.ts +77 -270
  83. package/dist/mcp/schemas.d.ts.map +1 -1
  84. package/dist/mcp/schemas.js +4 -4
  85. package/dist/memory/schemas.d.ts +37 -117
  86. package/dist/memory/schemas.d.ts.map +1 -1
  87. package/dist/prompts/schemas.d.ts +46 -190
  88. package/dist/prompts/schemas.d.ts.map +1 -1
  89. package/dist/resources/handlers/filesystem-handler.cjs +22 -3
  90. package/dist/resources/handlers/filesystem-handler.d.ts.map +1 -1
  91. package/dist/resources/handlers/filesystem-handler.js +22 -3
  92. package/dist/resources/schemas.d.ts +14 -68
  93. package/dist/resources/schemas.d.ts.map +1 -1
  94. package/dist/runtime/host-runtime.cjs +163 -0
  95. package/dist/runtime/host-runtime.d.ts +19 -0
  96. package/dist/runtime/host-runtime.d.ts.map +1 -0
  97. package/dist/runtime/host-runtime.js +133 -0
  98. package/dist/runtime/index.cjs +42 -0
  99. package/dist/runtime/index.d.ts +2 -0
  100. package/dist/runtime/index.d.ts.map +1 -0
  101. package/dist/runtime/index.js +21 -0
  102. package/dist/runtime/run-context.cjs +53 -0
  103. package/dist/runtime/run-context.d.ts +13 -0
  104. package/dist/runtime/run-context.d.ts.map +1 -0
  105. package/dist/runtime/run-context.js +34 -0
  106. package/dist/session/chat-session.cjs +30 -32
  107. package/dist/session/chat-session.d.ts +5 -15
  108. package/dist/session/chat-session.d.ts.map +1 -1
  109. package/dist/session/chat-session.js +31 -33
  110. package/dist/session/error-codes.cjs +1 -0
  111. package/dist/session/error-codes.d.ts +2 -1
  112. package/dist/session/error-codes.d.ts.map +1 -1
  113. package/dist/session/error-codes.js +1 -0
  114. package/dist/session/errors.cjs +13 -0
  115. package/dist/session/errors.d.ts +6 -0
  116. package/dist/session/errors.d.ts.map +1 -1
  117. package/dist/session/errors.js +13 -0
  118. package/dist/session/schemas.d.ts +1 -7
  119. package/dist/session/schemas.d.ts.map +1 -1
  120. package/dist/systemPrompt/schemas.cjs +2 -10
  121. package/dist/systemPrompt/schemas.d.ts +53 -343
  122. package/dist/systemPrompt/schemas.d.ts.map +1 -1
  123. package/dist/systemPrompt/schemas.js +2 -10
  124. package/dist/telemetry/decorators.cjs +75 -57
  125. package/dist/telemetry/decorators.d.ts +2 -0
  126. package/dist/telemetry/decorators.d.ts.map +1 -1
  127. package/dist/telemetry/decorators.js +75 -57
  128. package/dist/telemetry/schemas.cjs +1 -1
  129. package/dist/telemetry/schemas.d.ts +9 -44
  130. package/dist/telemetry/schemas.d.ts.map +1 -1
  131. package/dist/telemetry/schemas.js +1 -1
  132. package/dist/telemetry/utils.cjs +9 -6
  133. package/dist/telemetry/utils.d.ts +3 -0
  134. package/dist/telemetry/utils.d.ts.map +1 -1
  135. package/dist/telemetry/utils.js +9 -6
  136. package/dist/tools/schemas.cjs +1 -1
  137. package/dist/tools/schemas.d.ts +19 -62
  138. package/dist/tools/schemas.d.ts.map +1 -1
  139. package/dist/tools/schemas.js +1 -1
  140. package/dist/tools/session-tool-preferences-store.d.ts +3 -9
  141. package/dist/tools/session-tool-preferences-store.d.ts.map +1 -1
  142. package/dist/tools/tool-manager.cjs +92 -36
  143. package/dist/tools/tool-manager.d.ts +12 -3
  144. package/dist/tools/tool-manager.d.ts.map +1 -1
  145. package/dist/tools/tool-manager.js +92 -36
  146. package/dist/tools/types.d.ts +7 -1
  147. package/dist/tools/types.d.ts.map +1 -1
  148. package/dist/utils/result.cjs +11 -8
  149. package/dist/utils/result.d.ts +6 -6
  150. package/dist/utils/result.d.ts.map +1 -1
  151. package/dist/utils/result.js +12 -9
  152. package/dist/utils/schema.cjs +6 -2
  153. package/dist/utils/schema.d.ts +2 -7
  154. package/dist/utils/schema.d.ts.map +1 -1
  155. package/dist/utils/schema.js +6 -2
  156. package/dist/utils/zod-schema-converter.cjs +66 -66
  157. package/dist/utils/zod-schema-converter.d.ts +3 -2
  158. package/dist/utils/zod-schema-converter.d.ts.map +1 -1
  159. package/dist/utils/zod-schema-converter.js +66 -66
  160. package/package.json +4 -5
@@ -0,0 +1,163 @@
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 host_runtime_exports = {};
20
+ __export(host_runtime_exports, {
21
+ HostRuntimeContextSchema: () => HostRuntimeContextSchema,
22
+ HostRuntimeIdsSchema: () => HostRuntimeIdsSchema,
23
+ getHostRuntimeAttributes: () => getHostRuntimeAttributes,
24
+ getHostRuntimeBaggageEntries: () => getHostRuntimeBaggageEntries,
25
+ getHostRuntimeContextFromBaggage: () => getHostRuntimeContextFromBaggage,
26
+ isHostRuntimeBaggageKey: () => isHostRuntimeBaggageKey,
27
+ normalizeHostRuntimeContext: () => normalizeHostRuntimeContext,
28
+ resolveHostRuntimeContext: () => resolveHostRuntimeContext
29
+ });
30
+ module.exports = __toCommonJS(host_runtime_exports);
31
+ var import_api = require("@opentelemetry/api");
32
+ var import_zod = require("zod");
33
+ const HOST_RUNTIME_ENTRY_PREFIX = "hostRuntime.ids.";
34
+ const WELL_KNOWN_HOST_RUNTIME_ID_KEYS = ["runtimeId", "runId", "attemptId", "workspaceId"];
35
+ const WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET = new Set(WELL_KNOWN_HOST_RUNTIME_ID_KEYS);
36
+ const HostRuntimeIdKeySchema = import_zod.z.string().min(1).regex(
37
+ /^[A-Za-z0-9._-]+$/,
38
+ "Runtime ID keys may only contain letters, numbers, dot, underscore, or hyphen."
39
+ );
40
+ const HostRuntimeIdValueSchema = import_zod.z.string().trim().min(1);
41
+ const HostRuntimeIdsSchema = import_zod.z.record(HostRuntimeIdKeySchema, HostRuntimeIdValueSchema).describe(
42
+ "Host-owned runtime IDs keyed by a stable identifier name such as runId, attemptId, or workspaceId."
43
+ );
44
+ const HostRuntimeContextSchema = import_zod.z.object({
45
+ ids: HostRuntimeIdsSchema.optional().describe(
46
+ "Optional host-owned runtime IDs used for correlation across orchestration, telemetry, logs, and events."
47
+ )
48
+ }).strict().describe("Host-owned runtime context surfaced through core runtime flows.");
49
+ function freezeHostRuntimeContext(hostRuntime) {
50
+ return Object.freeze({
51
+ ids: Object.freeze({ ...hostRuntime.ids })
52
+ });
53
+ }
54
+ function normalizeHostRuntimeContext(input) {
55
+ if (input === void 0) {
56
+ return void 0;
57
+ }
58
+ const parsed = HostRuntimeContextSchema.parse(input);
59
+ if (!parsed.ids || Object.keys(parsed.ids).length === 0) {
60
+ return void 0;
61
+ }
62
+ return freezeHostRuntimeContext({ ids: parsed.ids });
63
+ }
64
+ function resolveHostRuntimeContext({
65
+ inherited,
66
+ explicit,
67
+ runId
68
+ }) {
69
+ const ids = {
70
+ ...inherited?.ids ?? {},
71
+ ...explicit?.ids ?? {},
72
+ ...runId !== void 0 ? { runId } : {}
73
+ };
74
+ if (Object.keys(ids).length === 0) {
75
+ return void 0;
76
+ }
77
+ return normalizeHostRuntimeContext({ ids });
78
+ }
79
+ function isWellKnownHostRuntimeIdKey(key) {
80
+ return WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET.has(key);
81
+ }
82
+ function isHostRuntimeBaggageKey(key) {
83
+ return key.startsWith(HOST_RUNTIME_ENTRY_PREFIX) || WELL_KNOWN_HOST_RUNTIME_ID_KEY_SET.has(key);
84
+ }
85
+ function getValidHostRuntimeIdValue(value) {
86
+ const parsed = HostRuntimeIdValueSchema.safeParse(value);
87
+ return parsed.success ? parsed.data : void 0;
88
+ }
89
+ function getHostRuntimeBaggageEntries(hostRuntime) {
90
+ const ids = hostRuntime?.ids;
91
+ if (!ids) {
92
+ return {};
93
+ }
94
+ const entries = {};
95
+ for (const [key, value] of Object.entries(ids)) {
96
+ entries[`${HOST_RUNTIME_ENTRY_PREFIX}${key}`] = { value };
97
+ if (isWellKnownHostRuntimeIdKey(key)) {
98
+ entries[key] = { value };
99
+ }
100
+ }
101
+ return entries;
102
+ }
103
+ function getHostRuntimeAttributes(hostRuntime) {
104
+ const ids = hostRuntime?.ids;
105
+ if (!ids) {
106
+ return {};
107
+ }
108
+ const attributes = {};
109
+ for (const [key, value] of Object.entries(ids)) {
110
+ attributes[`${HOST_RUNTIME_ENTRY_PREFIX}${key}`] = value;
111
+ if (isWellKnownHostRuntimeIdKey(key)) {
112
+ attributes[key] = value;
113
+ }
114
+ }
115
+ return attributes;
116
+ }
117
+ function getHostRuntimeContextFromBaggage(ctx) {
118
+ const baggage = import_api.propagation.getBaggage(ctx);
119
+ if (!baggage) {
120
+ return void 0;
121
+ }
122
+ const ids = {};
123
+ for (const [key, entry] of baggage.getAllEntries()) {
124
+ if (key.startsWith(HOST_RUNTIME_ENTRY_PREFIX)) {
125
+ const hostRuntimeKey = key.slice(HOST_RUNTIME_ENTRY_PREFIX.length);
126
+ if (!HostRuntimeIdKeySchema.safeParse(hostRuntimeKey).success) {
127
+ continue;
128
+ }
129
+ const value = getValidHostRuntimeIdValue(entry.value);
130
+ if (value === void 0) {
131
+ continue;
132
+ }
133
+ ids[hostRuntimeKey] = value;
134
+ }
135
+ }
136
+ for (const key of WELL_KNOWN_HOST_RUNTIME_ID_KEYS) {
137
+ if (ids[key] !== void 0) {
138
+ continue;
139
+ }
140
+ const entry = baggage.getEntry(key);
141
+ if (entry) {
142
+ const value = getValidHostRuntimeIdValue(entry.value);
143
+ if (value !== void 0) {
144
+ ids[key] = value;
145
+ }
146
+ }
147
+ }
148
+ if (Object.keys(ids).length === 0) {
149
+ return void 0;
150
+ }
151
+ return normalizeHostRuntimeContext({ ids });
152
+ }
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ HostRuntimeContextSchema,
156
+ HostRuntimeIdsSchema,
157
+ getHostRuntimeAttributes,
158
+ getHostRuntimeBaggageEntries,
159
+ getHostRuntimeContextFromBaggage,
160
+ isHostRuntimeBaggageKey,
161
+ normalizeHostRuntimeContext,
162
+ resolveHostRuntimeContext
163
+ });
@@ -0,0 +1,19 @@
1
+ import type { BaggageEntry, Context } from '@opentelemetry/api';
2
+ import { z } from 'zod';
3
+ export declare const HostRuntimeIdsSchema: z.ZodRecord<z.ZodString, z.ZodString>;
4
+ export declare const HostRuntimeContextSchema: z.ZodObject<{
5
+ ids: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
6
+ }, z.core.$strict>;
7
+ export type HostRuntimeIds = z.output<typeof HostRuntimeIdsSchema>;
8
+ export type HostRuntimeContext = z.output<typeof HostRuntimeContextSchema>;
9
+ export declare function normalizeHostRuntimeContext(input: z.input<typeof HostRuntimeContextSchema> | undefined): HostRuntimeContext | undefined;
10
+ export declare function resolveHostRuntimeContext({ inherited, explicit, runId, }: {
11
+ inherited?: HostRuntimeContext | undefined;
12
+ explicit?: HostRuntimeContext | undefined;
13
+ runId?: string | undefined;
14
+ }): HostRuntimeContext | undefined;
15
+ export declare function isHostRuntimeBaggageKey(key: string): boolean;
16
+ export declare function getHostRuntimeBaggageEntries(hostRuntime?: HostRuntimeContext): Record<string, BaggageEntry>;
17
+ export declare function getHostRuntimeAttributes(hostRuntime?: HostRuntimeContext): Record<string, string>;
18
+ export declare function getHostRuntimeContextFromBaggage(ctx: Context): HostRuntimeContext | undefined;
19
+ //# sourceMappingURL=host-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host-runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/host-runtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgBxB,eAAO,MAAM,oBAAoB,uCAI5B,CAAC;AAEN,eAAO,MAAM,wBAAwB;;kBAO2C,CAAC;AAEjF,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,oBAAoB,CAAC,CAAC;AACnE,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAQ3E,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,GAAG,SAAS,GAC5D,kBAAkB,GAAG,SAAS,CAWhC;AAED,wBAAgB,yBAAyB,CAAC,EACtC,SAAS,EACT,QAAQ,EACR,KAAK,GACR,EAAE;IACC,SAAS,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;IAC3C,QAAQ,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;IAC1C,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B,GAAG,kBAAkB,GAAG,SAAS,CAYjC;AAQD,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE5D;AAOD,wBAAgB,4BAA4B,CACxC,WAAW,CAAC,EAAE,kBAAkB,GACjC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAe9B;AAED,wBAAgB,wBAAwB,CAAC,WAAW,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAejG;AAED,wBAAgB,gCAAgC,CAAC,GAAG,EAAE,OAAO,GAAG,kBAAkB,GAAG,SAAS,CA0C7F"}
@@ -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 with forwarding)
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.setupEventForwarding();
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
- * Sets up event forwarding from session bus to global agent bus.
114
- *
115
- * All session events are automatically forwarded to the global bus with the same
116
- * event names, but with session context added to the payload. This allows the app
117
- * layer to continue listening to standard events while having access to session
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.setupTokenAccumulation();
130
- this.logger.debug(
131
- `[setupEventForwarding] Event forwarding setup complete for session=${this.id}`
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, { signal });
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.forwarders.forEach((forwarder, eventName) => {
540
- this.eventBus.off(eventName, forwarder);
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;