@anvia/core 0.8.0 → 0.9.1-preview.20260628T064832.aaab367

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.
Files changed (53) hide show
  1. package/README.md +10 -0
  2. package/dist/agent/index.d.ts +10 -9
  3. package/dist/agent/index.js +6 -6
  4. package/dist/{agent-BW19m79B.d.ts → agent-DVTjbqHH.d.ts} +6 -66
  5. package/dist/audio-generation/index.d.ts +1 -1
  6. package/dist/{chunk-VQE5PAAT.js → chunk-66IJDP6L.js} +4 -4
  7. package/dist/{chunk-4KQ536VD.js → chunk-BKIXV5C4.js} +4 -4
  8. package/dist/chunk-BYRCLOKA.js +416 -0
  9. package/dist/chunk-BYRCLOKA.js.map +1 -0
  10. package/dist/{chunk-EIBAVYOR.js → chunk-HBICH2OA.js} +6 -6
  11. package/dist/{chunk-USWULPZJ.js → chunk-IA47SXLS.js} +5 -5
  12. package/dist/{chunk-RAHDO2B2.js → chunk-TM4LFNPT.js} +3 -3
  13. package/dist/{chunk-ITYXP4CL.js → chunk-ZT2YH2GA.js} +2 -2
  14. package/dist/completion/index.d.ts +3 -3
  15. package/dist/{create-completion-BEbtU5pX.d.ts → create-completion-m0oO3ohS.d.ts} +1 -1
  16. package/dist/evals/index.d.ts +5 -4
  17. package/dist/evals/index.js +8 -8
  18. package/dist/extractor/index.d.ts +5 -4
  19. package/dist/extractor/index.js +7 -7
  20. package/dist/image-generation/index.d.ts +1 -1
  21. package/dist/index.d.ts +10 -8
  22. package/dist/index.js +22 -8
  23. package/dist/internal/agent.d.ts +5 -4
  24. package/dist/internal/agent.js +5 -5
  25. package/dist/loaders/index.d.ts +1 -1
  26. package/dist/mcp/index.d.ts +4 -4
  27. package/dist/memory/index.d.ts +1 -1
  28. package/dist/{middleware-CZocYAtZ.d.ts → middleware-7jwASFQb.d.ts} +2 -2
  29. package/dist/observability/index.d.ts +2 -2
  30. package/dist/pipeline/index.d.ts +5 -4
  31. package/dist/request-types-iGJLBlZv.d.ts +65 -0
  32. package/dist/skills/index.d.ts +4 -4
  33. package/dist/skills/index.js +5 -5
  34. package/dist/{think-tool-B8Rqw2i_.d.ts → think-tool-Vo9UM-qa.d.ts} +1 -1
  35. package/dist/tool/index.d.ts +4 -4
  36. package/dist/tool/index.js +4 -4
  37. package/dist/{tool-BLypI31e.d.ts → tool-C79JYHlw.d.ts} +1 -1
  38. package/dist/transcription/index.d.ts +1 -1
  39. package/dist/{types-C8loR4il.d.ts → types-BhXVKZCw.d.ts} +1 -1
  40. package/dist/{types-CIwm9p4w.d.ts → types-D9EnChuM.d.ts} +2 -2
  41. package/dist/{types-D-YTJdLj.d.ts → types-yFrkmqy3.d.ts} +1 -1
  42. package/dist/ui/index.d.ts +103 -0
  43. package/dist/ui/index.js +20 -0
  44. package/dist/ui/index.js.map +1 -0
  45. package/dist/vector-store/index.d.ts +2 -2
  46. package/dist/vector-store/index.js +2 -2
  47. package/package.json +6 -2
  48. /package/dist/{chunk-VQE5PAAT.js.map → chunk-66IJDP6L.js.map} +0 -0
  49. /package/dist/{chunk-4KQ536VD.js.map → chunk-BKIXV5C4.js.map} +0 -0
  50. /package/dist/{chunk-EIBAVYOR.js.map → chunk-HBICH2OA.js.map} +0 -0
  51. /package/dist/{chunk-USWULPZJ.js.map → chunk-IA47SXLS.js.map} +0 -0
  52. /package/dist/{chunk-RAHDO2B2.js.map → chunk-TM4LFNPT.js.map} +0 -0
  53. /package/dist/{chunk-ITYXP4CL.js.map → chunk-ZT2YH2GA.js.map} +0 -0
package/README.md CHANGED
@@ -96,6 +96,16 @@ for await (const event of createCompletionStream(model, {
96
96
  }
97
97
  ```
98
98
 
99
+ Use `createCompletionUIStream` when a React UI sends standardized `UIMessage[]`:
100
+
101
+ ```ts
102
+ import { createCompletionUIStream } from "@anvia/core/ui";
103
+
104
+ const uiEvents = createCompletionUIStream(model, {
105
+ messages,
106
+ });
107
+ ```
108
+
99
109
  Use `createParsedCompletion` when you want a direct completion to return schema-validated data:
100
110
 
101
111
  ```ts
@@ -1,16 +1,17 @@
1
- import { A as AnyTool, e as ToolApprovalsOptions, d as ToolApprovalRequest } from '../tool-BLypI31e.js';
2
- export { b as ToolApprovalDecision } from '../tool-BLypI31e.js';
3
- import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-CZocYAtZ.js';
4
- export { C as CompletionRequestMiddlewareArgs, a as CompletionRequestMiddlewareResult, b as CompletionResponseMiddlewareArgs, c as CompletionResponseMiddlewareResult, T as ToolInputMiddlewareArgs, d as ToolInputMiddlewareResult, f as ToolOutputMiddlewareArgs, g as ToolOutputMiddlewareResult, h as ToolResultMiddlewareArgs, i as createMiddleware, j as createToolMiddleware } from '../middleware-CZocYAtZ.js';
5
- import { D as DynamicContextOptions, f as DynamicToolOptions, g as PromptHook, h as AgentEventStore, i as AgentEventStoreOptions, e as Agent } from '../agent-BW19m79B.js';
6
- export { A as AgentChildStreamEvent, j as AgentEventAppendInput, k as AgentEventRecord, l as AgentEventStoreInclude, a as AgentStreamEvent, C as CompletionCallHookArgs, m as CompletionErrorHookArgs, n as CompletionResponseHookArgs, H as HookAction, o as HookResult, P as PromptResponse, R as RunControl, p as RunEndHookArgs, q as RunErrorHookArgs, u as RunStartHookArgs, T as ToolApprovalRequestOptions, v as ToolCallControl, w as ToolCallHookAction, x as ToolCallHookArgs, y as ToolCallHookResult, z as ToolErrorHookArgs, B as ToolHookArgs, E as ToolResultHookArgs, F as TurnEndHookArgs, G as TurnStartHookArgs, c as cancelPrompt, b as createHook, r as requestToolApproval, d as runControl, s as skipTool, t as toolCallControl } from '../agent-BW19m79B.js';
1
+ import { A as AnyTool, e as ToolApprovalsOptions, d as ToolApprovalRequest } from '../tool-C79JYHlw.js';
2
+ export { b as ToolApprovalDecision } from '../tool-C79JYHlw.js';
3
+ import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-7jwASFQb.js';
4
+ export { C as CompletionRequestMiddlewareArgs, a as CompletionRequestMiddlewareResult, b as CompletionResponseMiddlewareArgs, c as CompletionResponseMiddlewareResult, T as ToolInputMiddlewareArgs, d as ToolInputMiddlewareResult, f as ToolOutputMiddlewareArgs, g as ToolOutputMiddlewareResult, h as ToolResultMiddlewareArgs, i as createMiddleware, j as createToolMiddleware } from '../middleware-7jwASFQb.js';
5
+ import { D as DynamicContextOptions, d as DynamicToolOptions, P as PromptHook, e as AgentEventStore, f as AgentEventStoreOptions, A as Agent } from '../agent-DVTjbqHH.js';
6
+ export { g as AgentEventAppendInput, h as AgentEventRecord, i as AgentEventStoreInclude, C as CompletionCallHookArgs, j as CompletionErrorHookArgs, k as CompletionResponseHookArgs, H as HookAction, l as HookResult, R as RunControl, m as RunEndHookArgs, n as RunErrorHookArgs, o as RunStartHookArgs, T as ToolApprovalRequestOptions, p as ToolCallControl, q as ToolCallHookAction, u as ToolCallHookArgs, v as ToolCallHookResult, w as ToolErrorHookArgs, x as ToolHookArgs, y as ToolResultHookArgs, z as TurnEndHookArgs, B as TurnStartHookArgs, c as cancelPrompt, a as createHook, r as requestToolApproval, b as runControl, s as skipTool, t as toolCallControl } from '../agent-DVTjbqHH.js';
7
7
  import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
8
- import { C as CompletionModel, e as JsonValue, n as ToolChoice, M as Message } from '../types-C8loR4il.js';
9
- import { a as McpServer } from '../types-CIwm9p4w.js';
8
+ import { C as CompletionModel, e as JsonValue, q as ToolChoice, M as Message } from '../types-BhXVKZCw.js';
9
+ import { a as McpServer } from '../types-D9EnChuM.js';
10
10
  import { MemoryStore, MemoryOptions } from '../memory/index.js';
11
11
  import { AgentObserver, ObserveOptions } from '../observability/index.js';
12
- import { a as SkillSet } from '../types-D-YTJdLj.js';
12
+ import { a as SkillSet } from '../types-yFrkmqy3.js';
13
13
  import { VectorSearchIndex } from '../vector-store/index.js';
14
+ export { A as AgentChildStreamEvent, a as AgentStreamEvent, P as PromptResponse } from '../request-types-iGJLBlZv.js';
14
15
  import '../types-IB2e9u5M.js';
15
16
  import 'zod';
16
17
  import '@modelcontextprotocol/sdk/client/sse.js';
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-RAHDO2B2.js";
3
+ } from "../chunk-TM4LFNPT.js";
4
4
  import {
5
5
  MaxTurnsError,
6
6
  PromptCancelledError,
@@ -11,21 +11,21 @@ import {
11
11
  runControl,
12
12
  skipTool,
13
13
  toolCallControl
14
- } from "../chunk-EIBAVYOR.js";
14
+ } from "../chunk-HBICH2OA.js";
15
15
  import "../chunk-XUUY2L2D.js";
16
16
  import "../chunk-XXT2UCAR.js";
17
17
  import "../chunk-YK4WAAS4.js";
18
18
  import {
19
19
  createMiddleware,
20
20
  createToolMiddleware
21
- } from "../chunk-USWULPZJ.js";
22
- import "../chunk-VQE5PAAT.js";
21
+ } from "../chunk-IA47SXLS.js";
22
+ import "../chunk-66IJDP6L.js";
23
23
  import "../chunk-MMHG7WAM.js";
24
- import "../chunk-FTO3W4UP.js";
25
- import "../chunk-OIMLU4SF.js";
26
24
  import "../chunk-S5IGJ4RB.js";
27
25
  import "../chunk-WJO6NVE2.js";
28
26
  import "../chunk-WQKHFADH.js";
27
+ import "../chunk-FTO3W4UP.js";
28
+ import "../chunk-OIMLU4SF.js";
29
29
  export {
30
30
  AgentBuilder,
31
31
  MaxTurnsError,
@@ -1,9 +1,10 @@
1
- import { M as Message, U as Usage, c as CompletionResponse, k as ToolResultContent, x as ReasoningContentType, f as ToolCall, C as CompletionModel, D as Document, e as JsonValue, n as ToolChoice, J as JsonObject } from './types-C8loR4il.js';
1
+ import { M as Message, U as Usage, c as CompletionResponse, k as ToolResultContent, C as CompletionModel, D as Document, e as JsonValue, q as ToolChoice, J as JsonObject } from './types-BhXVKZCw.js';
2
2
  import { MemoryContext, MemoryRegistration, SessionOptions } from './memory/index.js';
3
- import { AgentTraceInfo, AgentTraceOptions, AgentObserverRegistration } from './observability/index.js';
4
- import { A as AgentMiddleware, e as ToolMiddleware, l as ToolSet, k as ToolSearchDocument } from './middleware-CZocYAtZ.js';
5
- import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-BLypI31e.js';
3
+ import { AgentTraceOptions, AgentObserverRegistration } from './observability/index.js';
4
+ import { A as AgentMiddleware, e as ToolMiddleware, l as ToolSet, k as ToolSearchDocument } from './middleware-7jwASFQb.js';
5
+ import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-C79JYHlw.js';
6
6
  import { VectorFilter, VectorSearchResult, VectorSearchIndex } from './vector-store/index.js';
7
+ import { P as PromptResponse, a as AgentStreamEvent } from './request-types-iGJLBlZv.js';
7
8
 
8
9
  type HookAction = {
9
10
  type: "continue";
@@ -122,67 +123,6 @@ interface PromptHook<RawResponse = unknown> {
122
123
  onToolError?: HookCallback<ToolErrorHookArgs>;
123
124
  }
124
125
 
125
- type PromptResponse = {
126
- output: string;
127
- usage: Usage;
128
- messages: Message[];
129
- trace?: AgentTraceInfo | undefined;
130
- };
131
- type AgentChildStreamEvent<RawResponse = unknown> = {
132
- type: "turn_start";
133
- turn: number;
134
- prompt: Message;
135
- history: Message[];
136
- } | {
137
- type: "text_delta";
138
- turn: number;
139
- delta: string;
140
- } | {
141
- type: "reasoning_delta";
142
- turn: number;
143
- delta: string;
144
- id?: string;
145
- contentType?: ReasoningContentType;
146
- signature?: string;
147
- } | {
148
- type: "tool_call";
149
- turn: number;
150
- toolCall: ToolCall;
151
- } | {
152
- type: "tool_result";
153
- turn: number;
154
- toolName: string;
155
- toolCallId?: string;
156
- internalCallId: string;
157
- args: string;
158
- result: string;
159
- structuredResult?: ToolResultContent[] | undefined;
160
- } | {
161
- type: "turn_end";
162
- turn: number;
163
- response: CompletionResponse<RawResponse>;
164
- } | {
165
- type: "final";
166
- runId: string;
167
- output: string;
168
- usage: Usage;
169
- messages: Message[];
170
- trace?: AgentTraceInfo | undefined;
171
- } | {
172
- type: "error";
173
- error: unknown;
174
- };
175
- type AgentStreamEvent<RawResponse = unknown> = AgentChildStreamEvent<RawResponse> | {
176
- type: "agent_tool_event";
177
- turn: number;
178
- toolName: string;
179
- toolCallId?: string;
180
- internalCallId: string;
181
- agentId: string;
182
- agentName?: string;
183
- event: AgentChildStreamEvent<RawResponse>;
184
- };
185
-
186
126
  declare class PromptRequest<M extends CompletionModel = CompletionModel> {
187
127
  private readonly agent;
188
128
  private readonly promptMessage;
@@ -374,4 +314,4 @@ declare class AgentSession<M extends CompletionModel = CompletionModel> {
374
314
  clear(): Promise<void>;
375
315
  }
376
316
 
377
- export { type AgentChildStreamEvent as A, type ToolHookArgs as B, type CompletionCallHookArgs as C, type DynamicContextOptions as D, type ToolResultHookArgs as E, type TurnEndHookArgs as F, type TurnStartHookArgs as G, type HookAction as H, type AgentEventStoreRegistration as I, type AgentOptions as J, AgentSession as K, type AgentToolOptions as L, DEFAULT_MAX_TURNS as M, type DynamicContextRegistration as N, type DynamicToolRegistration as O, type PromptResponse as P, type RunControl as R, type ToolApprovalRequestOptions as T, type AgentStreamEvent as a, createHook as b, cancelPrompt as c, runControl as d, Agent as e, type DynamicToolOptions as f, type PromptHook as g, type AgentEventStore as h, type AgentEventStoreOptions as i, type AgentEventAppendInput as j, type AgentEventRecord as k, type AgentEventStoreInclude as l, type CompletionErrorHookArgs as m, type CompletionResponseHookArgs as n, type HookResult as o, type RunEndHookArgs as p, type RunErrorHookArgs as q, requestToolApproval as r, skipTool as s, toolCallControl as t, type RunStartHookArgs as u, type ToolCallControl as v, type ToolCallHookAction as w, type ToolCallHookArgs as x, type ToolCallHookResult as y, type ToolErrorHookArgs as z };
317
+ export { Agent as A, type TurnStartHookArgs as B, type CompletionCallHookArgs as C, type DynamicContextOptions as D, type AgentEventStoreRegistration as E, type AgentOptions as F, AgentSession as G, type HookAction as H, type AgentToolOptions as I, DEFAULT_MAX_TURNS as J, type DynamicContextRegistration as K, type DynamicToolRegistration as L, type PromptHook as P, type RunControl as R, type ToolApprovalRequestOptions as T, createHook as a, runControl as b, cancelPrompt as c, type DynamicToolOptions as d, type AgentEventStore as e, type AgentEventStoreOptions as f, type AgentEventAppendInput as g, type AgentEventRecord as h, type AgentEventStoreInclude as i, type CompletionErrorHookArgs as j, type CompletionResponseHookArgs as k, type HookResult as l, type RunEndHookArgs as m, type RunErrorHookArgs as n, type RunStartHookArgs as o, type ToolCallControl as p, type ToolCallHookAction as q, requestToolApproval as r, skipTool as s, toolCallControl as t, type ToolCallHookArgs as u, type ToolCallHookResult as v, type ToolErrorHookArgs as w, type ToolHookArgs as x, type ToolResultHookArgs as y, type TurnEndHookArgs as z };
@@ -1,4 +1,4 @@
1
- import { e as JsonValue } from '../types-C8loR4il.js';
1
+ import { e as JsonValue } from '../types-BhXVKZCw.js';
2
2
 
3
3
  type AudioGenerationRequest = {
4
4
  text: string;
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  compact
3
3
  } from "./chunk-MMHG7WAM.js";
4
+ import {
5
+ toProviderJsonSchema
6
+ } from "./chunk-WQKHFADH.js";
4
7
  import {
5
8
  cosineSimilarity,
6
9
  embedText
7
10
  } from "./chunk-FTO3W4UP.js";
8
- import {
9
- toProviderJsonSchema
10
- } from "./chunk-WQKHFADH.js";
11
11
 
12
12
  // src/vector-store/index.ts
13
13
  import { z } from "zod";
@@ -325,4 +325,4 @@ export {
325
325
  InMemoryVectorIndex,
326
326
  createVectorSearchTool
327
327
  };
328
- //# sourceMappingURL=chunk-VQE5PAAT.js.map
328
+ //# sourceMappingURL=chunk-66IJDP6L.js.map
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "./chunk-RAHDO2B2.js";
3
+ } from "./chunk-TM4LFNPT.js";
4
4
  import {
5
5
  extractRagText
6
- } from "./chunk-EIBAVYOR.js";
6
+ } from "./chunk-HBICH2OA.js";
7
7
  import {
8
8
  createTool
9
- } from "./chunk-VQE5PAAT.js";
9
+ } from "./chunk-66IJDP6L.js";
10
10
  import {
11
11
  CompletionRequestBuilder
12
12
  } from "./chunk-S5IGJ4RB.js";
@@ -138,4 +138,4 @@ export {
138
138
  Extractor,
139
139
  ExtractorBuilder
140
140
  };
141
- //# sourceMappingURL=chunk-4KQ536VD.js.map
141
+ //# sourceMappingURL=chunk-BKIXV5C4.js.map
@@ -0,0 +1,416 @@
1
+ import {
2
+ createCompletionStream
3
+ } from "./chunk-S5IGJ4RB.js";
4
+ import {
5
+ AssistantContent,
6
+ Message,
7
+ ToolContent,
8
+ UserContent,
9
+ reasoningDisplayText,
10
+ serializeToolResultOutput,
11
+ textFromAssistantContent
12
+ } from "./chunk-WJO6NVE2.js";
13
+
14
+ // src/ui/index.ts
15
+ function uiMessagesToCoreMessages(messages) {
16
+ const coreMessages = [];
17
+ for (const message of messages) {
18
+ const text = textFromUIParts(message.parts);
19
+ if (message.role === "system") {
20
+ if (text.length > 0) {
21
+ coreMessages.push(Message.system(text));
22
+ }
23
+ continue;
24
+ }
25
+ if (message.role === "user") {
26
+ const content = message.parts.filter((part) => part.type === "text").map((part) => UserContent.text(part.text));
27
+ if (content.length > 0) {
28
+ coreMessages.push(Message.user(content));
29
+ }
30
+ continue;
31
+ }
32
+ if (message.role === "assistant") {
33
+ const content = [];
34
+ for (const part of message.parts) {
35
+ if (part.type === "text" && part.text.length > 0) {
36
+ content.push(AssistantContent.text(part.text));
37
+ continue;
38
+ }
39
+ if (part.type === "reasoning" && part.text.length > 0) {
40
+ content.push(AssistantContent.reasoning(part.text, part.reasoningId));
41
+ continue;
42
+ }
43
+ if (part.type === "tool" && (part.state === "input-streaming" || part.state === "input-available")) {
44
+ content.push(
45
+ AssistantContent.toolCall(
46
+ part.toolCallId,
47
+ part.toolName,
48
+ part.input ?? {},
49
+ part.callId
50
+ )
51
+ );
52
+ }
53
+ }
54
+ if (content.length > 0) {
55
+ coreMessages.push(Message.assistant(content, message.id));
56
+ }
57
+ continue;
58
+ }
59
+ const toolResults = [];
60
+ for (const part of message.parts) {
61
+ if (part.type !== "tool" || part.state !== "output-available") {
62
+ continue;
63
+ }
64
+ toolResults.push(
65
+ ToolContent.toolResult(
66
+ part.toolCallId,
67
+ outputToToolResultContent(part.output),
68
+ part.callId
69
+ )
70
+ );
71
+ }
72
+ if (toolResults.length > 0) {
73
+ coreMessages.push(Message.tool(toolResults));
74
+ }
75
+ }
76
+ return coreMessages;
77
+ }
78
+ function coreMessagesToUIMessages(messages) {
79
+ const uiMessages = [];
80
+ for (const message of messages) {
81
+ if (message.role === "system") {
82
+ uiMessages.push({
83
+ id: createId("msg"),
84
+ role: "system",
85
+ parts: [{ id: createId("part"), type: "text", text: message.content }]
86
+ });
87
+ continue;
88
+ }
89
+ if (message.role === "user") {
90
+ const parts2 = [];
91
+ for (const content of message.content) {
92
+ if (content.type === "text") {
93
+ parts2.push({ id: createId("part"), type: "text", text: content.text });
94
+ } else {
95
+ parts2.push({
96
+ id: createId("part"),
97
+ type: "data",
98
+ name: content.type,
99
+ data: content
100
+ });
101
+ }
102
+ }
103
+ uiMessages.push({ id: createId("msg"), role: "user", parts: parts2 });
104
+ continue;
105
+ }
106
+ if (message.role === "assistant") {
107
+ const parts2 = [];
108
+ for (const content of message.content) {
109
+ if (content.type === "text") {
110
+ parts2.push({ id: createId("part"), type: "text", text: content.text });
111
+ continue;
112
+ }
113
+ if (content.type === "reasoning") {
114
+ const part = {
115
+ id: createId("part"),
116
+ type: "reasoning",
117
+ text: reasoningDisplayText(content)
118
+ };
119
+ if (content.id !== void 0) {
120
+ part.reasoningId = content.id;
121
+ }
122
+ parts2.push(part);
123
+ continue;
124
+ }
125
+ if (content.type === "tool_call") {
126
+ const part = {
127
+ id: toolPartId(content.id),
128
+ type: "tool",
129
+ toolName: content.function.name,
130
+ toolCallId: content.id,
131
+ state: "input-available",
132
+ input: content.function.arguments
133
+ };
134
+ if (content.callId !== void 0) {
135
+ part.callId = content.callId;
136
+ }
137
+ parts2.push(part);
138
+ continue;
139
+ }
140
+ parts2.push({
141
+ id: createId("part"),
142
+ type: "data",
143
+ name: content.type,
144
+ data: content
145
+ });
146
+ }
147
+ uiMessages.push({ id: message.id ?? createId("msg"), role: "assistant", parts: parts2 });
148
+ continue;
149
+ }
150
+ const parts = [];
151
+ for (const content of message.content) {
152
+ const part = {
153
+ id: toolPartId(content.id),
154
+ type: "tool",
155
+ toolName: "tool",
156
+ toolCallId: content.id,
157
+ state: "output-available",
158
+ output: toolResultContentToJson(content.content)
159
+ };
160
+ if (content.callId !== void 0) {
161
+ part.callId = content.callId;
162
+ }
163
+ parts.push(part);
164
+ }
165
+ uiMessages.push({ id: createId("msg"), role: "tool", parts });
166
+ }
167
+ return uiMessages;
168
+ }
169
+ async function* createCompletionUIStream(model, options) {
170
+ const { messages, ...completionOptions } = options;
171
+ yield* completionStreamToUIStream(
172
+ createCompletionStream(model, {
173
+ ...completionOptions,
174
+ messages: uiMessagesToCoreMessages(messages)
175
+ })
176
+ );
177
+ }
178
+ async function* createAgentUIStream(agent, options) {
179
+ yield* agentStreamToUIStream(agent.prompt(uiMessagesToCoreMessages(options.messages)).stream());
180
+ }
181
+ async function* completionStreamToUIStream(events, options = {}) {
182
+ const messageId = options.messageId ?? createId("msg");
183
+ let accumulatedText = "";
184
+ yield messageStart(messageId);
185
+ for await (const event of events) {
186
+ if (event.type === "text_delta") {
187
+ accumulatedText += event.delta;
188
+ yield {
189
+ type: "text_delta",
190
+ messageId,
191
+ partId: textPartId(messageId),
192
+ delta: event.delta
193
+ };
194
+ continue;
195
+ }
196
+ if (event.type === "reasoning_delta") {
197
+ const partId = reasoningPartId(messageId, event.id);
198
+ yield {
199
+ type: "reasoning_delta",
200
+ messageId,
201
+ partId,
202
+ delta: event.delta
203
+ };
204
+ continue;
205
+ }
206
+ if (event.type === "tool_call_delta") {
207
+ const partId = toolPartId(event.id);
208
+ yield {
209
+ type: "tool_update",
210
+ messageId,
211
+ partId,
212
+ part: {
213
+ id: partId,
214
+ type: "tool",
215
+ toolName: event.name ?? "",
216
+ toolCallId: event.id,
217
+ ...event.callId === void 0 ? {} : { callId: event.callId },
218
+ state: "input-streaming",
219
+ ...event.argumentsDelta === void 0 ? {} : { input: event.argumentsDelta }
220
+ }
221
+ };
222
+ continue;
223
+ }
224
+ if (event.type === "tool_call") {
225
+ yield {
226
+ type: "tool_update",
227
+ messageId,
228
+ partId: toolPartId(event.toolCall.id),
229
+ part: {
230
+ id: toolPartId(event.toolCall.id),
231
+ type: "tool",
232
+ toolName: event.toolCall.function.name,
233
+ toolCallId: event.toolCall.id,
234
+ ...event.toolCall.callId === void 0 ? {} : { callId: event.toolCall.callId },
235
+ state: "input-available",
236
+ input: event.toolCall.function.arguments
237
+ }
238
+ };
239
+ continue;
240
+ }
241
+ if (event.type === "final") {
242
+ const finalText = textFromAssistantContent(event.response.choice);
243
+ const missingText = missingTextDelta(accumulatedText, finalText);
244
+ if (missingText !== void 0) {
245
+ accumulatedText += missingText;
246
+ yield {
247
+ type: "text_delta",
248
+ messageId,
249
+ partId: textPartId(messageId),
250
+ delta: missingText
251
+ };
252
+ }
253
+ const endEvent = {
254
+ type: "message_end",
255
+ messageId,
256
+ usage: event.response.usage
257
+ };
258
+ if (event.response.messageId !== void 0) {
259
+ endEvent.metadata = { providerMessageId: event.response.messageId };
260
+ }
261
+ yield endEvent;
262
+ continue;
263
+ }
264
+ if (event.type === "error") {
265
+ yield { type: "error", error: uiError(event.error) };
266
+ }
267
+ }
268
+ }
269
+ async function* agentStreamToUIStream(events, options = {}) {
270
+ const messageId = options.messageId ?? createId("msg");
271
+ let accumulatedText = "";
272
+ yield messageStart(messageId);
273
+ for await (const event of events) {
274
+ if (event.type === "text_delta") {
275
+ accumulatedText += event.delta;
276
+ yield {
277
+ type: "text_delta",
278
+ messageId,
279
+ partId: textPartId(messageId),
280
+ delta: event.delta
281
+ };
282
+ continue;
283
+ }
284
+ if (event.type === "reasoning_delta") {
285
+ yield {
286
+ type: "reasoning_delta",
287
+ messageId,
288
+ partId: reasoningPartId(messageId, event.id),
289
+ delta: event.delta
290
+ };
291
+ continue;
292
+ }
293
+ if (event.type === "tool_call") {
294
+ yield {
295
+ type: "tool_update",
296
+ messageId,
297
+ partId: toolPartId(event.toolCall.id),
298
+ part: {
299
+ id: toolPartId(event.toolCall.id),
300
+ type: "tool",
301
+ toolName: event.toolCall.function.name,
302
+ toolCallId: event.toolCall.id,
303
+ ...event.toolCall.callId === void 0 ? {} : { callId: event.toolCall.callId },
304
+ state: "input-available",
305
+ input: event.toolCall.function.arguments
306
+ }
307
+ };
308
+ continue;
309
+ }
310
+ if (event.type === "tool_result") {
311
+ const toolCallId = event.toolCallId ?? event.internalCallId;
312
+ yield {
313
+ type: "tool_update",
314
+ messageId,
315
+ partId: toolPartId(toolCallId),
316
+ part: {
317
+ id: toolPartId(toolCallId),
318
+ type: "tool",
319
+ toolName: event.toolName,
320
+ toolCallId,
321
+ ...event.toolCallId === void 0 ? {} : { callId: event.toolCallId },
322
+ state: "output-available",
323
+ output: event.structuredResult === void 0 ? event.result : event.structuredResult
324
+ }
325
+ };
326
+ continue;
327
+ }
328
+ if (event.type === "final") {
329
+ const missingText = missingTextDelta(accumulatedText, event.output);
330
+ if (missingText !== void 0) {
331
+ accumulatedText += missingText;
332
+ yield {
333
+ type: "text_delta",
334
+ messageId,
335
+ partId: textPartId(messageId),
336
+ delta: missingText
337
+ };
338
+ }
339
+ yield {
340
+ type: "message_end",
341
+ messageId,
342
+ usage: event.usage,
343
+ metadata: { runId: event.runId }
344
+ };
345
+ continue;
346
+ }
347
+ if (event.type === "error") {
348
+ yield { type: "error", error: uiError(event.error) };
349
+ }
350
+ }
351
+ }
352
+ function messageStart(messageId) {
353
+ return {
354
+ type: "message_start",
355
+ message: {
356
+ id: messageId,
357
+ role: "assistant",
358
+ parts: []
359
+ }
360
+ };
361
+ }
362
+ function textFromUIParts(parts) {
363
+ return parts.flatMap((part) => part.type === "text" ? [part.text] : []).join("");
364
+ }
365
+ function outputToToolResultContent(output) {
366
+ return [{ type: "text", text: serializeToolResultOutput(output ?? null) }];
367
+ }
368
+ function toolResultContentToJson(content) {
369
+ if (content.length === 1 && content[0]?.type === "text") {
370
+ return content[0].text;
371
+ }
372
+ return content;
373
+ }
374
+ function uiError(error) {
375
+ if (error instanceof Error) {
376
+ return { name: error.name, message: error.message };
377
+ }
378
+ return { message: typeof error === "string" ? error : serializeToolResultOutput(error) };
379
+ }
380
+ function missingTextDelta(current, finalText) {
381
+ if (finalText.length === 0 || finalText === current) {
382
+ return void 0;
383
+ }
384
+ if (finalText.startsWith(current)) {
385
+ return finalText.slice(current.length);
386
+ }
387
+ return current.length === 0 ? finalText : void 0;
388
+ }
389
+ function textPartId(messageId) {
390
+ return `${messageId}_text`;
391
+ }
392
+ function reasoningPartId(messageId, id) {
393
+ return id === void 0 ? `${messageId}_reasoning` : `${messageId}_reasoning_${id}`;
394
+ }
395
+ function toolPartId(toolCallId) {
396
+ return `tool_${toolCallId}`;
397
+ }
398
+ var nextId = 0;
399
+ function createId(prefix) {
400
+ const random = globalThis.crypto?.randomUUID?.();
401
+ if (random !== void 0) {
402
+ return `${prefix}_${random}`;
403
+ }
404
+ nextId += 1;
405
+ return `${prefix}_${nextId.toString(36)}`;
406
+ }
407
+
408
+ export {
409
+ uiMessagesToCoreMessages,
410
+ coreMessagesToUIMessages,
411
+ createCompletionUIStream,
412
+ createAgentUIStream,
413
+ completionStreamToUIStream,
414
+ agentStreamToUIStream
415
+ };
416
+ //# sourceMappingURL=chunk-BYRCLOKA.js.map