Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions sdk/ai/azure-ai-projects/PostEmitter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ foreach ($f in $files) {
Set-Content $f $c -NoNewline
}

# Fix generated multipart upload operations. The emitter currently generates
# JSON builders for multipart endpoints and retains a runtime-model as_dict()
# path even though Projects now uses TypedDict payloads for these request bodies.
$syncOperationsFile = 'azure\ai\projects\operations\_operations.py'
$c = Get-Content $syncOperationsFile -Raw
$c = $c -replace 'from \.\._utils\.utils import prepare_multipart_form_data', 'from .._utils.utils import FileType, prepare_multipart_form_data'
$c = $c -replace 'agent_name: str, \*, code_zip_sha256: str, json: _types\._CreateAgentVersionFromCodeContent, \*\*kwargs: Any', 'agent_name: str, *, code_zip_sha256: str, files: list[FileType], **kwargs: Any'
$c = $c -replace 'name: str, \*, json: _types\.CreateSkillVersionFromFilesBody, \*\*kwargs: Any', 'name: str, *, files: list[FileType], **kwargs: Any'
$c = [regex]::Replace($c, '(?s)(def build_agents_create_version_from_code_request.*?return HttpRequest\(method="POST", url=_url, params=_params, headers=_headers, )json=json(, \*\*kwargs\))', '$1files=files$2', 1)
$c = [regex]::Replace($c, '(?s)(def build_beta_skills_create_from_files_request.*?return HttpRequest\(method="POST", url=_url, params=_params, headers=_headers, )json=json(, \*\*kwargs\))', '$1files=files$2', 1)
Set-Content $syncOperationsFile $c -NoNewline

$files = 'azure\ai\projects\operations\_operations.py', 'azure\ai\projects\aio\operations\_operations.py'
$typedDictMultipartBody = @"
if not isinstance(content, MutableMapping):
raise TypeError("content must be a mapping")
_body = content

"@
foreach ($f in $files) {
$c = Get-Content $f -Raw
$c = $c -replace ' _body = content\.as_dict\(\) if isinstance\(content, _Model\) else content\r?\n', $typedDictMultipartBody
Set-Content $f $c -NoNewline
}

# A block of code in the implementation of "list_memories", in both sync
# and async _operations.py files, needs to be moved up. It's emitted in the wrong place,
# in the inline function named "prepare_request". Instead it should be moved up into the
Expand Down Expand Up @@ -160,6 +185,51 @@ foreach ($f in $files) {
Set-Content $f $c -NoNewline
}

# Generate shared TypedDict model contracts directly into azure-ai-extensions-openai
# and leave local azure.ai.projects files as compatibility re-export shims.
python ..\azure-ai-extensions-openai\_scripts\generate_shared_models.py projects
python _scripts\write_extension_type_shims.py

# Keep public model exports limited to models/enums defined by generated modules.
# Star imports from generated _models.py otherwise pull helper imports like
# datetime, Any, FileType, rest_field, Enum, and CaseInsensitiveEnumMeta into
# azure.ai.projects.models and APIView.
@'
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
# pylint: disable=wrong-import-position

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from ._patch import * # pylint: disable=unused-wildcard-import

from . import _enums as _enums_module
from . import _models as _models_module
from ._models import * # type: ignore # noqa: F401,F403
from ._enums import * # type: ignore # noqa: F401,F403
from ._patch import __all__ as _patch_all
from ._patch import *
from ._patch import patch_sdk as _patch_sdk

__all__ = [
name
for module in (_models_module, _enums_module)
for name, value in vars(module).items()
if not name.startswith("_") and isinstance(value, type) and getattr(value, "__module__", None) == module.__name__
] # pyright: ignore
__all__.extend([p for p in _patch_all if p not in __all__]) # pyright: ignore
for _name in list(globals()):
if not _name.startswith("_") and _name not in __all__:
del globals()[_name]
_patch_sdk()
'@ | Set-Content 'azure\ai\projects\models\__init__.py' -NoNewline


# Finishing by running 'black' tool to format code.
pip install black
Expand Down
49 changes: 49 additions & 0 deletions sdk/ai/azure-ai-projects/_scripts/write_extension_type_shims.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
"""Write local compatibility shims for extension-owned Projects types."""

from __future__ import annotations

import argparse
from pathlib import Path


COPYRIGHT = "# Copyright (c) Microsoft Corporation.\n# Licensed under the MIT license.\n"

TYPES_SHIM = (
COPYRIGHT
+ '"""Compatibility re-export for extension-owned Azure AI Projects generated types."""\n\n'
+ "from azure.ai.extensions.openai.projects._generated import types as _extension_types\n"
+ "from azure.ai.extensions.openai.projects._generated.types import * # type: ignore # noqa: F401,F403\n\n"
+ "for _name in dir(_extension_types):\n"
+ ' if not _name.startswith("__"):\n'
+ " globals()[_name] = getattr(_extension_types, _name)\n"
)

UNIONS_SHIM = (
COPYRIGHT
+ '"""Compatibility re-export for extension-owned Azure AI Projects generated types."""\n\n'
+ "from azure.ai.extensions.openai.projects._generated._unions import * # type: ignore # noqa: F401,F403\n"
)


def _write(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")


def write_shims(package_root: Path) -> None:
_write(package_root / "azure" / "ai" / "projects" / "types.py", TYPES_SHIM)
_write(package_root / "azure" / "ai" / "projects" / "_unions.py", UNIONS_SHIM)


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--package-root", type=Path, default=Path("."))
args = parser.parse_args()
write_shims(args.package_root)


if __name__ == "__main__":
main()
Loading