@truefoundry/trueforge-assistant-ui-runtime 0.0.0 → 0.2.0-rc.0
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/CHANGELOG.md +9 -0
- package/LICENSE +201 -0
- package/README.md +146 -4
- package/dist/chunk-2SQK6TIO.js +104 -0
- package/dist/chunk-2SQK6TIO.js.map +1 -0
- package/dist/index.d.ts +378 -0
- package/dist/index.js +4391 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.d.ts +1210 -0
- package/dist/server/index.js +9 -0
- package/dist/server/index.js.map +1 -0
- package/package.json +79 -16
- package/src/askUserQuestion.ts +38 -0
- package/src/attachmentAdapter.ts +63 -0
- package/src/collectPending.ts +167 -0
- package/src/constants.ts +2 -0
- package/src/convertTurnMessages.ts +1679 -0
- package/src/createSubAgent.ts +11 -0
- package/src/draft/agentSpec.ts +34 -0
- package/src/draft/draftSessionBridge.ts +28 -0
- package/src/draft/trueforgeDraftThreadListAdapter.ts +73 -0
- package/src/draft/useDraftAgentSpec.ts +289 -0
- package/src/extractTurnUserText.ts +23 -0
- package/src/foldPeerThreads.ts +553 -0
- package/src/hooks.ts +176 -0
- package/src/index.ts +227 -0
- package/src/lastUserMessageText.ts +19 -0
- package/src/listPages.ts +19 -0
- package/src/loadSessionSnapshot.ts +34 -0
- package/src/mcpAuth.ts +35 -0
- package/src/messageCustomMetadata.ts +50 -0
- package/src/modelMessageContent.ts +149 -0
- package/src/modelMessageImageContent.ts +154 -0
- package/src/requiredActionInputs.ts +38 -0
- package/src/sandboxDownload.ts +33 -0
- package/src/server/eventUtils.ts +125 -0
- package/src/server/events.ts +232 -0
- package/src/server/index.ts +178 -0
- package/src/server/types.ts +1191 -0
- package/src/sessionListStartTimestamp.ts +6 -0
- package/src/sessionSnapshot.ts +146 -0
- package/src/sessionThreadMetadata.ts +36 -0
- package/src/sessions.ts +17 -0
- package/src/streamTurn.ts +118 -0
- package/src/toolApproval.ts +413 -0
- package/src/toolResponse.ts +346 -0
- package/src/trueforgeExtras.ts +223 -0
- package/src/trueforgeOwnedSessionsThreadListAdapter.ts +71 -0
- package/src/trueforgeThreadListAdapter.ts +69 -0
- package/src/turnEventHelpers.ts +71 -0
- package/src/turnStreamUpdate.ts +11 -0
- package/src/types.ts +84 -0
- package/src/useTrueForgeAgentMessages.ts +1138 -0
- package/src/useTrueForgeAgentRuntime.ts +308 -0
- package/index.js +0 -6
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @truefoundry/trueforge-assistant-ui-runtime
|
|
2
|
+
|
|
3
|
+
## 0.2.0-rc.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a855122: Rename the published runtime package to `@truefoundry/trueforge-assistant-ui-runtime`, move it into the TrueForge workspace, rename its public runtime APIs to TrueForge, and remove the legacy TrueFoundry server adapter and server configuration.
|
|
8
|
+
- 829ac6e: Add OSS web-search provider settings and catalog (Parallel): singleton settings/catalog APIs, optional API key, and UI adapter without mode config so built-in web search works outside TrueFoundry mode.
|
|
9
|
+
- 829ac6e: Add web-search provider settings catalog port and Settings UI so admins can configure Parallel web search (API key + mode) in standalone/OIDC deployments.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,7 +1,149 @@
|
|
|
1
|
-
# trueforge-assistant-ui-runtime
|
|
1
|
+
# @truefoundry/trueforge-assistant-ui-runtime
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Headless React runtime that maps TrueForge sessions, turns, streaming events, tool approvals, and sub-agent threads onto assistant-ui's external-store runtime.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
The package accepts a ready `AgentChatServer`. It does not construct backend clients, read credentials, or depend on a backend SDK.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @truefoundry/trueforge-assistant-ui-runtime @assistant-ui/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
React 18 and 19 are supported. `@assistant-ui/core` and `@assistant-ui/store` are installed with the runtime.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
'use client';
|
|
19
|
+
|
|
20
|
+
import { AssistantRuntimeProvider } from '@assistant-ui/react';
|
|
21
|
+
import { useTrueForgeAgentRuntime, type AgentChatServer } from '@truefoundry/trueforge-assistant-ui-runtime';
|
|
22
|
+
|
|
23
|
+
function Chat({ server }: { server: AgentChatServer }) {
|
|
24
|
+
const runtime = useTrueForgeAgentRuntime({
|
|
25
|
+
server,
|
|
26
|
+
agent: { mode: 'named', agentName: 'support-agent' },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<AssistantRuntimeProvider runtime={runtime}>
|
|
31
|
+
{/* Render assistant-ui Thread, Composer, and ThreadList primitives here. */}
|
|
32
|
+
</AssistantRuntimeProvider>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For the complete TrueForge UI, use `@truefoundry/trueforge-ui`. It supplies the built-in TrueForge HTTP adapter and re-exports the host-facing server contracts.
|
|
38
|
+
|
|
39
|
+
## Agent modes
|
|
40
|
+
|
|
41
|
+
Named agents use an existing server-side agent:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import type { TrueForgeAgentConfig } from '@truefoundry/trueforge-assistant-ui-runtime';
|
|
45
|
+
|
|
46
|
+
const agent = {
|
|
47
|
+
mode: 'named',
|
|
48
|
+
agentName: 'support-agent',
|
|
49
|
+
} satisfies TrueForgeAgentConfig;
|
|
7
50
|
```
|
|
51
|
+
|
|
52
|
+
Draft agents run from an inline spec and synchronize edits through the server:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import type { TrueForgeAgentConfig } from '@truefoundry/trueforge-assistant-ui-runtime';
|
|
56
|
+
|
|
57
|
+
const agent = {
|
|
58
|
+
mode: 'draft',
|
|
59
|
+
defaultAgentSpec: {
|
|
60
|
+
model: { name: 'openai-main/gpt-4.1' },
|
|
61
|
+
instructions: 'Answer concisely.',
|
|
62
|
+
},
|
|
63
|
+
} satisfies TrueForgeAgentConfig;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`agentName` remains available as shorthand for named mode.
|
|
67
|
+
|
|
68
|
+
## Runtime options
|
|
69
|
+
|
|
70
|
+
`useTrueForgeAgentRuntime` accepts assistant-ui's external-store options plus:
|
|
71
|
+
|
|
72
|
+
| Option | Purpose |
|
|
73
|
+
| ------------------------------- | ----------------------------------------------------------- |
|
|
74
|
+
| `server` | Ready `AgentChatServer` implementation |
|
|
75
|
+
| `agent` | Named or draft agent configuration |
|
|
76
|
+
| `agentName` | Legacy named-agent shorthand |
|
|
77
|
+
| `initialSessionId` | Initial session to load |
|
|
78
|
+
| `threadId` / `onThreadIdChange` | Controlled active session |
|
|
79
|
+
| `listSessionsAgentId` | Optional history filter |
|
|
80
|
+
| `listSessionsCreatedByMe` | Restrict history to the authenticated subject |
|
|
81
|
+
| `onError` | Load, turn, and stream error callback |
|
|
82
|
+
| `adapters` | Attachment, speech, dictation, voice, and feedback adapters |
|
|
83
|
+
|
|
84
|
+
## Runtime extras
|
|
85
|
+
|
|
86
|
+
The package exposes typed hooks for TrueForge-specific state and actions:
|
|
87
|
+
|
|
88
|
+
- `useTrueForgeApprovals`
|
|
89
|
+
- `useTrueForgeToolResponses`
|
|
90
|
+
- `useTrueForgeMcpAuth`
|
|
91
|
+
- `useTrueForgeRespondToToolApproval`
|
|
92
|
+
- `useTrueForgeRespondToToolResponse`
|
|
93
|
+
- `useTrueForgeResumeMcpAuth`
|
|
94
|
+
- `useTrueForgeDownloadSandboxFile`
|
|
95
|
+
- `useTrueForgeCancel`
|
|
96
|
+
- `useTrueForgeHistoryPagination`
|
|
97
|
+
- `useTrueForgeResumeUnavailable`
|
|
98
|
+
- `useTrueForgeResetFromTurn`
|
|
99
|
+
- `useTrueForgeAgentSpec`
|
|
100
|
+
- `useTrueForgeUpdateAgentSpec`
|
|
101
|
+
- `useTrueForgeFlushAgentSpec`
|
|
102
|
+
- `useTrueForgeAdoptAgentSpec`
|
|
103
|
+
|
|
104
|
+
Use `trueForgeExtras`, `getTrueForgeExtras`, and `tryGetTrueForgeExtras` when integrating directly with assistant-ui state.
|
|
105
|
+
|
|
106
|
+
## Server contracts
|
|
107
|
+
|
|
108
|
+
Canonical server ports, DTOs, and stream events are exported from both the package root and the dedicated server entry:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import type {
|
|
112
|
+
AgentChatServer,
|
|
113
|
+
AgentUIServer,
|
|
114
|
+
Session,
|
|
115
|
+
Turn,
|
|
116
|
+
TurnStreamingEvent,
|
|
117
|
+
} from '@truefoundry/trueforge-assistant-ui-runtime/server';
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Important invariants:
|
|
121
|
+
|
|
122
|
+
- One server session maps to one assistant-ui thread.
|
|
123
|
+
- The root thread id is always `main`.
|
|
124
|
+
- Sub-agent threads nest below their creating tool call.
|
|
125
|
+
- Resuming a paused turn submits all pending approvals and tool responses across every thread in one request.
|
|
126
|
+
- Credentials remain host-owned.
|
|
127
|
+
|
|
128
|
+
## Attachments
|
|
129
|
+
|
|
130
|
+
`trueForgeAttachmentAdapter` converts assistant-ui attachment input into TrueForge user-message content. Hosts can override it through the runtime `adapters` option.
|
|
131
|
+
|
|
132
|
+
## Package exports
|
|
133
|
+
|
|
134
|
+
- `@truefoundry/trueforge-assistant-ui-runtime`
|
|
135
|
+
- `@truefoundry/trueforge-assistant-ui-runtime/server`
|
|
136
|
+
|
|
137
|
+
## Development
|
|
138
|
+
|
|
139
|
+
Run from the trueforge repository root:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
pnpm --filter @truefoundry/trueforge-assistant-ui-runtime build
|
|
143
|
+
pnpm test:assistant-ui-runtime
|
|
144
|
+
pnpm --filter @truefoundry/trueforge-assistant-ui-runtime typecheck
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
MIT
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/server/eventUtils.ts
|
|
2
|
+
function isEventDelta(event) {
|
|
3
|
+
return typeof event.type === "string" && event.type.endsWith(".delta");
|
|
4
|
+
}
|
|
5
|
+
function mergeEventDelta(base, delta) {
|
|
6
|
+
if (base.id !== delta.id) {
|
|
7
|
+
throw new Error(`Cannot merge delta into a different event: base id "${base.id}" != delta id "${delta.id}".`);
|
|
8
|
+
}
|
|
9
|
+
if (base.type === "model.message") {
|
|
10
|
+
mergeModelMessageDelta(base, delta);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function asToolInfo(value) {
|
|
14
|
+
if (value == null || typeof value !== "object") {
|
|
15
|
+
return void 0;
|
|
16
|
+
}
|
|
17
|
+
const type = Reflect.get(value, "type");
|
|
18
|
+
if (typeof type !== "string") {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
const name = Reflect.get(value, "name");
|
|
22
|
+
if (type === "trueforge-system") {
|
|
23
|
+
return typeof name === "string" ? { type, name } : void 0;
|
|
24
|
+
}
|
|
25
|
+
if (type === "mcp") {
|
|
26
|
+
const serverId = Reflect.get(value, "serverId");
|
|
27
|
+
const serverName = Reflect.get(value, "serverName");
|
|
28
|
+
return typeof serverId === "string" && typeof serverName === "string" && typeof name === "string" ? { type, serverId, serverName, name } : void 0;
|
|
29
|
+
}
|
|
30
|
+
return typeof name === "string" ? { type, name } : { type };
|
|
31
|
+
}
|
|
32
|
+
function mergeModelMessageDelta(base, delta) {
|
|
33
|
+
if (delta.content) {
|
|
34
|
+
if (base.content === void 0 || base.content === null || typeof base.content === "string") {
|
|
35
|
+
base.content = (base.content ?? "") + delta.content;
|
|
36
|
+
} else {
|
|
37
|
+
const last = base.content[base.content.length - 1];
|
|
38
|
+
if (last?.type === "text") {
|
|
39
|
+
last.text += delta.content;
|
|
40
|
+
} else {
|
|
41
|
+
base.content.push({ type: "text", text: delta.content });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (delta.refusal) {
|
|
46
|
+
base.refusal = (base.refusal ?? "") + delta.refusal;
|
|
47
|
+
}
|
|
48
|
+
if (delta.toolCalls) {
|
|
49
|
+
base.toolCalls ??= [];
|
|
50
|
+
for (const d of delta.toolCalls) {
|
|
51
|
+
let tc = base.toolCalls[d.index];
|
|
52
|
+
if (tc === void 0) {
|
|
53
|
+
const toolInfo2 = asToolInfo(d.toolInfo);
|
|
54
|
+
tc = {
|
|
55
|
+
id: d.id ?? "",
|
|
56
|
+
type: d.type ?? "function",
|
|
57
|
+
function: {
|
|
58
|
+
name: d.function?.name ?? "",
|
|
59
|
+
arguments: ""
|
|
60
|
+
},
|
|
61
|
+
...toolInfo2 != null ? { toolInfo: toolInfo2 } : {}
|
|
62
|
+
};
|
|
63
|
+
base.toolCalls[d.index] = tc;
|
|
64
|
+
}
|
|
65
|
+
if (d.id) {
|
|
66
|
+
tc.id = d.id;
|
|
67
|
+
}
|
|
68
|
+
if (d.type) {
|
|
69
|
+
tc.type = d.type;
|
|
70
|
+
}
|
|
71
|
+
if (d.function?.name) {
|
|
72
|
+
tc.function.name = d.function.name;
|
|
73
|
+
}
|
|
74
|
+
if (d.function?.arguments) {
|
|
75
|
+
tc.function.arguments += d.function.arguments;
|
|
76
|
+
}
|
|
77
|
+
const toolInfo = asToolInfo(d.toolInfo);
|
|
78
|
+
if (toolInfo != null) {
|
|
79
|
+
tc.toolInfo = toolInfo;
|
|
80
|
+
}
|
|
81
|
+
if (d.providerSpecificFields) {
|
|
82
|
+
tc.providerSpecificFields = {
|
|
83
|
+
...tc.providerSpecificFields ?? {},
|
|
84
|
+
...d.providerSpecificFields
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (delta.finishReason) {
|
|
90
|
+
base.finishReason = delta.finishReason;
|
|
91
|
+
}
|
|
92
|
+
if (delta.reasoningContent) {
|
|
93
|
+
base.reasoningContent = (base.reasoningContent ?? "") + delta.reasoningContent;
|
|
94
|
+
}
|
|
95
|
+
if (delta.usage) {
|
|
96
|
+
base.usage = delta.usage;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export {
|
|
101
|
+
isEventDelta,
|
|
102
|
+
mergeEventDelta
|
|
103
|
+
};
|
|
104
|
+
//# sourceMappingURL=chunk-2SQK6TIO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server/eventUtils.ts"],"sourcesContent":["/**\n * Local implementations of streaming delta helpers.\n * Formerly imported from trueforge-gateway-sdk/agents.\n */\n\nimport type {\n DeltaEvents,\n ModelMessageDeltaEvent,\n ModelMessageEvent,\n ToolCall,\n ToolInfo,\n TurnEvent,\n TurnStreamingEvent,\n} from './events.js';\n\n/** True for `.delta` streaming events. */\nexport function isEventDelta(event: TurnStreamingEvent): event is DeltaEvents {\n return typeof event.type === 'string' && event.type.endsWith('.delta');\n}\n\n/**\n * Merge `delta` into `base` in place (same `id` required).\n * Currently handles `model.message.delta` → `model.message`.\n */\nexport function mergeEventDelta(base: TurnEvent, delta: DeltaEvents): void {\n if (base.id !== delta.id) {\n throw new Error(`Cannot merge delta into a different event: base id \"${base.id}\" != delta id \"${delta.id}\".`);\n }\n if (base.type === 'model.message') {\n mergeModelMessageDelta(base, delta);\n }\n}\n\nfunction asToolInfo(value: unknown): ToolInfo | undefined {\n if (value == null || typeof value !== 'object') {\n return undefined;\n }\n const type: unknown = Reflect.get(value, 'type');\n if (typeof type !== 'string') {\n return undefined;\n }\n const name: unknown = Reflect.get(value, 'name');\n if (type === 'trueforge-system') {\n return typeof name === 'string' ? { type, name } : undefined;\n }\n if (type === 'mcp') {\n const serverId: unknown = Reflect.get(value, 'serverId');\n const serverName: unknown = Reflect.get(value, 'serverName');\n return typeof serverId === 'string' && typeof serverName === 'string' && typeof name === 'string'\n ? { type, serverId, serverName, name }\n : undefined;\n }\n return typeof name === 'string' ? { type, name } : { type };\n}\n\nfunction mergeModelMessageDelta(base: ModelMessageEvent, delta: ModelMessageDeltaEvent): void {\n if (delta.content) {\n if (base.content === undefined || base.content === null || typeof base.content === 'string') {\n base.content = (base.content ?? '') + delta.content;\n } else {\n const last = base.content[base.content.length - 1];\n if (last?.type === 'text') {\n last.text += delta.content;\n } else {\n base.content.push({ type: 'text', text: delta.content });\n }\n }\n }\n\n if (delta.refusal) {\n base.refusal = (base.refusal ?? '') + delta.refusal;\n }\n\n if (delta.toolCalls) {\n base.toolCalls ??= [];\n for (const d of delta.toolCalls) {\n let tc: ToolCall | undefined = base.toolCalls[d.index];\n if (tc === undefined) {\n const toolInfo = asToolInfo(d.toolInfo);\n tc = {\n id: d.id ?? '',\n type: d.type ?? 'function',\n function: {\n name: d.function?.name ?? '',\n arguments: '',\n },\n ...(toolInfo != null ? { toolInfo } : {}),\n };\n base.toolCalls[d.index] = tc;\n }\n if (d.id) {\n tc.id = d.id;\n }\n if (d.type) {\n tc.type = d.type;\n }\n if (d.function?.name) {\n tc.function.name = d.function.name;\n }\n if (d.function?.arguments) {\n tc.function.arguments += d.function.arguments;\n }\n const toolInfo = asToolInfo(d.toolInfo);\n if (toolInfo != null) {\n tc.toolInfo = toolInfo;\n }\n if (d.providerSpecificFields) {\n tc.providerSpecificFields = {\n ...(tc.providerSpecificFields ?? {}),\n ...d.providerSpecificFields,\n };\n }\n }\n }\n\n if (delta.finishReason) {\n base.finishReason = delta.finishReason;\n }\n if (delta.reasoningContent) {\n base.reasoningContent = (base.reasoningContent ?? '') + delta.reasoningContent;\n }\n if (delta.usage) {\n base.usage = delta.usage;\n }\n}\n"],"mappings":"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","names":["toolInfo"]}
|