Skip to content

Commit bdbc00d

Browse files
authored
Merge branch 'main' into ops-3103
2 parents c2614c2 + db034a5 commit bdbc00d

26 files changed

Lines changed: 372 additions & 258 deletions

File tree

packages/blocks/ai/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createBlock } from '@openops/blocks-framework';
22
import { aiAuth } from '@openops/common';
33
import { BlockCategory } from '@openops/shared';
4-
import { askAi } from './lib/actions/askAi';
4+
import { aiStep } from './lib/actions/ai-step';
55

66
export const ai = createBlock({
77
displayName: 'AI',
@@ -10,6 +10,6 @@ export const ai = createBlock({
1010
minimumSupportedRelease: '0.7.1',
1111
logoUrl: 'https://static.openops.com/blocks/openops-ai.svg',
1212
authors: [],
13-
actions: [askAi],
13+
actions: [aiStep],
1414
triggers: [],
1515
});

packages/blocks/ai/src/lib/actions/askAi.ts renamed to packages/blocks/ai/src/lib/actions/ai-step.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import {
1313
import { AiProviderEnum, analysisLLMSchema } from '@openops/shared';
1414
import { generateObject, generateText } from 'ai';
1515

16-
export const askAi = createAction({
17-
displayName: 'Ask AI',
16+
export const aiStep = createAction({
17+
displayName: 'AI Step',
1818
description:
19-
'Ask AI a question or transform input using an LLM based on a prompt',
19+
'Transform an input into a desired output through an AI analysis',
2020
name: 'analyze',
2121
auth: aiAuth,
2222
isWriteAction: false,

packages/blocks/ai/test/askAi-action.test.ts renamed to packages/blocks/ai/test/ai-step-action.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ jest.mock('ai', () => ({
1313

1414
import { AiProviderEnum, analysisLLMSchema } from '@openops/shared';
1515
import { generateObject, generateText } from 'ai';
16-
import { askAi } from '../src/lib/actions/askAi';
16+
import { aiStep } from '../src/lib/actions/ai-step';
1717

1818
describe('analyze action', () => {
1919
beforeEach(() => {
2020
jest.clearAllMocks();
2121
});
2222

2323
test('should expose correct props', () => {
24-
expect(askAi.props).toMatchObject({
24+
expect(aiStep.props).toMatchObject({
2525
model: {
2626
type: 'DYNAMIC',
2727
displayName: 'Model',
@@ -59,7 +59,7 @@ describe('analyze action', () => {
5959

6060
const context = createContext(auth, { prompt: 'Hello' });
6161

62-
const result = await askAi.run(context as any);
62+
const result = await aiStep.run(context as any);
6363

6464
expect(generateObject).toHaveBeenCalledWith(
6565
expect.objectContaining({
@@ -100,7 +100,7 @@ describe('analyze action', () => {
100100
additionalInput: ['s1', 's2'],
101101
});
102102

103-
const result = await askAi.run(context as any);
103+
const result = await aiStep.run(context as any);
104104

105105
expect(generateObject).toHaveBeenCalledTimes(1);
106106
const args = (generateObject as jest.Mock).mock.calls[0][0];
@@ -134,7 +134,7 @@ describe('analyze action', () => {
134134

135135
const context = createContext(auth, { prompt: 'Hi' });
136136

137-
const result = await askAi.run(context as any);
137+
const result = await aiStep.run(context as any);
138138

139139
expect(getAiProviderLanguageModel).toHaveBeenCalledWith({
140140
provider: AiProviderEnum.OPENAI,
@@ -174,7 +174,7 @@ describe('analyze action', () => {
174174
model: { model: 'gpt-from-props' },
175175
});
176176

177-
const result = await askAi.run(context as any);
177+
const result = await aiStep.run(context as any);
178178

179179
expect(getAiProviderLanguageModel).toHaveBeenCalledWith({
180180
provider: AiProviderEnum.OPENAI,
@@ -217,7 +217,7 @@ describe('analyze action', () => {
217217
const context = createContext(auth, { prompt: 'P' });
218218

219219
isLLMTelemetryEnabled.mockReturnValueOnce(isLLMTelemetryEnabledValue);
220-
await askAi.run(context as any);
220+
await aiStep.run(context as any);
221221

222222
expect(generateObject).toHaveBeenCalledWith(
223223
expect.objectContaining({
@@ -247,7 +247,7 @@ describe('analyze action', () => {
247247

248248
const context = createContext(auth, { prompt: 'Q' });
249249

250-
const result = await askAi.run(context as any);
250+
const result = await aiStep.run(context as any);
251251

252252
expect(generateText).toHaveBeenCalledWith(
253253
expect.objectContaining({ model: 'lm', prompt: 'Q' }),

packages/blocks/framework/src/lib/context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
AppConnectionValue,
3+
EncryptedObject,
34
ExecutionType,
45
FlowRunId,
56
PauseMetadata,
@@ -125,6 +126,8 @@ export type ServerContext = {
125126
apiUrl: string;
126127
publicUrl: string;
127128
token: string;
129+
tablesDatabaseId: number;
130+
tablesDatabaseToken: EncryptedObject;
128131
};
129132
export type BaseActionContext<
130133
ET extends ExecutionType,

packages/engine/src/lib/handler/block-executor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const executeAction: ActionHandler<BlockAction> = async ({
189189
token: constants.engineToken,
190190
apiUrl: constants.internalApiUrl,
191191
publicUrl: constants.publicUrl,
192+
tablesDatabaseId: constants.tablesDatabaseId,
193+
tablesDatabaseToken: constants.tablesDatabaseToken,
192194
},
193195
propsValue: processedInput,
194196
tags: createTagsManager(hookResponse),

packages/engine/src/lib/handler/context/engine-constants.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
EncryptedObject,
23
ExecuteFlowOperation,
34
ExecutePropsOptions,
45
ExecuteStepOperation,
@@ -38,8 +39,6 @@ export class EngineConstants {
3839
public static readonly BLOCK_SOURCES =
3940
process.env.OPS_BLOCKS_SOURCE ?? 'FILE';
4041

41-
private project: Project | null = null;
42-
4342
public get baseCodeDirectory(): string {
4443
return EngineConstants.BASE_CODE_DIRECTORY;
4544
}
@@ -67,12 +66,19 @@ export class EngineConstants {
6766
public readonly serverHandlerId: string | null,
6867
public readonly testRunActionLimits: TestRunLimitSettings,
6968
public readonly isTestRun: boolean,
69+
public readonly tablesDatabaseId: number,
70+
public readonly tablesDatabaseToken: EncryptedObject,
7071
public readonly resumePayload?: ResumePayload,
7172
) {}
7273

73-
public static fromExecuteFlowInput(
74+
public static async fromExecuteFlowInput(
7475
input: ExecuteFlowOperation,
75-
): EngineConstants {
76+
): Promise<EngineConstants> {
77+
const project = await EngineConstants.fetchProject(
78+
input.internalApiUrl,
79+
input.engineToken,
80+
);
81+
7682
return new EngineConstants(
7783
input.executionCorrelationId,
7884
input.flowVersion.flowId,
@@ -96,15 +102,22 @@ export class EngineConstants {
96102
input.serverHandlerId ?? null,
97103
input.flowVersion.testRunActionLimits,
98104
input.runEnvironment === 'TESTING',
105+
project.tablesDatabaseId,
106+
project.tablesDatabaseToken,
99107
input.executionType === ExecutionType.RESUME
100108
? input.resumePayload
101109
: undefined,
102110
);
103111
}
104112

105-
public static fromExecuteStepInput(
113+
public static async fromExecuteStepInput(
106114
input: ExecuteStepOperation,
107-
): EngineConstants {
115+
): Promise<EngineConstants> {
116+
const project = await EngineConstants.fetchProject(
117+
input.internalApiUrl,
118+
input.engineToken,
119+
);
120+
108121
return new EngineConstants(
109122
null,
110123
input.flowVersion.flowId,
@@ -128,12 +141,19 @@ export class EngineConstants {
128141
null,
129142
input.flowVersion.testRunActionLimits,
130143
true,
144+
project.tablesDatabaseId,
145+
project.tablesDatabaseToken,
131146
);
132147
}
133148

134-
public static fromExecutePropertyInput(
149+
public static async fromExecutePropertyInput(
135150
input: ExecutePropsOptions,
136-
): EngineConstants {
151+
): Promise<EngineConstants> {
152+
const project = await EngineConstants.fetchProject(
153+
input.internalApiUrl,
154+
input.engineToken,
155+
);
156+
137157
return new EngineConstants(
138158
null,
139159
input.flowVersion.flowId,
@@ -157,12 +177,19 @@ export class EngineConstants {
157177
null,
158178
input.flowVersion.testRunActionLimits,
159179
true,
180+
project.tablesDatabaseId,
181+
project.tablesDatabaseToken,
160182
);
161183
}
162184

163-
public static fromExecuteTriggerInput(
185+
public static async fromExecuteTriggerInput(
164186
input: ExecuteTriggerOperation<TriggerHookType>,
165-
): EngineConstants {
187+
): Promise<EngineConstants> {
188+
const project = await EngineConstants.fetchProject(
189+
input.internalApiUrl,
190+
input.engineToken,
191+
);
192+
166193
return new EngineConstants(
167194
null,
168195
input.flowVersion.flowId,
@@ -186,24 +213,26 @@ export class EngineConstants {
186213
null,
187214
input.flowVersion.testRunActionLimits,
188215
input.test,
216+
project.tablesDatabaseId,
217+
project.tablesDatabaseToken,
189218
);
190219
}
191220

192-
private async getProject(): Promise<Project> {
193-
if (this.project) {
194-
return this.project;
195-
}
196-
197-
const getWorkerProjectEndpoint = `${this.internalApiUrl}v1/worker/project`;
221+
private static async fetchProject(
222+
internalApiUrl: string,
223+
engineToken: string,
224+
): Promise<Project> {
225+
const getWorkerProjectEndpoint = `${addTrailingSlashIfMissing(
226+
internalApiUrl,
227+
)}v1/worker/project`;
198228

199229
const response = await fetch(getWorkerProjectEndpoint, {
200230
headers: {
201-
Authorization: `Bearer ${this.engineToken}`,
231+
Authorization: `Bearer ${engineToken}`,
202232
},
203233
});
204234

205-
this.project = (await response.json()) as Project;
206-
return this.project;
235+
return (await response.json()) as Project;
207236
}
208237
}
209238

packages/engine/src/lib/helper/block-helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ export const blockHelper = {
124124
token: params.engineToken,
125125
apiUrl: constants.internalApiUrl,
126126
publicUrl: params.publicUrl,
127+
tablesDatabaseId: constants.tablesDatabaseId,
128+
tablesDatabaseToken: constants.tablesDatabaseToken,
127129
},
128130
project: {
129131
id: params.projectId,

packages/engine/src/lib/operations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const executeFlow = async (
4545
context: FlowExecutorContext,
4646
): Promise<EngineResponse<Pick<FlowRunResponse, 'status' | 'error'>>> => {
4747
try {
48-
const constants = EngineConstants.fromExecuteFlowInput(input);
48+
const constants = await EngineConstants.fromExecuteFlowInput(input);
4949

5050
const response = await flowExecutor.triggerFlowExecutor({
5151
trigger: input.flowVersion.trigger,
@@ -92,7 +92,7 @@ async function executeStep(
9292
engineToken: input.engineToken,
9393
stepTestOutputs: input.stepTestOutputs,
9494
}),
95-
constants: EngineConstants.fromExecuteStepInput(input),
95+
constants: await EngineConstants.fromExecuteStepInput(input),
9696
});
9797

9898
const stepResult = output.steps[step.name];
@@ -203,7 +203,7 @@ export async function execute(
203203
stepTestOutputs: input.stepTestOutputs,
204204
}),
205205
searchValue: input.searchValue,
206-
constants: EngineConstants.fromExecutePropertyInput(input),
206+
constants: await EngineConstants.fromExecutePropertyInput(input),
207207
});
208208

209209
return {
@@ -222,7 +222,7 @@ export async function execute(
222222

223223
const output = await triggerHelper.executeTrigger({
224224
params: input,
225-
constants: EngineConstants.fromExecuteTriggerInput(input),
225+
constants: await EngineConstants.fromExecuteTriggerInput(input),
226226
});
227227

228228
return {

packages/engine/src/lib/resolve-variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function resolveVariable(
99
input: ResolveVariableOperation,
1010
): Promise<ResolveVariableResponse> {
1111
try {
12-
const constants = EngineConstants.fromExecuteStepInput({
12+
const constants = await EngineConstants.fromExecuteStepInput({
1313
projectId: input.projectId,
1414
engineToken: input.engineToken,
1515
internalApiUrl: input.internalApiUrl,

packages/engine/test/handler/test-helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const generateMockEngineConstants = (params?: Partial<EngineConstants>):
3030
params?.serverHandlerId ?? null,
3131
params?.testRunActionLimits ?? { isEnabled: false, limits: [] },
3232
params?.isTestRun ?? false,
33+
params?.tablesDatabaseId ?? 1,
34+
params?.tablesDatabaseToken ?? { iv: 'test-iv', data: 'test-data' },
3335
params?.resumePayload,
3436
)
3537
}

0 commit comments

Comments
 (0)