Skip to content

Commit

Permalink
[JS] fix: remove schema from AI class (#832)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #831 

## Details

#### Change details

- remove schame from action entry and method parameters

## Attestation Checklist

- [x] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (we use
[TypeDoc](https://typedoc.org/) to document our code)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes

### Additional information

> Feel free to add other relevant information below
  • Loading branch information
kuojianlu authored Nov 21, 2023
1 parent 70b69b2 commit 99f4354
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions js/packages/teams-ai/src/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { DefaultModerator } from './moderators';
import { Moderator } from './moderators/Moderator';
import { PredictedDoCommand, PredictedSayCommand, Planner, Plan } from './planners';
import { TurnState } from './TurnState';
import { Schema } from 'jsonschema';

/**
* Entities argument passed to the action handler for AI.DoCommandActionName.
Expand Down Expand Up @@ -358,25 +357,20 @@ export class AI<TState extends TurnState = TurnState> {
* @template TParameters Optional. The type of parameters that the action handler expects.
* @param name Unique name of the action.
* @param handler Function to call when the action is triggered.
* @param schema Optional. Schema for the actions parameters.
* @returns The AI system instance for chaining purposes.
*/
public action<TParameters extends Record<string, any> | undefined>(
name: string | string[],
handler: (context: TurnContext, state: TState, parameters: TParameters, action?: string) => Promise<string>,
schema?: Schema
handler: (context: TurnContext, state: TState, parameters: TParameters, action?: string) => Promise<string>
): this {
(Array.isArray(name) ? name : [name]).forEach((n) => {
if (!this._actions.has(n)) {
this._actions.set(n, { handler, schema, allowOverrides: false });
this._actions.set(n, { handler, allowOverrides: false });
} else {
const entry = this._actions.get(n);
if (entry!.allowOverrides) {
entry!.handler = handler;
entry!.allowOverrides = false; // Only override once
if (schema) {
entry!.schema = schema;
}
} else {
throw new Error(
`The AI.action() method was called with a previously registered action named "${n}".`
Expand All @@ -395,16 +389,14 @@ export class AI<TState extends TurnState = TurnState> {
* @template TParameters Optional. The type of parameters that the action handler expects.
* @param name Unique name of the action.
* @param handler Function to call when the action is triggered.
* @param schema Optional. The schema for the actions parameters.
* @returns The AI system instance for chaining purposes.
*/
public defaultAction<TParameters extends (Record<string, any> | undefined)>(
name: string | string[],
handler: (context: TurnContext, state: TState, parameters: TParameters, action?: string) => Promise<string>,
schema?: Schema
handler: (context: TurnContext, state: TState, parameters: TParameters, action?: string) => Promise<string>
): this {
(Array.isArray(name) ? name : [name]).forEach((n) => {
this._actions.set(n, { handler, schema, allowOverrides: true });
this._actions.set(n, { handler, allowOverrides: true });
});

return this;
Expand Down Expand Up @@ -433,19 +425,6 @@ export class AI<TState extends TurnState = TurnState> {
return await handler(context, state, parameters, action);
}

/**
* Gets the schema for a given action.
* @param action Name of the action to get the schema for.
* @returns The schema for the action or undefined if the action doesn't have a schema.
*/
public getActionSchema(action: string): Schema | undefined {
if (!this._actions.has(action)) {
throw new Error(`Can't find an action named '${action}'.`);
}

return this._actions.get(action)!.schema;
}

/**
* Checks to see if the AI system has a handler for a given action.
* @param action Name of the action to check.
Expand Down Expand Up @@ -586,6 +565,5 @@ export class AI<TState extends TurnState = TurnState> {
*/
interface ActionEntry<TState> {
handler: (context: TurnContext, state: TState, entities?: any, action?: string) => Promise<string>;
schema?: Schema;
allowOverrides: boolean;
}

0 comments on commit 99f4354

Please sign in to comment.