Skip to content

Commit 943be0c

Browse files
committed
feat: add use fallback
1 parent 1144264 commit 943be0c

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

packages/workflowai/src/WorkflowAI.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function optionsToRunRequest(
3737
input: Record<string, never>,
3838
options: Omit<RunOptions<true | false>, 'fetch'>
3939
): RunRequest {
40-
const { version, stream, metadata, useCache, privateFields } = options;
40+
const { version, stream, metadata, useCache, privateFields, useFallback } =
41+
options;
4142

4243
return {
4344
task_input: input,
@@ -46,6 +47,7 @@ function optionsToRunRequest(
4647
metadata,
4748
use_cache: useCache || 'auto',
4849
private_fields: privateFields,
50+
use_fallback: useFallback,
4951
};
5052
}
5153

packages/workflowai/src/api/generated/openapi.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export interface components {
8383
* @description Fields marked as private will not be saved, none by default.
8484
*/
8585
private_fields?: ('task_input' | string)[] | null;
86+
/**
87+
* Use Fallback
88+
* @description Whether to use fallback models if the primary model fails.
89+
*/
90+
use_fallback?: 'never' | 'auto' | string[] | null;
8691
};
8792
/**
8893
* TaskGroupProperties

packages/workflowai/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type RunOptions<Stream extends true | false = false> = {
1414
stream?: Stream;
1515
fetch?: FetchOptions;
1616
privateFields?: ('task_input' | 'task_output' | string)[];
17+
useFallback?: 'never' | 'auto' | string[];
1718
};
1819

1920
export type FileContentType =

tests/integration/agent.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,73 @@ describe('run', () => {
202202
expect(error.errorCode).toBeFalsy();
203203
}
204204
});
205+
206+
it('correctly handles useFallback when passed to the run function', async () => {
207+
const run1Fixture = await readFile(fixturePath('run1.json'), 'utf-8');
208+
mockFetch.mockResponseOnce(run1Fixture);
209+
210+
const result = await run({ animal: 'platypus' }, { useFallback: 'never' });
211+
expect(result.output).toEqual({
212+
is_cute: true,
213+
is_dangerous: true,
214+
explanation_of_reasoning: 'Plat plat',
215+
});
216+
217+
expect(mockFetch.mock.calls.length).toEqual(1);
218+
const req = mockFetch.mock.calls[0][0] as Request;
219+
expect(req.url).toEqual(
220+
'https://run.workflowai.com/v1/_/agents/animal-classification/schemas/4/run'
221+
);
222+
expect(req.method).toEqual('POST');
223+
expect(req.headers.get('Authorization')).toEqual('Bearer hello');
224+
const body = await req.json();
225+
expect(body).toEqual({
226+
version: 'production',
227+
stream: false,
228+
task_input: {
229+
animal: 'platypus',
230+
},
231+
use_cache: 'auto',
232+
use_fallback: 'never',
233+
});
234+
});
235+
236+
it('correctly handles useFallback when passed to the agent', async () => {
237+
const run2 = workflowAI.agent<
238+
AnimalClassificationInput,
239+
AnimalClassificationOutput
240+
>({
241+
id: 'animal-classification',
242+
schemaId: 4,
243+
version: 'production',
244+
useFallback: ['gpt-4o-mini', 'gpt-4o'],
245+
});
246+
const run1Fixture = await readFile(fixturePath('run1.json'), 'utf-8');
247+
mockFetch.mockResponseOnce(run1Fixture);
248+
249+
const result = await run2({ animal: 'platypus' });
250+
expect(result.output).toEqual({
251+
is_cute: true,
252+
is_dangerous: true,
253+
explanation_of_reasoning: 'Plat plat',
254+
});
255+
256+
expect(mockFetch.mock.calls.length).toEqual(1);
257+
const req = mockFetch.mock.calls[0][0] as Request;
258+
expect(req.url).toEqual(
259+
'https://run.workflowai.com/v1/_/agents/animal-classification/schemas/4/run'
260+
);
261+
expect(req.method).toEqual('POST');
262+
expect(req.headers.get('Authorization')).toEqual('Bearer hello');
263+
const body = await req.json();
264+
expect(body).toEqual({
265+
version: 'production',
266+
stream: false,
267+
task_input: {
268+
animal: 'platypus',
269+
},
270+
use_cache: 'auto',
271+
use_fallback: ['gpt-4o-mini', 'gpt-4o'],
272+
});
273+
});
205274
});

0 commit comments

Comments
 (0)