(opts: {
agentIterations?: Array<Array<StreamChunk>>
structuredValue?: Person
structuredRunFinishedUsage?: RunFinishedEvent['usage']
structuredOutputThrows?: Error
noNativeStructuredOutputStream?: boolean
recordStructuredCalls?: Recorder
})
| 137 | } |
| 138 | |
| 139 | function makeAdapter(opts: { |
| 140 | agentIterations?: Array<Array<StreamChunk>> |
| 141 | structuredValue?: Person |
| 142 | structuredRunFinishedUsage?: RunFinishedEvent['usage'] |
| 143 | structuredOutputThrows?: Error |
| 144 | noNativeStructuredOutputStream?: boolean |
| 145 | recordStructuredCalls?: Recorder |
| 146 | }) { |
| 147 | const value = opts.structuredValue ?? PERSON |
| 148 | const recordIn = opts.recordStructuredCalls |
| 149 | |
| 150 | const { adapter } = createMockAdapter({ |
| 151 | iterations: opts.agentIterations, |
| 152 | structuredOutput: async (o) => { |
| 153 | if (recordIn) { |
| 154 | recordIn.push({ |
| 155 | chatOptions: o.chatOptions, |
| 156 | outputSchema: o.outputSchema, |
| 157 | }) |
| 158 | } |
| 159 | if (opts.structuredOutputThrows) throw opts.structuredOutputThrows |
| 160 | return { data: value, rawText: JSON.stringify(value) } |
| 161 | }, |
| 162 | ...(opts.noNativeStructuredOutputStream |
| 163 | ? {} |
| 164 | : { |
| 165 | structuredOutputStream: (o) => { |
| 166 | if (recordIn) { |
| 167 | recordIn.push({ |
| 168 | chatOptions: o.chatOptions, |
| 169 | outputSchema: o.outputSchema, |
| 170 | }) |
| 171 | } |
| 172 | const chunks = buildStructuredStream( |
| 173 | value, |
| 174 | opts.structuredRunFinishedUsage, |
| 175 | ) |
| 176 | return (async function* () { |
| 177 | for (const c of chunks) yield c |
| 178 | })() |
| 179 | }, |
| 180 | }), |
| 181 | }) |
| 182 | |
| 183 | return adapter |
| 184 | } |
| 185 | |
| 186 | describe('chat({ outputSchema }) — Promise<T> path', () => { |
| 187 | it('middleware observes chunks attributed to phase=structuredOutput', async () => { |
no test coverage detected