microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.AI.Models.OpenAI/Builders/SequenceBuilder.cs

71lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Buffers;
5using System.Diagnostics;
6
7namespace Microsoft.Teams.AI.Models.OpenAI.Builders;
8
9/// <summary>
10/// https://github.com/openai/openai-dotnet/blob/main/examples/Chat/Example04_FunctionCallingStreaming.cs#L74
11/// </summary>
12[Obsolete("Microsoft.Teams.AI.Models.OpenAI is deprecated and will be removed by end of summer 2026.")]
13public class SequenceBuilder<T>
14{
15 Segment? _first;
16 Segment? _last;
17
18 public void Append(ReadOnlyMemory<T> data)
19 {
20 if (_first is null)
21 {
22 Debug.Assert(_last is null);
23 _first = new Segment(data);
24 _last = _first;
25 }
26 else
27 {
28 _last = _last!.Append(data);
29 }
30 }
31
32 public ReadOnlySequence<T> Build()
33 {
34 if (_first is null)
35 {
36 Debug.Assert(_last is null);
37 return ReadOnlySequence<T>.Empty;
38 }
39
40 if (_first == _last)
41 {
42 Debug.Assert(_first.Next is null);
43 return new ReadOnlySequence<T>(_first.Memory);
44 }
45
46 return new ReadOnlySequence<T>(_first, 0, _last!, _last!.Memory.Length);
47 }
48
49 private sealed class Segment : ReadOnlySequenceSegment<T>
50 {
51 public Segment(ReadOnlyMemory<T> items) : this(items, 0)
52 {
53 }
54
55 private Segment(ReadOnlyMemory<T> items, long runningIndex)
56 {
57 Debug.Assert(runningIndex >= 0);
58 Memory = items;
59 RunningIndex = runningIndex;
60 }
61
62 public Segment Append(ReadOnlyMemory<T> items)
63 {
64 long runningIndex;
65 checked { runningIndex = RunningIndex + Memory.Length; }
66 Segment segment = new(items, runningIndex);
67 Next = segment;
68 return segment;
69 }
70 }
71}