Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

root = true

[{src,scripts}/**.{ts,json,js}]
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
max_line_length = 120
2 changes: 1 addition & 1 deletion .github/workflows/branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: branches
on:
push:
branches:
- "**"
- '**'

permissions:
contents: read
Expand Down
11 changes: 2 additions & 9 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@
"tabWidth": 2,
"singleQuote": true,
"jsxSingleQuote": true,
"importOrder": [
"^@/(.*)$",
"^../(.*)$",
"^./(.*)$",
"^[./]"
],
"importOrder": ["^@/(.*)$", "^../(.*)$", "^./(.*)$", "^[./]"],
"importOrderSeparation": false,
"importOrderSortSpecifiers": true,
"plugins": [
"@trivago/prettier-plugin-sort-imports"
]
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
26 changes: 0 additions & 26 deletions bin/codegen/has-git-diff.ts

This file was deleted.

3 changes: 2 additions & 1 deletion configs/jest.e2e.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const config: Config = {
coverageReporters: ['json-summary', 'text', 'lcov'],
automock: false,
moduleNameMapper: {
'@workflowai/workflowai': '<rootDir>/src',
'@workflowai/workflowai': '<rootDir>/packages/workflowai/src',
'@workflowai/react': '<rootDir>/packages/react/src',
'(.+)\\.js': '$1',
},
};
Expand Down
3 changes: 0 additions & 3 deletions configs/postcjs.ts

This file was deleted.

3 changes: 0 additions & 3 deletions configs/postesm.ts

This file was deleted.

4 changes: 0 additions & 4 deletions configs/tsconfig.build.json

This file was deleted.

9 changes: 0 additions & 9 deletions configs/tsconfig.cjs.json

This file was deleted.

10 changes: 0 additions & 10 deletions configs/tsconfig.esm.json

This file was deleted.

8 changes: 0 additions & 8 deletions configs/tsconfig.types.json

This file was deleted.

5 changes: 3 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
"@types/react-dom": "^18.2.0"
},
"scripts": {
"prebuild": "tsx ../../configs/prebuild.ts",
"prebuild": "npm run clean && tsx ../../configs/prebuild.ts",
"clean": "rm -rf dist/*",
"build": "npm run clean && rollup -c",
"build": "rollup -c",
"dry": "npm run build && npm pub --dry-run",
"prepublishOnly": "npm run build"
}
}
26 changes: 12 additions & 14 deletions packages/workflowai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@workflowai/workflowai",
"version": "1.6.4",
"version": "1.6.5-dev.1",
"type": "module",
"description": "WorkflowAI JS SDK",
"author": "WorkflowAI",
"homepage": "https://workflowai.com",
Expand All @@ -10,14 +11,14 @@
"llm",
"agent"
],
"types": "./dist/types/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
"types": "./dist/index.d.ts",
"import": "./dist/index.esm.js",
"require": "./dist/index.cjs.js"
}
},
"files": [
Expand All @@ -43,18 +44,15 @@
"lint:fix": "eslint --cache --fix --ext .ts ./src",
"check": "npm run lint:check && npm run prettier:check",
"fix": "npm run lint:fix && npm run prettier:fix",
"test": "jest",
"test": "npm run test:unit",
"test:unit": "jest --projects ../../configs/jest.unit.config.ts",
"test:e2e": "jest tests/e2e",
"test:e2e": "jest --projects ../../configs/jest.e2e.config.ts",
"lint-staged": "lint-staged",
"check-version": "tsx ./bin/check-version.ts",
"-------- BUILD": "",
"clean": "rm -rf dist/*",
"prebuild": "tsx ../../configs/prebuild.ts",
"build:types": "tsc -p ./tsconfig.json --declaration --declarationMap --emitDeclarationOnly --outDir ./dist/types",
"build:cjs": "tsc -p ./tsconfig.json --module commonjs --declaration --outDir ./dist/cjs && tsx ../../configs/postcjs.ts",
"build:esm": "tsc -p ./tsconfig.json --module es2020 --target es2020 --outDir ./dist/esm && tsx ../../configs/postesm.ts",
"build": "npm run clean && npm run prebuild && npm run build:types && npm run build:cjs && npm run build:esm",
"prebuild": "npm run clean && tsx ../../configs/prebuild.ts",
"build": "rollup -c",
"dry": "npm run build && npm pub --dry-run",
"prepublishOnly": "npm run build"
}
Expand Down
26 changes: 26 additions & 0 deletions packages/workflowai/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import typescript from 'rollup-plugin-typescript2';

export default {
input: 'src/index.ts', // entry point of your library
output: [
{
file: 'dist/index.cjs.js',
format: 'cjs',
sourcemap: true,
},
{
file: 'dist/index.esm.js',
format: 'esm',
sourcemap: true,
},
],
plugins: [
peerDepsExternal(), // automatically externalize peerDependencies in package.json
resolve(), // resolves node_modules packages
commonjs(), // converts CommonJS modules to ES6
typescript({ tsconfig: './tsconfig.json' }),
],
};
71 changes: 71 additions & 0 deletions packages/workflowai/src/WorkflowAI.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetchMock from 'jest-fetch-mock';
import { WorkflowAI } from './WorkflowAI.js';
import { z } from './schema/zod/zod.js';

Expand Down Expand Up @@ -48,3 +49,73 @@ describe('useTask', () => {
expect(task).toBeDefined();
});
});

describe('sendFeedback', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

it('should successfully send feedback', async () => {
fetchMock.mockResponseOnce(JSON.stringify({}));

await workflowAI.sendFeedback({
feedbackToken: 'test-token',
outcome: 'positive',
comment: 'Great work!',
userID: 'user123',
});

expect(fetchMock).toHaveBeenCalledWith(
'https://test.workflowai.com/v1/feedback',
{
method: 'POST',
body: JSON.stringify({
feedback_token: 'test-token',
outcome: 'positive',
comment: 'Great work!',
user_id: 'user123',
}),
headers: {
'Content-Type': 'application/json',
},
}
);
});

it('should handle negative feedback', async () => {
fetchMock.mockResponseOnce(JSON.stringify({}));

await workflowAI.sendFeedback({
feedbackToken: 'test-token',
outcome: 'negative',
comment: 'Needs improvement',
});

expect(fetchMock).toHaveBeenCalledWith(
'https://test.workflowai.com/v1/feedback',
{
method: 'POST',
body: JSON.stringify({
feedback_token: 'test-token',
outcome: 'negative',
comment: 'Needs improvement',
user_id: undefined,
}),
headers: {
'Content-Type': 'application/json',
},
}
);
});

it('should throw error when feedback request fails', async () => {
fetchMock.mockResponseOnce('', { status: 400 });

await expect(
workflowAI.sendFeedback({
feedbackToken: 'test-token',
outcome: 'positive',
})
).rejects.toThrow('Failed to send feedback');
});
});
46 changes: 44 additions & 2 deletions packages/workflowai/src/WorkflowAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ function optionsToRunRequest(

export class WorkflowAI {
protected api: WorkflowAIApi;
protected api_url: string;

constructor(config?: WorkflowAIConfig | { api: WorkflowAIConfig }) {
// Old constructors used to be { api: WorkflowAIConfig }
const c = config && 'api' in config ? config.api : config;
this.api = initWorkflowAIApi({
...(c ?? {}),
});

this.api_url = this.api.url.replace('https://run.', 'https://api.');
}

protected async runTask<IS extends InputSchema, OS extends OutputSchema>(
Expand Down Expand Up @@ -102,7 +105,12 @@ export class WorkflowAI {
data.task_output
);

return { data: data as RunResponse, response, output };
return {
data: data as RunResponse,
response,
output,
feedbackToken: data.feedback_token,
};
}

// Streaming response, we receive partial results
Expand Down Expand Up @@ -133,6 +141,7 @@ export class WorkflowAI {
data,
output: parsed?.data,
partialOutput: partialParsed?.data?.partial,
feedbackToken: data?.feedback_token as string,
};
}
),
Expand Down Expand Up @@ -229,7 +238,12 @@ export class WorkflowAI {

const output = data.task_output as Output;

return { data: data as RunResponse, response, output };
return {
data: data as RunResponse,
response,
output,
feedbackToken: data.feedback_token,
};
}

// Streaming response, we receive partial results
Expand All @@ -247,6 +261,7 @@ export class WorkflowAI {
return {
data,
output: data?.task_output as DeepPartial<O>,
feedbackToken: data?.feedback_token as string,
};
}
),
Expand Down Expand Up @@ -302,4 +317,31 @@ export class WorkflowAI {
};
return run;
}

public async sendFeedback(req: {
feedbackToken: string;
outcome: 'positive' | 'negative';
comment?: string;
userID?: string;
}) {
const { feedbackToken, outcome, comment, userID } = req;
const body = {
feedback_token: feedbackToken,
outcome,
comment,
user_id: userID,
};

const response = await fetch(`${this.api_url}/v1/feedback`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
});

if (!response.ok) {
throw new Error('Failed to send feedback');
}
}
}
2 changes: 2 additions & 0 deletions packages/workflowai/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type RunResult<O extends Output> = {
data: RunResponse;
response: Response;
output: O;
feedbackToken: string;
};

// Raw async iterator that the API client returns for streaming a task run
Expand All @@ -28,6 +29,7 @@ export type RunStreamEvent<O extends Output> = AsyncIteratorValue<
RawRunStreamResult['stream']
> & {
output: DeepPartial<O> | undefined;
feedbackToken: string | undefined;
};

export type RunStreamResult<O extends Output> = Pick<
Expand Down
Loading