veryfront 0.1.255 → 0.1.256

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/src/deno.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  "name": "veryfront",
3
- "version": "0.1.255",
3
+ "version": "0.1.256",
4
4
  "license": "Apache-2.0",
5
5
  "nodeModulesDir": "auto",
6
6
  "workspace": [
@@ -0,0 +1,149 @@
1
+ import {
2
+ type ConversationAgentRunUsage,
3
+ type ConversationRunProjection,
4
+ finalizeConversationAgentRun,
5
+ } from "./durable.js";
6
+ import type { HostedLifecycleTerminalState } from "./hosted-lifecycle.js";
7
+
8
+ export interface ConversationHostedTerminalStateInput {
9
+ status: HostedLifecycleTerminalState["status"];
10
+ metadata?: HostedLifecycleTerminalState["metadata"];
11
+ terminalErrorCode?: string | null;
12
+ terminalErrorMessage?: string | null;
13
+ }
14
+
15
+ export interface CreateConversationHostedTerminalAdapterOptions {
16
+ authToken: string;
17
+ apiUrl: string;
18
+ run: ConversationRunProjection | null;
19
+ fallbackModelId: string;
20
+ resolveProvider: (modelId: string) => string;
21
+ onTerminalState?: (terminalState: HostedLifecycleTerminalState) => Promise<void> | void;
22
+ }
23
+
24
+ export interface ConversationHostedTerminalAdapter {
25
+ toTerminalState: (state: ConversationHostedTerminalStateInput) => HostedLifecycleTerminalState;
26
+ finalizeRun: (terminalState: HostedLifecycleTerminalState) => Promise<void>;
27
+ cancelRun: (terminalState: HostedLifecycleTerminalState) => Promise<void>;
28
+ onTerminalState: (terminalState: HostedLifecycleTerminalState) => Promise<void>;
29
+ dispatch: (state: ConversationHostedTerminalStateInput) => Promise<HostedLifecycleTerminalState>;
30
+ }
31
+
32
+ type HostedLifecycleUsage = NonNullable<HostedLifecycleTerminalState["metadata"]>["usage"];
33
+
34
+ function buildConversationHostedLifecycleUsage(
35
+ usage: HostedLifecycleUsage | undefined,
36
+ ): HostedLifecycleUsage | undefined {
37
+ if (!usage) {
38
+ return undefined;
39
+ }
40
+
41
+ return {
42
+ ...(usage.inputTokens !== undefined ? { inputTokens: usage.inputTokens } : {}),
43
+ ...(usage.outputTokens !== undefined ? { outputTokens: usage.outputTokens } : {}),
44
+ ...(usage.cachedInputTokens !== undefined
45
+ ? { cachedInputTokens: usage.cachedInputTokens }
46
+ : {}),
47
+ };
48
+ }
49
+
50
+ function buildConversationAgentRunUsage(
51
+ usage: HostedLifecycleUsage | undefined,
52
+ ): ConversationAgentRunUsage | undefined {
53
+ if (!usage) {
54
+ return undefined;
55
+ }
56
+
57
+ const inputTokens = usage.inputTokens ?? 0;
58
+ const outputTokens = usage.outputTokens ?? 0;
59
+
60
+ return {
61
+ inputTokens,
62
+ outputTokens,
63
+ totalTokens: inputTokens + outputTokens,
64
+ };
65
+ }
66
+
67
+ export function toConversationHostedTerminalState(input: {
68
+ fallbackModelId: string;
69
+ state: ConversationHostedTerminalStateInput;
70
+ }): HostedLifecycleTerminalState {
71
+ const modelId = input.state.metadata?.modelId ?? input.fallbackModelId;
72
+ const usage = buildConversationHostedLifecycleUsage(input.state.metadata?.usage);
73
+
74
+ return {
75
+ status: input.state.status,
76
+ ...(modelId || usage
77
+ ? {
78
+ metadata: {
79
+ ...(modelId ? { modelId } : {}),
80
+ ...(usage ? { usage } : {}),
81
+ },
82
+ }
83
+ : {}),
84
+ ...(input.state.terminalErrorCode !== undefined
85
+ ? { terminalErrorCode: input.state.terminalErrorCode }
86
+ : {}),
87
+ ...(input.state.terminalErrorMessage !== undefined
88
+ ? { terminalErrorMessage: input.state.terminalErrorMessage }
89
+ : {}),
90
+ };
91
+ }
92
+
93
+ export function createConversationHostedTerminalAdapter(
94
+ options: CreateConversationHostedTerminalAdapterOptions,
95
+ ): ConversationHostedTerminalAdapter {
96
+ let durableRunFinalized = false;
97
+
98
+ const finalizeDurableRun = async (
99
+ terminalState: HostedLifecycleTerminalState,
100
+ status: HostedLifecycleTerminalState["status"],
101
+ ): Promise<void> => {
102
+ if (!options.run || durableRunFinalized) {
103
+ return;
104
+ }
105
+
106
+ durableRunFinalized = true;
107
+ const modelId = terminalState.metadata?.modelId ?? options.fallbackModelId;
108
+
109
+ await finalizeConversationAgentRun({
110
+ authToken: options.authToken,
111
+ apiUrl: options.apiUrl,
112
+ conversationId: options.run.conversationId,
113
+ runId: options.run.runId,
114
+ status,
115
+ model: modelId,
116
+ provider: options.resolveProvider(modelId),
117
+ usage: buildConversationAgentRunUsage(terminalState.metadata?.usage),
118
+ terminalErrorCode: terminalState.terminalErrorCode,
119
+ terminalErrorMessage: terminalState.terminalErrorMessage,
120
+ });
121
+ };
122
+
123
+ return {
124
+ toTerminalState: (state) =>
125
+ toConversationHostedTerminalState({
126
+ fallbackModelId: options.fallbackModelId,
127
+ state,
128
+ }),
129
+ finalizeRun: (terminalState) => finalizeDurableRun(terminalState, terminalState.status),
130
+ cancelRun: (terminalState) => finalizeDurableRun(terminalState, "cancelled"),
131
+ onTerminalState: async (terminalState) => {
132
+ await options.onTerminalState?.(terminalState);
133
+ },
134
+ dispatch: async (state) => {
135
+ const terminalState = toConversationHostedTerminalState({
136
+ fallbackModelId: options.fallbackModelId,
137
+ state,
138
+ });
139
+
140
+ if (terminalState.status === "cancelled") {
141
+ await finalizeDurableRun(terminalState, "cancelled");
142
+ } else {
143
+ await finalizeDurableRun(terminalState, terminalState.status);
144
+ }
145
+ await options.onTerminalState?.(terminalState);
146
+ return terminalState;
147
+ },
148
+ };
149
+ }
@@ -212,6 +212,13 @@ export {
212
212
  type CreateConversationHostedLifecycleAdapterOptions,
213
213
  createConversationHostedStreamLifecycleAdapter,
214
214
  } from "./conversation-hosted-lifecycle.js";
215
+ export {
216
+ type ConversationHostedTerminalAdapter,
217
+ type ConversationHostedTerminalStateInput,
218
+ createConversationHostedTerminalAdapter,
219
+ type CreateConversationHostedTerminalAdapterOptions,
220
+ toConversationHostedTerminalState,
221
+ } from "./conversation-hosted-terminal.js";
215
222
  export {
216
223
  getConversationRunEventJsonByteLength,
217
224
  normalizeConversationRunEvent,