@langchain/langgraph-sdk 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react/index.cjs +10 -46
- package/dist/react/index.d.cts +11 -14
- package/dist/react/index.d.ts +11 -14
- package/dist/react/index.js +3 -13
- package/dist/react/stream.cjs +17 -0
- package/dist/react/stream.cjs.map +1 -0
- package/dist/react/stream.custom.cjs +162 -0
- package/dist/react/stream.custom.cjs.map +1 -0
- package/dist/react/stream.custom.d.cts +40 -0
- package/dist/react/stream.custom.d.cts.map +1 -0
- package/dist/react/stream.custom.d.ts +39 -0
- package/dist/react/stream.custom.d.ts.map +1 -0
- package/dist/react/stream.custom.js +160 -0
- package/dist/react/stream.custom.js.map +1 -0
- package/dist/react/stream.d.cts +10 -0
- package/dist/react/stream.d.cts.map +1 -0
- package/dist/react/stream.d.ts +9 -0
- package/dist/react/stream.d.ts.map +1 -0
- package/dist/react/stream.js +16 -0
- package/dist/react/stream.js.map +1 -0
- package/dist/react/stream.lgp.cjs +550 -0
- package/dist/react/stream.lgp.cjs.map +1 -0
- package/dist/react/stream.lgp.js +549 -0
- package/dist/react/stream.lgp.js.map +1 -0
- package/dist/react/thread.cjs +19 -0
- package/dist/react/thread.cjs.map +1 -0
- package/dist/react/thread.js +18 -0
- package/dist/react/thread.js.map +1 -0
- package/dist/react/types.d.cts +79 -0
- package/dist/react/types.d.cts.map +1 -0
- package/dist/react/types.d.ts +79 -0
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react-ui/client.cjs +136 -0
- package/dist/react-ui/client.cjs.map +1 -0
- package/dist/react-ui/client.d.cts +67 -0
- package/dist/react-ui/client.d.cts.map +1 -0
- package/dist/react-ui/client.d.ts +67 -0
- package/dist/react-ui/client.d.ts.map +1 -0
- package/dist/react-ui/client.js +130 -0
- package/dist/react-ui/client.js.map +1 -0
- package/dist/react-ui/index.cjs +13 -38
- package/dist/react-ui/index.cjs.map +1 -0
- package/dist/react-ui/index.d.cts +2 -1
- package/dist/react-ui/index.d.ts +2 -1
- package/dist/react-ui/index.js +7 -1
- package/dist/react-ui/index.js.map +1 -0
- package/dist/react-ui/server/index.cjs +4 -14
- package/dist/react-ui/server/index.d.cts +2 -1
- package/dist/react-ui/server/index.d.ts +2 -1
- package/dist/react-ui/server/index.js +2 -1
- package/dist/react-ui/server/server.cjs +56 -0
- package/dist/react-ui/server/server.cjs.map +1 -0
- package/dist/react-ui/server/server.d.cts +54 -0
- package/dist/react-ui/server/server.d.cts.map +1 -0
- package/dist/react-ui/server/server.d.ts +54 -0
- package/dist/react-ui/server/server.d.ts.map +1 -0
- package/dist/react-ui/server/server.js +55 -0
- package/dist/react-ui/server/server.js.map +1 -0
- package/dist/react-ui/types.cjs +37 -0
- package/dist/react-ui/types.cjs.map +1 -0
- package/dist/react-ui/types.d.cts +25 -0
- package/dist/react-ui/types.d.cts.map +1 -0
- package/dist/react-ui/types.d.ts +25 -0
- package/dist/react-ui/types.d.ts.map +1 -0
- package/dist/react-ui/types.js +35 -0
- package/dist/react-ui/types.js.map +1 -0
- package/dist/ui/index.d.cts +1 -1
- package/dist/ui/index.d.ts +1 -1
- package/package.json +11 -3
- package/dist/react/index.cjs.map +0 -1
- package/dist/react/index.d.cts.map +0 -1
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js.map +0 -1
package/dist/react/index.cjs
CHANGED
|
@@ -1,47 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
require("../
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
|
-
exports.FetchStreamTransport = _langchain_react.FetchStreamTransport;
|
|
15
|
-
Object.defineProperty(exports, "SubagentManager", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function() {
|
|
18
|
-
return _langchain_react.SubagentManager;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(exports, "calculateDepthFromNamespace", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function() {
|
|
24
|
-
return _langchain_react.calculateDepthFromNamespace;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(exports, "extractParentIdFromNamespace", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function() {
|
|
30
|
-
return _langchain_react.extractParentIdFromNamespace;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(exports, "extractToolCallIdFromNamespace", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function() {
|
|
36
|
-
return _langchain_react.extractToolCallIdFromNamespace;
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(exports, "isSubagentNamespace", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
get: function() {
|
|
42
|
-
return _langchain_react.isSubagentNamespace;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
exports.useStream = useStream;
|
|
46
|
-
|
|
47
|
-
//# sourceMappingURL=index.cjs.map
|
|
2
|
+
const require_subagents = require("../ui/subagents.cjs");
|
|
3
|
+
const require_stream_custom = require("./stream.custom.cjs");
|
|
4
|
+
const require_stream = require("./stream.cjs");
|
|
5
|
+
exports.FetchStreamTransport = require_stream_custom.FetchStreamTransport;
|
|
6
|
+
exports.SubagentManager = require_subagents.SubagentManager;
|
|
7
|
+
exports.calculateDepthFromNamespace = require_subagents.calculateDepthFromNamespace;
|
|
8
|
+
exports.extractParentIdFromNamespace = require_subagents.extractParentIdFromNamespace;
|
|
9
|
+
exports.extractToolCallIdFromNamespace = require_subagents.extractToolCallIdFromNamespace;
|
|
10
|
+
exports.isSubagentNamespace = require_subagents.isSubagentNamespace;
|
|
11
|
+
exports.useStream = require_stream.useStream;
|
package/dist/react/index.d.cts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { type AgentTypeConfigLike, type BaseStream, type BaseSubagentState, type CompiledSubAgentLike, type DeepAgentTypeConfigLike, type DefaultSubagentStates, type DefaultToolCall, type ExtractAgentConfig, type ExtractDeepAgentConfig, type ExtractSubAgentMiddleware, FetchStreamTransport, type GetToolCallsType, type InferAgentToolCalls, type InferBag, type InferDeepAgentSubagents, type InferNodeNames, type InferStateType, type InferSubagentByName, type InferSubagentNames, type InferSubagentState, type InferSubagentStates, type InferToolCalls, type IsAgentLike, type IsDeepAgentLike, type MessageMetadata, type ResolveStreamInterface, type ResolveStreamOptions, type SubAgentLike, SubagentManager, type SubagentStateMap, type SubagentStatus, type SubagentStream, type SubagentStreamInterface, type SubagentToolCall, type ToolCallFromTool, type ToolCallState, type ToolCallWithResult, type ToolCallsFromTools, type UseAgentStream, type UseAgentStreamOptions, type UseDeepAgentStream, type UseDeepAgentStreamOptions, type UseStream, type UseStreamCustom, type UseStreamCustomOptions, type UseStreamOptions, type UseStreamThread, type UseStreamTransport, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
|
|
14
|
-
//# sourceMappingURL=index.d.cts.map
|
|
1
|
+
import { DefaultToolCall, ToolCallFromTool, ToolCallState, ToolCallWithResult, ToolCallsFromTools } from "../types.messages.cjs";
|
|
2
|
+
import { AgentTypeConfigLike, BaseSubagentState, CompiledSubAgentLike, DeepAgentTypeConfigLike, DefaultSubagentStates, ExtractAgentConfig, ExtractDeepAgentConfig, ExtractSubAgentMiddleware, GetToolCallsType, InferAgentToolCalls, InferDeepAgentSubagents, InferSubagentByName, InferSubagentNames, InferSubagentState, IsAgentLike, IsDeepAgentLike, MessageMetadata, SubAgentLike, SubagentStateMap, SubagentStatus, SubagentStream, SubagentStreamInterface, SubagentToolCall, UseStreamCustomOptions, UseStreamOptions, UseStreamThread, UseStreamTransport } from "../ui/types.cjs";
|
|
3
|
+
import { BaseStream } from "../ui/stream/base.cjs";
|
|
4
|
+
import { UseAgentStream, UseAgentStreamOptions } from "../ui/stream/agent.cjs";
|
|
5
|
+
import { UseDeepAgentStream, UseDeepAgentStreamOptions } from "../ui/stream/deep-agent.cjs";
|
|
6
|
+
import { InferBag, InferNodeNames, InferStateType, InferSubagentStates, InferToolCalls, ResolveStreamInterface, ResolveStreamOptions } from "../ui/stream/index.cjs";
|
|
7
|
+
import { UseStream, UseStreamCustom } from "./types.cjs";
|
|
8
|
+
import { useStream } from "./stream.cjs";
|
|
9
|
+
import { FetchStreamTransport } from "./stream.custom.cjs";
|
|
10
|
+
import { SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace } from "../ui/subagents.cjs";
|
|
11
|
+
export { type AgentTypeConfigLike, type BaseStream, type BaseSubagentState, type CompiledSubAgentLike, type DeepAgentTypeConfigLike, type DefaultSubagentStates, type DefaultToolCall, type ExtractAgentConfig, type ExtractDeepAgentConfig, type ExtractSubAgentMiddleware, FetchStreamTransport, type GetToolCallsType, type InferAgentToolCalls, type InferBag, type InferDeepAgentSubagents, type InferNodeNames, type InferStateType, type InferSubagentByName, type InferSubagentNames, type InferSubagentState, type InferSubagentStates, type InferToolCalls, type IsAgentLike, type IsDeepAgentLike, type MessageMetadata, type ResolveStreamInterface, type ResolveStreamOptions, type SubAgentLike, SubagentManager, type SubagentStateMap, type SubagentStatus, type SubagentStream, type SubagentStreamInterface, type SubagentToolCall, type ToolCallFromTool, type ToolCallState, type ToolCallWithResult, type ToolCallsFromTools, type UseAgentStream, type UseAgentStreamOptions, type UseDeepAgentStream, type UseDeepAgentStreamOptions, type UseStream, type UseStreamCustom, type UseStreamCustomOptions, type UseStreamOptions, type UseStreamThread, type UseStreamTransport, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { type AgentTypeConfigLike, type BaseStream, type BaseSubagentState, type CompiledSubAgentLike, type DeepAgentTypeConfigLike, type DefaultSubagentStates, type DefaultToolCall, type ExtractAgentConfig, type ExtractDeepAgentConfig, type ExtractSubAgentMiddleware, FetchStreamTransport, type GetToolCallsType, type InferAgentToolCalls, type InferBag, type InferDeepAgentSubagents, type InferNodeNames, type InferStateType, type InferSubagentByName, type InferSubagentNames, type InferSubagentState, type InferSubagentStates, type InferToolCalls, type IsAgentLike, type IsDeepAgentLike, type MessageMetadata, type ResolveStreamInterface, type ResolveStreamOptions, type SubAgentLike, SubagentManager, type SubagentStateMap, type SubagentStatus, type SubagentStream, type SubagentStreamInterface, type SubagentToolCall, type ToolCallFromTool, type ToolCallState, type ToolCallWithResult, type ToolCallsFromTools, type UseAgentStream, type UseAgentStreamOptions, type UseDeepAgentStream, type UseDeepAgentStreamOptions, type UseStream, type UseStreamCustom, type UseStreamCustomOptions, type UseStreamOptions, type UseStreamThread, type UseStreamTransport, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
|
|
14
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
import { DefaultToolCall, ToolCallFromTool, ToolCallState, ToolCallWithResult, ToolCallsFromTools } from "../types.messages.js";
|
|
2
|
+
import { AgentTypeConfigLike, BaseSubagentState, CompiledSubAgentLike, DeepAgentTypeConfigLike, DefaultSubagentStates, ExtractAgentConfig, ExtractDeepAgentConfig, ExtractSubAgentMiddleware, GetToolCallsType, InferAgentToolCalls, InferDeepAgentSubagents, InferSubagentByName, InferSubagentNames, InferSubagentState, IsAgentLike, IsDeepAgentLike, MessageMetadata, SubAgentLike, SubagentStateMap, SubagentStatus, SubagentStream, SubagentStreamInterface, SubagentToolCall, UseStreamCustomOptions, UseStreamOptions, UseStreamThread, UseStreamTransport } from "../ui/types.js";
|
|
3
|
+
import { BaseStream } from "../ui/stream/base.js";
|
|
4
|
+
import { UseAgentStream, UseAgentStreamOptions } from "../ui/stream/agent.js";
|
|
5
|
+
import { UseDeepAgentStream, UseDeepAgentStreamOptions } from "../ui/stream/deep-agent.js";
|
|
6
|
+
import { InferBag, InferNodeNames, InferStateType, InferSubagentStates, InferToolCalls, ResolveStreamInterface, ResolveStreamOptions } from "../ui/stream/index.js";
|
|
7
|
+
import { UseStream, UseStreamCustom } from "./types.js";
|
|
8
|
+
import { useStream } from "./stream.js";
|
|
9
|
+
import { FetchStreamTransport } from "./stream.custom.js";
|
|
10
|
+
import { SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace } from "../ui/subagents.js";
|
|
11
|
+
export { type AgentTypeConfigLike, type BaseStream, type BaseSubagentState, type CompiledSubAgentLike, type DeepAgentTypeConfigLike, type DefaultSubagentStates, type DefaultToolCall, type ExtractAgentConfig, type ExtractDeepAgentConfig, type ExtractSubAgentMiddleware, FetchStreamTransport, type GetToolCallsType, type InferAgentToolCalls, type InferBag, type InferDeepAgentSubagents, type InferNodeNames, type InferStateType, type InferSubagentByName, type InferSubagentNames, type InferSubagentState, type InferSubagentStates, type InferToolCalls, type IsAgentLike, type IsDeepAgentLike, type MessageMetadata, type ResolveStreamInterface, type ResolveStreamOptions, type SubAgentLike, SubagentManager, type SubagentStateMap, type SubagentStatus, type SubagentStream, type SubagentStreamInterface, type SubagentToolCall, type ToolCallFromTool, type ToolCallState, type ToolCallWithResult, type ToolCallsFromTools, type UseAgentStream, type UseAgentStreamOptions, type UseDeepAgentStream, type UseDeepAgentStreamOptions, type UseStream, type UseStreamCustom, type UseStreamCustomOptions, type UseStreamOptions, type UseStreamThread, type UseStreamTransport, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
|
package/dist/react/index.js
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { FetchStreamTransport
|
|
3
|
-
|
|
4
|
-
function useStream(...args) {
|
|
5
|
-
const options = args[0];
|
|
6
|
-
return useStream$1({
|
|
7
|
-
...options,
|
|
8
|
-
toMessage: toMessageDict
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
1
|
+
import { SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace } from "../ui/subagents.js";
|
|
2
|
+
import { FetchStreamTransport } from "./stream.custom.js";
|
|
3
|
+
import { useStream } from "./stream.js";
|
|
12
4
|
export { FetchStreamTransport, SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
|
|
13
|
-
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_stream_lgp = require("./stream.lgp.cjs");
|
|
3
|
+
const require_stream_custom = require("./stream.custom.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
//#region src/react/stream.tsx
|
|
6
|
+
function isCustomOptions(options) {
|
|
7
|
+
return "transport" in options;
|
|
8
|
+
}
|
|
9
|
+
function useStream(options) {
|
|
10
|
+
const [isCustom] = (0, react.useState)(isCustomOptions(options));
|
|
11
|
+
if (isCustom) return require_stream_custom.useStreamCustom(options);
|
|
12
|
+
return require_stream_lgp.useStreamLGP(options);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.useStream = useStream;
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=stream.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.cjs","names":["useStreamCustom","useStreamLGP"],"sources":["../../src/react/stream.tsx"],"sourcesContent":["import { useState } from \"react\";\nimport { useStreamLGP } from \"./stream.lgp.js\";\nimport { useStreamCustom } from \"./stream.custom.js\";\nimport type { UseStreamOptions } from \"../ui/types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\nimport type { UseStreamCustomOptions } from \"./types.js\";\nimport type {\n ResolveStreamInterface,\n ResolveStreamOptions,\n InferBag,\n InferStateType,\n} from \"../ui/stream/index.js\";\n\nfunction isCustomOptions<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options:\n | UseStreamOptions<StateType, Bag>\n | UseStreamCustomOptions<StateType, Bag>\n): options is UseStreamCustomOptions<StateType, Bag> {\n return \"transport\" in options;\n}\n\nexport function useStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options: ResolveStreamOptions<T, InferBag<T, Bag>>\n): ResolveStreamInterface<T, InferBag<T, Bag>>;\n\nexport function useStream<\n T = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options: UseStreamCustomOptions<InferStateType<T>, InferBag<T, Bag>>\n): ResolveStreamInterface<T, InferBag<T, Bag>>;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useStream(options: any): any {\n // Store this in useState to make sure we're not changing the implementation in re-renders\n const [isCustom] = useState(isCustomOptions(options));\n\n if (isCustom) {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStreamCustom(options);\n }\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStreamLGP(options);\n}\n"],"mappings":";;;;;AAaA,SAAS,gBAIP,SAGmD;AACnD,QAAO,eAAe;;AAkBxB,SAAgB,UAAU,SAAmB;CAE3C,MAAM,CAAC,aAAA,GAAA,MAAA,UAAqB,gBAAgB,QAAQ,CAAC;AAErD,KAAI,SAEF,QAAOA,sBAAAA,gBAAgB,QAAQ;AAIjC,QAAOC,mBAAAA,aAAa,QAAQ"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
const require_sse = require("../utils/sse.cjs");
|
|
4
|
+
const require_stream = require("../utils/stream.cjs");
|
|
5
|
+
const require_messages = require("../ui/messages.cjs");
|
|
6
|
+
const require_tools = require("../utils/tools.cjs");
|
|
7
|
+
const require_manager = require("../ui/manager.cjs");
|
|
8
|
+
const require_thread = require("./thread.cjs");
|
|
9
|
+
let react = require("react");
|
|
10
|
+
//#region src/react/stream.custom.tsx
|
|
11
|
+
var FetchStreamTransport = class {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
}
|
|
15
|
+
async stream(payload) {
|
|
16
|
+
const { signal, ...body } = payload;
|
|
17
|
+
let requestInit = {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: {
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
...this.options.defaultHeaders
|
|
22
|
+
},
|
|
23
|
+
body: JSON.stringify(body),
|
|
24
|
+
signal
|
|
25
|
+
};
|
|
26
|
+
if (this.options.onRequest) requestInit = await this.options.onRequest(this.options.apiUrl, requestInit);
|
|
27
|
+
const response = await (this.options.fetch ?? fetch)(this.options.apiUrl, requestInit);
|
|
28
|
+
if (!response.ok) throw new Error(`Failed to stream: ${response.statusText}`);
|
|
29
|
+
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(require_sse.BytesLineDecoder()).pipeThrough(require_sse.SSEDecoder());
|
|
30
|
+
return require_stream.IterableReadableStream.fromReadableStream(stream);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function useStreamCustom(options) {
|
|
34
|
+
const [messageManager] = (0, react.useState)(() => new require_messages.MessageTupleManager());
|
|
35
|
+
const [stream] = (0, react.useState)(() => new require_manager.StreamManager(messageManager, {
|
|
36
|
+
throttle: options.throttle ?? false,
|
|
37
|
+
subagentToolNames: options.subagentToolNames,
|
|
38
|
+
filterSubagentMessages: options.filterSubagentMessages
|
|
39
|
+
}));
|
|
40
|
+
(0, react.useSyncExternalStore)(stream.subscribe, stream.getSnapshot, stream.getSnapshot);
|
|
41
|
+
const [threadId, onThreadId] = require_thread.useControllableThreadId(options);
|
|
42
|
+
const threadIdRef = (0, react.useRef)(threadId);
|
|
43
|
+
(0, react.useEffect)(() => {
|
|
44
|
+
if (threadIdRef.current !== threadId) {
|
|
45
|
+
threadIdRef.current = threadId;
|
|
46
|
+
stream.clear();
|
|
47
|
+
}
|
|
48
|
+
}, [threadId, stream]);
|
|
49
|
+
const getMessages = (value) => {
|
|
50
|
+
const messagesKey = options.messagesKey ?? "messages";
|
|
51
|
+
return Array.isArray(value[messagesKey]) ? value[messagesKey] : [];
|
|
52
|
+
};
|
|
53
|
+
const setMessages = (current, messages) => {
|
|
54
|
+
const messagesKey = options.messagesKey ?? "messages";
|
|
55
|
+
return {
|
|
56
|
+
...current,
|
|
57
|
+
[messagesKey]: messages
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const historyValues = options.initialValues ?? {};
|
|
61
|
+
const historyMessages = getMessages(historyValues);
|
|
62
|
+
const shouldReconstructSubagents = options.filterSubagentMessages && !stream.isLoading && historyMessages.length > 0;
|
|
63
|
+
(0, react.useEffect)(() => {
|
|
64
|
+
if (shouldReconstructSubagents) stream.reconstructSubagents(historyMessages, { skipIfPopulated: true });
|
|
65
|
+
}, [shouldReconstructSubagents, historyMessages.length]);
|
|
66
|
+
const stop = () => stream.stop(historyValues, { onStop: options.onStop });
|
|
67
|
+
const submit = async (values, submitOptions) => {
|
|
68
|
+
let usableThreadId = threadId;
|
|
69
|
+
stream.setStreamValues(() => {
|
|
70
|
+
if (submitOptions?.optimisticValues != null) return {
|
|
71
|
+
...historyValues,
|
|
72
|
+
...typeof submitOptions.optimisticValues === "function" ? submitOptions.optimisticValues(historyValues) : submitOptions.optimisticValues
|
|
73
|
+
};
|
|
74
|
+
return { ...historyValues };
|
|
75
|
+
});
|
|
76
|
+
await stream.start(async (signal) => {
|
|
77
|
+
if (!usableThreadId) {
|
|
78
|
+
usableThreadId = crypto.randomUUID();
|
|
79
|
+
threadIdRef.current = usableThreadId;
|
|
80
|
+
onThreadId(usableThreadId);
|
|
81
|
+
}
|
|
82
|
+
if (!usableThreadId) throw new Error("Failed to obtain valid thread ID.");
|
|
83
|
+
return options.transport.stream({
|
|
84
|
+
input: values,
|
|
85
|
+
context: submitOptions?.context,
|
|
86
|
+
command: submitOptions?.command,
|
|
87
|
+
signal,
|
|
88
|
+
config: {
|
|
89
|
+
...submitOptions?.config,
|
|
90
|
+
configurable: {
|
|
91
|
+
thread_id: usableThreadId,
|
|
92
|
+
...submitOptions?.config?.configurable
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}, {
|
|
97
|
+
getMessages,
|
|
98
|
+
setMessages,
|
|
99
|
+
initialValues: {},
|
|
100
|
+
callbacks: options,
|
|
101
|
+
onSuccess: () => void 0,
|
|
102
|
+
onError(error) {
|
|
103
|
+
options.onError?.(error, void 0);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
get values() {
|
|
109
|
+
return stream.values ?? {};
|
|
110
|
+
},
|
|
111
|
+
error: stream.error,
|
|
112
|
+
isLoading: stream.isLoading,
|
|
113
|
+
stop,
|
|
114
|
+
submit,
|
|
115
|
+
get interrupts() {
|
|
116
|
+
if (stream.values != null && "__interrupt__" in stream.values && Array.isArray(stream.values.__interrupt__)) {
|
|
117
|
+
const valueInterrupts = stream.values.__interrupt__;
|
|
118
|
+
if (valueInterrupts.length === 0) return [{ when: "breakpoint" }];
|
|
119
|
+
return valueInterrupts;
|
|
120
|
+
}
|
|
121
|
+
return [];
|
|
122
|
+
},
|
|
123
|
+
get interrupt() {
|
|
124
|
+
const all = this.interrupts;
|
|
125
|
+
if (all.length === 0) return void 0;
|
|
126
|
+
if (all.length === 1) return all[0];
|
|
127
|
+
return all;
|
|
128
|
+
},
|
|
129
|
+
get messages() {
|
|
130
|
+
if (!stream.values) return [];
|
|
131
|
+
return getMessages(stream.values);
|
|
132
|
+
},
|
|
133
|
+
get toolCalls() {
|
|
134
|
+
if (!stream.values) return [];
|
|
135
|
+
return require_tools.getToolCallsWithResults(getMessages(stream.values));
|
|
136
|
+
},
|
|
137
|
+
getToolCalls(message) {
|
|
138
|
+
if (!stream.values) return [];
|
|
139
|
+
return require_tools.getToolCallsWithResults(getMessages(stream.values)).filter((tc) => tc.aiMessage.id === message.id);
|
|
140
|
+
},
|
|
141
|
+
get subagents() {
|
|
142
|
+
return stream.getSubagents();
|
|
143
|
+
},
|
|
144
|
+
get activeSubagents() {
|
|
145
|
+
return stream.getActiveSubagents();
|
|
146
|
+
},
|
|
147
|
+
getSubagent(toolCallId) {
|
|
148
|
+
return stream.getSubagent(toolCallId);
|
|
149
|
+
},
|
|
150
|
+
getSubagentsByType(type) {
|
|
151
|
+
return stream.getSubagentsByType(type);
|
|
152
|
+
},
|
|
153
|
+
getSubagentsByMessage(messageId) {
|
|
154
|
+
return stream.getSubagentsByMessage(messageId);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
exports.FetchStreamTransport = FetchStreamTransport;
|
|
160
|
+
exports.useStreamCustom = useStreamCustom;
|
|
161
|
+
|
|
162
|
+
//# sourceMappingURL=stream.custom.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.custom.cjs","names":["BytesLineDecoder","SSEDecoder","IterableReadableStream","MessageTupleManager","StreamManager","useControllableThreadId","getToolCallsWithResults"],"sources":["../../src/react/stream.custom.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport { EventStreamEvent, StreamManager } from \"../ui/manager.js\";\nimport type {\n GetUpdateType,\n GetCustomEventType,\n GetInterruptType,\n GetToolCallsType,\n GetConfigurableType,\n UseStreamTransport,\n AnyStreamCustomOptions,\n CustomSubmitOptions,\n} from \"../ui/types.js\";\nimport type { UseStreamCustom } from \"./types.js\";\nimport { type Message } from \"../types.messages.js\";\nimport { getToolCallsWithResults } from \"../utils/tools.js\";\nimport { MessageTupleManager } from \"../ui/messages.js\";\nimport { Interrupt } from \"../schema.js\";\nimport { BytesLineDecoder, SSEDecoder } from \"../utils/sse.js\";\nimport { IterableReadableStream } from \"../utils/stream.js\";\nimport { useControllableThreadId } from \"./thread.js\";\nimport { Command } from \"../types.js\";\nimport type { BagTemplate } from \"../types.template.js\";\n\ninterface FetchStreamTransportOptions {\n /**\n * The URL of the API to use.\n */\n apiUrl: string;\n\n /**\n * Default headers to send with requests.\n */\n defaultHeaders?: HeadersInit;\n\n /**\n * Specify a custom fetch implementation.\n */\n fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any\n\n /**\n * Callback that is called before the request is made.\n */\n onRequest?: (\n url: string,\n init: RequestInit\n ) => Promise<RequestInit> | RequestInit;\n}\n\nexport class FetchStreamTransport<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n> implements UseStreamTransport<StateType, Bag>\n{\n constructor(private readonly options: FetchStreamTransportOptions) {}\n\n async stream(payload: {\n input: GetUpdateType<Bag, StateType> | null | undefined;\n context: GetConfigurableType<Bag> | undefined;\n command: Command | undefined;\n signal: AbortSignal;\n }): Promise<AsyncGenerator<{ id?: string; event: string; data: unknown }>> {\n const { signal, ...body } = payload;\n\n let requestInit: RequestInit = {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.options.defaultHeaders,\n },\n body: JSON.stringify(body),\n signal,\n };\n\n if (this.options.onRequest) {\n requestInit = await this.options.onRequest(\n this.options.apiUrl,\n requestInit\n );\n }\n const fetchFn = this.options.fetch ?? fetch;\n\n const response = await fetchFn(this.options.apiUrl, requestInit);\n if (!response.ok) {\n throw new Error(`Failed to stream: ${response.statusText}`);\n }\n\n const stream = (\n response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })\n )\n .pipeThrough(BytesLineDecoder())\n .pipeThrough(SSEDecoder());\n\n return IterableReadableStream.fromReadableStream(stream);\n }\n}\n\nexport function useStreamCustom<\n StateType extends Record<string, unknown> = Record<string, unknown>,\n Bag extends BagTemplate = BagTemplate\n>(\n options: AnyStreamCustomOptions<StateType, Bag>\n): UseStreamCustom<StateType, Bag> {\n type UpdateType = GetUpdateType<Bag, StateType>;\n type CustomType = GetCustomEventType<Bag>;\n type InterruptType = GetInterruptType<Bag>;\n type ConfigurableType = GetConfigurableType<Bag>;\n type ToolCallType = GetToolCallsType<StateType>;\n\n const [messageManager] = useState(() => new MessageTupleManager());\n const [stream] = useState(\n () =>\n new StreamManager<StateType, Bag>(messageManager, {\n throttle: options.throttle ?? false,\n subagentToolNames: options.subagentToolNames,\n filterSubagentMessages: options.filterSubagentMessages,\n })\n );\n\n useSyncExternalStore(\n stream.subscribe,\n stream.getSnapshot,\n stream.getSnapshot\n );\n\n const [threadId, onThreadId] = useControllableThreadId(options);\n const threadIdRef = useRef<string | null>(threadId);\n\n // Cancel the stream if thread ID has changed\n useEffect(() => {\n if (threadIdRef.current !== threadId) {\n threadIdRef.current = threadId;\n stream.clear();\n }\n }, [threadId, stream]);\n\n const getMessages = (value: StateType): Message[] => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return Array.isArray(value[messagesKey])\n ? (value[messagesKey] as Message[])\n : [];\n };\n\n const setMessages = (current: StateType, messages: Message[]): StateType => {\n const messagesKey = options.messagesKey ?? \"messages\";\n return { ...current, [messagesKey]: messages };\n };\n\n const historyValues = options.initialValues ?? ({} as StateType);\n\n // Reconstruct subagents from initialValues when:\n // 1. Subagent filtering is enabled\n // 2. Not currently streaming\n // 3. initialValues has messages\n // This ensures subagent visualization works with cached/persisted state\n const historyMessages = getMessages(historyValues);\n const shouldReconstructSubagents =\n options.filterSubagentMessages &&\n !stream.isLoading &&\n historyMessages.length > 0;\n\n useEffect(() => {\n if (shouldReconstructSubagents) {\n // skipIfPopulated: true ensures we don't overwrite subagents from active streaming\n stream.reconstructSubagents(historyMessages, { skipIfPopulated: true });\n }\n // We intentionally only run this when shouldReconstructSubagents changes\n // to avoid unnecessary reconstructions during streaming\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [shouldReconstructSubagents, historyMessages.length]);\n\n const stop = () => stream.stop(historyValues, { onStop: options.onStop });\n\n const submit = async (\n values: UpdateType | null | undefined,\n submitOptions?: CustomSubmitOptions<StateType, ConfigurableType>\n ) => {\n let usableThreadId = threadId;\n\n stream.setStreamValues(() => {\n if (submitOptions?.optimisticValues != null) {\n return {\n ...historyValues,\n ...(typeof submitOptions.optimisticValues === \"function\"\n ? submitOptions.optimisticValues(historyValues)\n : submitOptions.optimisticValues),\n };\n }\n\n return { ...historyValues };\n });\n\n await stream.start(\n async (signal: AbortSignal) => {\n if (!usableThreadId) {\n // generate random thread id\n usableThreadId = crypto.randomUUID();\n threadIdRef.current = usableThreadId;\n onThreadId(usableThreadId);\n }\n\n if (!usableThreadId) {\n throw new Error(\"Failed to obtain valid thread ID.\");\n }\n\n return options.transport.stream({\n input: values,\n context: submitOptions?.context,\n command: submitOptions?.command,\n signal,\n config: {\n ...submitOptions?.config,\n configurable: {\n thread_id: usableThreadId,\n ...submitOptions?.config?.configurable,\n } as unknown as GetConfigurableType<Bag>,\n },\n }) as Promise<\n AsyncGenerator<EventStreamEvent<StateType, UpdateType, CustomType>>\n >;\n },\n {\n getMessages,\n setMessages,\n\n initialValues: {} as StateType,\n callbacks: options,\n\n onSuccess: () => undefined,\n onError(error) {\n options.onError?.(error, undefined);\n },\n }\n );\n };\n\n return {\n get values() {\n return stream.values ?? ({} as StateType);\n },\n\n error: stream.error,\n isLoading: stream.isLoading,\n\n stop,\n submit,\n\n get interrupts(): Interrupt<InterruptType>[] {\n if (\n stream.values != null &&\n \"__interrupt__\" in stream.values &&\n Array.isArray(stream.values.__interrupt__)\n ) {\n const valueInterrupts = stream.values.__interrupt__;\n if (valueInterrupts.length === 0) return [{ when: \"breakpoint\" }];\n return valueInterrupts;\n }\n\n return [];\n },\n\n get interrupt(): Interrupt<InterruptType> | undefined {\n const all = this.interrupts;\n if (all.length === 0) return undefined;\n if (all.length === 1) return all[0];\n\n // Multiple interrupts: return the array for backward compat\n return all as Interrupt<InterruptType>;\n },\n\n get messages(): Message<ToolCallType>[] {\n if (!stream.values) return [];\n return getMessages(stream.values);\n },\n\n get toolCalls() {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n return getToolCallsWithResults<ToolCallType>(msgs);\n },\n\n getToolCalls(message) {\n if (!stream.values) return [];\n const msgs = getMessages(stream.values);\n const allToolCalls = getToolCallsWithResults<ToolCallType>(msgs);\n return allToolCalls.filter((tc) => tc.aiMessage.id === message.id);\n },\n\n get subagents() {\n return stream.getSubagents();\n },\n\n get activeSubagents() {\n return stream.getActiveSubagents();\n },\n\n getSubagent(toolCallId: string) {\n return stream.getSubagent(toolCallId);\n },\n\n getSubagentsByType(type: string) {\n return stream.getSubagentsByType(type);\n },\n\n getSubagentsByMessage(messageId: string) {\n return stream.getSubagentsByMessage(messageId);\n },\n };\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Command } from "../types.cjs";
|
|
2
|
+
import { BagTemplate } from "../types.template.cjs";
|
|
3
|
+
import { GetConfigurableType, GetUpdateType, UseStreamTransport } from "../ui/types.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/react/stream.custom.d.ts
|
|
6
|
+
interface FetchStreamTransportOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The URL of the API to use.
|
|
9
|
+
*/
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
/**
|
|
12
|
+
* Default headers to send with requests.
|
|
13
|
+
*/
|
|
14
|
+
defaultHeaders?: HeadersInit;
|
|
15
|
+
/**
|
|
16
|
+
* Specify a custom fetch implementation.
|
|
17
|
+
*/
|
|
18
|
+
fetch?: typeof fetch | ((...args: any[]) => any);
|
|
19
|
+
/**
|
|
20
|
+
* Callback that is called before the request is made.
|
|
21
|
+
*/
|
|
22
|
+
onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;
|
|
23
|
+
}
|
|
24
|
+
declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {
|
|
25
|
+
private readonly options;
|
|
26
|
+
constructor(options: FetchStreamTransportOptions);
|
|
27
|
+
stream(payload: {
|
|
28
|
+
input: GetUpdateType<Bag, StateType> | null | undefined;
|
|
29
|
+
context: GetConfigurableType<Bag> | undefined;
|
|
30
|
+
command: Command | undefined;
|
|
31
|
+
signal: AbortSignal;
|
|
32
|
+
}): Promise<AsyncGenerator<{
|
|
33
|
+
id?: string;
|
|
34
|
+
event: string;
|
|
35
|
+
data: unknown;
|
|
36
|
+
}>>;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { FetchStreamTransport };
|
|
40
|
+
//# sourceMappingURL=stream.custom.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.custom.d.cts","names":[],"sources":["../../src/react/stream.custom.tsx"],"mappings":";;;;;UA2BU,2BAAA;EAAA;;;EAIR,MAAA;EAUe;;;EALf,cAAA,GAAiB,WAAA;EAaW;;;EAR5B,KAAA,UAAe,KAAA,QAAa,IAAA;EAL5B;;;EAUA,SAAA,IACE,GAAA,UACA,IAAA,EAAM,WAAA,KACH,OAAA,CAAQ,WAAA,IAAe,WAAA;AAAA;AAAA,cAGjB,oBAAA,mBACO,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,aACf,kBAAA,CAAmB,SAAA,EAAW,GAAA;EAAA,iBAEZ,OAAA;EAA7B,WAAA,CAA6B,OAAA,EAAS,2BAAA;EAEhC,MAAA,CAAO,OAAA;IACX,KAAA,EAAO,aAAA,CAAc,GAAA,EAAK,SAAA;IAC1B,OAAA,EAAS,mBAAA,CAAoB,GAAA;IAC7B,OAAA,EAAS,OAAA;IACT,MAAA,EAAQ,WAAA;EAAA,IACN,OAAA,CAAQ,cAAA;IAAiB,EAAA;IAAa,KAAA;IAAe,IAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Command } from "../types.js";
|
|
2
|
+
import { BagTemplate } from "../types.template.js";
|
|
3
|
+
import { GetConfigurableType, GetUpdateType, UseStreamTransport } from "../ui/types.js";
|
|
4
|
+
//#region src/react/stream.custom.d.ts
|
|
5
|
+
interface FetchStreamTransportOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The URL of the API to use.
|
|
8
|
+
*/
|
|
9
|
+
apiUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* Default headers to send with requests.
|
|
12
|
+
*/
|
|
13
|
+
defaultHeaders?: HeadersInit;
|
|
14
|
+
/**
|
|
15
|
+
* Specify a custom fetch implementation.
|
|
16
|
+
*/
|
|
17
|
+
fetch?: typeof fetch | ((...args: any[]) => any);
|
|
18
|
+
/**
|
|
19
|
+
* Callback that is called before the request is made.
|
|
20
|
+
*/
|
|
21
|
+
onRequest?: (url: string, init: RequestInit) => Promise<RequestInit> | RequestInit;
|
|
22
|
+
}
|
|
23
|
+
declare class FetchStreamTransport<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> implements UseStreamTransport<StateType, Bag> {
|
|
24
|
+
private readonly options;
|
|
25
|
+
constructor(options: FetchStreamTransportOptions);
|
|
26
|
+
stream(payload: {
|
|
27
|
+
input: GetUpdateType<Bag, StateType> | null | undefined;
|
|
28
|
+
context: GetConfigurableType<Bag> | undefined;
|
|
29
|
+
command: Command | undefined;
|
|
30
|
+
signal: AbortSignal;
|
|
31
|
+
}): Promise<AsyncGenerator<{
|
|
32
|
+
id?: string;
|
|
33
|
+
event: string;
|
|
34
|
+
data: unknown;
|
|
35
|
+
}>>;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { FetchStreamTransport };
|
|
39
|
+
//# sourceMappingURL=stream.custom.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.custom.d.ts","names":[],"sources":["../../src/react/stream.custom.tsx"],"mappings":";;;;UA2BU,2BAAA;;AAF8C;;EAMtD,MAAA;EAKiB;;;EAAjB,cAAA,GAAiB,WAAA;EAaZ;;;EARL,KAAA,UAAe,KAAA,QAAa,IAAA;EAV5B;;;EAeA,SAAA,IACE,GAAA,UACA,IAAA,EAAM,WAAA,KACH,OAAA,CAAQ,WAAA,IAAe,WAAA;AAAA;AAAA,cAGjB,oBAAA,mBACO,MAAA,oBAA0B,MAAA,+BAChC,WAAA,GAAc,WAAA,aACf,kBAAA,CAAmB,SAAA,EAAW,GAAA;EAAA,iBAEZ,OAAA;EAA7B,WAAA,CAA6B,OAAA,EAAS,2BAAA;EAEhC,MAAA,CAAO,OAAA;IACX,KAAA,EAAO,aAAA,CAAc,GAAA,EAAK,SAAA;IAC1B,OAAA,EAAS,mBAAA,CAAoB,GAAA;IAC7B,OAAA,EAAS,OAAA;IACT,MAAA,EAAQ,WAAA;EAAA,IACN,OAAA,CAAQ,cAAA;IAAiB,EAAA;IAAa,KAAA;IAAe,IAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { BytesLineDecoder, SSEDecoder } from "../utils/sse.js";
|
|
3
|
+
import { IterableReadableStream } from "../utils/stream.js";
|
|
4
|
+
import { MessageTupleManager } from "../ui/messages.js";
|
|
5
|
+
import { getToolCallsWithResults } from "../utils/tools.js";
|
|
6
|
+
import { StreamManager } from "../ui/manager.js";
|
|
7
|
+
import { useControllableThreadId } from "./thread.js";
|
|
8
|
+
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
9
|
+
//#region src/react/stream.custom.tsx
|
|
10
|
+
var FetchStreamTransport = class {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
async stream(payload) {
|
|
15
|
+
const { signal, ...body } = payload;
|
|
16
|
+
let requestInit = {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: {
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
...this.options.defaultHeaders
|
|
21
|
+
},
|
|
22
|
+
body: JSON.stringify(body),
|
|
23
|
+
signal
|
|
24
|
+
};
|
|
25
|
+
if (this.options.onRequest) requestInit = await this.options.onRequest(this.options.apiUrl, requestInit);
|
|
26
|
+
const response = await (this.options.fetch ?? fetch)(this.options.apiUrl, requestInit);
|
|
27
|
+
if (!response.ok) throw new Error(`Failed to stream: ${response.statusText}`);
|
|
28
|
+
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(BytesLineDecoder()).pipeThrough(SSEDecoder());
|
|
29
|
+
return IterableReadableStream.fromReadableStream(stream);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function useStreamCustom(options) {
|
|
33
|
+
const [messageManager] = useState(() => new MessageTupleManager());
|
|
34
|
+
const [stream] = useState(() => new StreamManager(messageManager, {
|
|
35
|
+
throttle: options.throttle ?? false,
|
|
36
|
+
subagentToolNames: options.subagentToolNames,
|
|
37
|
+
filterSubagentMessages: options.filterSubagentMessages
|
|
38
|
+
}));
|
|
39
|
+
useSyncExternalStore(stream.subscribe, stream.getSnapshot, stream.getSnapshot);
|
|
40
|
+
const [threadId, onThreadId] = useControllableThreadId(options);
|
|
41
|
+
const threadIdRef = useRef(threadId);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (threadIdRef.current !== threadId) {
|
|
44
|
+
threadIdRef.current = threadId;
|
|
45
|
+
stream.clear();
|
|
46
|
+
}
|
|
47
|
+
}, [threadId, stream]);
|
|
48
|
+
const getMessages = (value) => {
|
|
49
|
+
const messagesKey = options.messagesKey ?? "messages";
|
|
50
|
+
return Array.isArray(value[messagesKey]) ? value[messagesKey] : [];
|
|
51
|
+
};
|
|
52
|
+
const setMessages = (current, messages) => {
|
|
53
|
+
const messagesKey = options.messagesKey ?? "messages";
|
|
54
|
+
return {
|
|
55
|
+
...current,
|
|
56
|
+
[messagesKey]: messages
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const historyValues = options.initialValues ?? {};
|
|
60
|
+
const historyMessages = getMessages(historyValues);
|
|
61
|
+
const shouldReconstructSubagents = options.filterSubagentMessages && !stream.isLoading && historyMessages.length > 0;
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (shouldReconstructSubagents) stream.reconstructSubagents(historyMessages, { skipIfPopulated: true });
|
|
64
|
+
}, [shouldReconstructSubagents, historyMessages.length]);
|
|
65
|
+
const stop = () => stream.stop(historyValues, { onStop: options.onStop });
|
|
66
|
+
const submit = async (values, submitOptions) => {
|
|
67
|
+
let usableThreadId = threadId;
|
|
68
|
+
stream.setStreamValues(() => {
|
|
69
|
+
if (submitOptions?.optimisticValues != null) return {
|
|
70
|
+
...historyValues,
|
|
71
|
+
...typeof submitOptions.optimisticValues === "function" ? submitOptions.optimisticValues(historyValues) : submitOptions.optimisticValues
|
|
72
|
+
};
|
|
73
|
+
return { ...historyValues };
|
|
74
|
+
});
|
|
75
|
+
await stream.start(async (signal) => {
|
|
76
|
+
if (!usableThreadId) {
|
|
77
|
+
usableThreadId = crypto.randomUUID();
|
|
78
|
+
threadIdRef.current = usableThreadId;
|
|
79
|
+
onThreadId(usableThreadId);
|
|
80
|
+
}
|
|
81
|
+
if (!usableThreadId) throw new Error("Failed to obtain valid thread ID.");
|
|
82
|
+
return options.transport.stream({
|
|
83
|
+
input: values,
|
|
84
|
+
context: submitOptions?.context,
|
|
85
|
+
command: submitOptions?.command,
|
|
86
|
+
signal,
|
|
87
|
+
config: {
|
|
88
|
+
...submitOptions?.config,
|
|
89
|
+
configurable: {
|
|
90
|
+
thread_id: usableThreadId,
|
|
91
|
+
...submitOptions?.config?.configurable
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}, {
|
|
96
|
+
getMessages,
|
|
97
|
+
setMessages,
|
|
98
|
+
initialValues: {},
|
|
99
|
+
callbacks: options,
|
|
100
|
+
onSuccess: () => void 0,
|
|
101
|
+
onError(error) {
|
|
102
|
+
options.onError?.(error, void 0);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
get values() {
|
|
108
|
+
return stream.values ?? {};
|
|
109
|
+
},
|
|
110
|
+
error: stream.error,
|
|
111
|
+
isLoading: stream.isLoading,
|
|
112
|
+
stop,
|
|
113
|
+
submit,
|
|
114
|
+
get interrupts() {
|
|
115
|
+
if (stream.values != null && "__interrupt__" in stream.values && Array.isArray(stream.values.__interrupt__)) {
|
|
116
|
+
const valueInterrupts = stream.values.__interrupt__;
|
|
117
|
+
if (valueInterrupts.length === 0) return [{ when: "breakpoint" }];
|
|
118
|
+
return valueInterrupts;
|
|
119
|
+
}
|
|
120
|
+
return [];
|
|
121
|
+
},
|
|
122
|
+
get interrupt() {
|
|
123
|
+
const all = this.interrupts;
|
|
124
|
+
if (all.length === 0) return void 0;
|
|
125
|
+
if (all.length === 1) return all[0];
|
|
126
|
+
return all;
|
|
127
|
+
},
|
|
128
|
+
get messages() {
|
|
129
|
+
if (!stream.values) return [];
|
|
130
|
+
return getMessages(stream.values);
|
|
131
|
+
},
|
|
132
|
+
get toolCalls() {
|
|
133
|
+
if (!stream.values) return [];
|
|
134
|
+
return getToolCallsWithResults(getMessages(stream.values));
|
|
135
|
+
},
|
|
136
|
+
getToolCalls(message) {
|
|
137
|
+
if (!stream.values) return [];
|
|
138
|
+
return getToolCallsWithResults(getMessages(stream.values)).filter((tc) => tc.aiMessage.id === message.id);
|
|
139
|
+
},
|
|
140
|
+
get subagents() {
|
|
141
|
+
return stream.getSubagents();
|
|
142
|
+
},
|
|
143
|
+
get activeSubagents() {
|
|
144
|
+
return stream.getActiveSubagents();
|
|
145
|
+
},
|
|
146
|
+
getSubagent(toolCallId) {
|
|
147
|
+
return stream.getSubagent(toolCallId);
|
|
148
|
+
},
|
|
149
|
+
getSubagentsByType(type) {
|
|
150
|
+
return stream.getSubagentsByType(type);
|
|
151
|
+
},
|
|
152
|
+
getSubagentsByMessage(messageId) {
|
|
153
|
+
return stream.getSubagentsByMessage(messageId);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { FetchStreamTransport, useStreamCustom };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=stream.custom.js.map
|