|
42 | 42 | func _respond(to prompt: [any Part], schema: FirebaseAI.GenerationSchema?, |
43 | 43 | includeSchemaInPrompt: Bool, options: any GenerationOptionsRepresentable) |
44 | 44 | async throws -> _ModelSessionResponse { |
45 | | - // If the secondary session contains history then a previous fallback occurred. |
46 | | - // Stick with the secondary session to maintain conversation consistency. |
47 | | - if secondary._hasHistory { |
48 | | - return try await secondary._respond( |
49 | | - to: prompt, |
50 | | - schema: schema, |
51 | | - includeSchemaInPrompt: includeSchemaInPrompt, |
52 | | - options: options |
53 | | - ) |
54 | | - } |
55 | | - |
56 | | - do { |
57 | | - // First try the primary session. |
58 | | - return try await primary._respond( |
59 | | - to: prompt, |
60 | | - schema: schema, |
61 | | - includeSchemaInPrompt: includeSchemaInPrompt, |
62 | | - options: options |
63 | | - ) |
64 | | - } catch { |
65 | | - // Do not fallback to second session if the primary session contains history. |
66 | | - if primary._hasHistory { |
67 | | - throw error |
| 45 | + return try await TaskLocals.$isHybridRequest.withValue(true) { |
| 46 | + // If the secondary session contains history then a previous fallback occurred. |
| 47 | + // Stick with the secondary session to maintain conversation consistency. |
| 48 | + if secondary._hasHistory { |
| 49 | + return try await secondary._respond( |
| 50 | + to: prompt, |
| 51 | + schema: schema, |
| 52 | + includeSchemaInPrompt: includeSchemaInPrompt, |
| 53 | + options: options |
| 54 | + ) |
68 | 55 | } |
69 | 56 |
|
70 | | - // Fallback to the second session if the first fails or is unavailable. |
71 | | - return try await secondary._respond( |
72 | | - to: prompt, |
73 | | - schema: schema, |
74 | | - includeSchemaInPrompt: includeSchemaInPrompt, |
75 | | - options: options |
76 | | - ) |
| 57 | + do { |
| 58 | + // First try the primary session. |
| 59 | + return try await primary._respond( |
| 60 | + to: prompt, |
| 61 | + schema: schema, |
| 62 | + includeSchemaInPrompt: includeSchemaInPrompt, |
| 63 | + options: options |
| 64 | + ) |
| 65 | + } catch { |
| 66 | + // Do not fallback to second session if the primary session contains history. |
| 67 | + if primary._hasHistory { |
| 68 | + throw error |
| 69 | + } |
| 70 | + |
| 71 | + // Fallback to the second session if the first fails or is unavailable. |
| 72 | + return try await secondary._respond( |
| 73 | + to: prompt, |
| 74 | + schema: schema, |
| 75 | + includeSchemaInPrompt: includeSchemaInPrompt, |
| 76 | + options: options |
| 77 | + ) |
| 78 | + } |
77 | 79 | } |
78 | 80 | } |
79 | 81 |
|
|
90 | 92 | includeSchemaInPrompt: Bool, |
91 | 93 | options: any GenerationOptionsRepresentable) |
92 | 94 | -> sending AsyncThrowingStream<_ModelSessionResponse, any Error> { |
93 | | - // If the secondary session contains history then a previous fallback occurred. |
94 | | - // Stick with the secondary session to maintain conversation consistency. |
95 | | - if secondary._hasHistory { |
96 | | - return secondary._streamResponse( |
97 | | - to: prompt, |
98 | | - schema: schema, |
99 | | - includeSchemaInPrompt: includeSchemaInPrompt, |
100 | | - options: options |
101 | | - ) |
102 | | - } |
103 | | - |
104 | | - return AsyncThrowingStream { continuation in |
105 | | - let task = Task { |
106 | | - // First try the primary session. |
107 | | - let stream = primary._streamResponse( |
| 95 | + return TaskLocals.$isHybridRequest.withValue(true) { |
| 96 | + // If the secondary session contains history then a previous fallback occurred. |
| 97 | + // Stick with the secondary session to maintain conversation consistency. |
| 98 | + if secondary._hasHistory { |
| 99 | + return secondary._streamResponse( |
108 | 100 | to: prompt, |
109 | 101 | schema: schema, |
110 | 102 | includeSchemaInPrompt: includeSchemaInPrompt, |
111 | 103 | options: options |
112 | 104 | ) |
| 105 | + } |
113 | 106 |
|
114 | | - var didYield = false |
115 | | - do { |
116 | | - for try await snapshot in stream { |
117 | | - didYield = true |
118 | | - continuation.yield(snapshot) |
119 | | - } |
120 | | - continuation.finish() |
121 | | - } catch { |
122 | | - // Do not fallback to second session if the primary session contains history or has |
123 | | - // already yielded data. |
124 | | - if didYield || primary._hasHistory { |
125 | | - continuation.finish(throwing: error) |
126 | | - return |
127 | | - } |
128 | | - |
129 | | - // Fallback to the second session if the first fails or is unavailable. |
130 | | - let stream = secondary._streamResponse( |
| 107 | + return AsyncThrowingStream { continuation in |
| 108 | + let task = Task { |
| 109 | + // First try the primary session. |
| 110 | + let stream = primary._streamResponse( |
131 | 111 | to: prompt, |
132 | 112 | schema: schema, |
133 | 113 | includeSchemaInPrompt: includeSchemaInPrompt, |
134 | 114 | options: options |
135 | 115 | ) |
136 | 116 |
|
| 117 | + var didYield = false |
137 | 118 | do { |
138 | 119 | for try await snapshot in stream { |
| 120 | + didYield = true |
139 | 121 | continuation.yield(snapshot) |
140 | 122 | } |
141 | 123 | continuation.finish() |
142 | 124 | } catch { |
143 | | - continuation.finish(throwing: error) |
| 125 | + // Do not fallback to second session if the primary session contains history or has |
| 126 | + // already yielded data. |
| 127 | + if didYield || primary._hasHistory { |
| 128 | + continuation.finish(throwing: error) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + // Fallback to the second session if the first fails or is unavailable. |
| 133 | + let stream = secondary._streamResponse( |
| 134 | + to: prompt, |
| 135 | + schema: schema, |
| 136 | + includeSchemaInPrompt: includeSchemaInPrompt, |
| 137 | + options: options |
| 138 | + ) |
| 139 | + |
| 140 | + do { |
| 141 | + for try await snapshot in stream { |
| 142 | + continuation.yield(snapshot) |
| 143 | + } |
| 144 | + continuation.finish() |
| 145 | + } catch { |
| 146 | + continuation.finish(throwing: error) |
| 147 | + } |
144 | 148 | } |
145 | 149 | } |
| 150 | + continuation.onTermination = { _ in task.cancel() } |
146 | 151 | } |
147 | | - continuation.onTermination = { _ in task.cancel() } |
148 | 152 | } |
149 | 153 | } |
150 | 154 | } |
|
0 commit comments