generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 85
com.openai.unity 8.8.8 #436
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
Open
StephenHodgson
wants to merge
9
commits into
main
Choose a base branch
from
development
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d13609
com.openai.unity 8.8.8
StephenHodgson 78d7632
cleanup
StephenHodgson 10e6a06
fix tests
StephenHodgson c1b20db
more cleanup
StephenHodgson 49c7020
Add support for adding assistant messages on user side (#437)
TypeDefinition cb7933f
fix compiler error
StephenHodgson 24301ac
fix webgl
StephenHodgson f4d17fd
PR feedback
StephenHodgson f879e92
fix SpeechClip dispose pattern
StephenHodgson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -46,6 +46,8 @@ obj/ | |
| *.suo | ||
| /*.sln | ||
| *.sln | ||
| /*.slnx | ||
| *.slnx | ||
| *.user | ||
| *.unityproj | ||
| *.ipch | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
86 changes: 73 additions & 13 deletions
86
OpenAI/Packages/com.openai.unity/Runtime/Extensions/TextureExtensions.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -1,34 +1,94 @@ | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Unity.Collections; | ||
| using UnityEngine; | ||
| using Utilities.Extensions; | ||
| using OpenAI.Images; | ||
| using Utilities.Async; | ||
| using System; | ||
| using Utilities.WebRequestRest; | ||
|
|
||
| #if !PLATFORM_WEBGL | ||
| using System.IO; | ||
| #endif | ||
|
|
||
| namespace OpenAI.Extensions | ||
| { | ||
| internal static class TextureExtensions | ||
| public static class TextureExtensions | ||
| { | ||
| public static async Task<(Texture2D, string)> ConvertFromBase64Async(string b64, bool debug, CancellationToken cancellationToken) | ||
| internal static async Task<(Texture2D, Uri)> ConvertFromBase64Async(string b64, bool debug, CancellationToken cancellationToken) | ||
| { | ||
| var imageData = Convert.FromBase64String(b64); | ||
| using var imageData = NativeArrayExtensions.FromBase64String(b64, Allocator.Persistent); | ||
| #if PLATFORM_WEBGL | ||
| var texture = new Texture2D(2, 2); | ||
| #if UNITY_6000_0_OR_NEWER | ||
| texture.LoadImage(imageData); | ||
| return await Task.FromResult((texture, string.Empty)); | ||
| #else | ||
| if (!Rest.TryGetDownloadCacheItem(b64, out var localFilePath)) | ||
| texture.LoadImage(imageData.ToArray()); | ||
| #endif // UNITY_6000_0_OR_NEWER | ||
| return await Task.FromResult((texture, null as Uri)); | ||
| #else | ||
| if (!Rest.TryGetDownloadCacheItem(b64, out Uri localUri)) | ||
| { | ||
| await File.WriteAllBytesAsync(localFilePath, imageData, cancellationToken).ConfigureAwait(true); | ||
| localFilePath = $"file://{localFilePath}"; | ||
| await using var fs = new FileStream(localUri.LocalPath, FileMode.Create, FileAccess.Write); | ||
| await fs.WriteAsync(imageData, cancellationToken: cancellationToken); | ||
| } | ||
|
|
||
| var texture = await Rest.DownloadTextureAsync(localFilePath, parameters: new RestParameters(debug: debug), cancellationToken: cancellationToken); | ||
| Rest.TryGetDownloadCacheItem(b64, out var cachedPath); | ||
| return (texture, cachedPath); | ||
| #endif | ||
| var texture = await Rest.DownloadTextureAsync(localUri.LocalPath, parameters: new RestParameters(debug: debug), cancellationToken: cancellationToken); | ||
| Rest.TryGetDownloadCacheItem(b64, out Uri cachedUri); | ||
| return (texture, cachedUri); | ||
| #endif // !PLATFORM_WEBGL | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Loads a Texture2D from an ImageResult, handling base64, cached path, or URL. | ||
| /// </summary> | ||
| /// <param name="imageResult">The <see cref="ImageResult"/> to load the texture for.</param> | ||
| /// <param name="debug">Optional, debug flag.</param> | ||
| /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> | ||
| /// <returns> | ||
| /// A tuple containing the converted <see cref="Texture2D"/> and the cached file path as a <see cref="Uri"/>. | ||
| /// </returns> | ||
| public static async Task<(Texture2D, Uri)> LoadTextureAsync(this ImageResult imageResult, bool debug = false, CancellationToken cancellationToken = default) | ||
| { | ||
| await Awaiters.UnityMainThread; | ||
|
|
||
| if (imageResult.Texture.IsNull()) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(imageResult.B64_Json)) | ||
| { | ||
| var (texture, cachedUri) = await ConvertFromBase64Async(imageResult.B64_Json, debug, cancellationToken); | ||
| imageResult.Texture = texture; | ||
| imageResult.CachedPathUri = cachedUri; | ||
| } | ||
| else | ||
| { | ||
| Texture2D texture; | ||
| Uri cachedPath; | ||
|
|
||
| if (imageResult.CachedPathUri != null) | ||
| { | ||
| texture = await Rest.DownloadTextureAsync(imageResult.CachedPathUri, parameters: new RestParameters(debug: debug), cancellationToken: cancellationToken); | ||
| cachedPath = imageResult.CachedPathUri; | ||
| } | ||
| else if (imageResult.Uri != null) | ||
| { | ||
| texture = await Rest.DownloadTextureAsync(imageResult.Uri, parameters: new RestParameters(debug: debug), cancellationToken: cancellationToken); | ||
| cachedPath = Rest.TryGetDownloadCacheItem(imageResult.Uri, out var path) ? path : null; | ||
| } | ||
| else | ||
| { | ||
| throw new InvalidOperationException("ImageResult does not contain valid image data."); | ||
| } | ||
|
|
||
| imageResult.Texture = texture; | ||
| imageResult.CachedPathUri = cachedPath; | ||
| } | ||
| } | ||
|
|
||
| return (imageResult.Texture, imageResult.CachedPathUri); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.