@google/adk 0.2.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/a2a/part_converter_utils.js +210 -0
- package/dist/cjs/agents/active_streaming_tool.js +1 -1
- package/dist/cjs/agents/base_agent.js +46 -24
- package/dist/cjs/agents/base_llm_processor.js +1 -1
- package/dist/cjs/agents/callback_context.js +5 -2
- package/dist/cjs/agents/content_processor_utils.js +16 -8
- package/dist/cjs/agents/functions.js +81 -30
- package/dist/cjs/agents/instructions.js +1 -1
- package/dist/cjs/agents/invocation_context.js +1 -1
- package/dist/cjs/agents/live_request_queue.js +1 -1
- package/dist/cjs/agents/llm_agent.js +106 -62
- package/dist/cjs/agents/loop_agent.js +3 -2
- package/dist/cjs/agents/parallel_agent.js +4 -5
- package/dist/cjs/agents/readonly_context.js +13 -1
- package/dist/cjs/agents/run_config.js +2 -1
- package/dist/cjs/agents/sequential_agent.js +1 -1
- package/dist/cjs/agents/transcription_entry.js +1 -1
- package/dist/cjs/artifacts/base_artifact_service.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +491 -0
- package/dist/cjs/artifacts/gcs_artifact_service.js +145 -58
- package/dist/cjs/artifacts/in_memory_artifact_service.js +72 -10
- package/dist/cjs/artifacts/registry.js +55 -0
- package/dist/cjs/auth/auth_credential.js +1 -1
- package/dist/cjs/auth/auth_handler.js +4 -2
- package/dist/cjs/auth/auth_schemes.js +1 -1
- package/dist/cjs/auth/auth_tool.js +1 -1
- package/dist/cjs/auth/credential_service/base_credential_service.js +1 -1
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +1 -1
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +1 -1
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +1 -1
- package/dist/cjs/code_executors/base_code_executor.js +4 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +8 -4
- package/dist/cjs/code_executors/code_execution_utils.js +1 -1
- package/dist/cjs/code_executors/code_executor_context.js +6 -6
- package/dist/cjs/common.js +18 -1
- package/dist/cjs/events/event.js +34 -7
- package/dist/cjs/events/event_actions.js +2 -2
- package/dist/cjs/events/structured_events.js +105 -0
- package/dist/cjs/examples/base_example_provider.js +1 -1
- package/dist/cjs/examples/example.js +1 -1
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +54 -83
- package/dist/cjs/index_web.js +1 -1
- package/dist/cjs/memory/base_memory_service.js +1 -1
- package/dist/cjs/memory/in_memory_memory_service.js +4 -2
- package/dist/cjs/memory/memory_entry.js +1 -1
- package/dist/cjs/models/apigee_llm.js +182 -0
- package/dist/cjs/models/base_llm.js +9 -5
- package/dist/cjs/models/base_llm_connection.js +1 -1
- package/dist/cjs/models/gemini_llm_connection.js +2 -1
- package/dist/cjs/models/google_llm.js +73 -54
- package/dist/cjs/models/llm_request.js +1 -1
- package/dist/cjs/models/llm_response.js +1 -1
- package/dist/cjs/models/registry.js +3 -1
- package/dist/cjs/plugins/base_plugin.js +13 -1
- package/dist/cjs/plugins/logging_plugin.js +51 -14
- package/dist/cjs/plugins/plugin_manager.js +57 -25
- package/dist/cjs/plugins/security_plugin.js +2 -2
- package/dist/cjs/runner/in_memory_runner.js +1 -1
- package/dist/cjs/runner/runner.js +142 -96
- package/dist/cjs/sessions/base_session_service.js +53 -3
- package/dist/cjs/sessions/database_session_service.js +364 -0
- package/dist/cjs/sessions/db/operations.js +114 -0
- package/dist/cjs/sessions/db/schema.js +204 -0
- package/dist/cjs/sessions/in_memory_session_service.js +60 -34
- package/dist/cjs/sessions/registry.js +49 -0
- package/dist/cjs/sessions/session.js +1 -1
- package/dist/cjs/sessions/state.js +1 -1
- package/dist/cjs/telemetry/google_cloud.js +8 -10
- package/dist/cjs/telemetry/setup.js +16 -8
- package/dist/cjs/telemetry/tracing.js +38 -16
- package/dist/cjs/tools/agent_tool.js +9 -5
- package/dist/cjs/tools/base_tool.js +5 -3
- package/dist/cjs/tools/base_toolset.js +1 -1
- package/dist/cjs/tools/forwarding_artifact_service.js +18 -2
- package/dist/cjs/tools/function_tool.js +2 -3
- package/dist/cjs/tools/google_search_tool.js +2 -3
- package/dist/cjs/tools/long_running_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_session_manager.js +17 -11
- package/dist/cjs/tools/mcp/mcp_tool.js +2 -4
- package/dist/cjs/tools/mcp/mcp_toolset.js +2 -2
- package/dist/cjs/tools/tool_confirmation.js +1 -1
- package/dist/cjs/tools/tool_context.js +1 -1
- package/dist/cjs/utils/client_labels.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +11 -2
- package/dist/cjs/utils/gemini_schema_util.js +10 -5
- package/dist/cjs/utils/logger.js +48 -4
- package/dist/cjs/utils/model_name.js +1 -1
- package/dist/cjs/utils/object_notation_utils.js +78 -0
- package/dist/cjs/utils/simple_zod_to_json.js +101 -142
- package/dist/cjs/utils/variant_utils.js +3 -9
- package/dist/cjs/version.js +2 -2
- package/dist/esm/a2a/part_converter_utils.js +171 -0
- package/dist/esm/agents/base_agent.js +50 -24
- package/dist/esm/agents/callback_context.js +4 -1
- package/dist/esm/agents/content_processor_utils.js +25 -9
- package/dist/esm/agents/functions.js +84 -29
- package/dist/esm/agents/llm_agent.js +110 -62
- package/dist/esm/agents/loop_agent.js +2 -1
- package/dist/esm/agents/parallel_agent.js +3 -4
- package/dist/esm/agents/readonly_context.js +12 -0
- package/dist/esm/agents/run_config.js +1 -0
- package/dist/esm/artifacts/file_artifact_service.js +451 -0
- package/dist/esm/artifacts/gcs_artifact_service.js +144 -57
- package/dist/esm/artifacts/in_memory_artifact_service.js +69 -8
- package/dist/esm/artifacts/registry.js +28 -0
- package/dist/esm/auth/auth_handler.js +3 -1
- package/dist/esm/code_executors/base_code_executor.js +3 -1
- package/dist/esm/code_executors/built_in_code_executor.js +7 -3
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +12 -2
- package/dist/esm/events/event.js +30 -5
- package/dist/esm/events/event_actions.js +1 -1
- package/dist/esm/events/structured_events.js +74 -0
- package/dist/esm/index.js +18 -88
- package/dist/esm/memory/in_memory_memory_service.js +3 -1
- package/dist/esm/models/apigee_llm.js +152 -0
- package/dist/esm/models/base_llm.js +8 -4
- package/dist/esm/models/gemini_llm_connection.js +1 -0
- package/dist/esm/models/google_llm.js +75 -53
- package/dist/esm/models/registry.js +2 -0
- package/dist/esm/plugins/base_plugin.js +12 -0
- package/dist/esm/plugins/logging_plugin.js +55 -14
- package/dist/esm/plugins/plugin_manager.js +56 -24
- package/dist/esm/plugins/security_plugin.js +1 -1
- package/dist/esm/runner/runner.js +145 -96
- package/dist/esm/sessions/base_session_service.js +49 -1
- package/dist/esm/sessions/database_session_service.js +350 -0
- package/dist/esm/sessions/db/operations.js +87 -0
- package/dist/esm/sessions/db/schema.js +172 -0
- package/dist/esm/sessions/in_memory_session_service.js +61 -33
- package/dist/esm/sessions/registry.js +25 -0
- package/dist/esm/telemetry/google_cloud.js +7 -9
- package/dist/esm/telemetry/setup.js +23 -9
- package/dist/esm/telemetry/tracing.js +37 -15
- package/dist/esm/tools/agent_tool.js +8 -4
- package/dist/esm/tools/base_tool.js +4 -2
- package/dist/esm/tools/forwarding_artifact_service.js +17 -1
- package/dist/esm/tools/function_tool.js +1 -2
- package/dist/esm/tools/google_search_tool.js +2 -5
- package/dist/esm/tools/long_running_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/esm/tools/mcp/mcp_tool.js +1 -3
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
- package/dist/esm/utils/env_aware_utils.js +9 -1
- package/dist/esm/utils/gemini_schema_util.js +9 -4
- package/dist/esm/utils/logger.js +43 -2
- package/dist/esm/utils/object_notation_utils.js +47 -0
- package/dist/esm/utils/simple_zod_to_json.js +102 -141
- package/dist/esm/utils/variant_utils.js +1 -7
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/part_converter_utils.d.ts +47 -0
- package/dist/types/agents/base_agent.d.ts +2 -1
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/llm_agent.d.ts +12 -12
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/parallel_agent.d.ts +1 -1
- package/dist/types/agents/readonly_context.d.ts +8 -0
- package/dist/types/agents/run_config.d.ts +6 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
- package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +8 -5
- package/dist/types/artifacts/registry.d.ts +7 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +13 -3
- package/dist/types/events/event.d.ts +15 -1
- package/dist/types/events/event_actions.d.ts +1 -1
- package/dist/types/events/structured_events.d.ts +106 -0
- package/dist/types/index.d.ts +7 -3
- package/dist/types/models/apigee_llm.d.ts +59 -0
- package/dist/types/models/base_llm_connection.d.ts +1 -1
- package/dist/types/models/google_llm.d.ts +5 -2
- package/dist/types/models/llm_response.d.ts +1 -1
- package/dist/types/plugins/logging_plugin.d.ts +12 -12
- package/dist/types/plugins/plugin_manager.d.ts +12 -12
- package/dist/types/plugins/security_plugin.d.ts +1 -1
- package/dist/types/runner/runner.d.ts +16 -1
- package/dist/types/sessions/base_session_service.d.ts +20 -0
- package/dist/types/sessions/database_session_service.d.ts +31 -0
- package/dist/types/sessions/db/operations.d.ts +29 -0
- package/dist/types/sessions/db/schema.d.ts +45 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +9 -6
- package/dist/types/sessions/registry.d.ts +7 -0
- package/dist/types/telemetry/setup.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +7 -6
- package/dist/types/tools/agent_tool.d.ts +1 -1
- package/dist/types/tools/base_tool.d.ts +1 -1
- package/dist/types/tools/base_toolset.d.ts +2 -1
- package/dist/types/tools/forwarding_artifact_service.d.ts +4 -2
- package/dist/types/tools/function_tool.d.ts +4 -3
- package/dist/types/tools/google_search_tool.d.ts +3 -3
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
- package/dist/types/utils/env_aware_utils.d.ts +7 -0
- package/dist/types/utils/gemini_schema_util.d.ts +4 -12
- package/dist/types/utils/logger.d.ts +11 -10
- package/dist/types/utils/object_notation_utils.d.ts +21 -0
- package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/part_converter_utils.js +171 -0
- package/dist/web/agents/base_agent.js +96 -35
- package/dist/web/agents/callback_context.js +4 -1
- package/dist/web/agents/content_processor_utils.js +25 -9
- package/dist/web/agents/functions.js +84 -29
- package/dist/web/agents/llm_agent.js +166 -83
- package/dist/web/agents/loop_agent.js +2 -1
- package/dist/web/agents/parallel_agent.js +3 -4
- package/dist/web/agents/readonly_context.js +12 -0
- package/dist/web/agents/run_config.js +2 -1
- package/dist/web/artifacts/file_artifact_service.js +506 -0
- package/dist/web/artifacts/gcs_artifact_service.js +139 -54
- package/dist/web/artifacts/in_memory_artifact_service.js +69 -8
- package/dist/web/artifacts/registry.js +28 -0
- package/dist/web/auth/auth_handler.js +3 -1
- package/dist/web/code_executors/base_code_executor.js +3 -1
- package/dist/web/code_executors/built_in_code_executor.js +7 -3
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +12 -2
- package/dist/web/events/event.js +30 -5
- package/dist/web/events/event_actions.js +1 -1
- package/dist/web/events/structured_events.js +74 -0
- package/dist/web/index.js +18 -8
- package/dist/web/memory/in_memory_memory_service.js +3 -1
- package/dist/web/models/apigee_llm.js +219 -0
- package/dist/web/models/base_llm.js +8 -4
- package/dist/web/models/gemini_llm_connection.js +1 -0
- package/dist/web/models/google_llm.js +75 -50
- package/dist/web/models/registry.js +2 -0
- package/dist/web/plugins/base_plugin.js +12 -0
- package/dist/web/plugins/logging_plugin.js +55 -14
- package/dist/web/plugins/plugin_manager.js +56 -24
- package/dist/web/plugins/security_plugin.js +1 -1
- package/dist/web/runner/runner.js +192 -108
- package/dist/web/sessions/base_session_service.js +49 -1
- package/dist/web/sessions/database_session_service.js +368 -0
- package/dist/web/sessions/db/operations.js +87 -0
- package/dist/web/sessions/db/schema.js +172 -0
- package/dist/web/sessions/in_memory_session_service.js +61 -33
- package/dist/web/sessions/registry.js +25 -0
- package/dist/web/telemetry/google_cloud.js +7 -9
- package/dist/web/telemetry/setup.js +23 -9
- package/dist/web/telemetry/tracing.js +37 -15
- package/dist/web/tools/agent_tool.js +8 -4
- package/dist/web/tools/base_tool.js +4 -2
- package/dist/web/tools/forwarding_artifact_service.js +17 -1
- package/dist/web/tools/function_tool.js +1 -2
- package/dist/web/tools/google_search_tool.js +2 -5
- package/dist/web/tools/long_running_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/web/tools/mcp/mcp_tool.js +1 -3
- package/dist/web/tools/mcp/mcp_toolset.js +1 -1
- package/dist/web/utils/env_aware_utils.js +9 -1
- package/dist/web/utils/gemini_schema_util.js +9 -4
- package/dist/web/utils/logger.js +43 -2
- package/dist/web/utils/object_notation_utils.js +47 -0
- package/dist/web/utils/simple_zod_to_json.js +102 -155
- package/dist/web/utils/variant_utils.js +1 -7
- package/dist/web/version.js +1 -1
- package/package.json +16 -5
- package/dist/cjs/index.js.map +0 -7
- package/dist/esm/index.js.map +0 -7
- package/dist/web/index.js.map +0 -7
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { createPartFromText } from "@google/genai";
|
|
7
|
-
import { trace } from "@opentelemetry/api";
|
|
7
|
+
import { context, trace } from "@opentelemetry/api";
|
|
8
8
|
import {
|
|
9
9
|
InvocationContext,
|
|
10
10
|
newInvocationContextId
|
|
@@ -18,6 +18,10 @@ import {
|
|
|
18
18
|
import { createEvent, getFunctionCalls } from "../events/event.js";
|
|
19
19
|
import { createEventActions } from "../events/event_actions.js";
|
|
20
20
|
import { PluginManager } from "../plugins/plugin_manager.js";
|
|
21
|
+
import {
|
|
22
|
+
runAsyncGeneratorWithOtelContext,
|
|
23
|
+
tracer
|
|
24
|
+
} from "../telemetry/tracing.js";
|
|
21
25
|
import { logger } from "../utils/logger.js";
|
|
22
26
|
import { isGemini2OrAbove } from "../utils/model_name.js";
|
|
23
27
|
class Runner {
|
|
@@ -31,6 +35,37 @@ class Runner {
|
|
|
31
35
|
this.memoryService = input.memoryService;
|
|
32
36
|
this.credentialService = input.credentialService;
|
|
33
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Runs the agent with a new, ephemeral session.
|
|
40
|
+
*
|
|
41
|
+
* @param params.userId The user ID of the session.
|
|
42
|
+
* @param params.newMessage A new message to append to the session.
|
|
43
|
+
* @param params.stateDelta An optional state delta to apply to the session.
|
|
44
|
+
* @param params.runConfig The run config for the agent.
|
|
45
|
+
* @yields The Events generated by the agent.
|
|
46
|
+
*/
|
|
47
|
+
async *runEphemeral(params) {
|
|
48
|
+
const session = await this.sessionService.createSession({
|
|
49
|
+
appName: this.appName,
|
|
50
|
+
userId: params.userId
|
|
51
|
+
});
|
|
52
|
+
const sessionId = session.id;
|
|
53
|
+
try {
|
|
54
|
+
yield* this.runAsync({
|
|
55
|
+
userId: params.userId,
|
|
56
|
+
sessionId,
|
|
57
|
+
newMessage: params.newMessage,
|
|
58
|
+
stateDelta: params.stateDelta,
|
|
59
|
+
runConfig: params.runConfig
|
|
60
|
+
});
|
|
61
|
+
} finally {
|
|
62
|
+
await this.sessionService.deleteSession({
|
|
63
|
+
appName: this.appName,
|
|
64
|
+
userId: params.userId,
|
|
65
|
+
sessionId
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
34
69
|
/**
|
|
35
70
|
* Runs the agent with the given message, and returns an async generator of
|
|
36
71
|
* events.
|
|
@@ -44,109 +79,123 @@ class Runner {
|
|
|
44
79
|
*/
|
|
45
80
|
// TODO - b/425992518: user, sessionId, and runConfig can be internalized.
|
|
46
81
|
async *runAsync(params) {
|
|
47
|
-
var _a;
|
|
48
82
|
const { userId, sessionId, stateDelta } = params;
|
|
49
83
|
const runConfig = createRunConfig(params.runConfig);
|
|
50
84
|
let newMessage = params.newMessage;
|
|
51
|
-
const span =
|
|
85
|
+
const span = tracer.startSpan("invocation");
|
|
86
|
+
const ctx = trace.setSpan(context.active(), span);
|
|
52
87
|
try {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
credentialService: this.credentialService,
|
|
82
|
-
invocationId: newInvocationContextId(),
|
|
83
|
-
agent: this.agent,
|
|
84
|
-
session,
|
|
85
|
-
userContent: newMessage,
|
|
86
|
-
runConfig,
|
|
87
|
-
pluginManager: this.pluginManager
|
|
88
|
-
});
|
|
89
|
-
const pluginUserMessage = await this.pluginManager.runOnUserMessageCallback({
|
|
90
|
-
userMessage: newMessage,
|
|
91
|
-
invocationContext
|
|
92
|
-
});
|
|
93
|
-
if (pluginUserMessage) {
|
|
94
|
-
newMessage = pluginUserMessage;
|
|
95
|
-
}
|
|
96
|
-
if (newMessage) {
|
|
97
|
-
if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
|
|
98
|
-
throw new Error("No parts in the newMessage.");
|
|
99
|
-
}
|
|
100
|
-
if (runConfig.saveInputBlobsAsArtifacts) {
|
|
101
|
-
await this.saveArtifacts(
|
|
102
|
-
invocationContext.invocationId,
|
|
103
|
-
session.userId,
|
|
104
|
-
session.id,
|
|
105
|
-
newMessage
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
await this.sessionService.appendEvent({
|
|
109
|
-
session,
|
|
110
|
-
event: createEvent({
|
|
111
|
-
invocationId: invocationContext.invocationId,
|
|
112
|
-
author: "user",
|
|
113
|
-
actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
|
|
114
|
-
content: newMessage
|
|
115
|
-
})
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
invocationContext.agent = this.determineAgentForResumption(
|
|
119
|
-
session,
|
|
120
|
-
this.agent
|
|
121
|
-
);
|
|
122
|
-
const beforeRunCallbackResponse = await this.pluginManager.runBeforeRunCallback({ invocationContext });
|
|
123
|
-
if (beforeRunCallbackResponse) {
|
|
124
|
-
const earlyExitEvent = createEvent({
|
|
125
|
-
invocationId: invocationContext.invocationId,
|
|
126
|
-
author: "model",
|
|
127
|
-
content: beforeRunCallbackResponse
|
|
128
|
-
});
|
|
129
|
-
await this.sessionService.appendEvent({ session, event: earlyExitEvent });
|
|
130
|
-
yield earlyExitEvent;
|
|
131
|
-
} else {
|
|
132
|
-
for await (const event of invocationContext.agent.runAsync(
|
|
133
|
-
invocationContext
|
|
134
|
-
)) {
|
|
135
|
-
if (!event.partial) {
|
|
136
|
-
await this.sessionService.appendEvent({ session, event });
|
|
88
|
+
yield* runAsyncGeneratorWithOtelContext(
|
|
89
|
+
ctx,
|
|
90
|
+
this,
|
|
91
|
+
async function* () {
|
|
92
|
+
var _a;
|
|
93
|
+
const session = await this.sessionService.getSession({
|
|
94
|
+
appName: this.appName,
|
|
95
|
+
userId,
|
|
96
|
+
sessionId
|
|
97
|
+
});
|
|
98
|
+
if (!session) {
|
|
99
|
+
if (!this.appName) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Session lookup failed: appName must be provided in runner constructor`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
105
|
+
}
|
|
106
|
+
if (runConfig.supportCfc && isLlmAgent(this.agent)) {
|
|
107
|
+
const modelName = this.agent.canonicalModel.model;
|
|
108
|
+
if (!isGemini2OrAbove(modelName)) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`CFC is not supported for model: ${modelName} in agent: ${this.agent.name}`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (!isBuiltInCodeExecutor(this.agent.codeExecutor)) {
|
|
114
|
+
this.agent.codeExecutor = new BuiltInCodeExecutor();
|
|
115
|
+
}
|
|
137
116
|
}
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
117
|
+
const invocationContext = new InvocationContext({
|
|
118
|
+
artifactService: this.artifactService,
|
|
119
|
+
sessionService: this.sessionService,
|
|
120
|
+
memoryService: this.memoryService,
|
|
121
|
+
credentialService: this.credentialService,
|
|
122
|
+
invocationId: newInvocationContextId(),
|
|
123
|
+
agent: this.agent,
|
|
124
|
+
session,
|
|
125
|
+
userContent: newMessage,
|
|
126
|
+
runConfig,
|
|
127
|
+
pluginManager: this.pluginManager
|
|
128
|
+
});
|
|
129
|
+
const pluginUserMessage = await this.pluginManager.runOnUserMessageCallback({
|
|
130
|
+
userMessage: newMessage,
|
|
131
|
+
invocationContext
|
|
141
132
|
});
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
133
|
+
if (pluginUserMessage) {
|
|
134
|
+
newMessage = pluginUserMessage;
|
|
135
|
+
}
|
|
136
|
+
if (newMessage) {
|
|
137
|
+
if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
|
|
138
|
+
throw new Error("No parts in the newMessage.");
|
|
139
|
+
}
|
|
140
|
+
if (runConfig.saveInputBlobsAsArtifacts) {
|
|
141
|
+
await this.saveArtifacts(
|
|
142
|
+
invocationContext.invocationId,
|
|
143
|
+
session.userId,
|
|
144
|
+
session.id,
|
|
145
|
+
newMessage
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
await this.sessionService.appendEvent({
|
|
149
|
+
session,
|
|
150
|
+
event: createEvent({
|
|
151
|
+
invocationId: invocationContext.invocationId,
|
|
152
|
+
author: "user",
|
|
153
|
+
actions: stateDelta ? createEventActions({ stateDelta }) : void 0,
|
|
154
|
+
content: newMessage
|
|
155
|
+
})
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
invocationContext.agent = this.determineAgentForResumption(
|
|
159
|
+
session,
|
|
160
|
+
this.agent
|
|
161
|
+
);
|
|
162
|
+
if (newMessage) {
|
|
163
|
+
const beforeRunCallbackResponse = await this.pluginManager.runBeforeRunCallback({
|
|
164
|
+
invocationContext
|
|
165
|
+
});
|
|
166
|
+
if (beforeRunCallbackResponse) {
|
|
167
|
+
const earlyExitEvent = createEvent({
|
|
168
|
+
invocationId: invocationContext.invocationId,
|
|
169
|
+
author: "model",
|
|
170
|
+
content: beforeRunCallbackResponse
|
|
171
|
+
});
|
|
172
|
+
await this.sessionService.appendEvent({
|
|
173
|
+
session,
|
|
174
|
+
event: earlyExitEvent
|
|
175
|
+
});
|
|
176
|
+
yield earlyExitEvent;
|
|
177
|
+
} else {
|
|
178
|
+
for await (const event of invocationContext.agent.runAsync(
|
|
179
|
+
invocationContext
|
|
180
|
+
)) {
|
|
181
|
+
if (!event.partial) {
|
|
182
|
+
await this.sessionService.appendEvent({ session, event });
|
|
183
|
+
}
|
|
184
|
+
const modifiedEvent = await this.pluginManager.runOnEventCallback({
|
|
185
|
+
invocationContext,
|
|
186
|
+
event
|
|
187
|
+
});
|
|
188
|
+
if (modifiedEvent) {
|
|
189
|
+
yield modifiedEvent;
|
|
190
|
+
} else {
|
|
191
|
+
yield event;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
await this.pluginManager.runAfterRunCallback({ invocationContext });
|
|
195
|
+
}
|
|
146
196
|
}
|
|
147
197
|
}
|
|
148
|
-
|
|
149
|
-
await this.pluginManager.runAfterRunCallback({ invocationContext });
|
|
198
|
+
);
|
|
150
199
|
} finally {
|
|
151
200
|
span.end();
|
|
152
201
|
}
|
|
@@ -3,8 +3,29 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
+
import { cloneDeep } from "lodash-es";
|
|
6
7
|
import { State } from "./state.js";
|
|
7
8
|
class BaseSessionService {
|
|
9
|
+
/**
|
|
10
|
+
* Gets a session or creates one if it doesn't exist.
|
|
11
|
+
*
|
|
12
|
+
* @param request The request to get or create a session.
|
|
13
|
+
* @return A promise that resolves to the session instance.
|
|
14
|
+
*/
|
|
15
|
+
async getOrCreateSession(request) {
|
|
16
|
+
if (!request.sessionId) {
|
|
17
|
+
return this.createSession(request);
|
|
18
|
+
}
|
|
19
|
+
const session = await this.getSession({
|
|
20
|
+
appName: request.appName,
|
|
21
|
+
userId: request.userId,
|
|
22
|
+
sessionId: request.sessionId
|
|
23
|
+
});
|
|
24
|
+
if (session) {
|
|
25
|
+
return session;
|
|
26
|
+
}
|
|
27
|
+
return this.createSession(request);
|
|
28
|
+
}
|
|
8
29
|
/**
|
|
9
30
|
* Appends an event to a session.
|
|
10
31
|
*
|
|
@@ -15,6 +36,7 @@ class BaseSessionService {
|
|
|
15
36
|
if (event.partial) {
|
|
16
37
|
return event;
|
|
17
38
|
}
|
|
39
|
+
event = trimTempDeltaState(event);
|
|
18
40
|
this.updateSessionState({ session, event });
|
|
19
41
|
session.events.push(event);
|
|
20
42
|
return event;
|
|
@@ -36,6 +58,32 @@ class BaseSessionService {
|
|
|
36
58
|
}
|
|
37
59
|
}
|
|
38
60
|
}
|
|
61
|
+
function trimTempDeltaState(event) {
|
|
62
|
+
if (!event.actions || !event.actions.stateDelta) {
|
|
63
|
+
return event;
|
|
64
|
+
}
|
|
65
|
+
const stateDelta = event.actions.stateDelta;
|
|
66
|
+
const filteredStateDelta = {};
|
|
67
|
+
for (const [key, value] of Object.entries(stateDelta)) {
|
|
68
|
+
if (!key.startsWith(State.TEMP_PREFIX)) {
|
|
69
|
+
filteredStateDelta[key] = value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
event.actions.stateDelta = filteredStateDelta;
|
|
73
|
+
return event;
|
|
74
|
+
}
|
|
75
|
+
function mergeStates(appState = {}, userState = {}, sessionState = {}) {
|
|
76
|
+
const merged = cloneDeep(sessionState);
|
|
77
|
+
for (const [k, v] of Object.entries(appState)) {
|
|
78
|
+
merged[State.APP_PREFIX + k] = v;
|
|
79
|
+
}
|
|
80
|
+
for (const [k, v] of Object.entries(userState)) {
|
|
81
|
+
merged[State.USER_PREFIX + k] = v;
|
|
82
|
+
}
|
|
83
|
+
return merged;
|
|
84
|
+
}
|
|
39
85
|
export {
|
|
40
|
-
BaseSessionService
|
|
86
|
+
BaseSessionService,
|
|
87
|
+
mergeStates,
|
|
88
|
+
trimTempDeltaState
|
|
41
89
|
};
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
LockMode,
|
|
8
|
+
MikroORM
|
|
9
|
+
} from "@mikro-orm/core";
|
|
10
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
11
|
+
import {
|
|
12
|
+
BaseSessionService,
|
|
13
|
+
mergeStates,
|
|
14
|
+
trimTempDeltaState
|
|
15
|
+
} from "./base_session_service.js";
|
|
16
|
+
import {
|
|
17
|
+
ensureDatabaseCreated,
|
|
18
|
+
getConnectionOptionsFromUri,
|
|
19
|
+
validateDatabaseSchemaVersion
|
|
20
|
+
} from "./db/operations.js";
|
|
21
|
+
import {
|
|
22
|
+
ENTITIES,
|
|
23
|
+
StorageAppState,
|
|
24
|
+
StorageEvent,
|
|
25
|
+
StorageSession,
|
|
26
|
+
StorageUserState
|
|
27
|
+
} from "./db/schema.js";
|
|
28
|
+
import { createSession } from "./session.js";
|
|
29
|
+
import { State } from "./state.js";
|
|
30
|
+
function isDatabaseConnectionString(uri) {
|
|
31
|
+
if (!uri) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return uri.startsWith("postgres://") || uri.startsWith("postgresql://") || uri.startsWith("mysql://") || uri.startsWith("mariadb://") || uri.startsWith("mssql://") || uri.startsWith("sqlite://");
|
|
35
|
+
}
|
|
36
|
+
class DatabaseSessionService extends BaseSessionService {
|
|
37
|
+
constructor(connectionStringOrOptions) {
|
|
38
|
+
super();
|
|
39
|
+
this.initialized = false;
|
|
40
|
+
if (typeof connectionStringOrOptions === "string") {
|
|
41
|
+
this.options = getConnectionOptionsFromUri(connectionStringOrOptions);
|
|
42
|
+
} else {
|
|
43
|
+
if (!connectionStringOrOptions.driver) {
|
|
44
|
+
throw new Error("Driver is required when passing options object.");
|
|
45
|
+
}
|
|
46
|
+
this.options = {
|
|
47
|
+
...connectionStringOrOptions,
|
|
48
|
+
entities: ENTITIES
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async init() {
|
|
53
|
+
if (this.initialized) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
this.orm = await MikroORM.init(this.options);
|
|
57
|
+
await ensureDatabaseCreated(this.orm);
|
|
58
|
+
await validateDatabaseSchemaVersion(this.orm);
|
|
59
|
+
this.initialized = true;
|
|
60
|
+
}
|
|
61
|
+
async createSession({
|
|
62
|
+
appName,
|
|
63
|
+
userId,
|
|
64
|
+
state,
|
|
65
|
+
sessionId
|
|
66
|
+
}) {
|
|
67
|
+
await this.init();
|
|
68
|
+
const em = this.orm.em.fork();
|
|
69
|
+
const id = sessionId || randomUUID();
|
|
70
|
+
const now = /* @__PURE__ */ new Date();
|
|
71
|
+
const existing = await em.findOne(StorageSession, {
|
|
72
|
+
id,
|
|
73
|
+
appName,
|
|
74
|
+
userId
|
|
75
|
+
});
|
|
76
|
+
if (existing) {
|
|
77
|
+
throw new Error(`Session with id ${id} already exists.`);
|
|
78
|
+
}
|
|
79
|
+
let appStateModel = await em.findOne(StorageAppState, { appName });
|
|
80
|
+
if (!appStateModel) {
|
|
81
|
+
appStateModel = em.create(StorageAppState, {
|
|
82
|
+
appName,
|
|
83
|
+
state: {},
|
|
84
|
+
updateTime: now
|
|
85
|
+
});
|
|
86
|
+
em.persist(appStateModel);
|
|
87
|
+
}
|
|
88
|
+
let userStateModel = await em.findOne(StorageUserState, { appName, userId });
|
|
89
|
+
if (!userStateModel) {
|
|
90
|
+
userStateModel = em.create(StorageUserState, {
|
|
91
|
+
appName,
|
|
92
|
+
userId,
|
|
93
|
+
state: {}
|
|
94
|
+
});
|
|
95
|
+
em.persist(userStateModel);
|
|
96
|
+
}
|
|
97
|
+
const appStateDelta = {};
|
|
98
|
+
const userStateDelta = {};
|
|
99
|
+
const sessionState = {};
|
|
100
|
+
if (state) {
|
|
101
|
+
for (const [key, value] of Object.entries(state)) {
|
|
102
|
+
if (key.startsWith(State.APP_PREFIX)) {
|
|
103
|
+
appStateDelta[key.replace(State.APP_PREFIX, "")] = value;
|
|
104
|
+
} else if (key.startsWith(State.USER_PREFIX)) {
|
|
105
|
+
userStateDelta[key.replace(State.USER_PREFIX, "")] = value;
|
|
106
|
+
} else {
|
|
107
|
+
sessionState[key] = value;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (Object.keys(appStateDelta).length > 0) {
|
|
112
|
+
appStateModel.state = { ...appStateModel.state, ...appStateDelta };
|
|
113
|
+
}
|
|
114
|
+
if (Object.keys(userStateDelta).length > 0) {
|
|
115
|
+
userStateModel.state = { ...userStateModel.state, ...userStateDelta };
|
|
116
|
+
}
|
|
117
|
+
const storageSession = em.create(StorageSession, {
|
|
118
|
+
id,
|
|
119
|
+
appName,
|
|
120
|
+
userId,
|
|
121
|
+
state: sessionState,
|
|
122
|
+
createTime: now,
|
|
123
|
+
updateTime: now
|
|
124
|
+
});
|
|
125
|
+
em.persist(storageSession);
|
|
126
|
+
await em.flush();
|
|
127
|
+
const mergedState = mergeStates(
|
|
128
|
+
appStateModel.state,
|
|
129
|
+
userStateModel.state,
|
|
130
|
+
sessionState
|
|
131
|
+
);
|
|
132
|
+
return createSession({
|
|
133
|
+
id,
|
|
134
|
+
appName,
|
|
135
|
+
userId,
|
|
136
|
+
state: mergedState,
|
|
137
|
+
events: [],
|
|
138
|
+
lastUpdateTime: storageSession.createTime.getTime()
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
async getSession({
|
|
142
|
+
appName,
|
|
143
|
+
userId,
|
|
144
|
+
sessionId,
|
|
145
|
+
config
|
|
146
|
+
}) {
|
|
147
|
+
await this.init();
|
|
148
|
+
const em = this.orm.em.fork();
|
|
149
|
+
const storageSession = await em.findOne(StorageSession, {
|
|
150
|
+
appName,
|
|
151
|
+
userId,
|
|
152
|
+
id: sessionId
|
|
153
|
+
});
|
|
154
|
+
if (!storageSession) {
|
|
155
|
+
return void 0;
|
|
156
|
+
}
|
|
157
|
+
const eventWhere = {
|
|
158
|
+
appName,
|
|
159
|
+
userId,
|
|
160
|
+
sessionId
|
|
161
|
+
};
|
|
162
|
+
if (config == null ? void 0 : config.afterTimestamp) {
|
|
163
|
+
eventWhere.timestamp = { $gt: new Date(config.afterTimestamp) };
|
|
164
|
+
}
|
|
165
|
+
const storageEvents = await em.find(StorageEvent, eventWhere, {
|
|
166
|
+
orderBy: { timestamp: "DESC" },
|
|
167
|
+
limit: config == null ? void 0 : config.numRecentEvents
|
|
168
|
+
});
|
|
169
|
+
storageEvents.reverse();
|
|
170
|
+
const appStateModel = await em.findOne(StorageAppState, { appName });
|
|
171
|
+
const userStateModel = await em.findOne(StorageUserState, {
|
|
172
|
+
appName,
|
|
173
|
+
userId
|
|
174
|
+
});
|
|
175
|
+
const mergedState = mergeStates(
|
|
176
|
+
(appStateModel == null ? void 0 : appStateModel.state) || {},
|
|
177
|
+
(userStateModel == null ? void 0 : userStateModel.state) || {},
|
|
178
|
+
storageSession.state
|
|
179
|
+
);
|
|
180
|
+
return createSession({
|
|
181
|
+
id: sessionId,
|
|
182
|
+
appName,
|
|
183
|
+
userId,
|
|
184
|
+
state: mergedState,
|
|
185
|
+
events: storageEvents.map((se) => se.eventData),
|
|
186
|
+
lastUpdateTime: storageSession.updateTime.getTime()
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async listSessions({
|
|
190
|
+
appName,
|
|
191
|
+
userId
|
|
192
|
+
}) {
|
|
193
|
+
await this.init();
|
|
194
|
+
const em = this.orm.em.fork();
|
|
195
|
+
const where = { appName };
|
|
196
|
+
if (userId) {
|
|
197
|
+
where.userId = userId;
|
|
198
|
+
}
|
|
199
|
+
const storageSessions = await em.find(StorageSession, where);
|
|
200
|
+
const appStateModel = await em.findOne(StorageAppState, { appName });
|
|
201
|
+
const appState = (appStateModel == null ? void 0 : appStateModel.state) || {};
|
|
202
|
+
const userStateMap = {};
|
|
203
|
+
if (userId) {
|
|
204
|
+
const u = await em.findOne(StorageUserState, { appName, userId });
|
|
205
|
+
if (u) userStateMap[userId] = u.state;
|
|
206
|
+
} else {
|
|
207
|
+
const allUserStates = await em.find(StorageUserState, { appName });
|
|
208
|
+
for (const u of allUserStates) {
|
|
209
|
+
userStateMap[u.userId] = u.state;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const sessions = storageSessions.map((ss) => {
|
|
213
|
+
const uState = userStateMap[ss.userId] || {};
|
|
214
|
+
const merged = mergeStates(appState, uState, ss.state);
|
|
215
|
+
return createSession({
|
|
216
|
+
id: ss.id,
|
|
217
|
+
appName: ss.appName,
|
|
218
|
+
userId: ss.userId,
|
|
219
|
+
state: merged,
|
|
220
|
+
events: [],
|
|
221
|
+
lastUpdateTime: ss.updateTime.getTime()
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
return { sessions };
|
|
225
|
+
}
|
|
226
|
+
async deleteSession({
|
|
227
|
+
appName,
|
|
228
|
+
userId,
|
|
229
|
+
sessionId
|
|
230
|
+
}) {
|
|
231
|
+
await this.init();
|
|
232
|
+
const em = this.orm.em.fork();
|
|
233
|
+
await em.nativeDelete(StorageSession, { appName, userId, id: sessionId });
|
|
234
|
+
await em.nativeDelete(StorageEvent, { appName, userId, sessionId });
|
|
235
|
+
}
|
|
236
|
+
async appendEvent({
|
|
237
|
+
session,
|
|
238
|
+
event
|
|
239
|
+
}) {
|
|
240
|
+
await this.init();
|
|
241
|
+
const em = this.orm.em.fork();
|
|
242
|
+
if (event.partial) {
|
|
243
|
+
return event;
|
|
244
|
+
}
|
|
245
|
+
const trimmedEvent = trimTempDeltaState(event);
|
|
246
|
+
await em.transactional(async (txEm) => {
|
|
247
|
+
const storageSession = await txEm.findOne(
|
|
248
|
+
StorageSession,
|
|
249
|
+
{
|
|
250
|
+
appName: session.appName,
|
|
251
|
+
userId: session.userId,
|
|
252
|
+
id: session.id
|
|
253
|
+
},
|
|
254
|
+
{ lockMode: LockMode.PESSIMISTIC_WRITE }
|
|
255
|
+
);
|
|
256
|
+
if (!storageSession) {
|
|
257
|
+
throw new Error(`Session ${session.id} not found for appendEvent`);
|
|
258
|
+
}
|
|
259
|
+
let appStateModel = await txEm.findOne(StorageAppState, {
|
|
260
|
+
appName: session.appName
|
|
261
|
+
});
|
|
262
|
+
if (!appStateModel) {
|
|
263
|
+
appStateModel = txEm.create(StorageAppState, {
|
|
264
|
+
appName: session.appName,
|
|
265
|
+
state: {},
|
|
266
|
+
updateTime: /* @__PURE__ */ new Date()
|
|
267
|
+
});
|
|
268
|
+
txEm.persist(appStateModel);
|
|
269
|
+
}
|
|
270
|
+
let userStateModel = await txEm.findOne(StorageUserState, {
|
|
271
|
+
appName: session.appName,
|
|
272
|
+
userId: session.userId
|
|
273
|
+
});
|
|
274
|
+
if (!userStateModel) {
|
|
275
|
+
userStateModel = txEm.create(StorageUserState, {
|
|
276
|
+
appName: session.appName,
|
|
277
|
+
userId: session.userId,
|
|
278
|
+
state: {}
|
|
279
|
+
});
|
|
280
|
+
txEm.persist(userStateModel);
|
|
281
|
+
}
|
|
282
|
+
if (storageSession.updateTime.getTime() > session.lastUpdateTime) {
|
|
283
|
+
const events = await txEm.find(
|
|
284
|
+
StorageEvent,
|
|
285
|
+
{
|
|
286
|
+
appName: session.appName,
|
|
287
|
+
userId: session.userId,
|
|
288
|
+
sessionId: session.id
|
|
289
|
+
},
|
|
290
|
+
{ orderBy: { timestamp: "ASC" } }
|
|
291
|
+
);
|
|
292
|
+
const mergedState = mergeStates(
|
|
293
|
+
appStateModel.state,
|
|
294
|
+
userStateModel.state,
|
|
295
|
+
storageSession.state
|
|
296
|
+
);
|
|
297
|
+
session.state = mergedState;
|
|
298
|
+
session.events = events.map((e) => e.eventData);
|
|
299
|
+
}
|
|
300
|
+
if (event.actions && event.actions.stateDelta) {
|
|
301
|
+
const appDelta = {};
|
|
302
|
+
const userDelta = {};
|
|
303
|
+
const sessionDelta = {};
|
|
304
|
+
for (const [key, value] of Object.entries(event.actions.stateDelta)) {
|
|
305
|
+
if (key.startsWith(State.APP_PREFIX)) {
|
|
306
|
+
appDelta[key.replace(State.APP_PREFIX, "")] = value;
|
|
307
|
+
} else if (key.startsWith(State.USER_PREFIX)) {
|
|
308
|
+
userDelta[key.replace(State.USER_PREFIX, "")] = value;
|
|
309
|
+
} else {
|
|
310
|
+
sessionDelta[key] = value;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (Object.keys(appDelta).length > 0) {
|
|
314
|
+
appStateModel.state = { ...appStateModel.state, ...appDelta };
|
|
315
|
+
}
|
|
316
|
+
if (Object.keys(userDelta).length > 0) {
|
|
317
|
+
userStateModel.state = { ...userStateModel.state, ...userDelta };
|
|
318
|
+
}
|
|
319
|
+
if (Object.keys(sessionDelta).length > 0) {
|
|
320
|
+
storageSession.state = { ...storageSession.state, ...sessionDelta };
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const newStorageEvent = txEm.create(StorageEvent, {
|
|
324
|
+
id: trimmedEvent.id,
|
|
325
|
+
appName: session.appName,
|
|
326
|
+
userId: session.userId,
|
|
327
|
+
sessionId: session.id,
|
|
328
|
+
invocationId: trimmedEvent.invocationId,
|
|
329
|
+
timestamp: new Date(trimmedEvent.timestamp),
|
|
330
|
+
eventData: trimmedEvent
|
|
331
|
+
});
|
|
332
|
+
txEm.persist(newStorageEvent);
|
|
333
|
+
await txEm.commit();
|
|
334
|
+
storageSession.updateTime = new Date(event.timestamp);
|
|
335
|
+
const newMergedState = mergeStates(
|
|
336
|
+
appStateModel.state,
|
|
337
|
+
userStateModel.state,
|
|
338
|
+
storageSession.state
|
|
339
|
+
);
|
|
340
|
+
session.state = newMergedState;
|
|
341
|
+
session.events.push(event);
|
|
342
|
+
session.lastUpdateTime = storageSession.updateTime.getTime();
|
|
343
|
+
});
|
|
344
|
+
return event;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export {
|
|
348
|
+
DatabaseSessionService,
|
|
349
|
+
isDatabaseConnectionString
|
|
350
|
+
};
|