lemma-sdk 0.2.37 → 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 +20 -11
- package/dist/browser/lemma-client.js +1 -114
- package/dist/client.d.ts +0 -2
- package/dist/client.js +0 -3
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -2
- package/dist/namespaces/conversations.d.ts +0 -5
- package/dist/namespaces/conversations.js +0 -15
- package/dist/openapi_client/models/AgentModelName.d.ts +1 -1
- package/dist/openapi_client/models/AgentModelName.js +1 -1
- package/dist/openapi_client/models/MessageResponse.d.ts +0 -1
- package/dist/openapi_client/services/AgentConversationsService.d.ts +4 -14
- package/dist/openapi_client/services/AgentConversationsService.js +4 -30
- package/dist/react/index.d.ts +0 -8
- package/dist/react/index.js +0 -4
- package/dist/react/useAssistantController.js +0 -2
- package/dist/react/useAssistantSession.d.ts +0 -2
- package/dist/react/useAssistantSession.js +8 -14
- package/dist/run-utils.d.ts +1 -5
- package/dist/run-utils.js +0 -10
- package/dist/types.d.ts +0 -42
- package/package.json +1 -1
- package/dist/namespaces/tasks.d.ts +0 -25
- package/dist/namespaces/tasks.js +0 -91
- package/dist/react/useAgentRun.d.ts +0 -38
- package/dist/react/useAgentRun.js +0 -149
- 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/run-utils.d.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import type { FlowRunStatus, FunctionRunStatus } from "./openapi_client/index.js";
|
|
2
|
-
|
|
3
|
-
export type AnyRunStatus = TaskStatus | FunctionRunStatus | FlowRunStatus | (string & {});
|
|
2
|
+
export type AnyRunStatus = FunctionRunStatus | FlowRunStatus | (string & {});
|
|
4
3
|
interface BackoffOptions {
|
|
5
4
|
baseMs?: number;
|
|
6
5
|
maxMs?: number;
|
|
7
6
|
factor?: number;
|
|
8
7
|
}
|
|
9
8
|
export declare function normalizeRunStatus(status: unknown): string | undefined;
|
|
10
|
-
export declare function isTerminalTaskStatus(status: unknown, options?: {
|
|
11
|
-
treatWaitingAsTerminal?: boolean;
|
|
12
|
-
}): boolean;
|
|
13
9
|
export declare function isTerminalFunctionStatus(status: unknown): boolean;
|
|
14
10
|
export declare function isTerminalFlowStatus(status: unknown, options?: {
|
|
15
11
|
treatWaitingAsTerminal?: boolean;
|
package/dist/run-utils.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const TASK_TERMINAL = new Set(["COMPLETED", "FAILED", "CANCELLED", "STOPPED"]);
|
|
2
1
|
const FUNCTION_TERMINAL = new Set(["COMPLETED", "FAILED", "CANCELLED"]);
|
|
3
2
|
const FLOW_TERMINAL = new Set(["COMPLETED", "FAILED", "CANCELLED"]);
|
|
4
3
|
export function normalizeRunStatus(status) {
|
|
@@ -8,15 +7,6 @@ export function normalizeRunStatus(status) {
|
|
|
8
7
|
const normalized = status.trim().toUpperCase();
|
|
9
8
|
return normalized.length > 0 ? normalized : undefined;
|
|
10
9
|
}
|
|
11
|
-
export function isTerminalTaskStatus(status, options = {}) {
|
|
12
|
-
const normalized = normalizeRunStatus(status);
|
|
13
|
-
if (!normalized)
|
|
14
|
-
return false;
|
|
15
|
-
if (normalized === "WAITING") {
|
|
16
|
-
return options.treatWaitingAsTerminal === true;
|
|
17
|
-
}
|
|
18
|
-
return TASK_TERMINAL.has(normalized);
|
|
19
|
-
}
|
|
20
10
|
export function isTerminalFunctionStatus(status) {
|
|
21
11
|
const normalized = normalizeRunStatus(status);
|
|
22
12
|
return !!normalized && FUNCTION_TERMINAL.has(normalized);
|
package/dist/types.d.ts
CHANGED
|
@@ -35,8 +35,6 @@ export interface RunFunctionOptions {
|
|
|
35
35
|
/** Input payload for the function */
|
|
36
36
|
input?: Record<string, unknown>;
|
|
37
37
|
}
|
|
38
|
-
/** Alias kept for backward compatibility; shape follows generated OpenAPI schema exactly. */
|
|
39
|
-
export type CreateTaskOptions = CreateTaskRequest;
|
|
40
38
|
export interface WorkflowRunInputs {
|
|
41
39
|
[key: string]: unknown;
|
|
42
40
|
}
|
|
@@ -59,51 +57,11 @@ export interface ConversationMessageResponse {
|
|
|
59
57
|
created_at: string;
|
|
60
58
|
conversation_id?: string;
|
|
61
59
|
sequence?: number;
|
|
62
|
-
agent_run_id?: string | null;
|
|
63
60
|
metadata?: Record<string, unknown> | null;
|
|
64
61
|
tool_call_id?: string | null;
|
|
65
62
|
tool_name?: string | null;
|
|
66
63
|
}
|
|
67
64
|
export type ConversationMessage = ConversationMessageResponse;
|
|
68
|
-
export type TaskStatus = "PENDING" | "RUNNING" | "WAITING" | "COMPLETED" | "FAILED" | "CANCELLED" | "STOPPED" | (string & {});
|
|
69
|
-
export interface CreateTaskRequest {
|
|
70
|
-
agent_name: string;
|
|
71
|
-
input_data?: Record<string, unknown> | null;
|
|
72
|
-
title?: string | null;
|
|
73
|
-
conversation_id?: string | null;
|
|
74
|
-
content?: string | null;
|
|
75
|
-
}
|
|
76
|
-
export interface AddMessageRequest {
|
|
77
|
-
content: string;
|
|
78
|
-
}
|
|
79
|
-
export interface TaskResponse {
|
|
80
|
-
id: string;
|
|
81
|
-
agent_id?: string | null;
|
|
82
|
-
agent_name?: string | null;
|
|
83
|
-
pod_id: string;
|
|
84
|
-
user_id?: string;
|
|
85
|
-
input_data?: Record<string, unknown> | null;
|
|
86
|
-
output_data?: unknown;
|
|
87
|
-
error?: string | null;
|
|
88
|
-
status?: TaskStatus;
|
|
89
|
-
created_at?: string;
|
|
90
|
-
updated_at?: string;
|
|
91
|
-
conversation?: Conversation;
|
|
92
|
-
}
|
|
93
|
-
export interface TaskListResponse {
|
|
94
|
-
items: TaskResponse[];
|
|
95
|
-
limit: number;
|
|
96
|
-
next_page_token?: string | null;
|
|
97
|
-
total?: number;
|
|
98
|
-
}
|
|
99
|
-
export type TaskMessageResponse = ConversationMessageResponse;
|
|
100
|
-
export interface TaskMessageListResponse {
|
|
101
|
-
items: TaskMessageResponse[];
|
|
102
|
-
limit: number;
|
|
103
|
-
next_page_token?: string | null;
|
|
104
|
-
}
|
|
105
|
-
export type Task = TaskResponse;
|
|
106
|
-
export type TaskMessage = TaskMessageResponse;
|
|
107
65
|
export type FunctionRun = FunctionRunResponse;
|
|
108
66
|
export type FlowRun = FlowRunEntity;
|
|
109
67
|
export type Workflow = FlowResponse;
|
package/package.json
CHANGED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { HttpClient } from "../http.js";
|
|
2
|
-
import type { AddMessageRequest, CreateTaskRequest, TaskListResponse, TaskMessageListResponse, TaskResponse } from "../types.js";
|
|
3
|
-
export declare class TasksNamespace {
|
|
4
|
-
private readonly http;
|
|
5
|
-
private readonly podId;
|
|
6
|
-
constructor(http: HttpClient, podId: () => string);
|
|
7
|
-
list(options?: {
|
|
8
|
-
agent_name?: string;
|
|
9
|
-
limit?: number;
|
|
10
|
-
page_token?: string;
|
|
11
|
-
}): Promise<TaskListResponse>;
|
|
12
|
-
create(payload: CreateTaskRequest): Promise<TaskResponse>;
|
|
13
|
-
get(taskId: string): Promise<TaskResponse>;
|
|
14
|
-
stop(taskId: string): Promise<TaskResponse>;
|
|
15
|
-
stream(taskId: string, options?: {
|
|
16
|
-
signal?: AbortSignal;
|
|
17
|
-
}): Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>;
|
|
18
|
-
readonly messages: {
|
|
19
|
-
list: (taskId: string, options?: {
|
|
20
|
-
limit?: number;
|
|
21
|
-
page_token?: string;
|
|
22
|
-
}) => Promise<TaskMessageListResponse>;
|
|
23
|
-
add: (taskId: string, payload: AddMessageRequest) => Promise<TaskResponse>;
|
|
24
|
-
};
|
|
25
|
-
}
|
package/dist/namespaces/tasks.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
function toTask(conversation, inputData) {
|
|
2
|
-
return {
|
|
3
|
-
id: conversation.id,
|
|
4
|
-
agent_id: conversation.agent_id,
|
|
5
|
-
pod_id: conversation.pod_id,
|
|
6
|
-
user_id: conversation.user_id,
|
|
7
|
-
input_data: inputData ?? null,
|
|
8
|
-
output_data: null,
|
|
9
|
-
error: null,
|
|
10
|
-
status: conversation.status?.toUpperCase() ?? "WAITING",
|
|
11
|
-
created_at: conversation.created_at,
|
|
12
|
-
updated_at: conversation.updated_at,
|
|
13
|
-
conversation,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
function normalizeConversation(conversation) {
|
|
17
|
-
return {
|
|
18
|
-
...conversation,
|
|
19
|
-
model: conversation.model ?? conversation.model_name ?? null,
|
|
20
|
-
status: conversation.status ?? "waiting",
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function normalizeMessages(messages) {
|
|
24
|
-
return messages;
|
|
25
|
-
}
|
|
26
|
-
export class TasksNamespace {
|
|
27
|
-
http;
|
|
28
|
-
podId;
|
|
29
|
-
constructor(http, podId) {
|
|
30
|
-
this.http = http;
|
|
31
|
-
this.podId = podId;
|
|
32
|
-
}
|
|
33
|
-
list(options = {}) {
|
|
34
|
-
return this.http.request("GET", `/pods/${this.podId()}/conversations`, {
|
|
35
|
-
params: {
|
|
36
|
-
agent_name: options.agent_name,
|
|
37
|
-
limit: options.limit ?? 100,
|
|
38
|
-
page_token: options.page_token,
|
|
39
|
-
},
|
|
40
|
-
}).then((response) => {
|
|
41
|
-
const conversations = (response.items ?? []).map(normalizeConversation);
|
|
42
|
-
return {
|
|
43
|
-
items: conversations.map((conversation) => toTask(conversation)),
|
|
44
|
-
limit: response.limit ?? options.limit ?? 100,
|
|
45
|
-
next_page_token: response.next_page_token ?? null,
|
|
46
|
-
total: response.total,
|
|
47
|
-
};
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
create(payload) {
|
|
51
|
-
return this.http.request("POST", `/pods/${this.podId()}/conversations`, {
|
|
52
|
-
body: {
|
|
53
|
-
agent_name: payload.agent_name,
|
|
54
|
-
title: payload.title ?? payload.content ?? payload.agent_name,
|
|
55
|
-
},
|
|
56
|
-
}).then((conversation) => toTask(normalizeConversation(conversation), payload.input_data));
|
|
57
|
-
}
|
|
58
|
-
get(taskId) {
|
|
59
|
-
return this.http.request("GET", `/pods/${this.podId()}/conversations/${taskId}`)
|
|
60
|
-
.then((conversation) => toTask(normalizeConversation(conversation)));
|
|
61
|
-
}
|
|
62
|
-
stop(taskId) {
|
|
63
|
-
return this.http.request("POST", `/pods/${this.podId()}/conversations/${taskId}/stop`, {
|
|
64
|
-
body: {},
|
|
65
|
-
}).then((conversation) => toTask(normalizeConversation(conversation)));
|
|
66
|
-
}
|
|
67
|
-
stream(taskId, options = {}) {
|
|
68
|
-
return this.http.stream(`/pods/${this.podId()}/conversations/${taskId}/stream`, {
|
|
69
|
-
signal: options.signal,
|
|
70
|
-
headers: {
|
|
71
|
-
Accept: "text/event-stream",
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
messages = {
|
|
76
|
-
list: (taskId, options = {}) => this.http.request("GET", `/pods/${this.podId()}/conversations/${taskId}/messages`, {
|
|
77
|
-
params: {
|
|
78
|
-
limit: options.limit ?? 100,
|
|
79
|
-
after_sequence: options.page_token,
|
|
80
|
-
},
|
|
81
|
-
}).then((response) => ({
|
|
82
|
-
...response,
|
|
83
|
-
items: normalizeMessages(response.items ?? []),
|
|
84
|
-
})),
|
|
85
|
-
add: (taskId, payload) => {
|
|
86
|
-
return this.http.request("POST", `/pods/${this.podId()}/conversations/${taskId}/messages`, {
|
|
87
|
-
body: payload,
|
|
88
|
-
}).then(() => this.get(taskId));
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { Conversation, ConversationMessage, Task } from "../types.js";
|
|
2
|
-
import { type UseAssistantSessionOptions } from "./useAssistantSession.js";
|
|
3
|
-
export interface UseAgentRunOptions extends Omit<UseAssistantSessionOptions, "agentName" | "assistantName" | "assistantId" | "conversationId"> {
|
|
4
|
-
agentName?: string;
|
|
5
|
-
conversationId?: string | null;
|
|
6
|
-
taskId?: string | null;
|
|
7
|
-
autoConnect?: boolean;
|
|
8
|
-
autoConnectOnStart?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface UseAgentRunResult {
|
|
11
|
-
taskId: string | null;
|
|
12
|
-
conversationId: string | null;
|
|
13
|
-
task: Task | null;
|
|
14
|
-
conversation: Conversation | null;
|
|
15
|
-
status?: string;
|
|
16
|
-
messages: ConversationMessage[];
|
|
17
|
-
output: unknown;
|
|
18
|
-
finalOutput: unknown;
|
|
19
|
-
outputText: string;
|
|
20
|
-
finalOutputText: string;
|
|
21
|
-
isStreaming: boolean;
|
|
22
|
-
error: Error | null;
|
|
23
|
-
isWaitingForInput: boolean;
|
|
24
|
-
isFinished: boolean;
|
|
25
|
-
setTaskId: (taskId: string | null) => void;
|
|
26
|
-
setConversationId: (conversationId: string | null) => void;
|
|
27
|
-
start: (inputData?: Record<string, unknown> | null, options?: {
|
|
28
|
-
agentName?: string;
|
|
29
|
-
}) => Promise<Task>;
|
|
30
|
-
submitInput: (content: string) => Promise<Task | null>;
|
|
31
|
-
refreshTask: (taskId?: string | null) => Promise<Task | null>;
|
|
32
|
-
loadMessages: (taskId?: string | null) => Promise<ConversationMessage[]>;
|
|
33
|
-
connect: (taskId?: string | null) => Promise<void>;
|
|
34
|
-
disconnect: () => void;
|
|
35
|
-
stop: () => Promise<Task | null>;
|
|
36
|
-
clearMessages: () => void;
|
|
37
|
-
}
|
|
38
|
-
export declare function useAgentRun({ client, podId, agentName, conversationId, taskId, autoConnect, autoConnectOnStart, autoLoad, autoResume, syncOnTurnEnd, onEvent, onStatus, onMessage, onError, }: UseAgentRunOptions): UseAgentRunResult;
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo } from "react";
|
|
2
|
-
import { extractConversationMessageText, getLatestAssistantMessage, isConversationRunningStatus, normalizeConversationStatus, } from "./assistant-output.js";
|
|
3
|
-
import { useAssistantSession, } from "./useAssistantSession.js";
|
|
4
|
-
function resolveAgentName(base, override) {
|
|
5
|
-
const resolved = override ?? base;
|
|
6
|
-
if (!resolved) {
|
|
7
|
-
throw new Error("agentName is required.");
|
|
8
|
-
}
|
|
9
|
-
return resolved;
|
|
10
|
-
}
|
|
11
|
-
function stringifyAgentInput(inputData) {
|
|
12
|
-
if (!inputData || Object.keys(inputData).length === 0) {
|
|
13
|
-
return "";
|
|
14
|
-
}
|
|
15
|
-
const prompt = inputData.prompt ?? inputData.message ?? inputData.content;
|
|
16
|
-
if (typeof prompt === "string" && prompt.trim().length > 0 && Object.keys(inputData).length === 1) {
|
|
17
|
-
return prompt.trim();
|
|
18
|
-
}
|
|
19
|
-
return JSON.stringify(inputData, null, 2);
|
|
20
|
-
}
|
|
21
|
-
function taskFromConversation(conversation, status, inputData, output) {
|
|
22
|
-
if (!conversation)
|
|
23
|
-
return null;
|
|
24
|
-
return {
|
|
25
|
-
id: conversation.id,
|
|
26
|
-
agent_id: conversation.agent_id,
|
|
27
|
-
pod_id: conversation.pod_id,
|
|
28
|
-
user_id: conversation.user_id,
|
|
29
|
-
input_data: inputData ?? null,
|
|
30
|
-
output_data: output ?? null,
|
|
31
|
-
error: null,
|
|
32
|
-
status: normalizeConversationStatus(status ?? conversation.status) ?? "WAITING",
|
|
33
|
-
created_at: conversation.created_at,
|
|
34
|
-
updated_at: conversation.updated_at,
|
|
35
|
-
conversation,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
export function useAgentRun({ client, podId, agentName, conversationId, taskId = null, autoConnect = true, autoConnectOnStart = true, autoLoad, autoResume, syncOnTurnEnd, onEvent, onStatus, onMessage, onError, }) {
|
|
39
|
-
const session = useAssistantSession({
|
|
40
|
-
client,
|
|
41
|
-
podId,
|
|
42
|
-
agentName,
|
|
43
|
-
conversationId: conversationId ?? taskId,
|
|
44
|
-
autoLoad: autoLoad ?? autoConnect,
|
|
45
|
-
autoResume: autoResume ?? autoConnect,
|
|
46
|
-
syncOnTurnEnd,
|
|
47
|
-
onEvent,
|
|
48
|
-
onStatus,
|
|
49
|
-
onMessage,
|
|
50
|
-
onError,
|
|
51
|
-
});
|
|
52
|
-
const start = useCallback(async (inputData, options) => {
|
|
53
|
-
const resolvedAgentName = resolveAgentName(agentName, options?.agentName);
|
|
54
|
-
const content = stringifyAgentInput(inputData);
|
|
55
|
-
const conversation = await session.createConversation({
|
|
56
|
-
agentName: resolvedAgentName,
|
|
57
|
-
title: content ? content.slice(0, 120) : resolvedAgentName,
|
|
58
|
-
setActive: true,
|
|
59
|
-
});
|
|
60
|
-
if (content) {
|
|
61
|
-
await session.sendMessage(content, {
|
|
62
|
-
conversationId: conversation.id,
|
|
63
|
-
createIfMissing: false,
|
|
64
|
-
syncOnTurnEnd,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
else if (autoConnectOnStart) {
|
|
68
|
-
await session.resume(conversation.id);
|
|
69
|
-
}
|
|
70
|
-
return taskFromConversation(conversation, session.status, inputData) ?? {
|
|
71
|
-
id: conversation.id,
|
|
72
|
-
agent_id: conversation.agent_id,
|
|
73
|
-
pod_id: conversation.pod_id,
|
|
74
|
-
user_id: conversation.user_id,
|
|
75
|
-
input_data: inputData ?? null,
|
|
76
|
-
output_data: null,
|
|
77
|
-
error: null,
|
|
78
|
-
status: "WAITING",
|
|
79
|
-
created_at: conversation.created_at,
|
|
80
|
-
updated_at: conversation.updated_at,
|
|
81
|
-
conversation,
|
|
82
|
-
};
|
|
83
|
-
}, [agentName, autoConnectOnStart, session, syncOnTurnEnd]);
|
|
84
|
-
const submitInput = useCallback(async (content) => {
|
|
85
|
-
const resolvedConversationId = session.conversationId;
|
|
86
|
-
if (!resolvedConversationId) {
|
|
87
|
-
throw new Error("conversationId is required to submit additional agent input.");
|
|
88
|
-
}
|
|
89
|
-
await session.sendMessage(content, {
|
|
90
|
-
conversationId: resolvedConversationId,
|
|
91
|
-
createIfMissing: false,
|
|
92
|
-
syncOnTurnEnd,
|
|
93
|
-
});
|
|
94
|
-
return taskFromConversation(session.conversation, session.status);
|
|
95
|
-
}, [session, syncOnTurnEnd]);
|
|
96
|
-
const refreshTask = useCallback(async (explicitTaskId) => {
|
|
97
|
-
const conversation = await session.refreshConversation(explicitTaskId);
|
|
98
|
-
return taskFromConversation(conversation, session.status);
|
|
99
|
-
}, [session]);
|
|
100
|
-
const loadMessages = useCallback(async (explicitTaskId) => {
|
|
101
|
-
const response = await session.loadMessages({ conversationId: explicitTaskId });
|
|
102
|
-
return response.items;
|
|
103
|
-
}, [session]);
|
|
104
|
-
const connect = useCallback(async (explicitTaskId) => {
|
|
105
|
-
await session.resume(explicitTaskId);
|
|
106
|
-
}, [session]);
|
|
107
|
-
const stop = useCallback(async () => {
|
|
108
|
-
await session.stop();
|
|
109
|
-
return taskFromConversation(session.conversation, "WAITING");
|
|
110
|
-
}, [session]);
|
|
111
|
-
return useMemo(() => {
|
|
112
|
-
const latestAssistantMessage = getLatestAssistantMessage(session.messages);
|
|
113
|
-
const output = latestAssistantMessage?.content ?? session.output ?? null;
|
|
114
|
-
const outputText = latestAssistantMessage
|
|
115
|
-
? extractConversationMessageText(latestAssistantMessage.content)
|
|
116
|
-
: session.outputText;
|
|
117
|
-
const normalizedStatus = normalizeConversationStatus(session.status);
|
|
118
|
-
const running = isConversationRunningStatus(normalizedStatus) || session.isStreaming;
|
|
119
|
-
const isWaitingForInput = normalizedStatus === "WAITING";
|
|
120
|
-
const isFinished = !!latestAssistantMessage && !running;
|
|
121
|
-
const task = taskFromConversation(session.conversation, session.status, null, output);
|
|
122
|
-
return {
|
|
123
|
-
taskId: session.conversationId,
|
|
124
|
-
conversationId: session.conversationId,
|
|
125
|
-
task,
|
|
126
|
-
conversation: session.conversation,
|
|
127
|
-
status: normalizedStatus,
|
|
128
|
-
messages: session.messages,
|
|
129
|
-
output,
|
|
130
|
-
finalOutput: isFinished ? output : null,
|
|
131
|
-
outputText,
|
|
132
|
-
finalOutputText: isFinished ? outputText : "",
|
|
133
|
-
isStreaming: session.isStreaming,
|
|
134
|
-
error: session.error,
|
|
135
|
-
isWaitingForInput,
|
|
136
|
-
isFinished,
|
|
137
|
-
setTaskId: session.setConversationId,
|
|
138
|
-
setConversationId: session.setConversationId,
|
|
139
|
-
start,
|
|
140
|
-
submitInput,
|
|
141
|
-
refreshTask,
|
|
142
|
-
loadMessages,
|
|
143
|
-
connect,
|
|
144
|
-
disconnect: session.cancel,
|
|
145
|
-
stop,
|
|
146
|
-
clearMessages: session.clearMessages,
|
|
147
|
-
};
|
|
148
|
-
}, [connect, loadMessages, refreshTask, session, start, stop, submitInput]);
|
|
149
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { LemmaClient } from "../client.js";
|
|
2
|
-
import type { Task } from "../types.js";
|
|
3
|
-
export interface UseAgentRunsOptions {
|
|
4
|
-
client: LemmaClient;
|
|
5
|
-
podId?: string;
|
|
6
|
-
agentName?: string;
|
|
7
|
-
enabled?: boolean;
|
|
8
|
-
autoLoad?: boolean;
|
|
9
|
-
limit?: number;
|
|
10
|
-
pageToken?: string;
|
|
11
|
-
initialTaskId?: string | null;
|
|
12
|
-
}
|
|
13
|
-
export interface UseAgentRunsResult {
|
|
14
|
-
runs: Task[];
|
|
15
|
-
total: number;
|
|
16
|
-
nextPageToken: string | null;
|
|
17
|
-
selectedTaskId: string | null;
|
|
18
|
-
effectiveSelectedTaskId: string | null;
|
|
19
|
-
selectedRun: Task | null;
|
|
20
|
-
isLoading: boolean;
|
|
21
|
-
isLoadingMore: boolean;
|
|
22
|
-
error: Error | null;
|
|
23
|
-
selectRun: (taskId: string | null) => void;
|
|
24
|
-
clearSelection: () => void;
|
|
25
|
-
refresh: (overrides?: {
|
|
26
|
-
limit?: number;
|
|
27
|
-
pageToken?: string;
|
|
28
|
-
}) => Promise<Task[]>;
|
|
29
|
-
loadMore: (overrides?: {
|
|
30
|
-
limit?: number;
|
|
31
|
-
}) => Promise<Task[]>;
|
|
32
|
-
}
|
|
33
|
-
export declare function useAgentRuns({ client, podId, agentName, enabled, autoLoad, limit, pageToken, initialTaskId, }: UseAgentRunsOptions): UseAgentRunsResult;
|
|
@@ -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
|
-
}
|