@jini-ai/chat 0.7.2 → 0.7.3-next.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/CHANGELOG.md +2 -2
- package/README.md +3 -3
- package/dist/contracts/chat-bubble-style.d.ts +1 -1
- package/dist/contracts/chat-bubble-style.js +1 -1
- package/dist/core/ag-ui/event-projection.d.ts +125 -0
- package/dist/core/ag-ui/event-projection.js +275 -0
- package/dist/core/ag-ui/event-reduction.d.ts +39 -0
- package/dist/core/ag-ui/event-reduction.js +99 -0
- package/dist/core/ag-ui/run-input.d.ts +53 -0
- package/dist/core/ag-ui/run-input.js +87 -0
- package/dist/core/agentic/chat-capabilities.d.ts +1 -1
- package/dist/core/agentic/index.d.ts +2 -4
- package/dist/core/agentic/index.js +2 -3
- package/dist/core/agents-port.d.ts +50 -0
- package/dist/core/agents-port.js +2 -0
- package/dist/core/assistant-client/ag-ui-fetch-agent.d.ts +16 -0
- package/dist/core/assistant-client/ag-ui-fetch-agent.js +24 -0
- package/dist/core/assistant-client/ag-ui-transport.d.ts +15 -1
- package/dist/core/assistant-client/ag-ui-transport.js +167 -145
- package/dist/core/assistant-client/attachment-liveness.js +13 -11
- package/dist/core/assistant-client/durable-run-subscription.d.ts +3 -2
- package/dist/core/assistant-client/durable-run-subscription.js +6 -5
- package/dist/core/assistant-client/messages.d.ts +7 -4
- package/dist/core/assistant-client/messages.js +5 -4
- package/dist/core/assistant-client/transport.d.ts +1 -1
- package/dist/core/assistant-client/transport.js +4 -4
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +1 -0
- package/dist/core/page-port.d.ts +15 -0
- package/dist/core/page-port.js +2 -0
- package/dist/core/run-activity.js +1 -1
- package/dist/core/run-events/entry.js +1 -0
- package/dist/core/user-text-redaction.d.ts +1 -3
- package/dist/core/user-text-redaction.js +3 -61
- package/dist/embed-widget/browser/agent-inventory.d.ts +37 -0
- package/dist/embed-widget/browser/agent-inventory.js +36 -0
- package/dist/embed-widget/browser/frontend-session-bridge.d.ts +2 -2
- package/dist/embed-widget/browser/frontend-session-bridge.js +17 -14
- package/dist/embed-widget/react/dialog.hooks.d.ts +16 -0
- package/dist/embed-widget/react/dialog.hooks.js +15 -0
- package/dist/embed-widget/react/embed.d.ts +7 -2
- package/dist/embed-widget/react/embed.js +6 -16
- package/dist/embed-widget/react/header.d.ts +4 -1
- package/dist/embed-widget/react/header.js +2 -2
- package/dist/embed-widget/vanilla/state.js +2 -2
- package/dist/embed-widget/vanilla/style.css +4 -4
- package/dist/react/components/A2uiSurfaceCard.d.ts +18 -4
- package/dist/react/components/A2uiSurfaceCard.hooks.d.ts +36 -0
- package/dist/react/components/A2uiSurfaceCard.hooks.js +174 -0
- package/dist/react/components/A2uiSurfaceCard.js +14 -227
- package/dist/react/components/A2uiSurfaceCard.rules.d.ts +14 -0
- package/dist/react/components/A2uiSurfaceCard.rules.js +24 -0
- package/dist/react/components/AttachmentPreviewModal.d.ts +21 -1
- package/dist/react/components/AttachmentPreviewModal.hooks.js +1 -3
- package/dist/react/components/AttachmentPreviewModal.js +11 -9
- package/dist/react/components/AttachmentTray.d.ts +11 -1
- package/dist/react/components/AttachmentTray.js +12 -8
- package/dist/react/components/ChatFab.d.ts +10 -1
- package/dist/react/components/ChatFab.js +2 -2
- package/dist/react/components/ChatSettingsDialog.d.ts +4 -3
- package/dist/react/components/ChatSettingsDialog.js +9 -6
- package/dist/react/components/ChatSettingsSegmented.d.ts +1 -1
- package/dist/react/components/ChatSettingsSegmented.js +1 -1
- package/dist/react/components/Composer.d.ts +9 -1
- package/dist/react/components/Composer.hooks.d.ts +79 -0
- package/dist/react/components/Composer.hooks.js +336 -0
- package/dist/react/components/Composer.js +40 -341
- package/dist/react/components/ComposerDiscovery.js +1 -1
- package/dist/react/components/ConversationList.d.ts +34 -1
- package/dist/react/components/ConversationList.hooks.d.ts +37 -0
- package/dist/react/components/ConversationList.hooks.js +177 -1
- package/dist/react/components/ConversationList.js +7 -174
- package/dist/react/components/ConversationTitleEditor.d.ts +17 -2
- package/dist/react/components/ConversationTitleEditor.hooks.d.ts +15 -0
- package/dist/react/components/ConversationTitleEditor.hooks.js +36 -0
- package/dist/react/components/ConversationTitleEditor.js +11 -34
- package/dist/react/components/CopyMessageButton.d.ts +6 -1
- package/dist/react/components/CopyMessageButton.js +4 -4
- package/dist/react/components/ExtEventErrorBoundary.d.ts +2 -0
- package/dist/react/components/ExtEventErrorBoundary.js +5 -3
- package/dist/react/components/FolderChips.d.ts +11 -1
- package/dist/react/components/FolderChips.hooks.d.ts +12 -0
- package/dist/react/components/FolderChips.hooks.js +10 -0
- package/dist/react/components/FolderChips.js +9 -9
- package/dist/react/components/Markdown.js +7 -5
- package/dist/react/components/McpUiSurfaceCard.d.ts +29 -5
- package/dist/react/components/McpUiSurfaceCard.js +29 -16
- package/dist/react/components/MessageList.d.ts +10 -1
- package/dist/react/components/MessageList.hooks.js +4 -2
- package/dist/react/components/MessageList.js +12 -8
- package/dist/react/components/MessageRow.d.ts +46 -2
- package/dist/react/components/MessageRow.hooks.d.ts +60 -0
- package/dist/react/components/MessageRow.hooks.js +94 -0
- package/dist/react/components/MessageRow.js +53 -113
- package/dist/react/components/MidRunUserMessage.d.ts +6 -1
- package/dist/react/components/MidRunUserMessage.js +5 -5
- package/dist/react/components/NextStepActions.d.ts +3 -1
- package/dist/react/components/NextStepActions.js +3 -3
- package/dist/react/components/QuestionForm.answers.hooks.d.ts +26 -0
- package/dist/react/components/QuestionForm.answers.hooks.js +89 -0
- package/dist/react/components/QuestionForm.d.ts +6 -0
- package/dist/react/components/QuestionForm.js +23 -261
- package/dist/react/components/QuestionForm.values.d.ts +22 -0
- package/dist/react/components/QuestionForm.values.js +58 -0
- package/dist/react/components/QuestionFormField.d.ts +17 -0
- package/dist/react/components/QuestionFormField.js +114 -0
- package/dist/react/components/QuestionsPanel.d.ts +3 -1
- package/dist/react/components/QuestionsPanel.js +7 -3
- package/dist/react/components/SentAttachment.d.ts +11 -2
- package/dist/react/components/SentAttachment.hooks.d.ts +1 -1
- package/dist/react/components/SentAttachment.hooks.js +6 -5
- package/dist/react/components/SentAttachment.js +5 -3
- package/dist/react/components/TodoCard.d.ts +3 -1
- package/dist/react/components/TodoCard.js +9 -4
- package/dist/react/components/ToolCallGroup.d.ts +11 -1
- package/dist/react/components/ToolCallGroup.js +10 -4
- package/dist/react/components/ToolCard.d.ts +6 -1
- package/dist/react/components/ToolCard.js +79 -69
- package/dist/react/components/mcp-ui-question-input.d.ts +1 -1
- package/dist/react/components/mcp-ui-question-input.js +3 -3
- package/dist/react/components/mcp-ui-surface-state.d.ts +1 -1
- package/dist/react/features/chat-pane/agent-tools.d.ts +1 -1
- package/dist/react/features/chat-pane/antigravity/model-options.d.ts +2 -0
- package/dist/react/features/chat-pane/antigravity/model-options.js +2 -0
- package/dist/react/features/chat-pane/claude/model-options.d.ts +2 -0
- package/dist/react/features/chat-pane/claude/model-options.js +2 -0
- package/dist/react/features/chat-pane/codex/model-options.d.ts +2 -0
- package/dist/react/features/chat-pane/codex/model-options.js +2 -0
- package/dist/react/features/chat-pane/components/AgentPluginUsage.d.ts +4 -1
- package/dist/react/features/chat-pane/components/AgentPluginUsage.js +2 -2
- package/dist/react/features/chat-pane/components/AgentRuntimePicker.d.ts +10 -6
- package/dist/react/features/chat-pane/components/AgentRuntimePicker.js +18 -132
- package/dist/react/features/chat-pane/components/ChatPane.d.ts +24 -2
- package/dist/react/features/chat-pane/components/ChatPane.js +45 -297
- package/dist/react/features/chat-pane/components/ChatPaneComposerArea.d.ts +33 -0
- package/dist/react/features/chat-pane/components/ChatPaneComposerArea.js +119 -0
- package/dist/react/features/chat-pane/components/ChatPaneLayout.d.ts +42 -0
- package/dist/react/features/chat-pane/components/ChatPaneLayout.js +54 -0
- package/dist/react/features/chat-pane/components/ChatPaneStatus.d.ts +27 -0
- package/dist/react/features/chat-pane/components/ChatPaneStatus.js +91 -0
- package/dist/react/features/chat-pane/components/RuntimeDropUp.d.ts +6 -1
- package/dist/react/features/chat-pane/components/RuntimeDropUp.js +20 -19
- package/dist/react/features/chat-pane/components/RuntimePickerSections.d.ts +62 -0
- package/dist/react/features/chat-pane/components/RuntimePickerSections.js +122 -0
- package/dist/react/features/chat-pane/create-mcp-ui-tool-caller.d.ts +1 -1
- package/dist/react/features/chat-pane/hooks/useChatPane.hooks.d.ts +5 -3
- package/dist/react/features/chat-pane/hooks/useChatPane.hooks.js +78 -26
- package/dist/react/features/chat-pane/hooks/useChatPaneAgentControl.hooks.js +6 -6
- package/dist/react/features/chat-pane/hooks/useChatPaneFileDrop.hooks.d.ts +5 -1
- package/dist/react/features/chat-pane/hooks/useChatPaneFileDrop.hooks.js +7 -7
- package/dist/react/features/chat-pane/hooks/useRuntimeDropUp.hooks.d.ts +7 -0
- package/dist/react/features/chat-pane/hooks/useRuntimeDropUp.hooks.js +5 -0
- package/dist/react/features/chat-pane/model-options.js +7 -4
- package/dist/react/features/chat-pane/styles.d.ts +1 -1
- package/dist/react/features/chat-pane/styles.js +14 -14
- package/dist/react/features/chat-pane/types.d.ts +29 -1
- package/dist/react/hooks/assistant-chats-contracts.d.ts +1 -1
- package/dist/react/hooks/attachment-preview-cache.d.ts +1 -1
- package/dist/react/hooks/attachment-preview-cache.js +1 -1
- package/dist/react/hooks/fab-position.js +16 -18
- package/dist/react/hooks/useConversation.d.ts +6 -1
- package/dist/react/hooks/useConversation.js +11 -10
- package/dist/react/hooks/useRunStream.d.ts +3 -0
- package/dist/react/hooks/useRunStream.js +12 -12
- package/dist/react/hooks/useSurfaceRecovery.d.ts +1 -1
- package/dist/react/hooks/useSurfaceRecovery.js +2 -2
- package/dist/react/index.d.ts +6 -3
- package/dist/react/index.js +5 -3
- package/dist/react/styles/chat-settings.css +9 -9
- package/dist/react/surfaces.d.ts +91 -0
- package/dist/react/surfaces.js +28 -0
- package/dist/server/durable-runs/recover.d.ts +4 -3
- package/dist/server/durable-runs/recover.js +4 -3
- package/dist/store/legacy/sqlite/conversations.d.ts +1 -2
- package/dist/store/legacy/sqlite/conversations.js +1 -1
- package/dist/store/legacy/sqlite/messages.d.ts +2 -3
- package/dist/store/legacy/sqlite/messages.js +6 -6
- package/dist/store/pglite/index.d.ts +7 -7
- package/dist/store/pglite/index.js +1 -1
- package/dist/store/ports.d.ts +5 -2
- package/dist/store/postgres/index.d.ts +7 -7
- package/dist/store/postgres/index.js +1 -1
- package/dist/store/runtime/chat-expiry-sweep.js +2 -4
- package/dist/store/runtime/chat-history-store.js +2 -3
- package/dist/store/runtime/durable-run-store.js +16 -2
- package/dist/store/runtime/history-tools.js +33 -11
- package/dist/store/runtime/kernel.d.ts +3 -2
- package/dist/store/runtime/run-ledger.js +2 -2
- package/dist/store/sql/boundary.d.ts +5 -5
- package/dist/store/sql/maintenance.d.ts +3 -2
- package/dist/store/sql/store.d.ts +16 -8
- package/dist/store/sql/store.js +31 -14
- package/dist/store/sqlite/index.d.ts +11 -12
- package/dist/store/sqlite/index.js +5 -5
- package/dist/store/sqlite/schema.d.ts +1 -1
- package/package.json +49 -27
- package/dist/core/ag-ui/entry.d.ts +0 -84
- package/dist/core/ag-ui/entry.js +0 -225
- package/dist/core/browser/page-actions.d.ts +0 -121
- package/dist/core/browser/page-actions.js +0 -113
- package/dist/embed-widget/browser/page-actions.d.ts +0 -2
- package/dist/embed-widget/browser/page-actions.js +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -212,7 +212,7 @@
|
|
|
212
212
|
caller's behalf; a caller that still wants that inference should resolve a manifest for the file
|
|
213
213
|
itself (see `@jini-ai/chat`'s `inferLegacyManifest`) before handing it to this registry. Verified
|
|
214
214
|
zero external callers of `RendererRegistry`/`resolveArtifactManifest`/`createDefaultRendererRegistry`
|
|
215
|
-
anywhere in Jini or
|
|
215
|
+
anywhere in Jini or the first host before making this change — `@jini-ai/chat`'s own `RendererRegistry` is an
|
|
216
216
|
unrelated, locally-defined class in `artifact-types.ts`, and `@jini-ai/artifacts`' own
|
|
217
217
|
`resolveArtifactManifest` is an unrelated 3-argument function; both are same-name coincidences, not
|
|
218
218
|
real dependents. The four bundled renderers (`HtmlRenderer`, `MarkdownRenderer`, `SvgRenderer`,
|
|
@@ -294,7 +294,7 @@
|
|
|
294
294
|
|
|
295
295
|
## 0.5.5 — 2026-10-08
|
|
296
296
|
|
|
297
|
-
-
|
|
297
|
+
- Host clean-up release: code the first host moved into Jini, plus the Jini clean-up (see the commit log).
|
|
298
298
|
|
|
299
299
|
## 0.5.4 — 2026-10-06
|
|
300
300
|
|
package/README.md
CHANGED
|
@@ -129,9 +129,9 @@ All database and React peers are optional; Kysely dev tests pin 0.29.6 and PGlit
|
|
|
129
129
|
|
|
130
130
|
```ts
|
|
131
131
|
import { createSqliteChatStore } from '@jini-ai/chat/store/sqlite';
|
|
132
|
-
const store = createSqliteChatStore({ kernel, scope: {
|
|
132
|
+
const store = createSqliteChatStore({ kernel, clock, scope: {
|
|
133
133
|
scopeId: 'workspace', ownerKind: 'user', ownerId: 'account',
|
|
134
|
-
} });
|
|
134
|
+
} }); // `clock` is a core Clock, e.g. createSystemClock() from @jini-ai/platform/system
|
|
135
135
|
const page = await store.pageConversations({}, { limit: 50 });
|
|
136
136
|
const conversation = await store.get({ id: 'conversation' });
|
|
137
137
|
await store.appendMessage({ conversationId: 'conversation', message });
|
|
@@ -197,7 +197,7 @@ required argument objects: e.g. `isTerminalRunStatus({ status })`,
|
|
|
197
197
|
`registerExtEventRenderer({ name, renderer }, { slotKey })`. A slot-key callback receives `{ data }`.
|
|
198
198
|
`interleaveMessageBlocks({ events, content, rows }, { slotOf })` retains its reconstruction checks.
|
|
199
199
|
The isolated `core/run-events`, `core/ag-ui`, `server/run-finalizer`, `react/embed`,
|
|
200
|
-
`transports/fetch-sse`, `browser/session-store` and `browser/frontend-session-bridge` subpaths are published. Page control uses the shared frontend bridge and page executor; the retired directive executor and cross-page action queue have been removed.
|
|
200
|
+
`transports/fetch-sse`, `browser/session-store`, `browser/agent-inventory` (last-known agent list as a fail-soft placeholder) and `browser/frontend-session-bridge` subpaths are published. Page control uses the shared frontend bridge and page executor; the retired directive executor and cross-page action queue have been removed.
|
|
201
201
|
`FrontendSessionBridgeArgs` is exported from `react`; hosts pass baseUrl and openStream, optionally request or native fetch.
|
|
202
202
|
The SSE transport takes an injected decoder; browser hosts must supply a browser-safe module.
|
|
203
203
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** Shared bubble styles for server placeholders and mounted DOM widgets; no runtime dependencies.
|
|
2
2
|
* Consent can suspend floating UI without moving a tall open panel beyond the viewport. */
|
|
3
|
-
export declare const CHAT_BUBBLE_STYLE = ".jini-chat-vanilla{display:var(--jini-floating-ui-display,block)}.jini-chat-bubble{position:fixed;right:var(--jini-embed-right,24px);bottom:24px;width:56px;height:56px;border:0;border-radius:50%;background:var(--jini-chat-accent
|
|
3
|
+
export declare const CHAT_BUBBLE_STYLE = ".jini-chat-vanilla{display:var(--jini-floating-ui-display,block)}.jini-chat-bubble{position:fixed;right:var(--jini-embed-right,24px);bottom:24px;width:56px;height:56px;border:0;border-radius:50%;background:var(--jini-chat-accent,#2563eb);color:var(--jini-chat-accent-text,#fff);box-shadow:0 4px 16px #0003;cursor:pointer;z-index:var(--jini-embed-z-index,1000);display:var(--jini-floating-ui-display,grid);place-items:center}\n.jini-chat-bubble[hidden]{display:none!important}\n.jini-chat-bubble svg{width:24px;height:24px}\n.jini-chat-bubble:focus-visible{outline:3px solid var(--jini-chat-accent,#2563eb);outline-offset:3px}\n@media(max-width:480px){.jini-chat-bubble{right:16px;bottom:16px}}\n.jini-chat-bubble[aria-busy=\"true\"]{cursor:progress;opacity:.65}\n.jini-chat-bubble[data-load-failed=\"true\"]{outline:2px solid var(--jini-chat-error,#b91c1c);outline-offset:3px}";
|
|
4
4
|
//# sourceMappingURL=chat-bubble-style.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** Shared bubble styles for server placeholders and mounted DOM widgets; no runtime dependencies.
|
|
2
2
|
* Consent can suspend floating UI without moving a tall open panel beyond the viewport. */
|
|
3
|
-
export const CHAT_BUBBLE_STYLE = '.jini-chat-vanilla{display:var(--jini-floating-ui-display,block)}.jini-chat-bubble{position:fixed;right:var(--jini-embed-right,24px);bottom:24px;width:56px;height:56px;border:0;border-radius:50%;background:var(--jini-chat-accent
|
|
3
|
+
export const CHAT_BUBBLE_STYLE = '.jini-chat-vanilla{display:var(--jini-floating-ui-display,block)}.jini-chat-bubble{position:fixed;right:var(--jini-embed-right,24px);bottom:24px;width:56px;height:56px;border:0;border-radius:50%;background:var(--jini-chat-accent,#2563eb);color:var(--jini-chat-accent-text,#fff);box-shadow:0 4px 16px #0003;cursor:pointer;z-index:var(--jini-embed-z-index,1000);display:var(--jini-floating-ui-display,grid);place-items:center}\n.jini-chat-bubble[hidden]{display:none!important}\n.jini-chat-bubble svg{width:24px;height:24px}\n.jini-chat-bubble:focus-visible{outline:3px solid var(--jini-chat-accent,#2563eb);outline-offset:3px}\n@media(max-width:480px){.jini-chat-bubble{right:16px;bottom:16px}}\n.jini-chat-bubble[aria-busy="true"]{cursor:progress;opacity:.65}\n.jini-chat-bubble[data-load-failed="true"]{outline:2px solid var(--jini-chat-error,#b91c1c);outline-offset:3px}';
|
|
4
4
|
//# sourceMappingURL=chat-bubble-style.js.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** Pure chat-event to AG-UI projection; lifecycle state belongs to one run.
|
|
2
|
+
* Daemon payloads are reduced to chat events by `run-events` `translateRunAgentPayload`, the one
|
|
3
|
+
* reducer; this file only projects. AG-UI has no standard field for a tool call's display
|
|
4
|
+
* correlation or for a tool result's error flag, media and outcome, so those ride as extra fields
|
|
5
|
+
* under their chat-event names (the official event schemas are passthrough); `event-reduction.ts`
|
|
6
|
+
* reads them back. Strict interruption ordering closes a message before a tool starts. IDs and custom event names
|
|
7
|
+
* are injected, with official AG-UI types authoritative; auth, routes and encoder stay with the host.
|
|
8
|
+
* The host emits RUN_STARTED and terminal RUN_FINISHED/RUN_ERROR outside this pure projection. */
|
|
9
|
+
import { EventType, type AGUIEvent, type Interrupt } from '@ag-ui/core';
|
|
10
|
+
import type { AgentEvent } from '../events.js';
|
|
11
|
+
import { type RunNotices } from '../run-events/entry.js';
|
|
12
|
+
export interface EventIdGenerator {
|
|
13
|
+
next(args: {
|
|
14
|
+
prefix: string;
|
|
15
|
+
ordinal: number;
|
|
16
|
+
}): string;
|
|
17
|
+
}
|
|
18
|
+
export interface CustomEventNames {
|
|
19
|
+
readonly usage: string;
|
|
20
|
+
readonly status: string;
|
|
21
|
+
readonly extensionPrefix: string;
|
|
22
|
+
}
|
|
23
|
+
export type AgUiEvent = Extract<AGUIEvent, {
|
|
24
|
+
type: EventType.RUN_STARTED | EventType.RUN_FINISHED | EventType.RUN_ERROR | EventType.TEXT_MESSAGE_START | EventType.TEXT_MESSAGE_CONTENT | EventType.TEXT_MESSAGE_END | EventType.REASONING_START | EventType.REASONING_MESSAGE_START | EventType.REASONING_MESSAGE_CONTENT | EventType.REASONING_MESSAGE_END | EventType.REASONING_END | EventType.TOOL_CALL_START | EventType.TOOL_CALL_ARGS | EventType.TOOL_CALL_END | EventType.TOOL_CALL_RESULT | EventType.RAW | EventType.CUSTOM;
|
|
25
|
+
}>;
|
|
26
|
+
export interface AgUiTranslationState {
|
|
27
|
+
openTextMessageId: string | null;
|
|
28
|
+
openReasoningMessageId: string | null;
|
|
29
|
+
nextId: number;
|
|
30
|
+
readonly ids: EventIdGenerator;
|
|
31
|
+
readonly customEventNames: CustomEventNames;
|
|
32
|
+
}
|
|
33
|
+
/** Create isolated per-run translation state with explicit IDs and custom-event wire names. */
|
|
34
|
+
export declare function createAgUiTranslationState({ ids, customEventNames }: {
|
|
35
|
+
ids: EventIdGenerator;
|
|
36
|
+
customEventNames: CustomEventNames;
|
|
37
|
+
}): AgUiTranslationState;
|
|
38
|
+
/** Emit ordered message/tool lifecycles; a tool call closes the preceding message first. */
|
|
39
|
+
export declare function translateAgentEventToAgUi({ event, state }: {
|
|
40
|
+
event: AgentEvent;
|
|
41
|
+
state: AgUiTranslationState;
|
|
42
|
+
}): AgUiEvent[];
|
|
43
|
+
/** Close open text/reasoning before the host emits RUN_FINISHED or RUN_ERROR. */
|
|
44
|
+
export declare function closeAgUiRun({ state }: {
|
|
45
|
+
state: AgUiTranslationState;
|
|
46
|
+
}): AgUiEvent[];
|
|
47
|
+
interface AgUiFrameContext {
|
|
48
|
+
reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
49
|
+
threadId: string;
|
|
50
|
+
runId: string;
|
|
51
|
+
state: AgUiTranslationState;
|
|
52
|
+
notices: RunNotices;
|
|
53
|
+
write: (event: AgUiEvent) => void;
|
|
54
|
+
}
|
|
55
|
+
/** Drains one AG-UI run's daemon frames to completion, writing translated events directly to
|
|
56
|
+
* `ctx.res` and ending the response either on an explicit terminal frame, on the stream simply
|
|
57
|
+
* closing (same dual-completion posture `consumeByokStream` uses for the identical case), or on a
|
|
58
|
+
* read error. Split out of `handleAgUiRun` so the try/catch and its loop don't add to that
|
|
59
|
+
* function's own branch count.
|
|
60
|
+
* @returns How the run ended, for its agent-run span: a terminal frame's own status (an `end` the
|
|
61
|
+
* daemon classified as failed or canceled is that, not `succeeded`); a natural close counts as
|
|
62
|
+
* `succeeded`, like the `RUN_FINISHED` it writes; a read error is `failed` with that error. */
|
|
63
|
+
export declare function drainRunFramesToAgUi(ctx: AgUiFrameContext): Promise<{
|
|
64
|
+
status: "succeeded" | "failed" | "canceled";
|
|
65
|
+
error?: unknown;
|
|
66
|
+
}>;
|
|
67
|
+
export declare function confirmationInterrupt({ threadId, runId, interrupt }: {
|
|
68
|
+
threadId: string;
|
|
69
|
+
runId: string;
|
|
70
|
+
interrupt: Interrupt;
|
|
71
|
+
}, _optional?: Record<string, never>): Extract<AgUiEvent, {
|
|
72
|
+
type: EventType.RUN_FINISHED;
|
|
73
|
+
}>;
|
|
74
|
+
export declare function readConfirmationResume({ input, threadId, runId, interruptId }: {
|
|
75
|
+
input: unknown;
|
|
76
|
+
threadId: string;
|
|
77
|
+
runId: string;
|
|
78
|
+
interruptId: string;
|
|
79
|
+
}, _optional?: Record<string, never>): {
|
|
80
|
+
decision: 'allow' | 'deny';
|
|
81
|
+
} | undefined;
|
|
82
|
+
export declare function createAgUiPauseStore({ caps, nowMs }: {
|
|
83
|
+
caps: {
|
|
84
|
+
global: number;
|
|
85
|
+
perScope: number;
|
|
86
|
+
};
|
|
87
|
+
nowMs: () => number;
|
|
88
|
+
}, _optional?: Record<string, never>): {
|
|
89
|
+
park(entry: Parameters<(key: string, value: {
|
|
90
|
+
owner: string;
|
|
91
|
+
threadId: string;
|
|
92
|
+
runId: string;
|
|
93
|
+
interruptId: string;
|
|
94
|
+
state: unknown;
|
|
95
|
+
scopeKey: string;
|
|
96
|
+
abandonAtMs: number;
|
|
97
|
+
}) => Map<string, {
|
|
98
|
+
owner: string;
|
|
99
|
+
threadId: string;
|
|
100
|
+
runId: string;
|
|
101
|
+
interruptId: string;
|
|
102
|
+
state: unknown;
|
|
103
|
+
scopeKey: string;
|
|
104
|
+
abandonAtMs: number;
|
|
105
|
+
}>>[1], _optional?: Record<string, never>): boolean;
|
|
106
|
+
claim({ owner, threadId, runId, interruptId }: {
|
|
107
|
+
owner: string;
|
|
108
|
+
threadId: string;
|
|
109
|
+
runId: string;
|
|
110
|
+
interruptId: string;
|
|
111
|
+
}, _optional?: Record<string, never>): {
|
|
112
|
+
owner: string;
|
|
113
|
+
threadId: string;
|
|
114
|
+
runId: string;
|
|
115
|
+
interruptId: string;
|
|
116
|
+
state: unknown;
|
|
117
|
+
scopeKey: string;
|
|
118
|
+
abandonAtMs: number;
|
|
119
|
+
} | undefined;
|
|
120
|
+
remove({ interruptId }: {
|
|
121
|
+
interruptId: string;
|
|
122
|
+
}, _optional?: Record<string, never>): boolean;
|
|
123
|
+
};
|
|
124
|
+
export {};
|
|
125
|
+
//# sourceMappingURL=event-projection.d.ts.map
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/** Pure chat-event to AG-UI projection; lifecycle state belongs to one run.
|
|
2
|
+
* Daemon payloads are reduced to chat events by `run-events` `translateRunAgentPayload`, the one
|
|
3
|
+
* reducer; this file only projects. AG-UI has no standard field for a tool call's display
|
|
4
|
+
* correlation or for a tool result's error flag, media and outcome, so those ride as extra fields
|
|
5
|
+
* under their chat-event names (the official event schemas are passthrough); `event-reduction.ts`
|
|
6
|
+
* reads them back. Strict interruption ordering closes a message before a tool starts. IDs and custom event names
|
|
7
|
+
* are injected, with official AG-UI types authoritative; auth, routes and encoder stay with the host.
|
|
8
|
+
* The host emits RUN_STARTED and terminal RUN_FINISHED/RUN_ERROR outside this pure projection. */
|
|
9
|
+
import { EventType, InterruptSchema, RunAgentInputSchema } from '@ag-ui/core';
|
|
10
|
+
import { readSseFrames, readTerminalReason, translateRunFrame } from '../run-events/entry.js';
|
|
11
|
+
/** Create isolated per-run translation state with explicit IDs and custom-event wire names. */
|
|
12
|
+
export function createAgUiTranslationState({ ids, customEventNames }) {
|
|
13
|
+
return { openTextMessageId: null, openReasoningMessageId: null, nextId: 0, ids, customEventNames: { ...customEventNames } };
|
|
14
|
+
}
|
|
15
|
+
function mintId({ state, prefix }) {
|
|
16
|
+
state.nextId += 1;
|
|
17
|
+
return state.ids.next({ prefix, ordinal: state.nextId });
|
|
18
|
+
}
|
|
19
|
+
function closeOpenMessages(state) {
|
|
20
|
+
const events = [];
|
|
21
|
+
if (state.openTextMessageId) {
|
|
22
|
+
events.push({ type: EventType.TEXT_MESSAGE_END, messageId: state.openTextMessageId });
|
|
23
|
+
state.openTextMessageId = null;
|
|
24
|
+
}
|
|
25
|
+
if (state.openReasoningMessageId) {
|
|
26
|
+
const messageId = state.openReasoningMessageId;
|
|
27
|
+
events.push({ type: EventType.REASONING_MESSAGE_END, messageId });
|
|
28
|
+
events.push({ type: EventType.REASONING_END, messageId });
|
|
29
|
+
state.openReasoningMessageId = null;
|
|
30
|
+
}
|
|
31
|
+
return events;
|
|
32
|
+
}
|
|
33
|
+
function handleTextAgentEvent(event, state) {
|
|
34
|
+
const events = [];
|
|
35
|
+
if (state.openReasoningMessageId)
|
|
36
|
+
events.push(...closeOpenMessages(state));
|
|
37
|
+
if (!state.openTextMessageId) {
|
|
38
|
+
state.openTextMessageId = mintId({ state, prefix: "msg" });
|
|
39
|
+
events.push({ type: EventType.TEXT_MESSAGE_START, messageId: state.openTextMessageId, role: "assistant" });
|
|
40
|
+
}
|
|
41
|
+
events.push({ type: EventType.TEXT_MESSAGE_CONTENT, messageId: state.openTextMessageId, delta: event.text });
|
|
42
|
+
return events;
|
|
43
|
+
}
|
|
44
|
+
function handleThinkingAgentEvent(event, state) {
|
|
45
|
+
const events = [];
|
|
46
|
+
if (state.openTextMessageId)
|
|
47
|
+
events.push(...closeOpenMessages(state));
|
|
48
|
+
if (!state.openReasoningMessageId) {
|
|
49
|
+
state.openReasoningMessageId = mintId({ state, prefix: "reasoning" });
|
|
50
|
+
events.push({ type: EventType.REASONING_START, messageId: state.openReasoningMessageId });
|
|
51
|
+
events.push({ type: EventType.REASONING_MESSAGE_START, messageId: state.openReasoningMessageId, role: "reasoning" });
|
|
52
|
+
}
|
|
53
|
+
events.push({ type: EventType.REASONING_MESSAGE_CONTENT, messageId: state.openReasoningMessageId, delta: event.text });
|
|
54
|
+
return events;
|
|
55
|
+
}
|
|
56
|
+
function handleToolUseAgentEvent(event, state) {
|
|
57
|
+
const events = [];
|
|
58
|
+
if (state.openTextMessageId || state.openReasoningMessageId)
|
|
59
|
+
events.push(...closeOpenMessages(state));
|
|
60
|
+
events.push({
|
|
61
|
+
type: EventType.TOOL_CALL_START, toolCallId: event.id, toolCallName: event.name,
|
|
62
|
+
// Which bridge call this row stands for: the transcript matches a repeated call by it, so
|
|
63
|
+
// without these two a client on this wire cannot tell a duplicate from a second call.
|
|
64
|
+
...(event.toolOrigin !== undefined ? { toolOrigin: event.toolOrigin } : {}),
|
|
65
|
+
...(event.bridgeCall !== undefined ? { bridgeCall: event.bridgeCall } : {}),
|
|
66
|
+
});
|
|
67
|
+
events.push({ type: EventType.TOOL_CALL_ARGS, toolCallId: event.id, delta: JSON.stringify(event.input ?? {}) });
|
|
68
|
+
events.push({ type: EventType.TOOL_CALL_END, toolCallId: event.id });
|
|
69
|
+
return events;
|
|
70
|
+
}
|
|
71
|
+
/** A result's optional fields are written only when set, so a plain successful result is the
|
|
72
|
+
* standard event and nothing more. `isError` is absent for `false`: a reader takes absence as success. */
|
|
73
|
+
function handleToolResultAgentEvent({ event, state }, _optional = {}) {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
77
|
+
messageId: mintId({ state, prefix: "tool_result_msg" }),
|
|
78
|
+
toolCallId: event.toolUseId,
|
|
79
|
+
content: event.content,
|
|
80
|
+
role: "tool",
|
|
81
|
+
...(event.isError ? { isError: true } : {}),
|
|
82
|
+
...(event.media !== undefined ? { media: event.media } : {}),
|
|
83
|
+
...(event.outcome !== undefined ? { outcome: event.outcome } : {}),
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/** Emit ordered message/tool lifecycles; a tool call closes the preceding message first. */
|
|
88
|
+
export function translateAgentEventToAgUi({ event, state }) {
|
|
89
|
+
switch (event.kind) {
|
|
90
|
+
case "text":
|
|
91
|
+
return handleTextAgentEvent(event, state);
|
|
92
|
+
case "thinking":
|
|
93
|
+
return handleThinkingAgentEvent(event, state);
|
|
94
|
+
case "tool_use":
|
|
95
|
+
return handleToolUseAgentEvent(event, state);
|
|
96
|
+
case "tool_result":
|
|
97
|
+
return handleToolResultAgentEvent({ event, state });
|
|
98
|
+
case "usage":
|
|
99
|
+
return [{ type: EventType.CUSTOM, name: state.customEventNames.usage, value: event }];
|
|
100
|
+
case "status":
|
|
101
|
+
return [{ type: EventType.CUSTOM, name: state.customEventNames.status, value: event }];
|
|
102
|
+
case "raw":
|
|
103
|
+
return [{ type: EventType.RAW, event: event.line }];
|
|
104
|
+
case "ext":
|
|
105
|
+
return [{ type: EventType.CUSTOM, name: `${state.customEventNames.extensionPrefix}${event.name}`, value: event.data }];
|
|
106
|
+
default: {
|
|
107
|
+
const neverEvent = event;
|
|
108
|
+
throw new Error(`translateAgentEventToAgUi: unhandled AgentEvent.kind ${JSON.stringify(neverEvent)}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Close open text/reasoning before the host emits RUN_FINISHED or RUN_ERROR. */
|
|
113
|
+
export function closeAgUiRun({ state }) {
|
|
114
|
+
return closeOpenMessages(state);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Hands Jini's `readSseFrames` the daemon response as a stream while this route keeps the reader.
|
|
118
|
+
*
|
|
119
|
+
* `readSseFrames` takes a stream and holds its only reader until the frames end. The route needs
|
|
120
|
+
* its own handle on the daemon response too: the `res.on("close", ...)` hook cancels that reader so
|
|
121
|
+
* a client that leaves stops the upstream read at once, not at the daemon's next frame. A
|
|
122
|
+
* `ReadableStream` can only have one active reader at a time — a second `body.getReader()` throws
|
|
123
|
+
* `TypeError: ReadableStream is locked` — so the route keeps the one reader and Jini pulls through
|
|
124
|
+
* it. Cancelling the reader resolves the pending pull as done, which ends the frames the same way
|
|
125
|
+
* a daemon close does.
|
|
126
|
+
*
|
|
127
|
+
* `id:` lines are ignored by the frame reader: this path has no reattach story to resume a cursor
|
|
128
|
+
* for.
|
|
129
|
+
*/
|
|
130
|
+
function streamFromReader({ reader }, _optional = {}) {
|
|
131
|
+
return new ReadableStream({
|
|
132
|
+
async pull(controller) {
|
|
133
|
+
const { done, value } = await reader.read();
|
|
134
|
+
if (done)
|
|
135
|
+
controller.close();
|
|
136
|
+
else
|
|
137
|
+
controller.enqueue(value);
|
|
138
|
+
},
|
|
139
|
+
cancel: (reason) => reader.cancel(reason),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/** Writes every event in `events` to `res`, in order — the repeated `for (const e of events)
|
|
143
|
+
* writeAgUiEvent(res, e)` pattern below, named once so each caller is a flat sequence of
|
|
144
|
+
* statements rather than its own loop. */
|
|
145
|
+
function writeAllAgUiEvents({ write, events }, _optional = {}) {
|
|
146
|
+
for (const event of events)
|
|
147
|
+
write(event);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Writes one translated daemon frame as AG-UI events. Every daemon SSE frame's `data:` line is the
|
|
151
|
+
* FULL `RunProtocolEventWire` envelope, not the inner payload; Jini's `translateRunFrame` unwraps
|
|
152
|
+
* it, and a frame it cannot read yields no events instead of failing the run.
|
|
153
|
+
*
|
|
154
|
+
* A terminal frame (`error`, or `end`) first writes its own notices, then closes any open
|
|
155
|
+
* message/reasoning segment, then the one terminal event: `RUN_ERROR` for an `error` frame and for
|
|
156
|
+
* an `end` the daemon classified as failed, else `RUN_FINISHED` — with reason `cancelled` for an
|
|
157
|
+
* `end` the daemon classified as canceled, else the frame's own reason (`stop` when it names none).
|
|
158
|
+
*
|
|
159
|
+
* @returns the run's final status once a terminal frame has closed it — the caller stops reading
|
|
160
|
+
* further frames and ends the response — else `null`.
|
|
161
|
+
*/
|
|
162
|
+
function writeFrameOutcome({ frame, outcome, ctx }, _optional = {}) {
|
|
163
|
+
for (const event of outcome.events)
|
|
164
|
+
writeAllAgUiEvents({ write: ctx.write, events: translateAgentEventToAgUi({ event, state: ctx.state }) });
|
|
165
|
+
if (!outcome.error && !outcome.terminal)
|
|
166
|
+
return null;
|
|
167
|
+
writeAllAgUiEvents({ write: ctx.write, events: closeAgUiRun({ state: ctx.state }) });
|
|
168
|
+
if (outcome.error) {
|
|
169
|
+
ctx.write({ type: EventType.RUN_ERROR, message: outcome.error.message });
|
|
170
|
+
return "failed";
|
|
171
|
+
}
|
|
172
|
+
const canceled = outcome.terminal === "canceled";
|
|
173
|
+
const reason = canceled ? "cancelled" : readTerminalReason({ raw: frame.data, wrapped: true }) || "stop";
|
|
174
|
+
ctx.write({ type: EventType.RUN_FINISHED, threadId: ctx.threadId, runId: ctx.runId, result: { reason } });
|
|
175
|
+
return canceled ? "canceled" : "succeeded";
|
|
176
|
+
}
|
|
177
|
+
/** Drains one AG-UI run's daemon frames to completion, writing translated events directly to
|
|
178
|
+
* `ctx.res` and ending the response either on an explicit terminal frame, on the stream simply
|
|
179
|
+
* closing (same dual-completion posture `consumeByokStream` uses for the identical case), or on a
|
|
180
|
+
* read error. Split out of `handleAgUiRun` so the try/catch and its loop don't add to that
|
|
181
|
+
* function's own branch count.
|
|
182
|
+
* @returns How the run ended, for its agent-run span: a terminal frame's own status (an `end` the
|
|
183
|
+
* daemon classified as failed or canceled is that, not `succeeded`); a natural close counts as
|
|
184
|
+
* `succeeded`, like the `RUN_FINISHED` it writes; a read error is `failed` with that error. */
|
|
185
|
+
export async function drainRunFramesToAgUi(ctx) {
|
|
186
|
+
// One carry per daemon connection, so the later chunks of a split echo line are dropped too.
|
|
187
|
+
const carry = {};
|
|
188
|
+
try {
|
|
189
|
+
for await (const frame of readSseFrames({ body: streamFromReader({ reader: ctx.reader }) })) {
|
|
190
|
+
const outcome = translateRunFrame({ kind: frame.event, raw: frame.data, notices: ctx.notices }, { carry });
|
|
191
|
+
if (frame.event === 'agent' && outcome.events.length > 0) {
|
|
192
|
+
const marker = InterruptSchema.safeParse(JSON.parse(frame.data).payload.pendingApproval);
|
|
193
|
+
if (marker.success) {
|
|
194
|
+
writeAllAgUiEvents({ write: ctx.write, events: closeAgUiRun({ state: ctx.state }) });
|
|
195
|
+
ctx.write(confirmationInterrupt({ threadId: ctx.threadId, runId: ctx.runId, interrupt: marker.data }));
|
|
196
|
+
return { status: 'succeeded' };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const status = writeFrameOutcome({ frame, outcome, ctx });
|
|
200
|
+
if (status) {
|
|
201
|
+
return { status };
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
writeAllAgUiEvents({ write: ctx.write, events: closeAgUiRun({ state: ctx.state }) });
|
|
205
|
+
ctx.write({ type: EventType.RUN_FINISHED, threadId: ctx.threadId, runId: ctx.runId, result: { reason: "stop" } });
|
|
206
|
+
return { status: "succeeded" };
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
writeAllAgUiEvents({ write: ctx.write, events: closeAgUiRun({ state: ctx.state }) });
|
|
210
|
+
ctx.write({ type: EventType.RUN_ERROR, message: error instanceof Error ? error.message : String(error) });
|
|
211
|
+
return { status: "failed", error };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
export function confirmationInterrupt({ threadId, runId, interrupt }, _optional = {}) {
|
|
215
|
+
return { type: EventType.RUN_FINISHED, threadId, runId,
|
|
216
|
+
outcome: { type: 'interrupt', interrupts: [{ ...interrupt, reason: 'confirmation' }] } };
|
|
217
|
+
}
|
|
218
|
+
export function readConfirmationResume({ input, threadId, runId, interruptId }, _optional = {}) {
|
|
219
|
+
const parsed = RunAgentInputSchema.safeParse(input);
|
|
220
|
+
if (!parsed.success)
|
|
221
|
+
return undefined;
|
|
222
|
+
const value = parsed.data;
|
|
223
|
+
if (value.threadId !== threadId || value.parentRunId !== runId)
|
|
224
|
+
return undefined;
|
|
225
|
+
if (value.resume?.length !== 1)
|
|
226
|
+
return undefined;
|
|
227
|
+
const entry = value.resume[0];
|
|
228
|
+
if (entry.interruptId !== interruptId)
|
|
229
|
+
return undefined;
|
|
230
|
+
if (entry.status === 'cancelled')
|
|
231
|
+
return { decision: 'deny' };
|
|
232
|
+
if (entry.payload === 'allow')
|
|
233
|
+
return { decision: 'allow' };
|
|
234
|
+
if (['deny', 'dismissed'].includes(entry.payload))
|
|
235
|
+
return { decision: 'deny' };
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
export function createAgUiPauseStore({ caps, nowMs }, _optional = {}) {
|
|
239
|
+
const records = new Map();
|
|
240
|
+
return {
|
|
241
|
+
park(entry, _optional = {}) {
|
|
242
|
+
const now = nowMs();
|
|
243
|
+
let scoped = 0;
|
|
244
|
+
for (const [id, record] of records) {
|
|
245
|
+
if (record.abandonAtMs <= now)
|
|
246
|
+
records.delete(id);
|
|
247
|
+
else if (record.scopeKey === entry.scopeKey)
|
|
248
|
+
scoped++;
|
|
249
|
+
}
|
|
250
|
+
if (entry.abandonAtMs <= now || records.has(entry.interruptId))
|
|
251
|
+
return false;
|
|
252
|
+
if (records.size >= caps.global || scoped >= caps.perScope)
|
|
253
|
+
return false;
|
|
254
|
+
records.set(entry.interruptId, { ...entry });
|
|
255
|
+
return true;
|
|
256
|
+
},
|
|
257
|
+
claim({ owner, threadId, runId, interruptId }, _optional = {}) {
|
|
258
|
+
const record = records.get(interruptId);
|
|
259
|
+
if (!record)
|
|
260
|
+
return undefined;
|
|
261
|
+
if (record.abandonAtMs <= nowMs()) {
|
|
262
|
+
records.delete(interruptId);
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (record.owner !== owner || record.threadId !== threadId || record.runId !== runId)
|
|
266
|
+
return undefined;
|
|
267
|
+
records.delete(interruptId);
|
|
268
|
+
return record;
|
|
269
|
+
},
|
|
270
|
+
remove({ interruptId }, _optional = {}) {
|
|
271
|
+
return records.delete(interruptId);
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
//# sourceMappingURL=event-projection.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AgentEvent } from '../events.js';
|
|
2
|
+
import type { AgUiEvent, CustomEventNames } from './event-projection.js';
|
|
3
|
+
/**
|
|
4
|
+
* Accumulates `TOOL_CALL_ARGS` deltas by `toolCallId` between a call's `TOOL_CALL_START` and
|
|
5
|
+
* `TOOL_CALL_END` — the inverse of `assistant-ag-ui.ts`'s server-side translator, which emits all
|
|
6
|
+
* three tool-call events back-to-back for one chat `tool_use` event today, but this side is written
|
|
7
|
+
* to accumulate real streamed deltas too (a future daemon driver that streams `tool_input_delta`
|
|
8
|
+
* incrementally needs no transport-layer change to render correctly). One instance per run, NOT a
|
|
9
|
+
* module-level singleton — the same reasoning `assistant-ag-ui.ts`'s `AgUiTranslationState` gives.
|
|
10
|
+
*/
|
|
11
|
+
export interface AgUiToAgentTranslationState {
|
|
12
|
+
toolCalls: Map<string, AgUiOpenToolCall>;
|
|
13
|
+
readonly customEventNames: CustomEventNames;
|
|
14
|
+
}
|
|
15
|
+
/** One call between its `TOOL_CALL_START` and `TOOL_CALL_END`. `toolOrigin` and `bridgeCall` are
|
|
16
|
+
* the start event's extra fields as received, not yet checked. */
|
|
17
|
+
export interface AgUiOpenToolCall {
|
|
18
|
+
name: string;
|
|
19
|
+
argsJson: string;
|
|
20
|
+
toolOrigin?: unknown;
|
|
21
|
+
bridgeCall?: unknown;
|
|
22
|
+
}
|
|
23
|
+
export declare function createAgUiToAgentTranslationState({ customEventNames }: {
|
|
24
|
+
customEventNames: CustomEventNames;
|
|
25
|
+
}, _options?: Record<string, never>): AgUiToAgentTranslationState;
|
|
26
|
+
/**
|
|
27
|
+
* Translates one AG-UI event into zero or more chat-core `AgentEvent`s, threading `state` across
|
|
28
|
+
* calls within a single run for tool-call argument accumulation.
|
|
29
|
+
*
|
|
30
|
+
* `TEXT_MESSAGE_START` preserves an explicit step boundary so two assistant messages cannot fuse
|
|
31
|
+
* into one word. End/reasoning markers carry no text. The two cases
|
|
32
|
+
* with real internal branching (`TOOL_CALL_END`, `CUSTOM`) and the one with a guard (`TOOL_CALL_ARGS`)
|
|
33
|
+
* are pulled into their own named functions above so this switch's own cost is just its case count.
|
|
34
|
+
*/
|
|
35
|
+
export declare function translateAgUiEventToAgentEvent({ event, state }: {
|
|
36
|
+
event: AgUiEvent;
|
|
37
|
+
state: AgUiToAgentTranslationState;
|
|
38
|
+
}, _options?: Record<string, never>): AgentEvent[];
|
|
39
|
+
//# sourceMappingURL=event-reduction.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** AG-UI events back to chat events: the reverse of `event-projection.ts`.
|
|
2
|
+
* Tool-call arguments are accumulated per run; the custom event names are the host's.
|
|
3
|
+
* A tool call's and a tool result's optional fields arrive as extra fields under their chat-event
|
|
4
|
+
* names. They are read by `run-events` `translateRunAgentPayload`, the one reducer, so this wire and
|
|
5
|
+
* the daemon wire accept and ignore exactly the same shapes. */
|
|
6
|
+
import { EventType } from '@ag-ui/core';
|
|
7
|
+
import { asString, translateRunAgentPayload } from '../run-events/entry.js';
|
|
8
|
+
export function createAgUiToAgentTranslationState({ customEventNames }, _options = {}) {
|
|
9
|
+
return { toolCalls: new Map(), customEventNames: { ...customEventNames } };
|
|
10
|
+
}
|
|
11
|
+
/** The `TOOL_CALL_ARGS` case of {@link translateAgUiEventToAgentEvent} — accumulates one delta onto
|
|
12
|
+
* the in-flight call's buffer, a silent no-op for an id with no matching `TOOL_CALL_START`. Split
|
|
13
|
+
* out so this `if` is scored in its own complexity budget rather than the switch's. */
|
|
14
|
+
function accumulateToolCallArgs({ toolCallId, delta, state }) {
|
|
15
|
+
const call = state.toolCalls.get(toolCallId);
|
|
16
|
+
if (call)
|
|
17
|
+
call.argsJson += delta;
|
|
18
|
+
}
|
|
19
|
+
/** `TEXT_MESSAGE_START` keeps an explicit step boundary so two assistant messages cannot fuse into
|
|
20
|
+
* one word; every other unmapped event translates to nothing. Split out so this `if` is scored in
|
|
21
|
+
* its own complexity budget rather than the switch's. */
|
|
22
|
+
function translateAgUiStepStart({ event }, _options = {}) {
|
|
23
|
+
if (event.type !== EventType.TEXT_MESSAGE_START)
|
|
24
|
+
return [];
|
|
25
|
+
return [{ kind: 'status', code: 'step_start', label: '', data: { messageId: event.messageId } }];
|
|
26
|
+
}
|
|
27
|
+
/** The `TOOL_CALL_END` case of {@link translateAgUiEventToAgentEvent} — parses the accumulated
|
|
28
|
+
* args buffer (already removed from `state` by the caller) into one `tool_use` event, or `[]` for
|
|
29
|
+
* an id with no matching `TOOL_CALL_START`. */
|
|
30
|
+
function finalizeToolCallAgentEvent({ toolCallId, call }) {
|
|
31
|
+
if (!call)
|
|
32
|
+
return [];
|
|
33
|
+
let input = {};
|
|
34
|
+
try {
|
|
35
|
+
input = call.argsJson.length > 0 ? JSON.parse(call.argsJson) : {};
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// A malformed/partial args buffer must not throw and drop the whole tool call — the raw
|
|
39
|
+
// string is still more useful to a human than nothing.
|
|
40
|
+
input = call.argsJson;
|
|
41
|
+
}
|
|
42
|
+
return reduced({ payload: { type: "tool_use", id: toolCallId, name: call.name, input, toolOrigin: call.toolOrigin, bridgeCall: call.bridgeCall } });
|
|
43
|
+
}
|
|
44
|
+
/** Hands a rebuilt daemon payload to the one reducer; a type it drops yields no event. */
|
|
45
|
+
function reduced({ payload }, _options = {}) {
|
|
46
|
+
const event = translateRunAgentPayload({ payload });
|
|
47
|
+
return event ? [event] : [];
|
|
48
|
+
}
|
|
49
|
+
/** The `CUSTOM` case of {@link translateAgUiEventToAgentEvent}. */
|
|
50
|
+
function translateAgUiCustomEvent(event, names) {
|
|
51
|
+
// Host-supplied usage/status names: the server's own translator put the ORIGINAL `AgentEvent`
|
|
52
|
+
// straight into `.value` (see `assistant-ag-ui.ts`'s `translateAgentEventToAgUi`), so this is
|
|
53
|
+
// an exact round trip, not a re-derivation.
|
|
54
|
+
if (event.name === names.usage || event.name === names.status)
|
|
55
|
+
return [event.value];
|
|
56
|
+
if (event.name.startsWith(names.extensionPrefix))
|
|
57
|
+
return [{ kind: "ext", name: event.name.slice(names.extensionPrefix.length), data: event.value }];
|
|
58
|
+
return [{ kind: "ext", name: event.name, data: event.value }];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Translates one AG-UI event into zero or more chat-core `AgentEvent`s, threading `state` across
|
|
62
|
+
* calls within a single run for tool-call argument accumulation.
|
|
63
|
+
*
|
|
64
|
+
* `TEXT_MESSAGE_START` preserves an explicit step boundary so two assistant messages cannot fuse
|
|
65
|
+
* into one word. End/reasoning markers carry no text. The two cases
|
|
66
|
+
* with real internal branching (`TOOL_CALL_END`, `CUSTOM`) and the one with a guard (`TOOL_CALL_ARGS`)
|
|
67
|
+
* are pulled into their own named functions above so this switch's own cost is just its case count.
|
|
68
|
+
*/
|
|
69
|
+
export function translateAgUiEventToAgentEvent({ event, state }, _options = {}) {
|
|
70
|
+
switch (event.type) {
|
|
71
|
+
case EventType.TEXT_MESSAGE_CONTENT:
|
|
72
|
+
return [{ kind: "text", text: event.delta }];
|
|
73
|
+
case EventType.REASONING_MESSAGE_CONTENT:
|
|
74
|
+
return [{ kind: "thinking", text: event.delta }];
|
|
75
|
+
case EventType.TOOL_CALL_START:
|
|
76
|
+
state.toolCalls.set(event.toolCallId, { name: event.toolCallName, argsJson: "", toolOrigin: event.toolOrigin, bridgeCall: event.bridgeCall });
|
|
77
|
+
return [];
|
|
78
|
+
case EventType.TOOL_CALL_ARGS:
|
|
79
|
+
accumulateToolCallArgs({ toolCallId: event.toolCallId, delta: event.delta, state });
|
|
80
|
+
return [];
|
|
81
|
+
case EventType.TOOL_CALL_END: {
|
|
82
|
+
const call = state.toolCalls.get(event.toolCallId);
|
|
83
|
+
state.toolCalls.delete(event.toolCallId);
|
|
84
|
+
return finalizeToolCallAgentEvent({ toolCallId: event.toolCallId, call });
|
|
85
|
+
}
|
|
86
|
+
case EventType.TOOL_CALL_RESULT:
|
|
87
|
+
// AG-UI's `ToolCallResultEvent` has no standard `isError`, `media` or `outcome` field. The
|
|
88
|
+
// projection writes them as extra fields; a server that sends none (an older one, or another
|
|
89
|
+
// AG-UI server) reads as a plain successful result, as before.
|
|
90
|
+
return reduced({ payload: { type: "tool_result", toolUseId: event.toolCallId, content: event.content, isError: event.isError, media: event.media, outcome: event.outcome } });
|
|
91
|
+
case EventType.RAW:
|
|
92
|
+
return [{ kind: "raw", line: asString({ value: event.event }) }];
|
|
93
|
+
case EventType.CUSTOM:
|
|
94
|
+
return translateAgUiCustomEvent(event, state.customEventNames);
|
|
95
|
+
default:
|
|
96
|
+
return translateAgUiStepStart({ event });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=event-reduction.js.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Parse and bound one inbound AG-UI run request (`RunAgentInput`) for a host route.
|
|
2
|
+
* Pure: the history bound and the id generator are host inputs, so no host default or Node
|
|
3
|
+
* built-in lives here. The route that calls this owns authentication and the HTTP answer. */
|
|
4
|
+
/** One history turn a run may carry: only user and assistant text survives parsing. */
|
|
5
|
+
export interface AgUiChatMessage {
|
|
6
|
+
role: 'user' | 'assistant';
|
|
7
|
+
content: string;
|
|
8
|
+
}
|
|
9
|
+
/** A validated run request. `threadId`, `runId` and `requestId` are always present; the three
|
|
10
|
+
* forwarded fields are `undefined` when the caller sent none or sent a malformed one. */
|
|
11
|
+
export interface AgUiRunInput {
|
|
12
|
+
frontendBindToken: string | undefined;
|
|
13
|
+
frontendTabId: string | undefined;
|
|
14
|
+
threadId: string;
|
|
15
|
+
runId: string;
|
|
16
|
+
requestId: string;
|
|
17
|
+
agentId: string | undefined;
|
|
18
|
+
messages: AgUiChatMessage[];
|
|
19
|
+
secretRedacted: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** The raw request body: every field is untrusted until parsed here. */
|
|
22
|
+
export interface AgUiRunBody {
|
|
23
|
+
threadId?: unknown;
|
|
24
|
+
runId?: unknown;
|
|
25
|
+
messages?: unknown;
|
|
26
|
+
forwardedProps?: unknown;
|
|
27
|
+
}
|
|
28
|
+
/** Mints the ids a caller left out. The host injects it, so tests pin ids and this file stays universal. */
|
|
29
|
+
export interface AgUiRunIdPort {
|
|
30
|
+
newId(): string;
|
|
31
|
+
}
|
|
32
|
+
/** Validates and bounds the client-supplied `messages` array: malformed entries are dropped, not
|
|
33
|
+
* refused (history is passive background context, not something the caller just authored this
|
|
34
|
+
* instant), and only the trailing `maxHistoryMessages` are kept. The bound is the host's: each turn
|
|
35
|
+
* re-sends its whole history, so an unbounded one makes every later message in a long chat more
|
|
36
|
+
* expensive than the last.
|
|
37
|
+
* @complexity O(h) time and space in the supplied history. */
|
|
38
|
+
export declare function resolveAgUiMessages({ raw, maxHistoryMessages }: {
|
|
39
|
+
raw: unknown;
|
|
40
|
+
maxHistoryMessages: number;
|
|
41
|
+
}, _optional?: Record<string, never>): AgUiChatMessage[];
|
|
42
|
+
/** Validates and normalizes one AG-UI run request body, or `null` when it fails the one hard
|
|
43
|
+
* requirement: a non-empty `messages` array ending in a user turn. A caller-supplied `threadId` /
|
|
44
|
+
* `runId` is echoed as given (AG-UI expects the caller to own them); an absent or empty one is
|
|
45
|
+
* minted so a well-formed-but-partial request still gets a coherent stream. Ids are minted in the
|
|
46
|
+
* order thread, run, request.
|
|
47
|
+
* @complexity O(h) time and space in the supplied history; attachment fields are constant work. */
|
|
48
|
+
export declare function resolveAgUiRunInput({ body, maxHistoryMessages, ids }: {
|
|
49
|
+
body: AgUiRunBody;
|
|
50
|
+
maxHistoryMessages: number;
|
|
51
|
+
ids: AgUiRunIdPort;
|
|
52
|
+
}, _optional?: Record<string, never>): AgUiRunInput | null;
|
|
53
|
+
//# sourceMappingURL=run-input.d.ts.map
|