lemma-sdk 0.2.36 → 0.2.38
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/README.md +45 -30
- package/dist/browser/lemma-client.js +224 -212
- package/dist/client.d.ts +1 -4
- package/dist/client.js +1 -6
- package/dist/index.d.ts +2 -5
- package/dist/index.js +1 -2
- package/dist/namespaces/agents.js +1 -1
- package/dist/namespaces/conversations.d.ts +68 -0
- package/dist/namespaces/conversations.js +159 -0
- package/dist/namespaces/files.d.ts +24 -6
- package/dist/namespaces/files.js +25 -12
- package/dist/namespaces/integrations.js +1 -1
- package/dist/namespaces/pod-surfaces.d.ts +1 -1
- package/dist/namespaces/pod-surfaces.js +6 -6
- package/dist/openapi_client/index.d.ts +17 -32
- package/dist/openapi_client/index.js +8 -12
- package/dist/openapi_client/models/AdminConsentInfoResponse.d.ts +2 -2
- package/dist/openapi_client/models/AgentListResponse.d.ts +2 -4
- package/dist/openapi_client/models/AgentMessageResponse.d.ts +0 -3
- package/dist/openapi_client/models/{AvailableModels.d.ts → AgentModelName.d.ts} +4 -1
- package/dist/openapi_client/models/AgentModelName.js +18 -0
- package/dist/openapi_client/models/AgentResponse.d.ts +18 -13
- package/dist/openapi_client/models/AgentSurfaceListResponse.d.ts +6 -0
- package/dist/openapi_client/models/{AssistantSurfaceResponse.d.ts → AgentSurfaceResponse.d.ts} +6 -5
- package/dist/openapi_client/models/{AssistantSurfaceStatus.d.ts → AgentSurfaceStatus.d.ts} +1 -1
- package/dist/openapi_client/models/AgentSurfaceStatus.js +10 -0
- package/dist/openapi_client/models/{ToolSet.d.ts → AgentToolset.d.ts} +3 -5
- package/dist/openapi_client/models/AgentToolset.js +17 -0
- package/dist/openapi_client/models/ApplicationAccessConfig.d.ts +3 -5
- package/dist/openapi_client/models/ApplicationMode.d.ts +3 -3
- package/dist/openapi_client/models/ApplicationMode.js +3 -3
- package/dist/openapi_client/models/ConversationResponse.d.ts +9 -20
- package/dist/openapi_client/models/CreateAgentRequest.d.ts +11 -7
- package/dist/openapi_client/models/CreateConversationRequest.d.ts +4 -8
- package/dist/openapi_client/models/CreateFolderRequest.d.ts +2 -5
- package/dist/openapi_client/models/CreateFunctionRequest.d.ts +2 -2
- package/dist/openapi_client/models/CreateSurfaceRequest.d.ts +1 -1
- package/dist/openapi_client/models/DatastoreFileUploadRequest.d.ts +2 -2
- package/dist/openapi_client/models/FileNamespace.d.ts +4 -0
- package/dist/openapi_client/models/FileNamespace.js +9 -0
- package/dist/openapi_client/models/FileResponse.d.ts +2 -2
- package/dist/openapi_client/models/FlowRunEntity.d.ts +10 -1
- package/dist/openapi_client/models/FunctionResponse.d.ts +2 -2
- package/dist/openapi_client/models/HarnessKind.d.ts +7 -0
- package/dist/openapi_client/models/HarnessKind.js +12 -0
- package/dist/openapi_client/models/MessageListResponse.d.ts +6 -0
- package/dist/openapi_client/models/{TaskMessageResponse.d.ts → MessageResponse.d.ts} +8 -7
- package/dist/openapi_client/models/OrganizationInvitationResponse.d.ts +1 -0
- package/dist/openapi_client/models/ReportFeedbackResponse.d.ts +0 -4
- package/dist/openapi_client/models/ResourceType.d.ts +0 -1
- package/dist/openapi_client/models/ResourceType.js +0 -1
- package/dist/openapi_client/models/SendMessageRequest.d.ts +5 -0
- package/dist/openapi_client/models/{ToolCallRequest.d.ts → ToolCallContent.d.ts} +1 -1
- package/dist/openapi_client/models/{ToolCallResponse.d.ts → ToolReturnContent.d.ts} +2 -1
- package/dist/openapi_client/models/UpdateAgentRequest.d.ts +11 -7
- package/dist/openapi_client/models/UpdateConversationRequest.d.ts +2 -5
- package/dist/openapi_client/models/UpdateFunctionRequest.d.ts +2 -2
- package/dist/openapi_client/models/app__modules__agent__domain__value_objects__TableAccessEntry.d.ts +8 -0
- package/dist/openapi_client/models/{TableAccessEntry.d.ts → app__modules__function__api__schemas__function_schemas__TableAccessEntry.d.ts} +1 -1
- package/dist/openapi_client/models/update.d.ts +2 -2
- package/dist/openapi_client/services/AgentConversationsService.d.ts +87 -0
- package/dist/openapi_client/services/{ConversationsService.js → AgentConversationsService.js} +61 -72
- package/dist/openapi_client/services/{AssistantSurfacesIngressService.d.ts → AgentSurfacesIngressService.d.ts} +2 -2
- package/dist/openapi_client/services/{AssistantSurfacesIngressService.js → AgentSurfacesIngressService.js} +2 -2
- package/dist/openapi_client/services/{AssistantSurfacesService.d.ts → AgentSurfacesService.d.ts} +11 -11
- package/dist/openapi_client/services/{AssistantSurfacesService.js → AgentSurfacesService.js} +10 -10
- package/dist/openapi_client/services/AgentToolsService.d.ts +8 -8
- package/dist/openapi_client/services/AgentToolsService.js +8 -8
- package/dist/openapi_client/services/AgentsService.d.ts +7 -7
- package/dist/openapi_client/services/AgentsService.js +8 -8
- package/dist/openapi_client/services/FilesService.d.ts +17 -8
- package/dist/openapi_client/services/FilesService.js +25 -8
- package/dist/openapi_client/services/WorkflowsService.d.ts +1 -1
- package/dist/openapi_client/services/WorkflowsService.js +1 -1
- package/dist/react/index.d.ts +0 -8
- package/dist/react/index.js +0 -4
- package/dist/react/useAssistantController.d.ts +6 -2
- package/dist/react/useAssistantController.js +56 -15
- package/dist/react/useAssistantSession.d.ts +15 -5
- package/dist/react/useAssistantSession.js +24 -26
- package/dist/react/useConversationMessages.d.ts +6 -2
- package/dist/react/useConversationMessages.js +2 -1
- package/dist/react/useConversations.d.ts +6 -2
- package/dist/react/useConversations.js +7 -5
- package/dist/react/useCreateFolder.d.ts +4 -2
- package/dist/react/useCreateFolder.js +6 -3
- package/dist/react/useDeleteFile.d.ts +4 -1
- package/dist/react/useDeleteFile.js +3 -3
- package/dist/react/useFile.d.ts +4 -2
- package/dist/react/useFile.js +3 -3
- package/dist/react/useFilePreview.d.ts +4 -1
- package/dist/react/useFilePreview.js +5 -4
- package/dist/react/useFileTree.d.ts +4 -2
- package/dist/react/useFileTree.js +3 -2
- package/dist/react/useFiles.d.ts +4 -2
- package/dist/react/useFiles.js +5 -3
- package/dist/react/useUpdateFile.d.ts +4 -2
- package/dist/react/useUpdateFile.js +6 -3
- package/dist/react/useUploadFile.d.ts +4 -2
- package/dist/react/useUploadFile.js +6 -3
- package/dist/react/useWorkflowRun.js +1 -1
- package/dist/react/useWorkflowStart.js +1 -1
- package/dist/run-utils.d.ts +2 -5
- package/dist/run-utils.js +0 -10
- package/dist/types.d.ts +24 -10
- package/package.json +1 -1
- package/dist/namespaces/assistants.d.ts +0 -96
- package/dist/namespaces/assistants.js +0 -160
- package/dist/namespaces/tasks.d.ts +0 -29
- package/dist/namespaces/tasks.js +0 -49
- package/dist/openapi_client/models/AddMessageRequest.d.ts +0 -6
- package/dist/openapi_client/models/AssistantListResponse.d.ts +0 -6
- package/dist/openapi_client/models/AssistantResponse.d.ts +0 -23
- package/dist/openapi_client/models/AssistantSurfaceListResponse.d.ts +0 -6
- package/dist/openapi_client/models/AssistantSurfaceStatus.js +0 -10
- package/dist/openapi_client/models/AvailableModelInfo.d.ts +0 -8
- package/dist/openapi_client/models/AvailableModels.js +0 -15
- package/dist/openapi_client/models/AvailableModelsListResponse.d.ts +0 -7
- package/dist/openapi_client/models/ConversationMessageListResponse.d.ts +0 -6
- package/dist/openapi_client/models/ConversationMessageResponse.d.ts +0 -19
- package/dist/openapi_client/models/ConversationStatus.d.ts +0 -7
- package/dist/openapi_client/models/ConversationStatus.js +0 -12
- package/dist/openapi_client/models/ConversationType.d.ts +0 -8
- package/dist/openapi_client/models/ConversationType.js +0 -13
- package/dist/openapi_client/models/CreateAssistantRequest.d.ts +0 -18
- package/dist/openapi_client/models/CreateAssistantRequest.js +0 -1
- package/dist/openapi_client/models/CreateMessageRequest.d.ts +0 -6
- package/dist/openapi_client/models/CreateMessageRequest.js +0 -1
- package/dist/openapi_client/models/CreateTaskRequest.d.ts +0 -10
- package/dist/openapi_client/models/CreateTaskRequest.js +0 -1
- package/dist/openapi_client/models/FileVisibility.d.ts +0 -4
- package/dist/openapi_client/models/FileVisibility.js +0 -9
- package/dist/openapi_client/models/TableAccessEntry.js +0 -1
- package/dist/openapi_client/models/TaskListResponse.d.ts +0 -9
- package/dist/openapi_client/models/TaskListResponse.js +0 -1
- package/dist/openapi_client/models/TaskMessageListResponse.d.ts +0 -9
- package/dist/openapi_client/models/TaskMessageListResponse.js +0 -1
- package/dist/openapi_client/models/TaskMessageResponse.js +0 -1
- package/dist/openapi_client/models/TaskResponse.d.ts +0 -16
- package/dist/openapi_client/models/TaskResponse.js +0 -1
- package/dist/openapi_client/models/TaskStatus.d.ts +0 -12
- package/dist/openapi_client/models/TaskStatus.js +0 -17
- package/dist/openapi_client/models/ToolCallRequest.js +0 -1
- package/dist/openapi_client/models/ToolCallResponse.js +0 -1
- package/dist/openapi_client/models/ToolSet.js +0 -19
- package/dist/openapi_client/models/UpdateAssistantRequest.d.ts +0 -17
- package/dist/openapi_client/models/UpdateAssistantRequest.js +0 -1
- package/dist/openapi_client/services/AssistantsService.d.ts +0 -49
- package/dist/openapi_client/services/AssistantsService.js +0 -112
- package/dist/openapi_client/services/ConversationsService.d.ts +0 -88
- package/dist/openapi_client/services/TasksService.d.ts +0 -76
- package/dist/openapi_client/services/TasksService.js +0 -167
- package/dist/react/useAgentRun.d.ts +0 -17
- package/dist/react/useAgentRun.js +0 -56
- package/dist/react/useAgentRuns.d.ts +0 -33
- package/dist/react/useAgentRuns.js +0 -149
- package/dist/react/useAssistantRun.d.ts +0 -27
- package/dist/react/useAssistantRun.js +0 -47
- package/dist/react/useTaskSession.d.ts +0 -35
- package/dist/react/useTaskSession.js +0 -269
- package/dist/task-events.d.ts +0 -8
- package/dist/task-events.js +0 -115
- /package/dist/openapi_client/models/{AddMessageRequest.js → AgentSurfaceListResponse.js} +0 -0
- /package/dist/openapi_client/models/{AssistantListResponse.js → AgentSurfaceResponse.js} +0 -0
- /package/dist/openapi_client/models/{AssistantResponse.js → MessageListResponse.js} +0 -0
- /package/dist/openapi_client/models/{AssistantSurfaceListResponse.js → MessageResponse.js} +0 -0
- /package/dist/openapi_client/models/{AssistantSurfaceResponse.js → SendMessageRequest.js} +0 -0
- /package/dist/openapi_client/models/{AvailableModelInfo.js → ToolCallContent.js} +0 -0
- /package/dist/openapi_client/models/{AvailableModelsListResponse.js → ToolReturnContent.js} +0 -0
- /package/dist/openapi_client/models/{ConversationMessageListResponse.js → app__modules__agent__domain__value_objects__TableAccessEntry.js} +0 -0
- /package/dist/openapi_client/models/{ConversationMessageResponse.js → app__modules__function__api__schemas__function_schemas__TableAccessEntry.js} +0 -0
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { normalizeError, resolvePodClient } from "./utils.js";
|
|
3
|
-
export function useAgentRuns({ client, podId, agentName, enabled = true, autoLoad = true, limit = 100, pageToken, initialTaskId = null, }) {
|
|
4
|
-
const [runs, setRuns] = useState([]);
|
|
5
|
-
const [total, setTotal] = useState(0);
|
|
6
|
-
const [nextPageToken, setNextPageToken] = useState(null);
|
|
7
|
-
const [selectedTaskId, setSelectedTaskId] = useState(initialTaskId);
|
|
8
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
9
|
-
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
|
10
|
-
const [error, setError] = useState(null);
|
|
11
|
-
const refresh = useCallback(async (overrides = {}, signal) => {
|
|
12
|
-
if (!enabled) {
|
|
13
|
-
setRuns([]);
|
|
14
|
-
setTotal(0);
|
|
15
|
-
setNextPageToken(null);
|
|
16
|
-
setError(null);
|
|
17
|
-
setIsLoading(false);
|
|
18
|
-
return [];
|
|
19
|
-
}
|
|
20
|
-
setIsLoading(true);
|
|
21
|
-
setError(null);
|
|
22
|
-
try {
|
|
23
|
-
const scopedClient = resolvePodClient(client, podId);
|
|
24
|
-
const response = await scopedClient.tasks.list({
|
|
25
|
-
agent_name: agentName,
|
|
26
|
-
limit: overrides.limit ?? limit,
|
|
27
|
-
page_token: overrides.pageToken ?? pageToken,
|
|
28
|
-
});
|
|
29
|
-
if (signal?.aborted)
|
|
30
|
-
return [];
|
|
31
|
-
const nextRuns = response.items ?? [];
|
|
32
|
-
setRuns(nextRuns);
|
|
33
|
-
setTotal(response.total ?? nextRuns.length);
|
|
34
|
-
setNextPageToken(response.next_page_token ?? null);
|
|
35
|
-
setSelectedTaskId((current) => (current && nextRuns.some((run) => run.id === current) ? current : initialTaskId));
|
|
36
|
-
return nextRuns;
|
|
37
|
-
}
|
|
38
|
-
catch (refreshError) {
|
|
39
|
-
if (signal?.aborted)
|
|
40
|
-
return [];
|
|
41
|
-
const normalized = normalizeError(refreshError, "Failed to load agent runs.");
|
|
42
|
-
setError(normalized);
|
|
43
|
-
return [];
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
if (!signal?.aborted)
|
|
47
|
-
setIsLoading(false);
|
|
48
|
-
}
|
|
49
|
-
}, [agentName, client, enabled, initialTaskId, limit, pageToken, podId]);
|
|
50
|
-
const loadMore = useCallback(async (overrides = {}) => {
|
|
51
|
-
if (!enabled || !nextPageToken || isLoading || isLoadingMore) {
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
54
|
-
setIsLoadingMore(true);
|
|
55
|
-
setError(null);
|
|
56
|
-
try {
|
|
57
|
-
const scopedClient = resolvePodClient(client, podId);
|
|
58
|
-
const response = await scopedClient.tasks.list({
|
|
59
|
-
agent_name: agentName,
|
|
60
|
-
limit: overrides.limit ?? limit,
|
|
61
|
-
page_token: nextPageToken,
|
|
62
|
-
});
|
|
63
|
-
const moreRuns = response.items ?? [];
|
|
64
|
-
setRuns((previous) => [...previous, ...moreRuns]);
|
|
65
|
-
setTotal(response.total ?? runs.length + moreRuns.length);
|
|
66
|
-
setNextPageToken(response.next_page_token ?? null);
|
|
67
|
-
return moreRuns;
|
|
68
|
-
}
|
|
69
|
-
catch (loadError) {
|
|
70
|
-
const normalized = normalizeError(loadError, "Failed to load more agent runs.");
|
|
71
|
-
setError(normalized);
|
|
72
|
-
return [];
|
|
73
|
-
}
|
|
74
|
-
finally {
|
|
75
|
-
setIsLoadingMore(false);
|
|
76
|
-
}
|
|
77
|
-
}, [agentName, client, enabled, isLoading, isLoadingMore, limit, nextPageToken, podId]);
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
setSelectedTaskId(initialTaskId);
|
|
80
|
-
}, [initialTaskId]);
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
if (!enabled) {
|
|
83
|
-
setRuns([]);
|
|
84
|
-
setTotal(0);
|
|
85
|
-
setNextPageToken(null);
|
|
86
|
-
setError(null);
|
|
87
|
-
setIsLoading(false);
|
|
88
|
-
setIsLoadingMore(false);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (!autoLoad)
|
|
92
|
-
return;
|
|
93
|
-
const controller = new AbortController();
|
|
94
|
-
let cancelled = false;
|
|
95
|
-
(async () => {
|
|
96
|
-
try {
|
|
97
|
-
await refresh({}, controller.signal);
|
|
98
|
-
}
|
|
99
|
-
catch {
|
|
100
|
-
if (!cancelled) {
|
|
101
|
-
setError(normalizeError(new Error("Failed to load agent runs."), "Failed to load agent runs."));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
})();
|
|
105
|
-
return () => {
|
|
106
|
-
cancelled = true;
|
|
107
|
-
controller.abort();
|
|
108
|
-
};
|
|
109
|
-
}, [autoLoad, enabled, refresh]);
|
|
110
|
-
const selectRun = useCallback((taskId) => {
|
|
111
|
-
setSelectedTaskId(taskId);
|
|
112
|
-
}, []);
|
|
113
|
-
const clearSelection = useCallback(() => {
|
|
114
|
-
setSelectedTaskId(null);
|
|
115
|
-
}, []);
|
|
116
|
-
return useMemo(() => {
|
|
117
|
-
const effectiveSelectedTaskId = selectedTaskId ?? runs[0]?.id ?? null;
|
|
118
|
-
const selectedRun = effectiveSelectedTaskId
|
|
119
|
-
? runs.find((run) => run.id === effectiveSelectedTaskId) ?? null
|
|
120
|
-
: null;
|
|
121
|
-
return {
|
|
122
|
-
runs,
|
|
123
|
-
total,
|
|
124
|
-
nextPageToken,
|
|
125
|
-
selectedTaskId,
|
|
126
|
-
effectiveSelectedTaskId,
|
|
127
|
-
selectedRun,
|
|
128
|
-
isLoading,
|
|
129
|
-
isLoadingMore,
|
|
130
|
-
error,
|
|
131
|
-
selectRun,
|
|
132
|
-
clearSelection,
|
|
133
|
-
refresh,
|
|
134
|
-
loadMore,
|
|
135
|
-
};
|
|
136
|
-
}, [
|
|
137
|
-
clearSelection,
|
|
138
|
-
error,
|
|
139
|
-
isLoading,
|
|
140
|
-
isLoadingMore,
|
|
141
|
-
loadMore,
|
|
142
|
-
nextPageToken,
|
|
143
|
-
refresh,
|
|
144
|
-
runs,
|
|
145
|
-
selectRun,
|
|
146
|
-
selectedTaskId,
|
|
147
|
-
total,
|
|
148
|
-
]);
|
|
149
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { LemmaClient } from "../client.js";
|
|
2
|
-
import type { SseRawEvent } from "../streams.js";
|
|
3
|
-
import type { ConversationMessage } from "../types.js";
|
|
4
|
-
export interface UseAssistantRunOptions {
|
|
5
|
-
client: LemmaClient;
|
|
6
|
-
podId?: string;
|
|
7
|
-
conversationId?: string | null;
|
|
8
|
-
onEvent?: (event: SseRawEvent, payload: unknown | null) => void;
|
|
9
|
-
onError?: (error: unknown) => void;
|
|
10
|
-
}
|
|
11
|
-
export interface UseAssistantRunResult {
|
|
12
|
-
isStreaming: boolean;
|
|
13
|
-
error: Error | null;
|
|
14
|
-
status?: string;
|
|
15
|
-
messages: ConversationMessage[];
|
|
16
|
-
output: ConversationMessage["content"] | null;
|
|
17
|
-
outputText: string;
|
|
18
|
-
finalOutput: ConversationMessage["content"] | null;
|
|
19
|
-
finalOutputText: string;
|
|
20
|
-
latestAssistantMessage: ConversationMessage | null;
|
|
21
|
-
refresh: () => Promise<ConversationMessage[]>;
|
|
22
|
-
sendMessage: (content: string) => Promise<void>;
|
|
23
|
-
resume: () => Promise<void>;
|
|
24
|
-
stop: () => Promise<void>;
|
|
25
|
-
cancel: () => void;
|
|
26
|
-
}
|
|
27
|
-
export declare function useAssistantRun({ client, podId, conversationId, onEvent, onError, }: UseAssistantRunOptions): UseAssistantRunResult;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo } from "react";
|
|
2
|
-
import { useConversationMessages } from "./useConversationMessages.js";
|
|
3
|
-
function requireConversationId(conversationId) {
|
|
4
|
-
if (!conversationId) {
|
|
5
|
-
throw new Error("conversationId is required.");
|
|
6
|
-
}
|
|
7
|
-
return conversationId;
|
|
8
|
-
}
|
|
9
|
-
export function useAssistantRun({ client, podId, conversationId, onEvent, onError, }) {
|
|
10
|
-
const messages = useConversationMessages({
|
|
11
|
-
client,
|
|
12
|
-
podId,
|
|
13
|
-
conversationId,
|
|
14
|
-
autoLoad: true,
|
|
15
|
-
autoResume: false,
|
|
16
|
-
onEvent,
|
|
17
|
-
onError,
|
|
18
|
-
});
|
|
19
|
-
const sendMessage = useCallback(async (content) => {
|
|
20
|
-
await messages.sendMessage(content, {
|
|
21
|
-
conversationId: requireConversationId(conversationId ?? messages.conversationId),
|
|
22
|
-
createIfMissing: false,
|
|
23
|
-
});
|
|
24
|
-
}, [conversationId, messages]);
|
|
25
|
-
const resume = useCallback(async () => {
|
|
26
|
-
await messages.resume(requireConversationId(conversationId ?? messages.conversationId));
|
|
27
|
-
}, [conversationId, messages]);
|
|
28
|
-
const stop = useCallback(async () => {
|
|
29
|
-
await messages.stop(requireConversationId(conversationId ?? messages.conversationId));
|
|
30
|
-
}, [conversationId, messages]);
|
|
31
|
-
return useMemo(() => ({
|
|
32
|
-
isStreaming: messages.isStreaming,
|
|
33
|
-
error: messages.error,
|
|
34
|
-
status: messages.status,
|
|
35
|
-
messages: messages.messages,
|
|
36
|
-
output: messages.output,
|
|
37
|
-
outputText: messages.outputText,
|
|
38
|
-
finalOutput: messages.finalOutput,
|
|
39
|
-
finalOutputText: messages.finalOutputText,
|
|
40
|
-
latestAssistantMessage: messages.latestAssistantMessage,
|
|
41
|
-
refresh: messages.refresh,
|
|
42
|
-
sendMessage,
|
|
43
|
-
resume,
|
|
44
|
-
stop,
|
|
45
|
-
cancel: messages.cancel,
|
|
46
|
-
}), [messages, sendMessage, resume, stop]);
|
|
47
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { LemmaClient } from "../client.js";
|
|
2
|
-
import { type SseRawEvent } from "../streams.js";
|
|
3
|
-
import type { Task, TaskMessage } from "../types.js";
|
|
4
|
-
export interface CreateTaskInput {
|
|
5
|
-
agentName: string;
|
|
6
|
-
inputData?: Record<string, unknown> | null;
|
|
7
|
-
}
|
|
8
|
-
export interface UseTaskSessionOptions {
|
|
9
|
-
client: LemmaClient;
|
|
10
|
-
podId?: string;
|
|
11
|
-
taskId?: string | null;
|
|
12
|
-
autoConnect?: boolean;
|
|
13
|
-
autoConnectOnStart?: boolean;
|
|
14
|
-
onEvent?: (event: SseRawEvent, payload: unknown | null) => void;
|
|
15
|
-
onStatus?: (status: string) => void;
|
|
16
|
-
onMessage?: (message: TaskMessage) => void;
|
|
17
|
-
onError?: (error: unknown) => void;
|
|
18
|
-
}
|
|
19
|
-
export interface UseTaskSessionResult {
|
|
20
|
-
taskId: string | null;
|
|
21
|
-
task: Task | null;
|
|
22
|
-
status?: string;
|
|
23
|
-
messages: TaskMessage[];
|
|
24
|
-
isStreaming: boolean;
|
|
25
|
-
error: Error | null;
|
|
26
|
-
setTaskId: (taskId: string | null) => void;
|
|
27
|
-
start: (input: CreateTaskInput) => Promise<Task>;
|
|
28
|
-
refreshTask: (taskId?: string | null) => Promise<Task | null>;
|
|
29
|
-
loadMessages: (taskId?: string | null) => Promise<TaskMessage[]>;
|
|
30
|
-
connect: (taskId?: string | null) => Promise<void>;
|
|
31
|
-
disconnect: () => void;
|
|
32
|
-
stop: () => Promise<Task | null>;
|
|
33
|
-
clearMessages: () => void;
|
|
34
|
-
}
|
|
35
|
-
export declare function useTaskSession({ client, podId, taskId: externalTaskId, autoConnect, autoConnectOnStart, onEvent, onStatus, onMessage, onError, }: UseTaskSessionOptions): UseTaskSessionResult;
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { parseSSEJson, readSSE } from "../streams.js";
|
|
3
|
-
import { isTerminalTaskStatus, normalizeRunStatus } from "../run-utils.js";
|
|
4
|
-
import { parseTaskStreamEvent, upsertTaskMessage } from "../task-events.js";
|
|
5
|
-
import { normalizeError, resolvePodClient, resolvePodId } from "./utils.js";
|
|
6
|
-
function sleep(ms) {
|
|
7
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
8
|
-
}
|
|
9
|
-
export function useTaskSession({ client, podId, taskId: externalTaskId = null, autoConnect = true, autoConnectOnStart = true, onEvent, onStatus, onMessage, onError, }) {
|
|
10
|
-
const [taskId, setTaskIdState] = useState(externalTaskId);
|
|
11
|
-
const [task, setTask] = useState(null);
|
|
12
|
-
const [status, setStatus] = useState(undefined);
|
|
13
|
-
const [messages, setMessages] = useState([]);
|
|
14
|
-
const [isStreaming, setIsStreaming] = useState(false);
|
|
15
|
-
const [error, setError] = useState(null);
|
|
16
|
-
const abortRef = useRef(null);
|
|
17
|
-
const previousExternalTaskIdRef = useRef(externalTaskId);
|
|
18
|
-
const taskIdRef = useRef(externalTaskId);
|
|
19
|
-
const statusRef = useRef(undefined);
|
|
20
|
-
const onEventRef = useRef(onEvent);
|
|
21
|
-
const onStatusRef = useRef(onStatus);
|
|
22
|
-
const onMessageRef = useRef(onMessage);
|
|
23
|
-
const onErrorRef = useRef(onError);
|
|
24
|
-
const setTaskStatus = useCallback((nextStatus) => {
|
|
25
|
-
const normalized = normalizeRunStatus(nextStatus);
|
|
26
|
-
setStatus(normalized);
|
|
27
|
-
statusRef.current = normalized;
|
|
28
|
-
if (normalized) {
|
|
29
|
-
onStatusRef.current?.(normalized);
|
|
30
|
-
}
|
|
31
|
-
}, []);
|
|
32
|
-
const setTaskId = useCallback((nextTaskId) => {
|
|
33
|
-
setTaskIdState((currentTaskId) => {
|
|
34
|
-
if (currentTaskId === nextTaskId) {
|
|
35
|
-
return currentTaskId;
|
|
36
|
-
}
|
|
37
|
-
abortRef.current?.abort();
|
|
38
|
-
abortRef.current = null;
|
|
39
|
-
taskIdRef.current = nextTaskId;
|
|
40
|
-
setError(null);
|
|
41
|
-
setIsStreaming(false);
|
|
42
|
-
if (!nextTaskId) {
|
|
43
|
-
setTask(null);
|
|
44
|
-
setTaskStatus(undefined);
|
|
45
|
-
setMessages([]);
|
|
46
|
-
}
|
|
47
|
-
return nextTaskId;
|
|
48
|
-
});
|
|
49
|
-
}, [setTaskStatus]);
|
|
50
|
-
const disconnect = useCallback(() => {
|
|
51
|
-
abortRef.current?.abort();
|
|
52
|
-
abortRef.current = null;
|
|
53
|
-
setIsStreaming(false);
|
|
54
|
-
}, []);
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
taskIdRef.current = taskId;
|
|
57
|
-
}, [taskId]);
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
statusRef.current = status;
|
|
60
|
-
}, [status]);
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
onEventRef.current = onEvent;
|
|
63
|
-
}, [onEvent]);
|
|
64
|
-
useEffect(() => {
|
|
65
|
-
onStatusRef.current = onStatus;
|
|
66
|
-
}, [onStatus]);
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
onMessageRef.current = onMessage;
|
|
69
|
-
}, [onMessage]);
|
|
70
|
-
useEffect(() => {
|
|
71
|
-
onErrorRef.current = onError;
|
|
72
|
-
}, [onError]);
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
if (previousExternalTaskIdRef.current === externalTaskId) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
previousExternalTaskIdRef.current = externalTaskId;
|
|
78
|
-
setTaskId(externalTaskId);
|
|
79
|
-
}, [externalTaskId, setTaskId]);
|
|
80
|
-
const refreshTask = useCallback(async (explicitTaskId) => {
|
|
81
|
-
const id = explicitTaskId ?? taskIdRef.current;
|
|
82
|
-
if (!id)
|
|
83
|
-
return null;
|
|
84
|
-
try {
|
|
85
|
-
const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
|
|
86
|
-
const nextTask = await scopedClient.tasks.get(id);
|
|
87
|
-
setTask(nextTask);
|
|
88
|
-
setTaskStatus(nextTask.status);
|
|
89
|
-
return nextTask;
|
|
90
|
-
}
|
|
91
|
-
catch (refreshError) {
|
|
92
|
-
const normalized = normalizeError(refreshError, "Failed to fetch task.");
|
|
93
|
-
setError(normalized);
|
|
94
|
-
onErrorRef.current?.(refreshError);
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
}, [client, podId, setTaskStatus]);
|
|
98
|
-
const loadMessages = useCallback(async (explicitTaskId) => {
|
|
99
|
-
const id = explicitTaskId ?? taskIdRef.current;
|
|
100
|
-
if (!id)
|
|
101
|
-
return [];
|
|
102
|
-
try {
|
|
103
|
-
const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
|
|
104
|
-
const response = await scopedClient.tasks.messages.list(id, { limit: 100 });
|
|
105
|
-
const nextMessages = response.items ?? [];
|
|
106
|
-
setMessages(nextMessages);
|
|
107
|
-
return nextMessages;
|
|
108
|
-
}
|
|
109
|
-
catch (messageError) {
|
|
110
|
-
const normalized = normalizeError(messageError, "Failed to fetch task messages.");
|
|
111
|
-
setError(normalized);
|
|
112
|
-
onErrorRef.current?.(messageError);
|
|
113
|
-
return [];
|
|
114
|
-
}
|
|
115
|
-
}, [client, podId]);
|
|
116
|
-
const connect = useCallback(async (explicitTaskId) => {
|
|
117
|
-
const id = explicitTaskId ?? taskIdRef.current;
|
|
118
|
-
if (!id)
|
|
119
|
-
return;
|
|
120
|
-
setTaskIdState(id);
|
|
121
|
-
taskIdRef.current = id;
|
|
122
|
-
disconnect();
|
|
123
|
-
const controller = new AbortController();
|
|
124
|
-
abortRef.current = controller;
|
|
125
|
-
setError(null);
|
|
126
|
-
setIsStreaming(true);
|
|
127
|
-
let reconnectDelayMs = 1000;
|
|
128
|
-
try {
|
|
129
|
-
while (!controller.signal.aborted) {
|
|
130
|
-
if (isTerminalTaskStatus(statusRef.current)) {
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
|
|
135
|
-
const stream = await scopedClient.tasks.stream(id, { signal: controller.signal });
|
|
136
|
-
reconnectDelayMs = 1000;
|
|
137
|
-
for await (const event of readSSE(stream)) {
|
|
138
|
-
if (controller.signal.aborted) {
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
const payload = parseSSEJson(event);
|
|
142
|
-
onEventRef.current?.(event, payload);
|
|
143
|
-
const parsed = parseTaskStreamEvent(payload);
|
|
144
|
-
if (parsed.task?.id === id) {
|
|
145
|
-
setTask(parsed.task);
|
|
146
|
-
setTaskStatus(parsed.task.status);
|
|
147
|
-
}
|
|
148
|
-
if (parsed.message) {
|
|
149
|
-
setMessages((previous) => upsertTaskMessage(previous, parsed.message));
|
|
150
|
-
onMessageRef.current?.(parsed.message);
|
|
151
|
-
}
|
|
152
|
-
if (parsed.status) {
|
|
153
|
-
setTaskStatus(parsed.status);
|
|
154
|
-
setTask((previous) => {
|
|
155
|
-
if (!previous || previous.id !== id)
|
|
156
|
-
return previous;
|
|
157
|
-
return {
|
|
158
|
-
...previous,
|
|
159
|
-
status: parsed.status,
|
|
160
|
-
};
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
if (isTerminalTaskStatus(statusRef.current)) {
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (controller.signal.aborted || isTerminalTaskStatus(statusRef.current)) {
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
await sleep(Math.max(reconnectDelayMs, 2000));
|
|
171
|
-
reconnectDelayMs = Math.min(Math.max(reconnectDelayMs * 2, 2000), 6000);
|
|
172
|
-
}
|
|
173
|
-
catch (streamError) {
|
|
174
|
-
if (streamError instanceof Error && streamError.name === "AbortError") {
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
const normalized = normalizeError(streamError, "Failed to stream task run.");
|
|
178
|
-
setError(normalized);
|
|
179
|
-
onErrorRef.current?.(streamError);
|
|
180
|
-
await sleep(reconnectDelayMs);
|
|
181
|
-
reconnectDelayMs = Math.min(reconnectDelayMs * 2, 6000);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
finally {
|
|
186
|
-
if (abortRef.current === controller) {
|
|
187
|
-
abortRef.current = null;
|
|
188
|
-
}
|
|
189
|
-
setIsStreaming(false);
|
|
190
|
-
}
|
|
191
|
-
}, [client, disconnect, podId, setTaskStatus]);
|
|
192
|
-
const start = useCallback(async (input) => {
|
|
193
|
-
setError(null);
|
|
194
|
-
const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
|
|
195
|
-
const created = await scopedClient.tasks.create({
|
|
196
|
-
agent_name: input.agentName,
|
|
197
|
-
input_data: input.inputData,
|
|
198
|
-
});
|
|
199
|
-
taskIdRef.current = created.id;
|
|
200
|
-
setTask(created);
|
|
201
|
-
setTaskIdState(created.id);
|
|
202
|
-
setMessages([]);
|
|
203
|
-
setTaskStatus(created.status);
|
|
204
|
-
if (autoConnectOnStart && !autoConnect) {
|
|
205
|
-
await connect(created.id);
|
|
206
|
-
}
|
|
207
|
-
return created;
|
|
208
|
-
}, [autoConnect, autoConnectOnStart, client, connect, podId, setTaskStatus]);
|
|
209
|
-
const stop = useCallback(async () => {
|
|
210
|
-
const id = taskIdRef.current;
|
|
211
|
-
if (!id)
|
|
212
|
-
return null;
|
|
213
|
-
try {
|
|
214
|
-
const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
|
|
215
|
-
const stopped = await scopedClient.tasks.stop(id);
|
|
216
|
-
setTask(stopped);
|
|
217
|
-
setTaskStatus(stopped.status);
|
|
218
|
-
return stopped;
|
|
219
|
-
}
|
|
220
|
-
catch (stopError) {
|
|
221
|
-
const normalized = normalizeError(stopError, "Failed to stop task run.");
|
|
222
|
-
setError(normalized);
|
|
223
|
-
onErrorRef.current?.(stopError);
|
|
224
|
-
return null;
|
|
225
|
-
}
|
|
226
|
-
}, [client, podId, setTaskStatus]);
|
|
227
|
-
const clearMessages = useCallback(() => {
|
|
228
|
-
setMessages([]);
|
|
229
|
-
}, []);
|
|
230
|
-
useEffect(() => {
|
|
231
|
-
if (!taskId)
|
|
232
|
-
return;
|
|
233
|
-
let cancelled = false;
|
|
234
|
-
const bootstrapTask = async () => {
|
|
235
|
-
const latestTask = await refreshTask(taskId);
|
|
236
|
-
if (cancelled)
|
|
237
|
-
return;
|
|
238
|
-
await loadMessages(taskId);
|
|
239
|
-
if (cancelled || !autoConnect)
|
|
240
|
-
return;
|
|
241
|
-
const latestStatus = normalizeRunStatus(latestTask?.status) ?? normalizeRunStatus(statusRef.current);
|
|
242
|
-
if (isTerminalTaskStatus(latestStatus)) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
await connect(taskId);
|
|
246
|
-
};
|
|
247
|
-
void bootstrapTask();
|
|
248
|
-
return () => {
|
|
249
|
-
cancelled = true;
|
|
250
|
-
disconnect();
|
|
251
|
-
};
|
|
252
|
-
}, [autoConnect, connect, disconnect, loadMessages, refreshTask, taskId]);
|
|
253
|
-
return {
|
|
254
|
-
taskId,
|
|
255
|
-
task,
|
|
256
|
-
status,
|
|
257
|
-
messages,
|
|
258
|
-
isStreaming,
|
|
259
|
-
error,
|
|
260
|
-
setTaskId,
|
|
261
|
-
start,
|
|
262
|
-
refreshTask,
|
|
263
|
-
loadMessages,
|
|
264
|
-
connect,
|
|
265
|
-
disconnect,
|
|
266
|
-
stop,
|
|
267
|
-
clearMessages,
|
|
268
|
-
};
|
|
269
|
-
}
|
package/dist/task-events.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Task, TaskMessage } from "./types.js";
|
|
2
|
-
export interface ParsedTaskStreamEvent {
|
|
3
|
-
message?: TaskMessage;
|
|
4
|
-
status?: string;
|
|
5
|
-
task?: Task;
|
|
6
|
-
}
|
|
7
|
-
export declare function parseTaskStreamEvent(value: unknown): ParsedTaskStreamEvent;
|
|
8
|
-
export declare function upsertTaskMessage(messages: TaskMessage[], incoming: TaskMessage): TaskMessage[];
|
package/dist/task-events.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
function isRecord(value) {
|
|
2
|
-
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
3
|
-
}
|
|
4
|
-
function normalizeStatus(status) {
|
|
5
|
-
if (typeof status !== "string")
|
|
6
|
-
return undefined;
|
|
7
|
-
const normalized = status.trim().toUpperCase();
|
|
8
|
-
return normalized.length > 0 ? normalized : undefined;
|
|
9
|
-
}
|
|
10
|
-
function toTaskMessage(value) {
|
|
11
|
-
if (!isRecord(value))
|
|
12
|
-
return undefined;
|
|
13
|
-
if (typeof value.id !== "string")
|
|
14
|
-
return undefined;
|
|
15
|
-
if (typeof value.role !== "string")
|
|
16
|
-
return undefined;
|
|
17
|
-
if (!("content" in value))
|
|
18
|
-
return undefined;
|
|
19
|
-
const content = value.content;
|
|
20
|
-
const message = {
|
|
21
|
-
id: value.id,
|
|
22
|
-
role: value.role,
|
|
23
|
-
content: content,
|
|
24
|
-
created_at: typeof value.created_at === "string" ? value.created_at : new Date().toISOString(),
|
|
25
|
-
metadata: isRecord(value.metadata) ? value.metadata : null,
|
|
26
|
-
};
|
|
27
|
-
return message;
|
|
28
|
-
}
|
|
29
|
-
function toTask(value) {
|
|
30
|
-
if (!isRecord(value))
|
|
31
|
-
return undefined;
|
|
32
|
-
if (typeof value.id !== "string")
|
|
33
|
-
return undefined;
|
|
34
|
-
const status = normalizeStatus(value.status);
|
|
35
|
-
if (!status)
|
|
36
|
-
return undefined;
|
|
37
|
-
if (typeof value.agent_id !== "string")
|
|
38
|
-
return undefined;
|
|
39
|
-
if (typeof value.pod_id !== "string")
|
|
40
|
-
return undefined;
|
|
41
|
-
if (typeof value.user_id !== "string")
|
|
42
|
-
return undefined;
|
|
43
|
-
if (typeof value.created_at !== "string")
|
|
44
|
-
return undefined;
|
|
45
|
-
if (typeof value.updated_at !== "string")
|
|
46
|
-
return undefined;
|
|
47
|
-
return {
|
|
48
|
-
id: value.id,
|
|
49
|
-
agent_id: value.agent_id,
|
|
50
|
-
pod_id: value.pod_id,
|
|
51
|
-
user_id: value.user_id,
|
|
52
|
-
input_data: isRecord(value.input_data) ? value.input_data : null,
|
|
53
|
-
output_data: isRecord(value.output_data) ? value.output_data : null,
|
|
54
|
-
error: typeof value.error === "string" ? value.error : null,
|
|
55
|
-
status: status,
|
|
56
|
-
created_at: value.created_at,
|
|
57
|
-
updated_at: value.updated_at,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
function extractPayload(record) {
|
|
61
|
-
if ("data" in record)
|
|
62
|
-
return record.data;
|
|
63
|
-
if ("payload" in record)
|
|
64
|
-
return record.payload;
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
function extractStatus(payload) {
|
|
68
|
-
if (isRecord(payload)) {
|
|
69
|
-
return normalizeStatus(payload.status)
|
|
70
|
-
?? normalizeStatus(payload.task_status)
|
|
71
|
-
?? (isRecord(payload.task) ? normalizeStatus(payload.task.status) : undefined);
|
|
72
|
-
}
|
|
73
|
-
return normalizeStatus(payload);
|
|
74
|
-
}
|
|
75
|
-
export function parseTaskStreamEvent(value) {
|
|
76
|
-
const directMessage = toTaskMessage(value);
|
|
77
|
-
if (directMessage) {
|
|
78
|
-
return { message: directMessage };
|
|
79
|
-
}
|
|
80
|
-
if (!isRecord(value)) {
|
|
81
|
-
return {};
|
|
82
|
-
}
|
|
83
|
-
const eventType = typeof value.type === "string" ? value.type.toLowerCase() : "";
|
|
84
|
-
const payload = extractPayload(value);
|
|
85
|
-
if (eventType === "message" || eventType === "message_added") {
|
|
86
|
-
const message = toTaskMessage(payload);
|
|
87
|
-
return message ? { message } : {};
|
|
88
|
-
}
|
|
89
|
-
if (eventType === "status"
|
|
90
|
-
|| eventType === "task_status"
|
|
91
|
-
|| eventType === "task"
|
|
92
|
-
|| eventType === "task_updated") {
|
|
93
|
-
const task = toTask(payload) ?? (isRecord(payload) ? toTask(payload.task) : undefined);
|
|
94
|
-
const status = extractStatus(payload);
|
|
95
|
-
if (task || status) {
|
|
96
|
-
return {
|
|
97
|
-
task,
|
|
98
|
-
status: status ?? task?.status,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return {};
|
|
103
|
-
}
|
|
104
|
-
export function upsertTaskMessage(messages, incoming) {
|
|
105
|
-
const next = [...messages];
|
|
106
|
-
const index = next.findIndex((message) => message.id === incoming.id);
|
|
107
|
-
if (index >= 0) {
|
|
108
|
-
next[index] = incoming;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
next.push(incoming);
|
|
112
|
-
}
|
|
113
|
-
next.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
|
114
|
-
return next;
|
|
115
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|