chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,19 @@
# Copyright (c) Microsoft. All rights reserved.
from samples.getting_started_with_processes.step03.steps.cut_food_step import CutFoodStep
from samples.getting_started_with_processes.step03.steps.cut_food_with_sharpening_step import CutFoodWithSharpeningStep
from samples.getting_started_with_processes.step03.steps.external_step import ExternalStep
from samples.getting_started_with_processes.step03.steps.fry_food_step import FryFoodStep
from samples.getting_started_with_processes.step03.steps.gather_ingredients_step import (
GatherIngredientsStep,
GatherIngredientsWithStockStep,
)
__all__ = [
"ExternalStep",
"CutFoodStep",
"GatherIngredientsStep",
"GatherIngredientsWithStockStep",
"CutFoodWithSharpeningStep",
"FryFoodStep",
]
@@ -0,0 +1,38 @@
# Copyright (c) Microsoft. All rights reserved.
from enum import Enum
from semantic_kernel.functions import kernel_function
from semantic_kernel.processes.kernel_process import (
KernelProcessStep,
KernelProcessStepContext,
kernel_process_step_metadata,
)
@kernel_process_step_metadata("CutFoodStep.V1")
class CutFoodStep(KernelProcessStep):
class Functions(Enum):
ChopFood = "ChopFood"
SliceFood = "SliceFood"
class OutputEvents(Enum):
ChoppingReady = "ChoppingReady"
SlicingReady = "SlicingReady"
def get_action_string(self, food: str, action: str) -> str:
return f"{food}_{action}"
@kernel_function(name=Functions.ChopFood)
async def chop_food(self, context: KernelProcessStepContext, food_actions: list[str]):
food_to_be_cut = food_actions[0]
food_actions.append(self.get_action_string(food_to_be_cut, "chopped"))
print(f"CUTTING_STEP: Ingredient {food_to_be_cut} has been chopped!")
await context.emit_event(process_event=CutFoodStep.OutputEvents.ChoppingReady, data=food_actions)
@kernel_function(name=Functions.SliceFood)
async def slice_food(self, context: KernelProcessStepContext, food_actions: list[str]):
food_to_be_cut = food_actions[0]
food_actions.append(self.get_action_string(food_to_be_cut, "sliced"))
print(f"CUTTING_STEP: Ingredient {food_to_be_cut} has been sliced!")
await context.emit_event(process_event=CutFoodStep.OutputEvents.SlicingReady, data=food_actions)
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft. All rights reserved.
from enum import Enum
from pydantic import Field
from semantic_kernel.functions import kernel_function
from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.processes.kernel_process import (
KernelProcessStep,
KernelProcessStepContext,
KernelProcessStepState,
kernel_process_step_metadata,
)
class CutFoodWithSharpeningState(KernelBaseModel):
knife_sharpness: int = Field(default=5, alias="KnifeSharpness")
needs_sharpening_limit: int = Field(default=3, alias="NeedsSharpeningLimit")
sharpening_boost: int = Field(default=5, alias="SharpeningBoost")
@kernel_process_step_metadata("CutFoodStep.V2")
class CutFoodWithSharpeningStep(KernelProcessStep[CutFoodWithSharpeningState]):
class Functions(Enum):
ChopFood = "ChopFood"
SliceFood = "SliceFood"
SharpenKnife = "SharpenKnife"
class OutputEvents(Enum):
ChoppingReady = "ChoppingReady"
SlicingReady = "SlicingReady"
KnifeNeedsSharpening = "KnifeNeedsSharpening"
KnifeSharpened = "KnifeSharpened"
state: CutFoodWithSharpeningState | None = None
async def activate(self, state: KernelProcessStepState[CutFoodWithSharpeningState]) -> None:
self.state = state.state
def knife_needs_sharpening(self) -> bool:
return self.state.knife_sharpness == self.state.needs_sharpening_limit
def get_action_string(self, food: str, action: str) -> str:
return f"{food}_{action}"
@kernel_function(name=Functions.ChopFood)
async def chop_food(self, context: KernelProcessStepContext, food_actions: list[str]):
food_to_be_cut = food_actions[0]
if self.knife_needs_sharpening():
print(f"CUTTING_STEP: Dull knife, cannot chop {food_to_be_cut} - needs sharpening.")
await context.emit_event(
process_event=CutFoodWithSharpeningStep.OutputEvents.KnifeNeedsSharpening, data=food_actions
)
return
# Update knife sharpness
self.state.knife_sharpness -= 1
food_actions.append(self.get_action_string(food_to_be_cut, "chopped"))
print(
f"CUTTING_STEP: Ingredient {food_to_be_cut} has been chopped! - knife sharpness: {self.state.knife_sharpness}" # noqa: E501
)
await context.emit_event(process_event=CutFoodWithSharpeningStep.OutputEvents.ChoppingReady, data=food_actions)
@kernel_function(name=Functions.SliceFood)
async def slice_food(self, context: KernelProcessStepContext, food_actions: list[str]):
food_to_be_cut = food_actions[0]
if self.knife_needs_sharpening():
print(f"CUTTING_STEP: Dull knife, cannot slice {food_to_be_cut} - needs sharpening.")
await context.emit_event(
process_event=CutFoodWithSharpeningStep.OutputEvents.KnifeNeedsSharpening, data=food_actions
)
return
# Update knife sharpness
self.state.knife_sharpness -= 1
food_actions.append(self.get_action_string(food_to_be_cut, "sliced"))
print(
f"CUTTING_STEP: Ingredient {food_to_be_cut} has been sliced! - knife sharpness: {self.state.knife_sharpness}" # noqa: E501
)
await context.emit_event(process_event=CutFoodWithSharpeningStep.OutputEvents.SlicingReady, data=food_actions)
@kernel_function(name=Functions.SharpenKnife)
async def sharpen_knife(self, context: KernelProcessStepContext, food_actions: list[str]):
self.state.knife_sharpness += self.state.sharpening_boost
print(f"KNIFE SHARPENED: Knife sharpness is now {self.state.knife_sharpness}!")
await context.emit_event(process_event=CutFoodWithSharpeningStep.OutputEvents.KnifeSharpened, data=food_actions)
@@ -0,0 +1,24 @@
# Copyright (c) Microsoft. All rights reserved.
from typing import Any
from semantic_kernel.functions import kernel_function
from semantic_kernel.processes.kernel_process import (
KernelProcessEventVisibility,
KernelProcessStep,
KernelProcessStepContext,
)
class ExternalStep(KernelProcessStep):
external_event_name: str
def __init__(self, external_event_name: str):
super().__init__(external_event_name=external_event_name)
@kernel_function()
async def emit_external_event(self, context: KernelProcessStepContext, data: Any):
await context.emit_event(
process_event=self.external_event_name, data=data, visibility=KernelProcessEventVisibility.Public
)
@@ -0,0 +1,46 @@
# Copyright (c) Microsoft. All rights reserved.
from enum import Enum
from random import Random
from pydantic import Field
from semantic_kernel.functions import kernel_function
from semantic_kernel.processes.kernel_process import (
KernelProcessEventVisibility,
KernelProcessStep,
KernelProcessStepContext,
kernel_process_step_metadata,
)
@kernel_process_step_metadata("FryFoodStep.V1")
class FryFoodStep(KernelProcessStep):
class Functions(Enum):
FryFood = "FryFood"
class OutputEvents(Enum):
FoodRuined = "FoodRuined"
FriedFoodReady = "FriedFoodReady"
random_seed: Random = Field(default_factory=Random)
@kernel_function(name=Functions.FryFood)
async def fry_food(self, context: KernelProcessStepContext, food_actions: list[str]):
food_to_fry = food_actions[0]
fryer_malfunction = self.random_seed.randint(0, 10)
# Oh no! Food got burnt :(
if fryer_malfunction < 5:
food_actions.append(f"{food_to_fry}_frying_failed")
print(f"FRYING_STEP: Ingredient {food_to_fry} got burnt while frying :(")
await context.emit_event(process_event=FryFoodStep.OutputEvents.FoodRuined, data=food_actions)
return
food_actions.append(f"{food_to_fry}_frying_succeeded")
print(f"FRYING_STEP: Ingredient {food_to_fry} is ready!")
await context.emit_event(
process_event=FryFoodStep.OutputEvents.FriedFoodReady,
data=food_actions,
visibility=KernelProcessEventVisibility.Public,
)
@@ -0,0 +1,85 @@
# Copyright (c) Microsoft. All rights reserved.
from enum import Enum
from pydantic import Field
from samples.getting_started_with_processes.step03.models.food_ingredients import FoodIngredients
from semantic_kernel.functions import kernel_function
from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.processes.kernel_process import KernelProcessStep, KernelProcessStepContext, KernelProcessStepState
class GatherIngredientsStep(KernelProcessStep):
class Functions(Enum):
GatherIngredients = "GatherIngredients"
class OutputEvents(Enum):
IngredientsGathered = "IngredientsGathered"
IngredientsOutOfStock = "IngredientsOutOfStock"
ingredient: FoodIngredients
def __init__(self, ingredient: FoodIngredients):
super().__init__(ingredient=ingredient)
@kernel_function(name=Functions.GatherIngredients)
async def gather_ingredients(self, context: KernelProcessStepContext, food_actions: list[str]):
ingredient = self.ingredient.to_friendly_string()
updated_food_actions = []
updated_food_actions.extend(food_actions)
if len(updated_food_actions) == 0:
updated_food_actions.append(ingredient)
updated_food_actions.append(f"{ingredient}_gathered")
print(f"GATHER_INGREDIENT: Gathered ingredient {ingredient}")
await context.emit_event(
process_event=GatherIngredientsStep.OutputEvents.IngredientsGathered, data=updated_food_actions
)
class GatherIngredientsState(KernelBaseModel):
ingredients_stock: int = Field(default=5, alias="IngredientsStock")
class GatherIngredientsWithStockStep(KernelProcessStep[GatherIngredientsState]):
class Functions(Enum):
GatherIngredients = "GatherIngredients"
class OutputEvents(Enum):
IngredientsGathered = "IngredientsGathered"
IngredientsOutOfStock = "IngredientsOutOfStock"
ingredient: FoodIngredients
state: GatherIngredientsState | None = None
def __init__(self, ingredient: FoodIngredients):
super().__init__(ingredient=ingredient)
async def activate(self, state: KernelProcessStepState[GatherIngredientsState]) -> None:
self.state = state.state
@kernel_function(name=Functions.GatherIngredients)
async def gather_ingredients(self, context: KernelProcessStepContext, food_actions: list[str]):
ingredient = self.ingredient.to_friendly_string()
updated_food_actions = []
updated_food_actions.extend(food_actions)
if self.state.ingredients_stock == 0:
print(f"GATHER_INGREDIENT: Could not gather {ingredient} - OUT OF STOCK!")
await context.emit_event(
process_event=GatherIngredientsWithStockStep.OutputEvents.IngredientsOutOfStock,
data=updated_food_actions,
)
return
if len(updated_food_actions) == 0:
updated_food_actions.append(ingredient)
updated_food_actions.append(f"{ingredient}_gathered")
self.state.ingredients_stock -= 1
print(f"GATHER_INGREDIENT: Gathered ingredient {ingredient} - remaining: {self.state.ingredients_stock}")
await context.emit_event(
process_event=GatherIngredientsWithStockStep.OutputEvents.IngredientsGathered,
data=updated_food_actions,
)