mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-09-16 14:57:15 +00:00
feat(api): expose task status to api keys
This commit is contained in:
@@ -7,7 +7,7 @@ from .. import group
|
||||
from .....utils import constants
|
||||
from .....entity.persistence.metadata import WorkspaceMetadata
|
||||
from ...authz import Permission
|
||||
from ...context import RequestContext
|
||||
from ...context import PrincipalType, RequestContext
|
||||
from .....provider.tools.loaders.mcp_policy import stdio_mcp_enabled
|
||||
from .....workspace.invitation_delivery import InvitationDeliveryService
|
||||
|
||||
@@ -24,6 +24,11 @@ SYSTEM_CAPABILITY_OPERATIONS = (
|
||||
'pipeline.update',
|
||||
'pipeline.delete',
|
||||
'pipeline.copy',
|
||||
'task.list',
|
||||
'task.get',
|
||||
'knowledge_base.get',
|
||||
'knowledge_base.file.store',
|
||||
'file.document.upload',
|
||||
)
|
||||
|
||||
|
||||
@@ -258,7 +263,7 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
@self.route(
|
||||
'/tasks',
|
||||
methods=['GET'],
|
||||
auth_type=group.AuthType.USER_TOKEN,
|
||||
auth_type=group.AuthType.USER_TOKEN_OR_API_KEY,
|
||||
permission=Permission.RESOURCE_VIEW,
|
||||
)
|
||||
async def _(request_context: RequestContext) -> str:
|
||||
@@ -277,18 +282,23 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
instance_uuid=request_context.instance_uuid,
|
||||
workspace_uuid=request_context.workspace_uuid,
|
||||
placement_generation=request_context.placement_generation,
|
||||
public=request_context.principal.principal_type == PrincipalType.API_KEY,
|
||||
)
|
||||
)
|
||||
|
||||
@self.route(
|
||||
'/tasks/<task_id>',
|
||||
methods=['GET'],
|
||||
auth_type=group.AuthType.USER_TOKEN,
|
||||
auth_type=group.AuthType.USER_TOKEN_OR_API_KEY,
|
||||
permission=Permission.RESOURCE_VIEW,
|
||||
)
|
||||
async def _(task_id: str, request_context: RequestContext) -> str:
|
||||
try:
|
||||
task_index = int(task_id)
|
||||
except (TypeError, ValueError):
|
||||
return self.http_status(404, 404, 'Task not found')
|
||||
task = self.ap.task_mgr.get_task_by_id(
|
||||
int(task_id),
|
||||
task_index,
|
||||
instance_uuid=request_context.instance_uuid,
|
||||
workspace_uuid=request_context.workspace_uuid,
|
||||
placement_generation=request_context.placement_generation,
|
||||
@@ -297,6 +307,8 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
if task is None:
|
||||
return self.http_status(404, 404, 'Task not found')
|
||||
|
||||
if request_context.principal.principal_type == PrincipalType.API_KEY:
|
||||
return self.success(data=task.to_public_dict())
|
||||
return self.success(data=task.to_dict())
|
||||
|
||||
@self.route(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import typing
|
||||
import datetime
|
||||
import time
|
||||
@@ -197,6 +198,41 @@ class TaskWrapper:
|
||||
},
|
||||
}
|
||||
|
||||
def to_public_dict(self) -> dict:
|
||||
"""Return the stable task projection exposed to API-key callers."""
|
||||
if self.task.cancelled():
|
||||
status = 'cancelled'
|
||||
error = {'type': 'task_cancelled', 'message': 'Task was cancelled'}
|
||||
result = None
|
||||
elif not self.task.done():
|
||||
status = 'running'
|
||||
error = None
|
||||
result = None
|
||||
else:
|
||||
exception = self.assume_exception()
|
||||
if exception is not None:
|
||||
status = 'failed'
|
||||
error = {'type': 'task_failed', 'message': 'Task execution failed'}
|
||||
result = None
|
||||
else:
|
||||
status = 'succeeded'
|
||||
error = None
|
||||
result = self.assume_result()
|
||||
try:
|
||||
json.dumps(result)
|
||||
except (TypeError, ValueError):
|
||||
result = None
|
||||
|
||||
return {
|
||||
'id': self.id,
|
||||
'task_type': self.task_type,
|
||||
'kind': self.kind,
|
||||
'status': status,
|
||||
'error': error,
|
||||
'result': result,
|
||||
'created_at': self.created_at,
|
||||
}
|
||||
|
||||
def cancel(self):
|
||||
self.task.cancel()
|
||||
|
||||
@@ -325,19 +361,20 @@ class AsyncTaskManager:
|
||||
instance_uuid: str | None = None,
|
||||
workspace_uuid: str | None = None,
|
||||
placement_generation: int | None = None,
|
||||
public: bool = False,
|
||||
) -> dict:
|
||||
return {
|
||||
'tasks': [
|
||||
t.to_dict()
|
||||
for t in self.tasks
|
||||
if (type is None or t.task_type == type)
|
||||
and (kind is None or t.kind == kind)
|
||||
and (instance_uuid is None or t.instance_uuid == instance_uuid)
|
||||
and (workspace_uuid is None or t.workspace_uuid == workspace_uuid)
|
||||
and (placement_generation is None or t.placement_generation == placement_generation)
|
||||
],
|
||||
'id_index': TaskWrapper._id_index,
|
||||
}
|
||||
tasks = [
|
||||
t.to_public_dict() if public else t.to_dict()
|
||||
for t in self.tasks
|
||||
if (type is None or t.task_type == type)
|
||||
and (kind is None or t.kind == kind)
|
||||
and (instance_uuid is None or t.instance_uuid == instance_uuid)
|
||||
and (workspace_uuid is None or t.workspace_uuid == workspace_uuid)
|
||||
and (placement_generation is None or t.placement_generation == placement_generation)
|
||||
]
|
||||
if public:
|
||||
return {'tasks': tasks}
|
||||
return {'tasks': tasks, 'id_index': TaskWrapper._id_index}
|
||||
|
||||
def get_stats(self) -> dict:
|
||||
completed = sum(1 for t in self.tasks if t.task.done())
|
||||
|
||||
Reference in New Issue
Block a user