@dremio/js-sdk 0.34.0 → 0.34.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { catchError, map, of, scan, share } from "rxjs";
|
|
16
|
+
import { catchError, endWith, map, of, scan, share, startWith, tap } from "rxjs";
|
|
17
17
|
import { ChatSession } from "./chat/ChatSession.js";
|
|
18
18
|
import { AgentChatResponse } from "./chat/AgentChatResponse.js";
|
|
19
19
|
import { fromTextEventStream } from "../../common/fromTextEventStream.js";
|
|
@@ -24,6 +24,7 @@ import { safeParseResultToResult } from "../../common/safeParseResultToResult.js
|
|
|
24
24
|
import { Err, Ok } from "ts-results-es";
|
|
25
25
|
import { Problem } from "../../common/Problem.js";
|
|
26
26
|
import { AgentChatExchange } from "./chat/eventWrappers/AgentChatExchange.js";
|
|
27
|
+
import { AgentEndTurn } from "../interfaces.js";
|
|
27
28
|
export class AIResource {
|
|
28
29
|
#config;
|
|
29
30
|
constructor(config) {
|
|
@@ -94,7 +95,8 @@ export class AIResource {
|
|
|
94
95
|
* with events from the Agent.
|
|
95
96
|
*/
|
|
96
97
|
createAgentChatExchange$(chatSession, userChatMessage) {
|
|
97
|
-
|
|
98
|
+
const initialAgentChatExchange = new AgentChatExchange(userChatMessage, []);
|
|
99
|
+
return this.sendChatMessage(chatSession)(userChatMessage).pipe(map((eventResult) => eventResult.unwrap()), endWith(new AgentEndTurn({ type: "reply_finished" })), catchError((error) => of(new AgentEndTurn({ error, type: "error" }))), scan((agentChatExchange, agentChatEvent) => agentChatExchange.addChatEvent(agentChatEvent), initialAgentChatExchange), startWith(initialAgentChatExchange), share());
|
|
98
100
|
}
|
|
99
101
|
/**
|
|
100
102
|
* Sends a chat message to the Dremio AI Agent and returns an Observable containing
|
|
@@ -119,7 +121,11 @@ export class AIResource {
|
|
|
119
121
|
throw result.error;
|
|
120
122
|
}
|
|
121
123
|
return result.value;
|
|
122
|
-
})).pipe(map((event) => Ok(AgentChatResponse.fromJSON(JSON.parse(event.data)))),
|
|
124
|
+
})).pipe(map((event) => Ok(AgentChatResponse.fromJSON(JSON.parse(event.data)))), tap((agentChatResponse) => {
|
|
125
|
+
if (agentChatResponse.isOk()) {
|
|
126
|
+
chatSession.assign(agentChatResponse.value.session);
|
|
127
|
+
}
|
|
128
|
+
}), catchError((e) => of(Err(refineChatErrors(e)))), share());
|
|
123
129
|
}
|
|
124
130
|
}
|
|
125
131
|
function refineChatErrors(e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIResource.js","sourceRoot":"","sources":["../../../src/enterprise/ai/AIResource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,wBAAwB,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAE/F,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAElF,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2CAA2C,CAAC;AAC9E,MAAM,OAAO,UAAU;IACrB,OAAO,CAAoC;IAE3C,YAAY,MAAyC;QAC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E,CAAC,iBAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EACtE,UAAU,CAAC,CAAC,CAAY,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC3D,CAAC;IACN,CAAC;CACF;AAWD,SAAS,gBAAgB,CAAC,CAAY;IACpC,IAAI,CAAC,CAAC,IAAI,YAAY,OAAO,EAAE,CAAC;QAC9B,OAAO,CAAC,CAAC,IAA4C,CAAC;IACxD,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { catchError, map, of, scan, share } from \"rxjs\";\nimport { ChatSession } from \"./chat/ChatSession.ts\";\nimport type { UserChatMessage } from \"./chat/UserChatMessage.ts\";\nimport { AgentChatResponse } from \"./chat/AgentChatResponse.ts\";\nimport type { Logger, SonarV4Config, V4Config } from \"../../common/Config.ts\";\nimport { fromTextEventStream } from \"../../common/fromTextEventStream.ts\";\nimport { ModelProvider } from \"./ModelProvider.ts\";\n\nimport * as z from \"zod/mini\";\nimport { modelProviderCreateCodec, modelProviderRetrieveCodec } from \"./modelProviderCodec.js\";\nimport type { SignalParam } from \"../../common/Params.ts\";\nimport { safeParseResultToResult } from \"../../common/safeParseResultToResult.ts\";\nimport type { listModelsPropertiesSchema } from \"./modelProviderConfigSchemas.ts\";\nimport { Err, Ok } from \"ts-results-es\";\nimport type { HttpError } from \"../../common/HttpError.ts\";\nimport { Problem } from \"../../common/Problem.ts\";\nimport { AgentChatExchange } from \"./chat/eventWrappers/AgentChatExchange.ts\";\nexport class AIResource {\n #config: Logger & V4Config & SonarV4Config;\n\n constructor(config: Logger & V4Config & SonarV4Config) {\n this.#config = config;\n }\n\n createModelProvider(properties: z.output<typeof modelProviderCreateCodec>) {\n const body = z.encode(modelProviderCreateCodec, properties);\n return this.#config\n .v4Request(`model-provider/config`, {\n body: JSON.stringify(body),\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n keepalive: true,\n method: \"POST\",\n })\n .map((res) => res.json() as Promise<z.input<typeof modelProviderRetrieveCodec>>)\n .map(\n (entity) => new ModelProvider(this.#config, z.decode(modelProviderRetrieveCodec, entity)),\n );\n }\n\n listModelProviders() {\n const getPage = ({ signal }: SignalParam = {}) => {\n return this.#config\n .v4Request(\"model-provider/config\", {\n headers: {\n Accept: \"application/json\",\n },\n signal,\n })\n .map(\n (res) => res.json() as Promise<{ configs: z.input<typeof modelProviderRetrieveCodec>[] }>,\n )\n .map((response) => ({\n data: response.configs.map((entity) =>\n safeParseResultToResult(z.safeDecode(modelProviderRetrieveCodec, entity)).map(\n (properties) => new ModelProvider(this.#config, properties),\n ),\n ),\n }));\n };\n return {\n async *data({ signal }: SignalParam = {}) {\n yield* (await getPage({ signal }).promise).map((response) => response.data).unwrap();\n },\n getPage,\n };\n }\n\n listAvailableModels(properties: z.infer<typeof listModelsPropertiesSchema>) {\n return this.#config\n .v4Request(\"model-provider:listModels\", {\n body: JSON.stringify({ access: properties }),\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n method: \"POST\",\n })\n .map((res) => res.json() as Promise<{ models: string[] }>)\n .map((result) => new Set(result.models));\n }\n\n retrieveModelProvider(id: string, { signal }: SignalParam = {}) {\n return this.#config\n .v4Request(`model-provider/config/${id}`, {\n headers: {\n Accept: \"application/json\",\n },\n signal,\n })\n .map((res) => res.json() as Promise<z.input<typeof modelProviderRetrieveCodec>>)\n .map(\n (entity) => new ModelProvider(this.#config, z.decode(modelProviderRetrieveCodec, entity)),\n );\n }\n\n /**\n * Creates an Observable that emits each time the `AgentChatExchange` is updated\n * with events from the Agent.\n */\n createAgentChatExchange$(chatSession: ChatSession, userChatMessage: UserChatMessage) {\n return this.sendChatMessage(chatSession)(userChatMessage).pipe(\n map((eventResult) => eventResult.unwrap()),\n scan(\n (agentChatExchange, agentChatEvent) => agentChatExchange.addChatEvent(agentChatEvent),\n new AgentChatExchange(userChatMessage, []),\n ),\n share(),\n );\n }\n\n /**\n * Sends a chat message to the Dremio AI Agent and returns an Observable containing\n * Agent replies.\n */\n sendChatMessage(chatSession: ChatSession) {\n return (chatMessage: UserChatMessage) =>\n fromTextEventStream(({ signal }) =>\n this.#config\n .sonarV4Request(\"agent\", {\n body: JSON.stringify({\n message: chatMessage.toString(),\n sessionId: chatSession.sessionId,\n } satisfies {\n message: string;\n modelProviderId?: string;\n sessionId?: string;\n }),\n headers: {\n Accept: \"text/event-stream\",\n \"Content-Type\": \"application/json\",\n },\n method: \"POST\",\n signal,\n })\n .promise.then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ).pipe(\n map((event) => Ok(AgentChatResponse.fromJSON(JSON.parse(event.data)))),\n catchError((e: HttpError) => of(Err(refineChatErrors(e)))),\n );\n }\n}\n\ntype SendChatMessageProblemCodes =\n | \"https://api.dremio.dev/problems/ai-agent/chat/agent-disabled\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/input-too-large\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/model-provider-not-found\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/no-default-model-provider\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/no-model-provider-privilege\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/preview-disabled\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/rate-limit-exceeded\";\n\nfunction refineChatErrors(e: HttpError) {\n if (e.body instanceof Problem) {\n return e.body as Problem<SendChatMessageProblemCodes>;\n }\n return e;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AIResource.js","sourceRoot":"","sources":["../../../src/enterprise/ai/AIResource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AACjF,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,wBAAwB,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAE/F,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAElF,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAExC,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2CAA2C,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,MAAM,OAAO,UAAU;IACrB,OAAO,CAAoC;IAE3C,YAAY,MAAyC;QACnD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,mBAAmB,CAAC,UAAqD;QACvE,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,wBAAwB,EAAE,UAAU,CAAC,CAAC;QAC5D,OAAO,IAAI,CAAC,OAAO;aAChB,SAAS,CAAC,uBAAuB,EAAE;YAClC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,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,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,iBAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EACtE,GAAG,CAAC,CAAC,iBAAiB,EAAE,EAAE;YACxB,IAAI,iBAAiB,CAAC,IAAI,EAAE,EAAE,CAAC;gBAC7B,WAAW,CAAC,MAAM,CAAC,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACtD,CAAC;QACH,CAAC,CAAC,EACF,UAAU,CAAC,CAAC,CAAY,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1D,KAAK,EAAE,CACR,CAAC;IACN,CAAC;CACF;AAWD,SAAS,gBAAgB,CAAC,CAAY;IACpC,IAAI,CAAC,CAAC,IAAI,YAAY,OAAO,EAAE,CAAC;QAC9B,OAAO,CAAC,CAAC,IAA4C,CAAC;IACxD,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { catchError, endWith, map, of, scan, share, startWith, tap } from \"rxjs\";\nimport { ChatSession } from \"./chat/ChatSession.ts\";\nimport type { UserChatMessage } from \"./chat/UserChatMessage.ts\";\nimport { AgentChatResponse } from \"./chat/AgentChatResponse.ts\";\nimport type { Logger, SonarV4Config, V4Config } from \"../../common/Config.ts\";\nimport { fromTextEventStream } from \"../../common/fromTextEventStream.ts\";\nimport { ModelProvider } from \"./ModelProvider.ts\";\n\nimport * as z from \"zod/mini\";\nimport { modelProviderCreateCodec, modelProviderRetrieveCodec } from \"./modelProviderCodec.js\";\nimport type { SignalParam } from \"../../common/Params.ts\";\nimport { safeParseResultToResult } from \"../../common/safeParseResultToResult.ts\";\nimport type { listModelsPropertiesSchema } from \"./modelProviderConfigSchemas.ts\";\nimport { Err, Ok } from \"ts-results-es\";\nimport type { HttpError } from \"../../common/HttpError.ts\";\nimport { Problem } from \"../../common/Problem.ts\";\nimport { AgentChatExchange } from \"./chat/eventWrappers/AgentChatExchange.ts\";\nimport { AgentEndTurn } from \"../interfaces.ts\";\nexport class AIResource {\n #config: Logger & V4Config & SonarV4Config;\n\n constructor(config: Logger & V4Config & SonarV4Config) {\n this.#config = config;\n }\n\n createModelProvider(properties: z.output<typeof modelProviderCreateCodec>) {\n const body = z.encode(modelProviderCreateCodec, properties);\n return this.#config\n .v4Request(`model-provider/config`, {\n body: JSON.stringify(body),\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n keepalive: true,\n method: \"POST\",\n })\n .map((res) => res.json() as Promise<z.input<typeof modelProviderRetrieveCodec>>)\n .map(\n (entity) => new ModelProvider(this.#config, z.decode(modelProviderRetrieveCodec, entity)),\n );\n }\n\n listModelProviders() {\n const getPage = ({ signal }: SignalParam = {}) => {\n return this.#config\n .v4Request(\"model-provider/config\", {\n headers: {\n Accept: \"application/json\",\n },\n signal,\n })\n .map(\n (res) => res.json() as Promise<{ configs: z.input<typeof modelProviderRetrieveCodec>[] }>,\n )\n .map((response) => ({\n data: response.configs.map((entity) =>\n safeParseResultToResult(z.safeDecode(modelProviderRetrieveCodec, entity)).map(\n (properties) => new ModelProvider(this.#config, properties),\n ),\n ),\n }));\n };\n return {\n async *data({ signal }: SignalParam = {}) {\n yield* (await getPage({ signal }).promise).map((response) => response.data).unwrap();\n },\n getPage,\n };\n }\n\n listAvailableModels(properties: z.infer<typeof listModelsPropertiesSchema>) {\n return this.#config\n .v4Request(\"model-provider:listModels\", {\n body: JSON.stringify({ access: properties }),\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n method: \"POST\",\n })\n .map((res) => res.json() as Promise<{ models: string[] }>)\n .map((result) => new Set(result.models));\n }\n\n retrieveModelProvider(id: string, { signal }: SignalParam = {}) {\n return this.#config\n .v4Request(`model-provider/config/${id}`, {\n headers: {\n Accept: \"application/json\",\n },\n signal,\n })\n .map((res) => res.json() as Promise<z.input<typeof modelProviderRetrieveCodec>>)\n .map(\n (entity) => new ModelProvider(this.#config, z.decode(modelProviderRetrieveCodec, entity)),\n );\n }\n\n /**\n * Creates an Observable that emits each time the `AgentChatExchange` is updated\n * with events from the Agent.\n */\n createAgentChatExchange$(chatSession: ChatSession, userChatMessage: UserChatMessage) {\n const initialAgentChatExchange = new AgentChatExchange(userChatMessage, []);\n\n return this.sendChatMessage(chatSession)(userChatMessage).pipe(\n map((eventResult) => eventResult.unwrap()),\n endWith(new AgentEndTurn({ type: \"reply_finished\" })),\n catchError((error: unknown) => of(new AgentEndTurn({ error, type: \"error\" }))),\n scan(\n (agentChatExchange, agentChatEvent) => agentChatExchange.addChatEvent(agentChatEvent),\n initialAgentChatExchange,\n ),\n startWith(initialAgentChatExchange),\n share(),\n );\n }\n\n /**\n * Sends a chat message to the Dremio AI Agent and returns an Observable containing\n * Agent replies.\n */\n sendChatMessage(chatSession: ChatSession) {\n return (chatMessage: UserChatMessage) =>\n fromTextEventStream(({ signal }) =>\n this.#config\n .sonarV4Request(\"agent\", {\n body: JSON.stringify({\n message: chatMessage.toString(),\n sessionId: chatSession.sessionId,\n } satisfies {\n message: string;\n modelProviderId?: string;\n sessionId?: string;\n }),\n headers: {\n Accept: \"text/event-stream\",\n \"Content-Type\": \"application/json\",\n },\n method: \"POST\",\n signal,\n })\n .promise.then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ).pipe(\n map((event) => Ok(AgentChatResponse.fromJSON(JSON.parse(event.data)))),\n tap((agentChatResponse) => {\n if (agentChatResponse.isOk()) {\n chatSession.assign(agentChatResponse.value.session);\n }\n }),\n catchError((e: HttpError) => of(Err(refineChatErrors(e)))),\n share(),\n );\n }\n}\n\ntype SendChatMessageProblemCodes =\n | \"https://api.dremio.dev/problems/ai-agent/chat/agent-disabled\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/input-too-large\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/model-provider-not-found\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/no-default-model-provider\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/no-model-provider-privilege\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/preview-disabled\"\n | \"https://api.dremio.dev/problems/ai-agent/chat/rate-limit-exceeded\";\n\nfunction refineChatErrors(e: HttpError) {\n if (e.body instanceof Problem) {\n return e.body as Problem<SendChatMessageProblemCodes>;\n }\n return e;\n}\n"]}
|