-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45b3b83
commit fe89ecb
Showing
16 changed files
with
573 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/backend/server/src/plugins/copilot/workflow/executor/chat-text.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { ChatPrompt, PromptService } from '../../prompt'; | ||
import { CopilotProviderService } from '../../providers'; | ||
import { CopilotChatOptions, CopilotTextProvider } from '../../types'; | ||
import { | ||
NodeData, | ||
WorkflowNodeType, | ||
WorkflowResult, | ||
WorkflowResultType, | ||
} from '../types'; | ||
import { WorkflowExecutorType } from './types'; | ||
import { AutoRegisteredWorkflowExecutor } from './utils'; | ||
|
||
@Injectable() | ||
export class CopilotChatTextExecutor extends AutoRegisteredWorkflowExecutor { | ||
constructor( | ||
private readonly promptService: PromptService, | ||
private readonly providerService: CopilotProviderService | ||
) { | ||
super(); | ||
} | ||
|
||
private async initExecutor( | ||
data: NodeData | ||
): Promise< | ||
[ | ||
NodeData & { nodeType: WorkflowNodeType.Basic }, | ||
ChatPrompt, | ||
CopilotTextProvider, | ||
] | ||
> { | ||
if (data.nodeType !== WorkflowNodeType.Basic) { | ||
throw new Error( | ||
`Executor ${this.type} not support ${data.nodeType} node` | ||
); | ||
} | ||
|
||
const prompt = await this.promptService.get(data.promptName); | ||
if (!prompt) { | ||
throw new Error( | ||
`Prompt ${data.promptName} not found when running workflow node ${data.name}` | ||
); | ||
} | ||
const provider = await this.providerService.getProviderByModel( | ||
prompt.model | ||
); | ||
if (provider && 'generateText' in provider) { | ||
return [data, prompt, provider]; | ||
} | ||
|
||
throw new Error( | ||
`Provider not found for model ${prompt.model} when running workflow node ${data.name}` | ||
); | ||
} | ||
|
||
override get type() { | ||
return WorkflowExecutorType.ChatText; | ||
} | ||
|
||
override async *next( | ||
data: NodeData, | ||
params: Record<string, string>, | ||
options?: CopilotChatOptions | ||
): AsyncIterable<WorkflowResult> { | ||
const [{ paramKey, id }, prompt, provider] = await this.initExecutor(data); | ||
|
||
const finalMessage = prompt.finish(params); | ||
if (paramKey) { | ||
// update params with custom key | ||
yield { | ||
type: WorkflowResultType.Params, | ||
params: { | ||
[paramKey]: await provider.generateText( | ||
finalMessage, | ||
prompt.model, | ||
options | ||
), | ||
}, | ||
}; | ||
} else { | ||
for await (const content of provider.generateTextStream( | ||
finalMessage, | ||
prompt.model, | ||
options | ||
)) { | ||
yield { | ||
type: WorkflowResultType.Content, | ||
nodeId: id, | ||
content, | ||
}; | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/backend/server/src/plugins/copilot/workflow/executor/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { CopilotChatTextExecutor } from './chat-text'; | ||
|
||
export const CopilotWorkflowExecutors = [CopilotChatTextExecutor]; | ||
|
||
export { type WorkflowExecutor, WorkflowExecutorType } from './types'; | ||
export { getWorkflowExecutor } from './utils'; | ||
export { CopilotChatTextExecutor }; |
15 changes: 15 additions & 0 deletions
15
packages/backend/server/src/plugins/copilot/workflow/executor/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CopilotChatOptions } from '../../types'; | ||
import { NodeData, WorkflowResult } from '../types'; | ||
|
||
export enum WorkflowExecutorType { | ||
ChatText = 'ChatText', | ||
} | ||
|
||
export abstract class WorkflowExecutor { | ||
abstract get type(): WorkflowExecutorType; | ||
abstract next( | ||
data: NodeData, | ||
params: Record<string, string | string[]>, | ||
options?: CopilotChatOptions | ||
): AsyncIterable<WorkflowResult>; | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/backend/server/src/plugins/copilot/workflow/executor/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Logger, OnModuleInit } from '@nestjs/common'; | ||
|
||
import { WorkflowExecutor, type WorkflowExecutorType } from './types'; | ||
|
||
const WORKFLOW_EXECUTOR: Map<string, WorkflowExecutor> = new Map(); | ||
|
||
function registerWorkflowExecutor(e: WorkflowExecutor) { | ||
const existing = WORKFLOW_EXECUTOR.get(e.type); | ||
if (existing && existing === e) return false; | ||
WORKFLOW_EXECUTOR.set(e.type, e); | ||
return true; | ||
} | ||
|
||
export function getWorkflowExecutor( | ||
type: WorkflowExecutorType | ||
): WorkflowExecutor { | ||
const executor = WORKFLOW_EXECUTOR.get(type); | ||
if (!executor) { | ||
throw new Error(`Executor ${type} not defined`); | ||
} | ||
|
||
return executor; | ||
} | ||
|
||
export abstract class AutoRegisteredWorkflowExecutor | ||
extends WorkflowExecutor | ||
implements OnModuleInit | ||
{ | ||
onModuleInit() { | ||
this.register(); | ||
} | ||
|
||
register() { | ||
if (registerWorkflowExecutor(this)) { | ||
new Logger(`CopilotWorkflowExecutor:${this.type}`).log( | ||
'Workflow executor registered.' | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.