Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projectSecrets): add CRUDL for project secrets #40

Merged
merged 9 commits into from
Jan 31, 2025
66 changes: 66 additions & 0 deletions src/runs/dtos/run-submit-tool-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { FromSchema, JSONSchema } from 'json-schema-to-ts';

import { runParamsSchema, runSchema } from './run.js';

import { eventSchema } from '@/streaming/dtos/event.js';

export const runSubmitToolInputsParamsSchema = runParamsSchema;
export type RunSubmitToolInputsParams = FromSchema<typeof runSubmitToolInputsParamsSchema>;

export const runSubmitToolInputsBodySchema = {
type: 'object',
required: ['tool_inputs'],
additionalProperties: false,
properties: {
tool_inputs: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: ['tool_call_id', 'inputs'],
properties: {
tool_call_id: { type: 'string' },
inputs: {
type: 'array',
items: {
type: 'object',
required: ['name', 'value'],
additionalProperties: false,
properties: {
name: { type: 'string' },
value: { type: 'string' }
}
}
}
}
}
},
stream: {
type: 'boolean',
nullable: true
}
}
} as const satisfies JSONSchema;
export type RunSubmitToolInputsBody = FromSchema<typeof runSubmitToolInputsBodySchema>;

export const runSubmitToolInputsResponseSchema = runSchema;
export type RunSubmitToolInputsResponse = FromSchema<typeof runSubmitToolInputsResponseSchema>;

export const runSubmitToolInputsStreamSchema = eventSchema;
export type RunSubmitToolInputsStream = FromSchema<typeof runSubmitToolInputsStreamSchema>;
17 changes: 17 additions & 0 deletions src/runs/dtos/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ export const runSchema = {
}
}
},
{
required: ['type', 'submit_tool_inputs'],
properties: {
type: { const: 'submit_tool_inputs' },
submit_tool_inputs: {
type: 'object',
required: ['tool_calls', 'input_fields'],
properties: {
tool_calls: {
type: 'array',
items: toolCallSchema
},
input_fields: { type: 'array', items: { type: 'string' } }
}
}
}
},
{
required: ['type', 'submit_tool_approvals'],
properties: {
Expand Down
6 changes: 4 additions & 2 deletions src/runs/entities/requiredAction.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import { Embeddable, Enum, Property } from '@mikro-orm/core';

import { RequiredToolApprove } from './requiredToolApprove.entity';
import { RequiredToolOutput } from './requiredToolOutput.entity';
import { RequiredToolInput } from './requiredToolInput.entity';

import { generatePrefixedObjectId } from '@/utils/id';

export enum RequiredActionType {
OUTPUT = 'output',
APPROVE = 'approve'
APPROVE = 'approve',
INPUT = 'input'
}

@Embeddable({ abstract: true, discriminatorColumn: 'type' })
Expand All @@ -35,4 +37,4 @@ export abstract class RequiredAction {
type!: RequiredActionType;
}

export type AnyRequiredAction = RequiredToolApprove | RequiredToolOutput;
export type AnyRequiredAction = RequiredToolApprove | RequiredToolInput | RequiredToolOutput;
45 changes: 45 additions & 0 deletions src/runs/entities/requiredToolInput.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Embeddable, Embedded, Property } from '@mikro-orm/core';

import { RequiredAction, RequiredActionType } from './requiredAction.entity';

import { CodeInterpreterCall } from '@/tools/entities/tool-calls/code-interpreter-call.entity';
import { FileSearchCall } from '@/tools/entities/tool-calls/file-search-call.entity';
import { FunctionCall } from '@/tools/entities/tool-calls/function-call.entity';
import { SystemCall } from '@/tools/entities/tool-calls/system-call.entity';
import { UserCall } from '@/tools/entities/tool-calls/user-call.entity';

@Embeddable({ discriminatorValue: RequiredActionType.INPUT })
export class RequiredToolInput extends RequiredAction {
type = RequiredActionType.INPUT;

// Union must be defined in alphabetical order, otherwise Mikro-ORM won't discovered the auto-created virtual polymorphic entity
@Embedded({ object: true })
toolCalls!: (CodeInterpreterCall | FileSearchCall | FunctionCall | SystemCall | UserCall)[];

@Property()
inputFields!: string[];

constructor({ toolCalls, inputFields }: RequiredToolInputInput) {
super();
this.toolCalls = toolCalls;
this.inputFields = inputFields;
}
}

export type RequiredToolInputInput = Pick<RequiredToolInput, 'toolCalls' | 'inputFields'>;
3 changes: 2 additions & 1 deletion src/runs/entities/run.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RUN_EXPIRATION_MILLISECONDS } from '../execution/constants.js';
import { RequiredToolApprove } from './requiredToolApprove.entity.js';
import { RequiredToolOutput } from './requiredToolOutput.entity.js';
import { ToolApproval } from './toolApproval.entity.js';
import { RequiredToolInput } from './requiredToolInput.entity.js';

import { Assistant } from '@/assistants/assistant.entity.js';
import { Thread } from '@/threads/thread.entity.js';
Expand Down Expand Up @@ -75,7 +76,7 @@ export class Run extends PrincipalScopedEntity {

// Union must be defined in alphabetical order, otherwise Mikro-ORM won't discovered the auto-created virtual polymorphic entity
@Embedded({ object: true })
requiredAction?: RequiredToolApprove | RequiredToolOutput;
requiredAction?: RequiredToolApprove | RequiredToolInput | RequiredToolOutput;

@Embedded({ object: true })
toolApprovals?: ToolApproval[];
Expand Down
77 changes: 9 additions & 68 deletions src/runs/execution/event-handlers/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { ref } from '@mikro-orm/core';
import { Role } from 'bee-agent-framework/llms/primitives/message';
import { BeeCallbacks } from 'bee-agent-framework/agents/bee/types';
import { Summary } from 'prom-client';
import { ToolError } from 'bee-agent-framework/tools/base';
import {
StreamlitEvents as StreamlitEventsFramework,
StreamlitRunOutput
Expand All @@ -30,7 +29,12 @@ import { Agent } from '../constants';

import { AgentContext } from '@/runs/execution/execute.js';
import { getLogger } from '@/logger.js';
import { createToolCall, finalizeToolCall } from '@/runs/execution/tools/helpers.js';
import {
createToolCall,
finalizeToolCall,
requireToolApproval,
requireToolInput
} from '@/runs/execution/tools/helpers.js';
import { RunStep, RunStepStatus } from '@/run-steps/entities/run-step.entity.js';
import { RunStepToolCalls } from '@/run-steps/entities/details/run-step-tool-calls.entity.js';
import { ORM } from '@/database.js';
Expand All @@ -43,11 +47,6 @@ import { RunStatus } from '@/runs/entities/run.entity.js';
import { APIError } from '@/errors/error.entity.js';
import { jobRegistry } from '@/metrics.js';
import { EmitterEvent } from '@/run-steps/entities/emitter-event.entity';
import { createApproveChannel, toRunDto } from '@/runs/runs.service';
import { RequiredToolApprove } from '@/runs/entities/requiredToolApprove.entity';
import { ToolApprovalType } from '@/runs/entities/toolApproval.entity';
import { ToolType } from '@/tools/entities/tool/tool.entity';
import { withRedisClient } from '@/redis.js';
import { Trace } from '@/observe/entities/trace.entity';

const agentToolExecutionTime = new Summary({
Expand Down Expand Up @@ -103,67 +102,9 @@ export function createBeeStreamingHandler(ctx: AgentContext) {
data: toRunStepDto(ctx.runStep)
});

const toolCall = ctx.toolCall;
if (toolCall) {
if (
ctx.run.toolApprovals?.find(
(approval) =>
approval.toolId ===
(toolCall.type === ToolType.USER
? toolCall.tool.id
: toolCall.type === ToolType.SYSTEM
? toolCall.toolId
: toolCall.type)
)?.requireApproval === ToolApprovalType.ALWAYS
) {
await withRedisClient(
(client) =>
new Promise((resolve, reject) => {
client.subscribe(createApproveChannel(ctx.run, toolCall), async (err) => {
try {
if (err) {
reject(err);
} else {
ctx.run.requireAction(
new RequiredToolApprove({
toolCalls: [...(ctx.run.requiredAction?.toolCalls ?? []), toolCall]
})
);
await ORM.em.flush();
await ctx.publish({
event: 'thread.run.requires_action',
data: toRunDto(ctx.run)
});
await ctx.publish({
event: 'done',
data: '[DONE]'
});
}
} catch (err) {
reject(err);
}
});
client.on('message', async (_, approval) => {
try {
ctx.run.submitAction();
await ORM.em.flush();
if (approval !== 'true') {
reject(
new ToolError('User has not approved this tool to run.', [], {
isFatal: false,
isRetryable: false
})
);
}
resolve(true);
} catch (err) {
reject(err);
}
});
})
);
}
}
await requireToolApproval(ctx);

await requireToolInput(ctx);

await ctx.publish({
event: 'thread.run.step.in_progress',
Expand Down
Loading
Loading