Skip to content
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

Add sequence number for ai stream objects #1597

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
1 change: 1 addition & 0 deletions assets/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export type AgentMigrationAttributes = {
export type AiDelta = {
__typename?: 'AiDelta';
content: Scalars['String']['output'];
seq: Scalars['Int']['output'];
};

/** A representation of a LLM-derived insight */
Expand Down
1 change: 1 addition & 0 deletions go/client/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/console/ai/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ defmodule Console.AI.Stream do

def stream(), do: Process.get(@stream)

def publish(%__MODULE__{topic: topic}, chunk) when is_binary(topic) do
def publish(%__MODULE__{topic: topic}, delta) when is_binary(topic) do
Absinthe.Subscription.publish(
ConsoleWeb.Endpoint,
%{content: chunk},
delta,
[ai_stream: topic]
)
end
Expand Down
10 changes: 6 additions & 4 deletions lib/console/ai/stream/exec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ defmodule Console.AI.Stream.Exec do
defp handle_openai(_), do: :pass

defp exec(fun, %AIStream{} = stream, reducer) when is_function(reducer, 1) do
Enum.reduce_while(build_stream(fun), [], fn
%AIStream.SSE.Event{data: data}, acc ->
build_stream(fun)
|> Stream.with_index()
|> Enum.reduce_while([], fn
{%AIStream.SSE.Event{data: data}, ind}, acc ->
case reducer.(data) do
c when is_binary(c) ->
AIStream.publish(stream, c)
AIStream.publish(stream, %{content: c, seq: ind})
{:cont, [c | acc]}
_ -> {:cont, acc}
end

{:error, error}, _ -> {:halt, {:error, "ai service error: #{inspect(error)}"}}
{{:error, error}, _}, _ -> {:halt, {:error, "ai service error: #{inspect(error)}"}}

_, acc -> {:cont, acc}
end)
Expand Down
1 change: 1 addition & 0 deletions lib/console/graphql/ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ defmodule Console.GraphQl.AI do
end

object :ai_delta do
field :seq, non_null(:integer)
field :content, non_null(:string)
end

Expand Down
1 change: 1 addition & 0 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6217,6 +6217,7 @@ type ClusterInsightComponent {
}

type AiDelta {
seq: Int!
content: String!
}

Expand Down
2 changes: 1 addition & 1 deletion test/console_web/channels/graphql/ai_subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ConsoleWeb.GraphQl.AISubscriptionTest do
assert_reply(ref, :ok, %{subscriptionId: _})

stream = %Stream{topic: Stream.topic(:thread, thread.id, user)}
Stream.publish(stream, "something")
Stream.publish(stream, %{content: "something", seq: 1})
assert_push("subscription:data", %{result: %{data: %{"aiStream" => %{"content" => "something"}}}})
end
end
Expand Down
Loading