agentengine-sdk-python 0.8.7__py3-none-any.whl → 0.8.8__py3-none-any.whl
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.
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/METADATA +14 -9
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/RECORD +98 -68
- ksadk/__init__.py +2 -1
- ksadk/_build_provenance.json +1 -1
- ksadk/_mcp_compat.py +305 -0
- ksadk/agent.py +29 -0
- ksadk/agui/_agent_kernel.py +176 -142
- ksadk/agui/agent.py +74 -5
- ksadk/agui/hosted.py +343 -0
- ksadk/agui/request_identity.py +11 -0
- ksadk/agui/routes.py +77 -2
- ksadk/builders/code_builder.py +30 -23
- ksadk/builders/container_builder.py +5 -5
- ksadk/builders/mcp_builder.py +142 -0
- ksadk/builders/project_imports.py +66 -0
- ksadk/cli/cmd_mcp.py +1 -1
- ksadk/cli/cmd_run.py +2 -4
- ksadk/cli/cmd_web.py +8 -0
- ksadk/cli/runtime_bootstrap.py +4 -1
- ksadk/common/cloud_headers.py +33 -6
- ksadk/compat/adk_compat.py +2 -0
- ksadk/compat/adk_stream.py +35 -0
- ksadk/configs/env_registry.py +31 -0
- ksadk/conversations/projector.py +6 -0
- ksadk/conversations/runtime_observability.py +1 -0
- ksadk/detection/mcp_detector.py +80 -22
- ksadk/harness/__init__.py +3 -0
- ksadk/harness/service_presentation.py +44 -0
- ksadk/harness/service_runtime.py +1038 -0
- ksadk/interaction/providers/harness.py +9 -4
- ksadk/kernel/ingress.py +35 -25
- ksadk/kernel/worker.py +2 -2
- ksadk/kernel/worker_identity.py +15 -8
- ksadk/knowledge_base/client.py +10 -3
- ksadk/mcp_runtime/oauth/_agent.py +77 -9
- ksadk/mcp_runtime/oauth/langgraph.py +47 -15
- ksadk/mcp_runtime/oauth/local/broker.py +23 -1
- ksadk/mcp_runtime/oauth/tools.py +46 -8
- ksadk/mcp_runtime/oauth/transport.py +19 -2
- ksadk/memory/adk/backends/sdk_ltm_backend.py +21 -12
- ksadk/memory/scope_ids.py +49 -0
- ksadk/plugins/providers/harness.py +65 -28
- ksadk/plugins/providers/harness_service.py +198 -0
- ksadk/plugins/providers/harness_service_context.py +175 -0
- ksadk/plugins/providers/harness_service_hooks.py +168 -0
- ksadk/plugins/providers/harness_service_memory.py +479 -0
- ksadk/plugins/providers/platform_resources.py +2 -2
- ksadk/resource_runtime/langgraph.py +7 -3
- ksadk/resource_runtime/mcp_server.py +3 -3
- ksadk/resource_runtime/worker.py +19 -4
- ksadk/runners/_adk_canonical_stream.py +38 -10
- ksadk/runners/_adk_tunnel_history.py +96 -0
- ksadk/runners/_langgraph_canonical_stream.py +16 -0
- ksadk/runners/_langgraph_factory.py +79 -0
- ksadk/runners/_langgraph_runner_streams.py +6 -0
- ksadk/runners/_session_identity.py +8 -3
- ksadk/runners/adk_runner.py +95 -19
- ksadk/runners/langgraph_runner.py +15 -21
- ksadk/runtime/factory.py +17 -5
- ksadk/runtime/framework_adapters.py +10 -0
- ksadk/runtime/langgraph_checkpoint_view.py +28 -7
- ksadk/runtime/launch.py +2 -0
- ksadk/server/factory.py +55 -0
- ksadk/server/routes/common.py +2 -0
- ksadk/server/routes/harness_service_view.py +38 -0
- ksadk/server/routes/ui_bootstrap.py +16 -2
- ksadk/skills/mcp_server/server.py +239 -217
- ksadk/studio/contracts.py +8 -0
- ksadk/studio/mcp_runtime.py +23 -8
- ksadk/studio/native_mcp_disclosure.py +8 -6
- ksadk/studio/native_mcp_tool_port.py +3 -1
- ksadk/studio/native_memory_context.py +10 -8
- ksadk/studio/native_product.py +3 -4
- ksadk/studio/plugin_composition.py +8 -1
- ksadk/studio/plugin_native_activation.py +4 -2
- ksadk/studio/plugin_runtime.py +9 -0
- ksadk/studio/service.py +2 -0
- ksadk/tracing/setup.py +53 -3
- ksadk/tunnel/__init__.py +37 -0
- ksadk/tunnel/connector.py +384 -0
- ksadk/tunnel/discovery.py +107 -0
- ksadk/tunnel/failure.py +168 -0
- ksadk/tunnel/http_tools.py +100 -0
- ksadk/tunnel/langgraph.py +125 -0
- ksadk/tunnel/middleware.py +28 -0
- ksadk/tunnel/peers.py +872 -0
- ksadk/tunnel/protocol.py +97 -0
- ksadk/tunnel/registry.py +306 -0
- ksadk/tunnel/runtime.py +147 -0
- ksadk/tunnel/server.py +201 -0
- ksadk/tunnel/services.py +366 -0
- ksadk/tunnel/toolset.py +78 -0
- ksadk/version.py +1 -1
- ksadk_runtime_common/memory_backend/providers/mem0.py +20 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/WHEEL +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.8.7.dist-info → agentengine_sdk_python-0.8.8.dist-info}/top_level.txt +0 -0
ksadk/agui/_agent_kernel.py
CHANGED
|
@@ -7,6 +7,8 @@ import json
|
|
|
7
7
|
import logging
|
|
8
8
|
from typing import Any
|
|
9
9
|
|
|
10
|
+
from fastapi import HTTPException
|
|
11
|
+
|
|
10
12
|
from ag_ui.core import (
|
|
11
13
|
CustomEvent,
|
|
12
14
|
Interrupt,
|
|
@@ -36,6 +38,10 @@ class AguiInteractionAlreadyTerminal(ValueError):
|
|
|
36
38
|
"""A stale or mixed answer batch must not submit another command."""
|
|
37
39
|
|
|
38
40
|
|
|
41
|
+
class AguiKernelRejected(ValueError):
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
|
|
39
45
|
def public_interrupt(payload: dict[str, Any]) -> Interrupt:
|
|
40
46
|
request = payload.get("request") or payload
|
|
41
47
|
presentation = request.get("presentation") or {}
|
|
@@ -54,6 +60,24 @@ def public_interrupt(payload: dict[str, Any]) -> Interrupt:
|
|
|
54
60
|
|
|
55
61
|
|
|
56
62
|
class AGUIKernelMixin:
|
|
63
|
+
async def _kernel_barrier_interrupts(self, stream, interaction_ids):
|
|
64
|
+
"""Recover wait descriptions omitted by a resume subscription's cursor."""
|
|
65
|
+
interrupts = []
|
|
66
|
+
for key in interaction_ids:
|
|
67
|
+
pending = stream.run.pending.get(key)
|
|
68
|
+
if stream.readonly or pending is None:
|
|
69
|
+
record = await stream.read_interaction(key)
|
|
70
|
+
if record is None or record["run_id"] != stream.target_run:
|
|
71
|
+
# OAuth has a separate wait ledger and stays on its stream.
|
|
72
|
+
continue
|
|
73
|
+
if stream.readonly and record["status"] != "pending":
|
|
74
|
+
continue
|
|
75
|
+
interrupt = public_interrupt(record)
|
|
76
|
+
else:
|
|
77
|
+
interrupt = pending.interrupt
|
|
78
|
+
interrupts.append(interrupt)
|
|
79
|
+
return interrupts
|
|
80
|
+
|
|
57
81
|
async def _kernel_ready_interrupts(self, ready, run, trusted, input):
|
|
58
82
|
current = await ingress.get_agent_kernel().pending_interactions(
|
|
59
83
|
AgentStatusQuery(
|
|
@@ -86,7 +110,7 @@ class AGUIKernelMixin:
|
|
|
86
110
|
]
|
|
87
111
|
return any(item is not None for item in known)
|
|
88
112
|
|
|
89
|
-
async def _kernel_run(self, input):
|
|
113
|
+
async def _kernel_run(self, input, admission=None):
|
|
90
114
|
yield RunStartedEvent(
|
|
91
115
|
thread_id=input.thread_id,
|
|
92
116
|
run_id=input.run_id,
|
|
@@ -98,127 +122,18 @@ class AGUIKernelMixin:
|
|
|
98
122
|
input.thread_id, input.run_id, f"{input.run_id}:assistant", f"{input.run_id}:reasoning"
|
|
99
123
|
)
|
|
100
124
|
try:
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
)
|
|
114
|
-
initial_waits = []
|
|
115
|
-
unconsumed = {entry.interrupt_id for entry in input.resume or []}
|
|
116
|
-
target_run = None
|
|
117
|
-
if input.resume:
|
|
118
|
-
initial_waits, commands, target_run = await self._kernel_resumes(input, trusted)
|
|
119
|
-
else:
|
|
120
|
-
commands = [
|
|
121
|
-
ingress.map_agui_request(
|
|
122
|
-
session_id=input.thread_id,
|
|
123
|
-
idempotency_key=input.run_id,
|
|
124
|
-
content=self._latest_user_input(input),
|
|
125
|
-
run_id=input.run_id,
|
|
126
|
-
trusted=trusted,
|
|
127
|
-
runtime_options={
|
|
128
|
-
"model": (input.forwarded_props or {}).get("model"),
|
|
129
|
-
"ag-ui": self._request_config(input)["ag-ui"],
|
|
130
|
-
"conversation": {
|
|
131
|
-
"model_metadata": (input.forwarded_props or {}).get(
|
|
132
|
-
"modelMetadata"
|
|
133
|
-
),
|
|
134
|
-
"model_options": (input.forwarded_props or {}).get("modelOptions"),
|
|
135
|
-
"request_metadata": {
|
|
136
|
-
"transport": "ag-ui",
|
|
137
|
-
"tool_approval_mode": (input.forwarded_props or {}).get(
|
|
138
|
-
"toolApprovalMode"
|
|
139
|
-
),
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
)
|
|
144
|
-
]
|
|
145
|
-
readonly_resume = bool(input.resume) and not commands
|
|
146
|
-
if readonly_resume:
|
|
147
|
-
unconsumed.clear()
|
|
148
|
-
receipts = []
|
|
149
|
-
for command in commands:
|
|
150
|
-
# Mutation permits are single-command nonces. A parallel
|
|
151
|
-
# answer batch must not reuse one nonce for distinct commands.
|
|
152
|
-
admission = ingress.trusted_context(
|
|
153
|
-
source_kind="agui",
|
|
154
|
-
source_ref=input.run_id,
|
|
155
|
-
session_id=input.thread_id,
|
|
156
|
-
operations=(command.command_type,),
|
|
157
|
-
launch_context=self._shared.launch_context,
|
|
158
|
-
)
|
|
159
|
-
if (
|
|
160
|
-
admission.tenant_id != trusted.tenant_id
|
|
161
|
-
or admission.agent_instance_id != trusted.agent_instance_id
|
|
162
|
-
):
|
|
163
|
-
raise ValueError("Trusted admission scope changed")
|
|
164
|
-
payload = dict(command.payload)
|
|
165
|
-
if command.command_type == "submit_interaction":
|
|
166
|
-
payload["token_ref"] = admission.permit.permit_id
|
|
167
|
-
command = command.model_copy(
|
|
168
|
-
update={
|
|
169
|
-
"authorization_ref": admission.permit.permit_id,
|
|
170
|
-
"payload": payload,
|
|
171
|
-
}
|
|
172
|
-
)
|
|
173
|
-
receipt = await ingress.submit_command(command, permit=admission.permit)
|
|
174
|
-
if receipt.status not in {"accepted", "duplicate"}:
|
|
175
|
-
yield RunErrorEvent(
|
|
176
|
-
message="Agent kernel rejected command", code=receipt.status.upper()
|
|
177
|
-
)
|
|
178
|
-
return
|
|
179
|
-
receipts.append(receipt)
|
|
180
|
-
if target_run is None:
|
|
181
|
-
target_run = execution_grant_run_id(
|
|
182
|
-
commands[0].model_copy(update={"command_id": receipts[0].command_id})
|
|
183
|
-
)
|
|
184
|
-
# Receipts and subsequent facts are read from the same physical
|
|
185
|
-
# Session cursor. A duplicate uses its original accepted cursor.
|
|
186
|
-
# Terminal interactions already have an authoritative original Run.
|
|
187
|
-
# Replay its public facts without admitting a new answer command.
|
|
188
|
-
after_seq = min((int(item.accepted_seq or 0) for item in receipts), default=0)
|
|
189
|
-
run = _ThreadRun(
|
|
190
|
-
RunHandle(
|
|
191
|
-
run_id=target_run or input.run_id,
|
|
192
|
-
session_id=input.thread_id,
|
|
193
|
-
runtime_type="ksadk",
|
|
194
|
-
)
|
|
195
|
-
)
|
|
196
|
-
for payload in initial_waits:
|
|
197
|
-
if payload["run_id"] != target_run:
|
|
198
|
-
continue
|
|
199
|
-
interrupt = public_interrupt(payload)
|
|
200
|
-
run.pending[interrupt.id] = _PendingInterrupt(interrupt, "", payload["kind"])
|
|
201
|
-
if readonly_resume:
|
|
202
|
-
interrupts = await self._kernel_ready_interrupts(
|
|
203
|
-
list(run.pending), run, trusted, input,
|
|
204
|
-
)
|
|
205
|
-
if interrupts:
|
|
206
|
-
yield RunFinishedEvent(
|
|
207
|
-
thread_id=input.thread_id, run_id=input.run_id,
|
|
208
|
-
outcome=RunFinishedInterruptOutcome(interrupts=interrupts),
|
|
209
|
-
)
|
|
210
|
-
return
|
|
211
|
-
command_ids = {str(item.command_id) for item in receipts}
|
|
212
|
-
submitted_interactions = {
|
|
213
|
-
str(receipt.command_id): command.payload["interaction_id"]
|
|
214
|
-
for command, receipt in zip(commands, receipts)
|
|
215
|
-
if command.command_type == "submit_interaction"
|
|
216
|
-
}
|
|
217
|
-
async for _, envelope in ingress.subscribe_projected(
|
|
218
|
-
input.thread_id,
|
|
219
|
-
trusted=trusted,
|
|
220
|
-
after_seq=after_seq,
|
|
221
|
-
):
|
|
125
|
+
from ksadk.agui.hosted import observe
|
|
126
|
+
|
|
127
|
+
stream = (await observe(self, input, admission) if admission is not None
|
|
128
|
+
else await self._local_kernel_stream(input))
|
|
129
|
+
target_run, run = stream.target_run, stream.run
|
|
130
|
+
command_ids, submitted_interactions = stream.command_ids, stream.submissions
|
|
131
|
+
readonly_resume, unconsumed = stream.readonly, stream.unconsumed
|
|
132
|
+
if stream.ready:
|
|
133
|
+
yield RunFinishedEvent(thread_id=input.thread_id, run_id=input.run_id,
|
|
134
|
+
outcome=RunFinishedInterruptOutcome(interrupts=stream.ready))
|
|
135
|
+
return
|
|
136
|
+
async for _, envelope in stream.events:
|
|
222
137
|
if envelope.family == "control":
|
|
223
138
|
if (envelope.event_type.startswith("mcp.authorization.")
|
|
224
139
|
and envelope.run_id == target_run):
|
|
@@ -251,15 +166,7 @@ class AGUIKernelMixin:
|
|
|
251
166
|
# Another submission may win after the pending
|
|
252
167
|
# preflight. Classify only this admitted occurrence,
|
|
253
168
|
# using its authorized public state, never its answer.
|
|
254
|
-
current = await
|
|
255
|
-
AgentStatusQuery(
|
|
256
|
-
tenant_id=trusted.tenant_id,
|
|
257
|
-
agent_instance_id=trusted.agent_instance_id,
|
|
258
|
-
session_id=input.thread_id,
|
|
259
|
-
authorization_ref=trusted.permit.permit_id,
|
|
260
|
-
),
|
|
261
|
-
interaction_id, permit=trusted.permit,
|
|
262
|
-
)
|
|
169
|
+
current = await stream.read_interaction(interaction_id)
|
|
263
170
|
if (current is not None and current["run_id"] == target_run
|
|
264
171
|
and current["status"] in {"resolved", "cancelled", "expired"}):
|
|
265
172
|
raise AguiInteractionAlreadyTerminal()
|
|
@@ -271,15 +178,13 @@ class AGUIKernelMixin:
|
|
|
271
178
|
if (envelope.event_type == "control.interaction_wait_ready"
|
|
272
179
|
and envelope.run_id == target_run):
|
|
273
180
|
# This cursor marks the end of this activation. A fast
|
|
274
|
-
# answer may already have resolved the durable wait
|
|
275
|
-
#
|
|
276
|
-
#
|
|
277
|
-
interrupts =
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
]
|
|
282
|
-
if interrupts and not unconsumed and not readonly_resume:
|
|
181
|
+
# answer may already have resolved the durable wait.
|
|
182
|
+
# Keep this activation's boundary for admitted calls;
|
|
183
|
+
# read-only retries expose only still-pending waits.
|
|
184
|
+
interrupts = (await self._kernel_barrier_interrupts(
|
|
185
|
+
stream, envelope.payload.get("interaction_ids", [])
|
|
186
|
+
)) if not unconsumed else []
|
|
187
|
+
if interrupts:
|
|
283
188
|
async for close in self._close_open_messages(wire):
|
|
284
189
|
yield close
|
|
285
190
|
yield RunFinishedEvent(
|
|
@@ -320,8 +225,10 @@ class AGUIKernelMixin:
|
|
|
320
225
|
if isinstance(ready, list):
|
|
321
226
|
# Read old private-candidate logs without producing new
|
|
322
227
|
# synthetic RunProgress facts for this transport barrier.
|
|
323
|
-
interrupts =
|
|
324
|
-
|
|
228
|
+
interrupts = (await self._kernel_barrier_interrupts(
|
|
229
|
+
stream, ready
|
|
230
|
+
)) if not unconsumed else []
|
|
231
|
+
if interrupts:
|
|
325
232
|
async for close in self._close_open_messages(wire):
|
|
326
233
|
yield close
|
|
327
234
|
yield RunFinishedEvent(
|
|
@@ -336,6 +243,8 @@ class AGUIKernelMixin:
|
|
|
336
243
|
if not readonly_resume:
|
|
337
244
|
await self._update_session_metadata_after_assistant_turn(input, wire)
|
|
338
245
|
return
|
|
246
|
+
except AguiKernelRejected as exc:
|
|
247
|
+
yield RunErrorEvent(message="Agent kernel rejected command", code=str(exc).upper())
|
|
339
248
|
except AguiInteractionAlreadyTerminal:
|
|
340
249
|
yield RunErrorEvent(
|
|
341
250
|
message="Interaction is already terminal; refresh pending inputs before submitting",
|
|
@@ -350,10 +259,135 @@ class AGUIKernelMixin:
|
|
|
350
259
|
yield RunErrorEvent(
|
|
351
260
|
message="Response does not satisfy the input schema", code="INPUT_RESPONSE_INVALID"
|
|
352
261
|
)
|
|
262
|
+
except HTTPException as exc:
|
|
263
|
+
yield RunErrorEvent(message=str(exc.detail), code=f"AGUI_ADMISSION_{exc.status_code}")
|
|
353
264
|
except Exception:
|
|
354
265
|
logger.exception("AG-UI Kernel projection failed")
|
|
355
266
|
yield RunErrorEvent(message="Agent kernel ingress failed", code="KERNEL_ERROR")
|
|
356
267
|
|
|
268
|
+
async def _local_kernel_stream(self, input):
|
|
269
|
+
from ksadk.agui.hosted import KernelStream
|
|
270
|
+
|
|
271
|
+
trusted = ingress.trusted_context(
|
|
272
|
+
source_kind="agui",
|
|
273
|
+
source_ref=input.run_id,
|
|
274
|
+
session_id=input.thread_id,
|
|
275
|
+
operations=(
|
|
276
|
+
"enqueue",
|
|
277
|
+
"submit_interaction",
|
|
278
|
+
"interrupt",
|
|
279
|
+
"get_status",
|
|
280
|
+
"subscribe_events",
|
|
281
|
+
),
|
|
282
|
+
launch_context=self._shared.launch_context,
|
|
283
|
+
)
|
|
284
|
+
initial_waits = []
|
|
285
|
+
unconsumed = {entry.interrupt_id for entry in input.resume or []}
|
|
286
|
+
target_run = None
|
|
287
|
+
if input.resume:
|
|
288
|
+
initial_waits, commands, target_run = await self._kernel_resumes(input, trusted)
|
|
289
|
+
else:
|
|
290
|
+
commands = [
|
|
291
|
+
ingress.map_agui_request(
|
|
292
|
+
session_id=input.thread_id,
|
|
293
|
+
idempotency_key=input.run_id,
|
|
294
|
+
content=self._latest_user_input(input),
|
|
295
|
+
run_id=input.run_id,
|
|
296
|
+
trusted=trusted,
|
|
297
|
+
runtime_options={
|
|
298
|
+
"model": (input.forwarded_props or {}).get("model"),
|
|
299
|
+
"ag-ui": self._request_config(input)["ag-ui"],
|
|
300
|
+
"conversation": {
|
|
301
|
+
"model_metadata": (input.forwarded_props or {}).get(
|
|
302
|
+
"modelMetadata"
|
|
303
|
+
),
|
|
304
|
+
"model_options": (input.forwarded_props or {}).get("modelOptions"),
|
|
305
|
+
"request_metadata": {
|
|
306
|
+
"transport": "ag-ui",
|
|
307
|
+
"tool_approval_mode": (input.forwarded_props or {}).get(
|
|
308
|
+
"toolApprovalMode"
|
|
309
|
+
),
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
)
|
|
314
|
+
]
|
|
315
|
+
readonly_resume = bool(input.resume) and not commands
|
|
316
|
+
if readonly_resume:
|
|
317
|
+
unconsumed.clear()
|
|
318
|
+
receipts = []
|
|
319
|
+
for command in commands:
|
|
320
|
+
# Mutation permits are single-command nonces. A parallel
|
|
321
|
+
# answer batch must not reuse one nonce for distinct commands.
|
|
322
|
+
admission = ingress.trusted_context(
|
|
323
|
+
source_kind="agui",
|
|
324
|
+
source_ref=input.run_id,
|
|
325
|
+
session_id=input.thread_id,
|
|
326
|
+
operations=(command.command_type,),
|
|
327
|
+
launch_context=self._shared.launch_context,
|
|
328
|
+
)
|
|
329
|
+
if (
|
|
330
|
+
admission.tenant_id != trusted.tenant_id
|
|
331
|
+
or admission.agent_instance_id != trusted.agent_instance_id
|
|
332
|
+
):
|
|
333
|
+
raise ValueError("Trusted admission scope changed")
|
|
334
|
+
payload = dict(command.payload)
|
|
335
|
+
if not self._identity.is_empty:
|
|
336
|
+
payload["invocation_identity"] = self._identity.to_payload()
|
|
337
|
+
if command.command_type == "submit_interaction":
|
|
338
|
+
payload["token_ref"] = admission.permit.permit_id
|
|
339
|
+
command = command.model_copy(
|
|
340
|
+
update={
|
|
341
|
+
"authorization_ref": admission.permit.permit_id,
|
|
342
|
+
"payload": payload,
|
|
343
|
+
}
|
|
344
|
+
)
|
|
345
|
+
receipt = await ingress.submit_command(command, permit=admission.permit)
|
|
346
|
+
if receipt.status not in {"accepted", "duplicate"}:
|
|
347
|
+
raise AguiKernelRejected(receipt.status)
|
|
348
|
+
receipts.append(receipt)
|
|
349
|
+
if target_run is None:
|
|
350
|
+
target_run = execution_grant_run_id(
|
|
351
|
+
commands[0].model_copy(update={"command_id": receipts[0].command_id})
|
|
352
|
+
)
|
|
353
|
+
# Receipts and subsequent facts are read from the same physical
|
|
354
|
+
# Session cursor. A duplicate uses its original accepted cursor.
|
|
355
|
+
# Terminal interactions already have an authoritative original Run.
|
|
356
|
+
# Replay its public facts without admitting a new answer command.
|
|
357
|
+
after_seq = min((int(item.accepted_seq or 0) for item in receipts), default=0)
|
|
358
|
+
run = _ThreadRun(
|
|
359
|
+
RunHandle(
|
|
360
|
+
run_id=target_run or input.run_id,
|
|
361
|
+
session_id=input.thread_id,
|
|
362
|
+
runtime_type="ksadk",
|
|
363
|
+
)
|
|
364
|
+
)
|
|
365
|
+
for payload in initial_waits:
|
|
366
|
+
if payload["run_id"] != target_run:
|
|
367
|
+
continue
|
|
368
|
+
interrupt = public_interrupt(payload)
|
|
369
|
+
run.pending[interrupt.id] = _PendingInterrupt(interrupt, "", payload["kind"])
|
|
370
|
+
ready = []
|
|
371
|
+
if readonly_resume:
|
|
372
|
+
ready = await self._kernel_ready_interrupts(list(run.pending), run, trusted, input)
|
|
373
|
+
command_ids = {str(item.command_id) for item in receipts}
|
|
374
|
+
submitted_interactions = {
|
|
375
|
+
str(receipt.command_id): command.payload["interaction_id"]
|
|
376
|
+
for command, receipt in zip(commands, receipts)
|
|
377
|
+
if command.command_type == "submit_interaction"
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async def read_interaction(key):
|
|
381
|
+
return await ingress.get_agent_kernel().public_interaction(
|
|
382
|
+
AgentStatusQuery(tenant_id=trusted.tenant_id,
|
|
383
|
+
agent_instance_id=trusted.agent_instance_id, session_id=input.thread_id,
|
|
384
|
+
authorization_ref=trusted.permit.permit_id), key, permit=trusted.permit,
|
|
385
|
+
)
|
|
386
|
+
return KernelStream(target_run, run,
|
|
387
|
+
ingress.subscribe_projected(input.thread_id, trusted=trusted, after_seq=after_seq),
|
|
388
|
+
read_interaction, readonly=readonly_resume, command_ids=command_ids,
|
|
389
|
+
submissions=submitted_interactions, unconsumed=unconsumed, ready=ready)
|
|
390
|
+
|
|
357
391
|
async def _kernel_resumes(self, input, trusted):
|
|
358
392
|
"""Official resume entries become real Inbox commands on the original Run."""
|
|
359
393
|
ids = [entry.interrupt_id for entry in input.resume]
|
ksadk/agui/agent.py
CHANGED
|
@@ -6,6 +6,7 @@ import asyncio
|
|
|
6
6
|
import hashlib
|
|
7
7
|
import json
|
|
8
8
|
import logging
|
|
9
|
+
import os
|
|
9
10
|
import time
|
|
10
11
|
from importlib import import_module
|
|
11
12
|
from typing import Any, AsyncIterator, Callable, Mapping, Optional, cast
|
|
@@ -52,6 +53,7 @@ from ksadk.runtime.adapter import (
|
|
|
52
53
|
StartRequest,
|
|
53
54
|
)
|
|
54
55
|
from ksadk.runtime.executor import RuntimeExecutor
|
|
56
|
+
from ksadk.runtime_context import TRUSTED_IDENTITY_METADATA_KEY, PlatformIdentityContext
|
|
55
57
|
|
|
56
58
|
EventStoreFactory = Callable[[], Any]
|
|
57
59
|
SessionServiceFactory = Callable[[], Any]
|
|
@@ -91,6 +93,9 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
91
93
|
_shared: Optional[_SharedRuntime] = None,
|
|
92
94
|
) -> None:
|
|
93
95
|
self.name = name
|
|
96
|
+
self._identity = PlatformIdentityContext()
|
|
97
|
+
self._admission = None
|
|
98
|
+
self._session = None
|
|
94
99
|
if _shared is not None:
|
|
95
100
|
self._shared = _shared
|
|
96
101
|
else:
|
|
@@ -105,7 +110,44 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
105
110
|
)
|
|
106
111
|
|
|
107
112
|
def clone(self) -> "KsadkAGUIAgent":
|
|
108
|
-
|
|
113
|
+
from ksadk.agui.request_identity import agui_admission, agui_request_identity
|
|
114
|
+
|
|
115
|
+
agent = type(self)(name=self.name, _shared=self._shared)
|
|
116
|
+
agent._identity = agui_request_identity.get() or PlatformIdentityContext()
|
|
117
|
+
agent._admission = agui_admission.get()
|
|
118
|
+
return agent
|
|
119
|
+
|
|
120
|
+
def _session_agent_id(self) -> str:
|
|
121
|
+
if self._identity.is_empty:
|
|
122
|
+
return self.name
|
|
123
|
+
return (
|
|
124
|
+
os.getenv("AGENT_RUNTIME_ID", "").strip()
|
|
125
|
+
or str(self._shared.launch_context.config.get("agent_id") or "")
|
|
126
|
+
or self.name
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
async def _authorize_session(self, input: RunAgentInput) -> None:
|
|
130
|
+
from ksadk.conversations.runtime_persistence import (
|
|
131
|
+
ensure_conversation_session,
|
|
132
|
+
require_conversation_session,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Legacy/local requests without a platform identity retain the native
|
|
136
|
+
# session and resume behavior. Hosted preparation checks identity first.
|
|
137
|
+
if self._identity.is_empty:
|
|
138
|
+
return
|
|
139
|
+
factory = self._shared.session_service_factory
|
|
140
|
+
if factory is None:
|
|
141
|
+
return
|
|
142
|
+
forwarded = input.forwarded_props if isinstance(input.forwarded_props, dict) else {}
|
|
143
|
+
resolve = require_conversation_session if input.resume else ensure_conversation_session
|
|
144
|
+
self._session = await resolve(
|
|
145
|
+
agent_id=self._session_agent_id(),
|
|
146
|
+
user_id=str(forwarded.get("userId") or forwarded.get("user_id") or "agui-user"),
|
|
147
|
+
session_id=input.thread_id,
|
|
148
|
+
session_service_provider=factory,
|
|
149
|
+
invocation_identity=self._identity,
|
|
150
|
+
)
|
|
109
151
|
|
|
110
152
|
async def run(self, input: RunAgentInput) -> AsyncIterator[BaseEvent]:
|
|
111
153
|
from ksadk.agui.config import AGUI_PROTOCOL_VERSION
|
|
@@ -117,6 +159,28 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
117
159
|
)
|
|
118
160
|
return
|
|
119
161
|
|
|
162
|
+
# Authorize before starting a runner or resuming a cached thread. The
|
|
163
|
+
# request's forwardedProps are compatibility hints, never an owner.
|
|
164
|
+
from fastapi import HTTPException
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
await self._authorize_session(input)
|
|
168
|
+
except HTTPException as exc:
|
|
169
|
+
yield RunErrorEvent(
|
|
170
|
+
message="Session not found" if exc.status_code == 404 else str(exc.detail),
|
|
171
|
+
code="SESSION_NOT_FOUND" if exc.status_code == 404 else "SESSION_IDENTITY_CONFLICT",
|
|
172
|
+
)
|
|
173
|
+
return
|
|
174
|
+
except Exception:
|
|
175
|
+
logger.exception("AG-UI session admission failed for thread=%s", input.thread_id)
|
|
176
|
+
yield RunErrorEvent(message="Session admission failed", code="RUNTIME_ERROR")
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
if self._admission is not None:
|
|
180
|
+
async for event in self._kernel_run(input, admission=self._admission):
|
|
181
|
+
yield event
|
|
182
|
+
return
|
|
183
|
+
|
|
120
184
|
if (
|
|
121
185
|
not _kernel_ingress.kernel_route_active()
|
|
122
186
|
and self._shared.local_kernel_provider is not None
|
|
@@ -263,12 +327,17 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
263
327
|
request_config = self._request_config(input)
|
|
264
328
|
request = StartRequest(
|
|
265
329
|
input=self._latest_user_input(input),
|
|
266
|
-
user_id=
|
|
330
|
+
user_id=(self._session.user_id if self._session is not None else
|
|
331
|
+
str(forwarded.get("userId") or forwarded.get("user_id") or "agui-user")),
|
|
267
332
|
session_id=input.thread_id,
|
|
268
|
-
agent_id=self.
|
|
333
|
+
agent_id=self._session.agent_id if self._session is not None else self._session_agent_id(),
|
|
269
334
|
model=str(forwarded.get("model") or "") or None,
|
|
270
335
|
config=request_config,
|
|
271
|
-
metadata={
|
|
336
|
+
metadata={
|
|
337
|
+
"invocation_id": input.run_id, "transport": "ag-ui",
|
|
338
|
+
**({TRUSTED_IDENTITY_METADATA_KEY: self._identity.to_payload()}
|
|
339
|
+
if not self._identity.is_empty else {}),
|
|
340
|
+
},
|
|
272
341
|
)
|
|
273
342
|
handle = await self._shared.executor.start(
|
|
274
343
|
self._shared.launch_context,
|
|
@@ -319,7 +388,7 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
319
388
|
source=SourceRef(
|
|
320
389
|
framework="ksadk",
|
|
321
390
|
metadata={
|
|
322
|
-
"agent_id":
|
|
391
|
+
"agent_id": request.agent_id,
|
|
323
392
|
"user_id": request.user_id,
|
|
324
393
|
"session_id": input.thread_id,
|
|
325
394
|
"input": self._json_safe(request.input),
|