@osdk/aip-core 0.2.0

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.
Files changed (131) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +211 -0
  3. package/ai-sdk.d.ts +17 -0
  4. package/build/browser/ai-sdk/convert-prompt.js +136 -0
  5. package/build/browser/ai-sdk/convert-prompt.js.map +1 -0
  6. package/build/browser/ai-sdk/foundry-chat-language-model.js +386 -0
  7. package/build/browser/ai-sdk/foundry-chat-language-model.js.map +1 -0
  8. package/build/browser/ai-sdk/map-finish-reason.js +42 -0
  9. package/build/browser/ai-sdk/map-finish-reason.js.map +1 -0
  10. package/build/browser/ai-sdk/provider.js +62 -0
  11. package/build/browser/ai-sdk/provider.js.map +1 -0
  12. package/build/browser/ai-sdk/types.js +2 -0
  13. package/build/browser/ai-sdk/types.js.map +1 -0
  14. package/build/browser/generateText.js +76 -0
  15. package/build/browser/generateText.js.map +1 -0
  16. package/build/browser/index.js +23 -0
  17. package/build/browser/index.js.map +1 -0
  18. package/build/browser/internal/__test__/mockPlatformClient.js +36 -0
  19. package/build/browser/internal/__test__/mockPlatformClient.js.map +1 -0
  20. package/build/browser/internal/ids.js +23 -0
  21. package/build/browser/internal/ids.js.map +1 -0
  22. package/build/browser/internal/openAi.js +293 -0
  23. package/build/browser/internal/openAi.js.map +1 -0
  24. package/build/browser/internal/options.js +58 -0
  25. package/build/browser/internal/options.js.map +1 -0
  26. package/build/browser/internal/runStep.js +107 -0
  27. package/build/browser/internal/runStep.js.map +1 -0
  28. package/build/browser/internal/streamStep.js +223 -0
  29. package/build/browser/internal/streamStep.js.map +1 -0
  30. package/build/browser/internal/uiMessageBridge.js +55 -0
  31. package/build/browser/internal/uiMessageBridge.js.map +1 -0
  32. package/build/browser/lmsChatTransport.js +185 -0
  33. package/build/browser/lmsChatTransport.js.map +1 -0
  34. package/build/browser/model.js +74 -0
  35. package/build/browser/model.js.map +1 -0
  36. package/build/browser/public/ai-sdk.js +18 -0
  37. package/build/browser/public/ai-sdk.js.map +1 -0
  38. package/build/browser/streamText.js +210 -0
  39. package/build/browser/streamText.js.map +1 -0
  40. package/build/browser/types.js +2 -0
  41. package/build/browser/types.js.map +1 -0
  42. package/build/browser/uiMessage.js +34 -0
  43. package/build/browser/uiMessage.js.map +1 -0
  44. package/build/cjs/index.cjs +1012 -0
  45. package/build/cjs/index.cjs.map +1 -0
  46. package/build/cjs/index.d.cts +467 -0
  47. package/build/cjs/model-B5uS56VB.d.cts +45 -0
  48. package/build/cjs/public/ai-sdk.cjs +534 -0
  49. package/build/cjs/public/ai-sdk.cjs.map +1 -0
  50. package/build/cjs/public/ai-sdk.d.cts +49 -0
  51. package/build/esm/ai-sdk/convert-prompt.js +136 -0
  52. package/build/esm/ai-sdk/convert-prompt.js.map +1 -0
  53. package/build/esm/ai-sdk/foundry-chat-language-model.js +386 -0
  54. package/build/esm/ai-sdk/foundry-chat-language-model.js.map +1 -0
  55. package/build/esm/ai-sdk/map-finish-reason.js +42 -0
  56. package/build/esm/ai-sdk/map-finish-reason.js.map +1 -0
  57. package/build/esm/ai-sdk/provider.js +62 -0
  58. package/build/esm/ai-sdk/provider.js.map +1 -0
  59. package/build/esm/ai-sdk/types.js +2 -0
  60. package/build/esm/ai-sdk/types.js.map +1 -0
  61. package/build/esm/generateText.js +76 -0
  62. package/build/esm/generateText.js.map +1 -0
  63. package/build/esm/index.js +23 -0
  64. package/build/esm/index.js.map +1 -0
  65. package/build/esm/internal/__test__/mockPlatformClient.js +36 -0
  66. package/build/esm/internal/__test__/mockPlatformClient.js.map +1 -0
  67. package/build/esm/internal/ids.js +23 -0
  68. package/build/esm/internal/ids.js.map +1 -0
  69. package/build/esm/internal/openAi.js +293 -0
  70. package/build/esm/internal/openAi.js.map +1 -0
  71. package/build/esm/internal/options.js +58 -0
  72. package/build/esm/internal/options.js.map +1 -0
  73. package/build/esm/internal/runStep.js +107 -0
  74. package/build/esm/internal/runStep.js.map +1 -0
  75. package/build/esm/internal/streamStep.js +223 -0
  76. package/build/esm/internal/streamStep.js.map +1 -0
  77. package/build/esm/internal/uiMessageBridge.js +55 -0
  78. package/build/esm/internal/uiMessageBridge.js.map +1 -0
  79. package/build/esm/lmsChatTransport.js +185 -0
  80. package/build/esm/lmsChatTransport.js.map +1 -0
  81. package/build/esm/model.js +74 -0
  82. package/build/esm/model.js.map +1 -0
  83. package/build/esm/public/ai-sdk.js +18 -0
  84. package/build/esm/public/ai-sdk.js.map +1 -0
  85. package/build/esm/streamText.js +210 -0
  86. package/build/esm/streamText.js.map +1 -0
  87. package/build/esm/types.js +2 -0
  88. package/build/esm/types.js.map +1 -0
  89. package/build/esm/uiMessage.js +34 -0
  90. package/build/esm/uiMessage.js.map +1 -0
  91. package/build/types/ai-sdk/convert-prompt.d.ts +19 -0
  92. package/build/types/ai-sdk/convert-prompt.d.ts.map +1 -0
  93. package/build/types/ai-sdk/foundry-chat-language-model.d.ts +20 -0
  94. package/build/types/ai-sdk/foundry-chat-language-model.d.ts.map +1 -0
  95. package/build/types/ai-sdk/map-finish-reason.d.ts +2 -0
  96. package/build/types/ai-sdk/map-finish-reason.d.ts.map +1 -0
  97. package/build/types/ai-sdk/provider.d.ts +46 -0
  98. package/build/types/ai-sdk/provider.d.ts.map +1 -0
  99. package/build/types/ai-sdk/types.d.ts +1 -0
  100. package/build/types/ai-sdk/types.d.ts.map +1 -0
  101. package/build/types/generateText.d.ts +56 -0
  102. package/build/types/generateText.d.ts.map +1 -0
  103. package/build/types/index.d.ts +12 -0
  104. package/build/types/index.d.ts.map +1 -0
  105. package/build/types/internal/__test__/mockPlatformClient.d.ts +13 -0
  106. package/build/types/internal/__test__/mockPlatformClient.d.ts.map +1 -0
  107. package/build/types/internal/ids.d.ts +1 -0
  108. package/build/types/internal/ids.d.ts.map +1 -0
  109. package/build/types/internal/openAi.d.ts +107 -0
  110. package/build/types/internal/openAi.d.ts.map +1 -0
  111. package/build/types/internal/options.d.ts +4 -0
  112. package/build/types/internal/options.d.ts.map +1 -0
  113. package/build/types/internal/runStep.d.ts +29 -0
  114. package/build/types/internal/runStep.d.ts.map +1 -0
  115. package/build/types/internal/streamStep.d.ts +52 -0
  116. package/build/types/internal/streamStep.d.ts.map +1 -0
  117. package/build/types/internal/uiMessageBridge.d.ts +15 -0
  118. package/build/types/internal/uiMessageBridge.d.ts.map +1 -0
  119. package/build/types/lmsChatTransport.d.ts +35 -0
  120. package/build/types/lmsChatTransport.d.ts.map +1 -0
  121. package/build/types/model.d.ts +42 -0
  122. package/build/types/model.d.ts.map +1 -0
  123. package/build/types/public/ai-sdk.d.ts +2 -0
  124. package/build/types/public/ai-sdk.d.ts.map +1 -0
  125. package/build/types/streamText.d.ts +82 -0
  126. package/build/types/streamText.d.ts.map +1 -0
  127. package/build/types/types.d.ts +189 -0
  128. package/build/types/types.d.ts.map +1 -0
  129. package/build/types/uiMessage.d.ts +85 -0
  130. package/build/types/uiMessage.d.ts.map +1 -0
  131. package/package.json +87 -0
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export function mapFinishReason(raw) {
18
+ let unified;
19
+ switch (raw) {
20
+ case "stop":
21
+ unified = "stop";
22
+ break;
23
+ case "length":
24
+ unified = "length";
25
+ break;
26
+ case "tool_calls":
27
+ case "function_call":
28
+ unified = "tool-calls";
29
+ break;
30
+ case "content_filter":
31
+ unified = "content-filter";
32
+ break;
33
+ default:
34
+ unified = "other";
35
+ break;
36
+ }
37
+ return {
38
+ unified,
39
+ raw
40
+ };
41
+ }
42
+ //# sourceMappingURL=map-finish-reason.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-finish-reason.js","names":["mapFinishReason","raw","unified"],"sources":["map-finish-reason.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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 type { LanguageModelV3FinishReason } from \"@ai-sdk/provider\";\n\nexport function mapFinishReason(\n raw: string | undefined,\n): LanguageModelV3FinishReason {\n let unified: LanguageModelV3FinishReason[\"unified\"];\n switch (raw) {\n case \"stop\":\n unified = \"stop\";\n break;\n case \"length\":\n unified = \"length\";\n break;\n case \"tool_calls\":\n case \"function_call\":\n unified = \"tool-calls\";\n break;\n case \"content_filter\":\n unified = \"content-filter\";\n break;\n default:\n unified = \"other\";\n break;\n }\n return { unified, raw };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA,OAAO,SAASA,eAAeA,CAC7BC,GAAuB,EACM;EAC7B,IAAIC,OAA+C;EACnD,QAAQD,GAAG;IACT,KAAK,MAAM;MACTC,OAAO,GAAG,MAAM;MAChB;IACF,KAAK,QAAQ;MACXA,OAAO,GAAG,QAAQ;MAClB;IACF,KAAK,YAAY;IACjB,KAAK,eAAe;MAClBA,OAAO,GAAG,YAAY;MACtB;IACF,KAAK,gBAAgB;MACnBA,OAAO,GAAG,gBAAgB;MAC1B;IACF;MACEA,OAAO,GAAG,OAAO;MACjB;EACJ;EACA,OAAO;IAAEA,OAAO;IAAED;EAAI,CAAC;AACzB","ignoreList":[]}
@@ -0,0 +1,62 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { FoundryChatLanguageModel } from "./foundry-chat-language-model.js";
18
+
19
+ /**
20
+ * A Foundry AI provider for the AI SDK.
21
+ *
22
+ * Can be called directly as a function to get a language model, or use
23
+ * the `.languageModel()` method.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { generateText } from "ai";
28
+ * import { createFoundryAI } from "@osdk/aip-core/ai-sdk";
29
+ *
30
+ * const foundryAI = createFoundryAI({ client });
31
+ * const { text } = await generateText({
32
+ * model: foundryAI("gpt-4o"),
33
+ * prompt: "Hello, world",
34
+ * });
35
+ * ```
36
+ */
37
+
38
+ /**
39
+ * Create a Foundry AI provider for the AI SDK.
40
+ *
41
+ * @param options - Configuration including the Foundry PlatformClient.
42
+ * @returns A provider that can be called directly or via `.languageModel()`.
43
+ */
44
+ export function createFoundryAI(options) {
45
+ const createLanguageModel = model => {
46
+ const identifier = typeof model === "string" ? {
47
+ type: "lmsModel",
48
+ apiName: model
49
+ } : model;
50
+ const id = identifier.type === "lmsModel" ? identifier.apiName : identifier.registeredModelRid;
51
+ return new FoundryChatLanguageModel(`foundry/${id}`, {
52
+ client: options.client,
53
+ identifier
54
+ });
55
+ };
56
+ const provider = function (model) {
57
+ return createLanguageModel(model);
58
+ };
59
+ provider.languageModel = createLanguageModel;
60
+ return provider;
61
+ }
62
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","names":["FoundryChatLanguageModel","createFoundryAI","options","createLanguageModel","model","identifier","type","apiName","id","registeredModelRid","client","provider","languageModel"],"sources":["provider.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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 type { LanguageModelV3 } from \"@ai-sdk/provider\";\nimport type { PlatformClient } from \"@osdk/client\";\nimport { FoundryChatLanguageModel } from \"./foundry-chat-language-model.js\";\nimport type { ModelIdentifier } from \"./types.js\";\n\nexport interface FoundryAIProviderOptions {\n /** Foundry PlatformClient providing the auth token + base URL. */\n client: PlatformClient;\n}\n\n/**\n * A Foundry AI provider for the AI SDK.\n *\n * Can be called directly as a function to get a language model, or use\n * the `.languageModel()` method.\n *\n * @example\n * ```ts\n * import { generateText } from \"ai\";\n * import { createFoundryAI } from \"@osdk/aip-core/ai-sdk\";\n *\n * const foundryAI = createFoundryAI({ client });\n * const { text } = await generateText({\n * model: foundryAI(\"gpt-4o\"),\n * prompt: \"Hello, world\",\n * });\n * ```\n */\nexport interface FoundryAIProvider {\n /**\n * Create a language model for the given model name or identifier.\n *\n * @param model - LMS model API name (e.g. \"gpt-4o\") or a structured ModelIdentifier.\n */\n (model: string | ModelIdentifier): LanguageModelV3;\n\n /**\n * Create a language model for the given model name or identifier.\n *\n * @param model - LMS model API name (e.g. \"gpt-4o\") or a structured ModelIdentifier.\n */\n languageModel(model: string | ModelIdentifier): LanguageModelV3;\n}\n\n/**\n * Create a Foundry AI provider for the AI SDK.\n *\n * @param options - Configuration including the Foundry PlatformClient.\n * @returns A provider that can be called directly or via `.languageModel()`.\n */\nexport function createFoundryAI(\n options: FoundryAIProviderOptions,\n): FoundryAIProvider {\n const createLanguageModel = (\n model: string | ModelIdentifier,\n ): LanguageModelV3 => {\n const identifier: ModelIdentifier = typeof model === \"string\"\n ? { type: \"lmsModel\", apiName: model }\n : model;\n\n const id = identifier.type === \"lmsModel\"\n ? identifier.apiName\n : identifier.registeredModelRid;\n\n return new FoundryChatLanguageModel(`foundry/${id}`, {\n client: options.client,\n identifier,\n });\n };\n\n const provider = function(\n model: string | ModelIdentifier,\n ): LanguageModelV3 {\n return createLanguageModel(model);\n };\n\n provider.languageModel = createLanguageModel;\n\n return provider as FoundryAIProvider;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA,SAASA,wBAAwB,QAAQ,kCAAkC;;AAQ3E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAC7BC,OAAiC,EACd;EACnB,MAAMC,mBAAmB,GACvBC,KAA+B,IACX;IACpB,MAAMC,UAA2B,GAAG,OAAOD,KAAK,KAAK,QAAQ,GACzD;MAAEE,IAAI,EAAE,UAAU;MAAEC,OAAO,EAAEH;IAAM,CAAC,GACpCA,KAAK;IAET,MAAMI,EAAE,GAAGH,UAAU,CAACC,IAAI,KAAK,UAAU,GACrCD,UAAU,CAACE,OAAO,GAClBF,UAAU,CAACI,kBAAkB;IAEjC,OAAO,IAAIT,wBAAwB,CAAC,WAAWQ,EAAE,EAAE,EAAE;MACnDE,MAAM,EAAER,OAAO,CAACQ,MAAM;MACtBL;IACF,CAAC,CAAC;EACJ,CAAC;EAED,MAAMM,QAAQ,GAAG,SAAAA,CACfP,KAA+B,EACd;IACjB,OAAOD,mBAAmB,CAACC,KAAK,CAAC;EACnC,CAAC;EAEDO,QAAQ,CAACC,aAAa,GAAGT,mBAAmB;EAE5C,OAAOQ,QAAQ;AACjB","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["types.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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\nexport type { ModelIdentifier } from \"../model.js\";\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,76 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { resolveMessages } from "./internal/options.js";
18
+ import { runStep } from "./internal/runStep.js";
19
+
20
+ /**
21
+ * Options for {@link generateText}.
22
+ */
23
+
24
+ /**
25
+ * Result of {@link generateText}.
26
+ */
27
+
28
+ /**
29
+ * Generate a text completion via the Foundry Language Model Service.
30
+ *
31
+ * v0: single-step. Multi-step tool execution loops, image/file content,
32
+ * structured output, and Zod tool schemas are not yet supported.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { foundryModel, generateText } from "@osdk/aip-core";
37
+ *
38
+ * const { text } = await generateText({
39
+ * model: foundryModel({ client, model: "gpt-4o" }),
40
+ * system: "You are a concise assistant.",
41
+ * prompt: "Summarise this PR.",
42
+ * });
43
+ * ```
44
+ */
45
+ export async function generateText(options) {
46
+ const messages = resolveMessages("generateText", options.system, options.prompt, options.messages);
47
+ const step = await runStep({
48
+ model: options.model,
49
+ messages,
50
+ tools: options.tools,
51
+ toolChoice: options.toolChoice,
52
+ maxOutputTokens: options.maxOutputTokens,
53
+ temperature: options.temperature,
54
+ topP: options.topP,
55
+ stopSequences: options.stopSequences,
56
+ seed: options.seed,
57
+ presencePenalty: options.presencePenalty,
58
+ frequencyPenalty: options.frequencyPenalty,
59
+ abortSignal: options.abortSignal,
60
+ headers: options.headers,
61
+ warnings: []
62
+ });
63
+ await options.onStepFinish?.(step);
64
+ const totalUsage = step.usage;
65
+ await options.onFinish?.({
66
+ ...step,
67
+ steps: [step],
68
+ totalUsage
69
+ });
70
+ return {
71
+ ...step,
72
+ totalUsage,
73
+ steps: [step]
74
+ };
75
+ }
76
+ //# sourceMappingURL=generateText.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateText.js","names":["resolveMessages","runStep","generateText","options","messages","system","prompt","step","model","tools","toolChoice","maxOutputTokens","temperature","topP","stopSequences","seed","presencePenalty","frequencyPenalty","abortSignal","headers","warnings","onStepFinish","totalUsage","usage","onFinish","steps"],"sources":["generateText.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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 { resolveMessages } from \"./internal/options.js\";\nimport { runStep } from \"./internal/runStep.js\";\nimport type { LanguageModel } from \"./model.js\";\nimport type {\n LanguageModelUsage,\n ModelMessage,\n StepResult,\n SystemModelMessage,\n ToolChoice,\n ToolSet,\n} from \"./types.js\";\n\n/**\n * Options for {@link generateText}.\n */\nexport interface GenerateTextOptions<TOOLS extends ToolSet = ToolSet> {\n /** The language model to use. Build one via `foundryModel(...)`. */\n model: LanguageModel;\n\n /** Plain text prompt. Mutually exclusive with `messages`. */\n prompt?: string | Array<ModelMessage>;\n\n /** Conversation history. Mutually exclusive with `prompt`. */\n messages?: Array<ModelMessage>;\n\n /** System instructions, prepended to the conversation. */\n system?: string | SystemModelMessage | Array<SystemModelMessage>;\n\n // Tools --------------------------------------------------------------------\n\n tools?: TOOLS;\n toolChoice?: ToolChoice<TOOLS>;\n\n // Sampling -----------------------------------------------------------------\n\n maxOutputTokens?: number;\n temperature?: number;\n topP?: number;\n presencePenalty?: number;\n frequencyPenalty?: number;\n stopSequences?: Array<string>;\n seed?: number;\n\n // Execution ----------------------------------------------------------------\n\n abortSignal?: AbortSignal;\n headers?: Record<string, string | undefined>;\n\n // Callbacks ----------------------------------------------------------------\n\n onStepFinish?: (step: StepResult<TOOLS>) => void | Promise<void>;\n onFinish?: (\n event:\n & StepResult<TOOLS>\n & { steps: Array<StepResult<TOOLS>>; totalUsage: LanguageModelUsage },\n ) => void | PromiseLike<void>;\n}\n\n/**\n * Result of {@link generateText}.\n */\nexport type GenerateTextResult<TOOLS extends ToolSet = ToolSet> =\n & StepResult<TOOLS>\n & {\n totalUsage: LanguageModelUsage;\n steps: Array<StepResult<TOOLS>>;\n };\n\n/**\n * Generate a text completion via the Foundry Language Model Service.\n *\n * v0: single-step. Multi-step tool execution loops, image/file content,\n * structured output, and Zod tool schemas are not yet supported.\n *\n * @example\n * ```ts\n * import { foundryModel, generateText } from \"@osdk/aip-core\";\n *\n * const { text } = await generateText({\n * model: foundryModel({ client, model: \"gpt-4o\" }),\n * system: \"You are a concise assistant.\",\n * prompt: \"Summarise this PR.\",\n * });\n * ```\n */\nexport async function generateText<TOOLS extends ToolSet = ToolSet>(\n options: GenerateTextOptions<TOOLS>,\n): Promise<GenerateTextResult<TOOLS>> {\n const messages = resolveMessages(\n \"generateText\",\n options.system,\n options.prompt,\n options.messages,\n );\n\n const step = await runStep<TOOLS>({\n model: options.model,\n messages,\n tools: options.tools,\n toolChoice: options.toolChoice,\n maxOutputTokens: options.maxOutputTokens,\n temperature: options.temperature,\n topP: options.topP,\n stopSequences: options.stopSequences,\n seed: options.seed,\n presencePenalty: options.presencePenalty,\n frequencyPenalty: options.frequencyPenalty,\n abortSignal: options.abortSignal,\n headers: options.headers,\n warnings: [],\n });\n\n await options.onStepFinish?.(step);\n\n const totalUsage = step.usage;\n\n await options.onFinish?.({\n ...step,\n steps: [step],\n totalUsage,\n });\n\n return { ...step, totalUsage, steps: [step] };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,eAAe,QAAQ,uBAAuB;AACvD,SAASC,OAAO,QAAQ,uBAAuB;;AAW/C;AACA;AACA;;AA4CA;AACA;AACA;;AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeC,YAAYA,CAChCC,OAAmC,EACC;EACpC,MAAMC,QAAQ,GAAGJ,eAAe,CAC9B,cAAc,EACdG,OAAO,CAACE,MAAM,EACdF,OAAO,CAACG,MAAM,EACdH,OAAO,CAACC,QACV,CAAC;EAED,MAAMG,IAAI,GAAG,MAAMN,OAAO,CAAQ;IAChCO,KAAK,EAAEL,OAAO,CAACK,KAAK;IACpBJ,QAAQ;IACRK,KAAK,EAAEN,OAAO,CAACM,KAAK;IACpBC,UAAU,EAAEP,OAAO,CAACO,UAAU;IAC9BC,eAAe,EAAER,OAAO,CAACQ,eAAe;IACxCC,WAAW,EAAET,OAAO,CAACS,WAAW;IAChCC,IAAI,EAAEV,OAAO,CAACU,IAAI;IAClBC,aAAa,EAAEX,OAAO,CAACW,aAAa;IACpCC,IAAI,EAAEZ,OAAO,CAACY,IAAI;IAClBC,eAAe,EAAEb,OAAO,CAACa,eAAe;IACxCC,gBAAgB,EAAEd,OAAO,CAACc,gBAAgB;IAC1CC,WAAW,EAAEf,OAAO,CAACe,WAAW;IAChCC,OAAO,EAAEhB,OAAO,CAACgB,OAAO;IACxBC,QAAQ,EAAE;EACZ,CAAC,CAAC;EAEF,MAAMjB,OAAO,CAACkB,YAAY,GAAGd,IAAI,CAAC;EAElC,MAAMe,UAAU,GAAGf,IAAI,CAACgB,KAAK;EAE7B,MAAMpB,OAAO,CAACqB,QAAQ,GAAG;IACvB,GAAGjB,IAAI;IACPkB,KAAK,EAAE,CAAClB,IAAI,CAAC;IACbe;EACF,CAAC,CAAC;EAEF,OAAO;IAAE,GAAGf,IAAI;IAAEe,UAAU;IAAEG,KAAK,EAAE,CAAClB,IAAI;EAAE,CAAC;AAC/C","ignoreList":[]}
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export { generateText } from "./generateText.js";
18
+ export { streamText } from "./streamText.js";
19
+ export { LmsChatTransport } from "./lmsChatTransport.js";
20
+ export { foundryModel } from "./model.js";
21
+ export { generateMessageId } from "./internal/ids.js";
22
+ export { getUIMessageText } from "./uiMessage.js";
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["generateText","streamText","LmsChatTransport","foundryModel","generateMessageId","getUIMessageText"],"sources":["index.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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\nexport { generateText } from \"./generateText.js\";\nexport type {\n GenerateTextOptions,\n GenerateTextResult,\n} from \"./generateText.js\";\n\nexport { streamText } from \"./streamText.js\";\nexport type {\n StreamTextOptions,\n StreamTextResult,\n TextStreamChunk,\n} from \"./streamText.js\";\n\nexport { LmsChatTransport } from \"./lmsChatTransport.js\";\nexport type { LmsChatTransportOptions } from \"./lmsChatTransport.js\";\n\nexport { foundryModel } from \"./model.js\";\nexport type {\n FoundryModelOptions,\n LanguageModel,\n ModelIdentifier,\n} from \"./model.js\";\n\nexport { generateMessageId } from \"./internal/ids.js\";\n\nexport { getUIMessageText } from \"./uiMessage.js\";\nexport type {\n ChatTransport,\n ChatTransportReconnectArgs,\n ChatTransportSendMessagesArgs,\n ChatTransportTrigger,\n UIMessage,\n UIMessageChunk,\n UIMessagePart,\n UIMessageRole,\n} from \"./uiMessage.js\";\n\nexport type {\n AssistantModelMessage,\n ContentPart,\n FilePart,\n FinishReason,\n ImagePart,\n LanguageModelUsage,\n ModelMessage,\n ProviderMetadata,\n ProviderOptions,\n ReasoningOutput,\n ReasoningPart,\n RequestMetadata,\n ResponseMetadata,\n Role,\n StepResult,\n SystemModelMessage,\n TextPart,\n Tool,\n ToolCall,\n ToolCallPart,\n ToolChoice,\n ToolModelMessage,\n ToolResult,\n ToolResultOutput,\n ToolResultPart,\n ToolSet,\n UserModelMessage,\n Warning,\n} from \"./types.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,YAAY,QAAQ,mBAAmB;AAMhD,SAASC,UAAU,QAAQ,iBAAiB;AAO5C,SAASC,gBAAgB,QAAQ,uBAAuB;AAGxD,SAASC,YAAY,QAAQ,YAAY;AAOzC,SAASC,iBAAiB,QAAQ,mBAAmB;AAErD,SAASC,gBAAgB,QAAQ,gBAAgB","ignoreList":[]}
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * Build a typed `PlatformClient` for tests. `PlatformClient` is structurally
19
+ * `{ baseUrl, fetch, tokenProvider }`; we satisfy that shape directly so
20
+ * tests don't reach for `as unknown as PlatformClient`.
21
+ */
22
+ export function mockPlatformClient(args) {
23
+ return {
24
+ baseUrl: args.baseUrl,
25
+ fetch: args.fetch,
26
+ tokenProvider: () => Promise.resolve(args.token ?? "test-token")
27
+ };
28
+ }
29
+
30
+ /** Throws with a labelled error if `value` is null/undefined. */
31
+ export function assertDefined(value, label) {
32
+ if (value == null) {
33
+ throw new Error(`${label} was unexpectedly null/undefined`);
34
+ }
35
+ }
36
+ //# sourceMappingURL=mockPlatformClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mockPlatformClient.js","names":["mockPlatformClient","args","baseUrl","fetch","tokenProvider","Promise","resolve","token","assertDefined","value","label","Error"],"sources":["mockPlatformClient.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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 type { PlatformClient } from \"@osdk/client\";\n\n/**\n * Build a typed `PlatformClient` for tests. `PlatformClient` is structurally\n * `{ baseUrl, fetch, tokenProvider }`; we satisfy that shape directly so\n * tests don't reach for `as unknown as PlatformClient`.\n */\nexport function mockPlatformClient(args: {\n baseUrl: string;\n fetch: typeof globalThis.fetch;\n token?: string;\n}): PlatformClient {\n return {\n baseUrl: args.baseUrl,\n fetch: args.fetch,\n tokenProvider: () => Promise.resolve(args.token ?? \"test-token\"),\n };\n}\n\n/** Throws with a labelled error if `value` is null/undefined. */\nexport function assertDefined<T>(\n value: T,\n label: string,\n): asserts value is NonNullable<T> {\n if (value == null) {\n throw new Error(`${label} was unexpectedly null/undefined`);\n }\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,kBAAkBA,CAACC,IAIlC,EAAkB;EACjB,OAAO;IACLC,OAAO,EAAED,IAAI,CAACC,OAAO;IACrBC,KAAK,EAAEF,IAAI,CAACE,KAAK;IACjBC,aAAa,EAAEA,CAAA,KAAMC,OAAO,CAACC,OAAO,CAACL,IAAI,CAACM,KAAK,IAAI,YAAY;EACjE,CAAC;AACH;;AAEA;AACA,OAAO,SAASC,aAAaA,CAC3BC,KAAQ,EACRC,KAAa,EACoB;EACjC,IAAID,KAAK,IAAI,IAAI,EAAE;IACjB,MAAM,IAAIE,KAAK,CAAC,GAAGD,KAAK,kCAAkC,CAAC;EAC7D;AACF","ignoreList":[]}
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export function generateMessageId() {
18
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
19
+ return crypto.randomUUID();
20
+ }
21
+ return `msg-${Math.random().toString(36).slice(2, 10)}-${Date.now()}`;
22
+ }
23
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.js","names":["generateMessageId","crypto","randomUUID","Math","random","toString","slice","Date","now"],"sources":["ids.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\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\nexport function generateMessageId(): string {\n if (\n typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\"\n ) {\n return crypto.randomUUID();\n }\n return `msg-${Math.random().toString(36).slice(2, 10)}-${Date.now()}`;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,SAASA,iBAAiBA,CAAA,EAAW;EAC1C,IACE,OAAOC,MAAM,KAAK,WAAW,IAAI,OAAOA,MAAM,CAACC,UAAU,KAAK,UAAU,EACxE;IACA,OAAOD,MAAM,CAACC,UAAU,CAAC,CAAC;EAC5B;EACA,OAAO,OAAOC,IAAI,CAACC,MAAM,CAAC,CAAC,CAACC,QAAQ,CAAC,EAAE,CAAC,CAACC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAIC,IAAI,CAACC,GAAG,CAAC,CAAC,EAAE;AACvE","ignoreList":[]}
@@ -0,0 +1,293 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { getOpenAiBaseUrl } from "@osdk/language-models";
18
+ import { _getFoundryInternal } from "../model.js";
19
+ export function getModelApiName(identifier) {
20
+ return identifier.type === "lmsModel" ? identifier.apiName : identifier.registeredModelRid;
21
+ }
22
+ export function mapUsage(raw) {
23
+ return {
24
+ inputTokens: raw.prompt_tokens,
25
+ outputTokens: raw.completion_tokens,
26
+ totalTokens: raw.total_tokens,
27
+ reasoningTokens: raw.completion_tokens_details?.reasoning_tokens,
28
+ cachedInputTokens: raw.prompt_tokens_details?.cached_tokens
29
+ };
30
+ }
31
+ export const EMPTY_USAGE = {
32
+ inputTokens: undefined,
33
+ outputTokens: undefined,
34
+ totalTokens: undefined
35
+ };
36
+ export function mapFinishReason(reason) {
37
+ switch (reason) {
38
+ case "stop":
39
+ return "stop";
40
+ case "length":
41
+ return "length";
42
+ case "tool_calls":
43
+ case "function_call":
44
+ return "tool-calls";
45
+ case "content_filter":
46
+ return "content-filter";
47
+ default:
48
+ return "other";
49
+ }
50
+ }
51
+ export function convertMessage(m, warnings) {
52
+ switch (m.role) {
53
+ case "system":
54
+ return [{
55
+ role: "system",
56
+ content: m.content
57
+ }];
58
+ case "user":
59
+ {
60
+ if (typeof m.content === "string") {
61
+ return [{
62
+ role: "user",
63
+ content: m.content
64
+ }];
65
+ }
66
+ const parts = [];
67
+ for (const p of m.content) {
68
+ if (p.type === "text") {
69
+ parts.push({
70
+ type: "text",
71
+ text: p.text
72
+ });
73
+ } else {
74
+ warnings.push({
75
+ type: "other",
76
+ message: `Unsupported user content part "${p.type}": ignored in v0`
77
+ });
78
+ }
79
+ }
80
+ return [{
81
+ role: "user",
82
+ content: parts
83
+ }];
84
+ }
85
+ case "assistant":
86
+ {
87
+ if (typeof m.content === "string") {
88
+ return [{
89
+ role: "assistant",
90
+ content: m.content
91
+ }];
92
+ }
93
+ let text = "";
94
+ const toolCalls = [];
95
+ for (const p of m.content) {
96
+ switch (p.type) {
97
+ case "text":
98
+ text += p.text;
99
+ break;
100
+ case "tool-call":
101
+ toolCalls.push({
102
+ id: p.toolCallId,
103
+ type: "function",
104
+ function: {
105
+ name: p.toolName,
106
+ arguments: JSON.stringify(p.input ?? {})
107
+ }
108
+ });
109
+ break;
110
+ case "reasoning":
111
+ case "file":
112
+ warnings.push({
113
+ type: "other",
114
+ message: `Unsupported assistant content part "${p.type}": ignored in v0`
115
+ });
116
+ break;
117
+ }
118
+ }
119
+ return [{
120
+ role: "assistant",
121
+ content: text.length > 0 ? text : null,
122
+ tool_calls: toolCalls.length > 0 ? toolCalls : undefined
123
+ }];
124
+ }
125
+ case "tool":
126
+ return m.content.map(part => ({
127
+ role: "tool",
128
+ content: stringifyToolResult(part, warnings),
129
+ tool_call_id: part.toolCallId
130
+ }));
131
+ }
132
+ }
133
+ function stringifyToolResult(part, warnings) {
134
+ switch (part.output.type) {
135
+ case "text":
136
+ case "error-text":
137
+ return part.output.value;
138
+ case "json":
139
+ case "error-json":
140
+ return JSON.stringify(part.output.value);
141
+ case "content":
142
+ return part.output.value.map(c => {
143
+ if (c.type === "text") {
144
+ return c.text;
145
+ }
146
+ warnings.push({
147
+ type: "other",
148
+ message: `Unsupported tool result media: ignored in v0`
149
+ });
150
+ return "";
151
+ }).filter(s => s.length > 0).join("\n");
152
+ }
153
+ }
154
+ export function convertTools(tools, warnings) {
155
+ return Object.entries(tools).map(([name, tool]) => {
156
+ const parameters = isJsonSchemaLike(tool.inputSchema) ? tool.inputSchema : (warnings.push({
157
+ type: "unsupported-tool",
158
+ details: `Tool "${name}" inputSchema is not a JSON Schema; v0 only supports plain JSON Schema objects. Defaulting to {} parameters.`
159
+ }), {
160
+ type: "object",
161
+ properties: {}
162
+ });
163
+ return {
164
+ type: "function",
165
+ function: {
166
+ name,
167
+ description: tool.description,
168
+ parameters
169
+ }
170
+ };
171
+ });
172
+ }
173
+ export function convertToolChoice(choice) {
174
+ if (choice == null) {
175
+ return undefined;
176
+ }
177
+ if (choice === "auto" || choice === "none" || choice === "required") {
178
+ return choice;
179
+ }
180
+ return {
181
+ type: "function",
182
+ function: {
183
+ name: choice.toolName
184
+ }
185
+ };
186
+ }
187
+ function isJsonSchemaLike(value) {
188
+ return typeof value === "object" && value != null && ("type" in value || "properties" in value || "$ref" in value || "oneOf" in value || "anyOf" in value || "allOf" in value);
189
+ }
190
+ export function parseToolArguments(args, warnings) {
191
+ if (args === "") {
192
+ return {};
193
+ }
194
+ try {
195
+ return JSON.parse(args);
196
+ } catch {
197
+ warnings.push({
198
+ type: "other",
199
+ message: `Tool call arguments were not valid JSON; passing through as a raw string`
200
+ });
201
+ return args;
202
+ }
203
+ }
204
+ export function buildOpenAiRequestBody(args, streaming) {
205
+ const common = {
206
+ model: args.apiName,
207
+ messages: args.messages.map(m => convertMessage(m, args.warnings)).flat(),
208
+ max_tokens: args.maxOutputTokens,
209
+ temperature: args.temperature,
210
+ top_p: args.topP,
211
+ stop: args.stopSequences,
212
+ seed: args.seed,
213
+ presence_penalty: args.presencePenalty,
214
+ frequency_penalty: args.frequencyPenalty,
215
+ tools: args.tools != null ? convertTools(args.tools, args.warnings) : undefined,
216
+ tool_choice: convertToolChoice(args.toolChoice)
217
+ };
218
+ if (streaming) {
219
+ return {
220
+ ...common,
221
+ stream: true,
222
+ stream_options: {
223
+ include_usage: true
224
+ }
225
+ };
226
+ }
227
+ return {
228
+ ...common,
229
+ stream: false
230
+ };
231
+ }
232
+ export function buildAssistantContent(text, toolCalls) {
233
+ if (toolCalls.length === 0) {
234
+ return text;
235
+ }
236
+ const parts = [];
237
+ if (text.length > 0) {
238
+ parts.push({
239
+ type: "text",
240
+ text
241
+ });
242
+ }
243
+ for (const tc of toolCalls) {
244
+ parts.push({
245
+ type: "tool-call",
246
+ toolCallId: tc.toolCallId,
247
+ toolName: tc.toolName,
248
+ input: tc.input
249
+ });
250
+ }
251
+ return parts;
252
+ }
253
+ export function filterHeaders(input) {
254
+ const out = {};
255
+ for (const [k, v] of Object.entries(input)) {
256
+ if (v != null) {
257
+ out[k] = v;
258
+ }
259
+ }
260
+ return out;
261
+ }
262
+ export async function safeReadText(res) {
263
+ try {
264
+ return await res.text();
265
+ } catch {
266
+ return undefined;
267
+ }
268
+ }
269
+ /**
270
+ * POST against the LMS OpenAI proxy's chat/completions endpoint and return
271
+ * the raw response. Throws on a non-2xx response.
272
+ */
273
+ export async function postChatCompletions(args) {
274
+ const handle = _getFoundryInternal(args.model);
275
+ const baseUrl = getOpenAiBaseUrl(handle.client);
276
+ const url = new URL("chat/completions", `${baseUrl}/`).toString();
277
+ const res = await handle.client.fetch(url, {
278
+ method: "POST",
279
+ headers: filterHeaders({
280
+ "Content-Type": "application/json",
281
+ Accept: args.accept,
282
+ ...(args.headers ?? {})
283
+ }),
284
+ body: JSON.stringify(args.body),
285
+ signal: args.abortSignal
286
+ });
287
+ if (!res.ok) {
288
+ const errBody = await safeReadText(res);
289
+ throw new Error(`LMS chat/completions request failed: ${res.status} ${res.statusText}` + (errBody ? `: ${errBody}` : ""));
290
+ }
291
+ return res;
292
+ }
293
+ //# sourceMappingURL=openAi.js.map