@langchain/openai 1.4.1 → 1.4.3
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.
- package/CHANGELOG.md +15 -0
- package/dist/azure/chat_models/common.d.cts.map +1 -1
- package/dist/azure/embeddings.d.cts +1 -1
- package/dist/azure/embeddings.d.ts +1 -1
- package/dist/azure/llms.cjs.map +1 -1
- package/dist/azure/llms.js.map +1 -1
- package/dist/chat_models/base.cjs +1 -1
- package/dist/chat_models/base.cjs.map +1 -1
- package/dist/chat_models/base.d.cts +4 -4
- package/dist/chat_models/base.d.cts.map +1 -1
- package/dist/chat_models/base.d.ts +4 -4
- package/dist/chat_models/base.d.ts.map +1 -1
- package/dist/chat_models/base.js +1 -1
- package/dist/chat_models/base.js.map +1 -1
- package/dist/chat_models/completions.cjs.map +1 -1
- package/dist/chat_models/completions.js.map +1 -1
- package/dist/chat_models/index.d.cts +36 -36
- package/dist/chat_models/index.d.cts.map +1 -1
- package/dist/chat_models/index.d.ts +36 -36
- package/dist/chat_models/index.d.ts.map +1 -1
- package/dist/converters/completions.cjs +2 -2
- package/dist/converters/completions.cjs.map +1 -1
- package/dist/converters/completions.d.cts.map +1 -1
- package/dist/converters/completions.d.ts.map +1 -1
- package/dist/converters/completions.js +2 -2
- package/dist/converters/completions.js.map +1 -1
- package/dist/converters/index.d.cts +2 -0
- package/dist/converters/responses.cjs +3 -3
- package/dist/converters/responses.cjs.map +1 -1
- package/dist/converters/responses.js +3 -3
- package/dist/converters/responses.js.map +1 -1
- package/dist/embeddings.d.cts +1 -1
- package/dist/embeddings.d.ts +1 -1
- package/dist/llms.cjs +1 -1
- package/dist/llms.cjs.map +1 -1
- package/dist/llms.js +1 -1
- package/dist/llms.js.map +1 -1
- package/dist/tools/applyPatch.d.cts +13 -12
- package/dist/tools/applyPatch.d.cts.map +1 -1
- package/dist/tools/applyPatch.d.ts +13 -12
- package/dist/tools/applyPatch.d.ts.map +1 -1
- package/dist/tools/computerUse.cjs.map +1 -1
- package/dist/tools/computerUse.d.cts +53 -52
- package/dist/tools/computerUse.d.cts.map +1 -1
- package/dist/tools/computerUse.d.ts +53 -52
- package/dist/tools/computerUse.d.ts.map +1 -1
- package/dist/tools/computerUse.js.map +1 -1
- package/dist/tools/custom.cjs.map +1 -1
- package/dist/tools/custom.js.map +1 -1
- package/dist/tools/dalle.cjs.map +1 -1
- package/dist/tools/dalle.js.map +1 -1
- package/dist/tools/localShell.d.cts +9 -8
- package/dist/tools/localShell.d.cts.map +1 -1
- package/dist/tools/localShell.d.ts +9 -8
- package/dist/tools/localShell.d.ts.map +1 -1
- package/dist/tools/shell.d.cts +6 -5
- package/dist/tools/shell.d.cts.map +1 -1
- package/dist/tools/shell.d.ts +6 -5
- package/dist/tools/shell.d.ts.map +1 -1
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/misc.cjs +5 -1
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts.map +1 -1
- package/dist/utils/misc.d.ts.map +1 -1
- package/dist/utils/misc.js +5 -1
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/tools.cjs.map +1 -1
- package/dist/utils/tools.d.cts +0 -1
- package/dist/utils/tools.d.cts.map +1 -1
- package/dist/utils/tools.d.ts +0 -1
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js.map +1 -1
- package/package.json +7 -16
package/dist/llms.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llms.js","names":["OpenAI","OpenAIClient"],"sources":["../src/llms.ts"],"sourcesContent":["import type { TiktokenModel } from \"js-tiktoken/lite\";\nimport { type ClientOptions, OpenAI as OpenAIClient } from \"openai\";\nimport { calculateMaxTokens } from \"@langchain/core/language_models/base\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport { GenerationChunk, type LLMResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseLLM,\n type BaseLLMParams,\n} from \"@langchain/core/language_models/llms\";\nimport { chunkArray } from \"@langchain/core/utils/chunk_array\";\nimport type {\n OpenAIApiKey,\n OpenAICallOptions,\n OpenAICoreRequestOptions,\n OpenAIInput,\n} from \"./types.js\";\nimport {\n OpenAIEndpointConfig,\n getEndpoint,\n getHeadersWithUserAgent,\n} from \"./utils/azure.js\";\nimport { wrapOpenAIClientError } from \"./utils/client.js\";\n\nexport type { OpenAICallOptions, OpenAIInput };\n\n/**\n * Interface for tracking token usage in OpenAI calls.\n */\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\n/**\n * Wrapper around OpenAI large language models.\n *\n * To use you should have the `openai` package installed, with the\n * `OPENAI_API_KEY` environment variable set.\n *\n * To use with Azure, import the `AzureOpenAI` class.\n *\n * @remarks\n * Any parameters that are valid to be passed to {@link\n * https://platform.openai.com/docs/api-reference/completions/create |\n * `openai.createCompletion`} can be passed through {@link modelKwargs}, even\n * if not explicitly available on this class.\n * @example\n * ```typescript\n * const model = new OpenAI({\n * modelName: \"gpt-4\",\n * temperature: 0.7,\n * maxTokens: 1000,\n * maxRetries: 5,\n * });\n *\n * const res = await model.invoke(\n * \"Question: What would be a good company name for a company that makes colorful socks?\\nAnswer:\"\n * );\n * console.log({ res });\n * ```\n */\nexport class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>\n extends BaseLLM<CallOptions>\n implements Partial<OpenAIInput>\n{\n static lc_name() {\n return \"OpenAI\";\n }\n\n get callKeys() {\n return [...super.callKeys, \"options\"];\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n openAIApiKey: \"OPENAI_API_KEY\",\n apiKey: \"OPENAI_API_KEY\",\n organization: \"OPENAI_ORGANIZATION\",\n };\n }\n\n get lc_aliases(): Record<string, string> {\n return {\n modelName: \"model\",\n openAIApiKey: \"openai_api_key\",\n apiKey: \"openai_api_key\",\n };\n }\n\n temperature?: number;\n\n maxTokens?: number;\n\n topP?: number;\n\n frequencyPenalty?: number;\n\n presencePenalty?: number;\n\n n = 1;\n\n bestOf?: number;\n\n logitBias?: Record<string, number>;\n\n model = \"gpt-3.5-turbo-instruct\";\n\n /** @deprecated Use \"model\" instead */\n modelName: string;\n\n modelKwargs?: OpenAIInput[\"modelKwargs\"];\n\n batchSize = 20;\n\n timeout?: number;\n\n stop?: string[];\n\n stopSequences?: string[];\n\n user?: string;\n\n streaming = false;\n\n openAIApiKey?: OpenAIApiKey;\n\n apiKey?: OpenAIApiKey;\n\n organization?: string;\n\n protected client: OpenAIClient;\n\n protected clientConfig: ClientOptions;\n\n constructor(\n fields?: Partial<OpenAIInput> &\n BaseLLMParams & {\n configuration?: ClientOptions;\n }\n ) {\n super(fields ?? {});\n this._addVersion(\"@langchain/openai\", __PKG_VERSION__);\n\n this.openAIApiKey =\n fields?.apiKey ??\n fields?.openAIApiKey ??\n getEnvironmentVariable(\"OPENAI_API_KEY\");\n this.apiKey = this.openAIApiKey;\n\n this.organization =\n fields?.configuration?.organization ??\n getEnvironmentVariable(\"OPENAI_ORGANIZATION\");\n\n this.model = fields?.model ?? fields?.modelName ?? this.model;\n if (\n (this.model?.startsWith(\"gpt-3.5-turbo\") ||\n this.model?.startsWith(\"gpt-4\") ||\n this.model?.startsWith(\"o1\")) &&\n !this.model?.includes(\"-instruct\")\n ) {\n throw new Error(\n [\n `Your chosen OpenAI model, \"${this.model}\", is a chat model and not a text-in/text-out LLM.`,\n `Passing it into the \"OpenAI\" class is no longer supported.`,\n `Please use the \"ChatOpenAI\" class instead.`,\n \"\",\n `See this page for more information:`,\n \"|\",\n `└> https://js.langchain.com/docs/integrations/chat/openai`,\n ].join(\"\\n\")\n );\n }\n this.modelName = this.model;\n this.modelKwargs = fields?.modelKwargs ?? {};\n this.batchSize = fields?.batchSize ?? this.batchSize;\n this.timeout = fields?.timeout;\n\n this.temperature = fields?.temperature ?? this.temperature;\n this.maxTokens = fields?.maxTokens ?? this.maxTokens;\n this.topP = fields?.topP ?? this.topP;\n this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;\n this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;\n this.n = fields?.n ?? this.n;\n this.bestOf = fields?.bestOf ?? this.bestOf;\n this.logitBias = fields?.logitBias;\n this.stop = fields?.stopSequences ?? fields?.stop;\n this.stopSequences = this.stop;\n this.user = fields?.user;\n\n this.streaming = fields?.streaming ?? false;\n\n if (this.streaming && this.bestOf && this.bestOf > 1) {\n throw new Error(\"Cannot stream results when bestOf > 1\");\n }\n\n this.clientConfig = {\n apiKey: this.apiKey,\n organization: this.organization,\n dangerouslyAllowBrowser: true,\n ...fields?.configuration,\n };\n }\n\n /**\n * Get the parameters used to invoke the model\n */\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> {\n return {\n model: this.model,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n frequency_penalty: this.frequencyPenalty,\n presence_penalty: this.presencePenalty,\n n: this.n,\n best_of: this.bestOf,\n logit_bias: this.logitBias,\n stop: options?.stop ?? this.stopSequences,\n user: this.user,\n stream: this.streaming,\n ...this.modelKwargs,\n };\n }\n\n /** @ignore */\n _identifyingParams(): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> & {\n model_name: string;\n } & ClientOptions {\n return {\n model_name: this.model,\n ...this.invocationParams(),\n ...this.clientConfig,\n };\n }\n\n /**\n * Get the identifying parameters for the model\n */\n identifyingParams(): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> & {\n model_name: string;\n } & ClientOptions {\n return this._identifyingParams();\n }\n\n /**\n * Call out to OpenAI's endpoint with k unique prompts\n *\n * @param [prompts] - The prompts to pass into the model.\n * @param [options] - Optional list of stop words to use when generating.\n * @param [runManager] - Optional callback manager to use when generating.\n *\n * @returns The full LLM output.\n *\n * @example\n * ```ts\n * import { OpenAI } from \"langchain/llms/openai\";\n * const openai = new OpenAI();\n * const response = await openai.generate([\"Tell me a joke.\"]);\n * ```\n */\n async _generate(\n prompts: string[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<LLMResult> {\n const subPrompts = chunkArray(prompts, this.batchSize);\n const choices: OpenAIClient.CompletionChoice[] = [];\n const tokenUsage: TokenUsage = {};\n\n const params = this.invocationParams(options);\n\n if (params.max_tokens === -1) {\n if (prompts.length !== 1) {\n throw new Error(\n \"max_tokens set to -1 not supported for multiple inputs\"\n );\n }\n params.max_tokens = await calculateMaxTokens({\n prompt: prompts[0],\n // Cast here to allow for other models that may not fit the union\n modelName: this.model as TiktokenModel,\n });\n }\n\n for (let i = 0; i < subPrompts.length; i += 1) {\n const data = params.stream\n ? await (async () => {\n const choices: OpenAIClient.CompletionChoice[] = [];\n let response: Omit<OpenAIClient.Completion, \"choices\"> | undefined;\n const stream = await this.completionWithRetry(\n {\n ...params,\n stream: true,\n prompt: subPrompts[i],\n },\n options\n );\n for await (const message of stream) {\n // on the first message set the response properties\n if (!response) {\n response = {\n id: message.id,\n object: message.object,\n created: message.created,\n model: message.model,\n };\n }\n\n // on all messages, update choice\n for (const part of message.choices) {\n if (!choices[part.index]) {\n choices[part.index] = part;\n } else {\n const choice = choices[part.index];\n choice.text += part.text;\n choice.finish_reason = part.finish_reason;\n choice.logprobs = part.logprobs;\n }\n // eslint-disable-next-line no-void\n void runManager?.handleLLMNewToken(part.text, {\n prompt: Math.floor(part.index / this.n),\n completion: part.index % this.n,\n });\n }\n }\n if (options.signal?.aborted) {\n throw new Error(\"AbortError\");\n }\n return { ...response, choices };\n })()\n : await this.completionWithRetry(\n {\n ...params,\n stream: false,\n prompt: subPrompts[i],\n },\n {\n signal: options.signal,\n ...options.options,\n }\n );\n\n choices.push(...data.choices);\n const {\n completion_tokens: completionTokens,\n prompt_tokens: promptTokens,\n total_tokens: totalTokens,\n } = data.usage\n ? data.usage\n : {\n completion_tokens: undefined,\n prompt_tokens: undefined,\n total_tokens: undefined,\n };\n\n if (completionTokens) {\n tokenUsage.completionTokens =\n (tokenUsage.completionTokens ?? 0) + completionTokens;\n }\n\n if (promptTokens) {\n tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;\n }\n\n if (totalTokens) {\n tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;\n }\n }\n\n const generations = chunkArray(choices, this.n).map((promptChoices) =>\n promptChoices.map((choice) => ({\n text: choice.text ?? \"\",\n generationInfo: {\n finishReason: choice.finish_reason,\n logprobs: choice.logprobs,\n },\n }))\n );\n return {\n generations,\n llmOutput: { tokenUsage },\n };\n }\n\n // TODO(jacoblee): Refactor with _generate(..., {stream: true}) implementation?\n async *_streamResponseChunks(\n input: string,\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<GenerationChunk> {\n const params = {\n ...this.invocationParams(options),\n prompt: input,\n stream: true as const,\n };\n const stream = await this.completionWithRetry(params, options);\n for await (const data of stream) {\n const choice = data?.choices[0];\n if (!choice) {\n continue;\n }\n const chunk = new GenerationChunk({\n text: choice.text,\n generationInfo: {\n finishReason: choice.finish_reason,\n },\n });\n yield chunk;\n // eslint-disable-next-line no-void\n void runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n if (options.signal?.aborted) {\n throw new Error(\"AbortError\");\n }\n }\n\n /**\n * Calls the OpenAI API with retry logic in case of failures.\n * @param request The request to send to the OpenAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the OpenAI API.\n */\n async completionWithRetry(\n request: OpenAIClient.CompletionCreateParamsStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<AsyncIterable<OpenAIClient.Completion>>;\n\n async completionWithRetry(\n request: OpenAIClient.CompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<OpenAIClient.Completions.Completion>;\n\n async completionWithRetry(\n request:\n | OpenAIClient.CompletionCreateParamsStreaming\n | OpenAIClient.CompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<\n AsyncIterable<OpenAIClient.Completion> | OpenAIClient.Completions.Completion\n > {\n const requestOptions = this._getClientOptions(options);\n return this.caller.call(async () => {\n try {\n const res = await this.client.completions.create(\n request,\n requestOptions\n );\n return res;\n } catch (e) {\n const error = wrapOpenAIClientError(e);\n throw error;\n }\n });\n }\n\n /**\n * Calls the OpenAI API with retry logic in case of failures.\n * @param request The request to send to the OpenAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the OpenAI API.\n */\n protected _getClientOptions(\n options: OpenAICoreRequestOptions | undefined\n ): OpenAICoreRequestOptions {\n if (!this.client) {\n const openAIEndpointConfig: OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n\n if (!params.baseURL) {\n delete params.baseURL;\n }\n\n params.defaultHeaders = getHeadersWithUserAgent(params.defaultHeaders);\n\n this.client = new OpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as OpenAICoreRequestOptions;\n return requestOptions;\n }\n\n _llmType() {\n return \"openai\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAaA,WAAb,cACU,QAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,WAAW;AACb,SAAO,CAAC,GAAG,MAAM,UAAU,UAAU;;CAGvC,kBAAkB;CAElB,IAAI,aAAoD;AACtD,SAAO;GACL,cAAc;GACd,QAAQ;GACR,cAAc;GACf;;CAGH,IAAI,aAAqC;AACvC,SAAO;GACL,WAAW;GACX,cAAc;GACd,QAAQ;GACT;;CAGH;CAEA;CAEA;CAEA;CAEA;CAEA,IAAI;CAEJ;CAEA;CAEA,QAAQ;;CAGR;CAEA;CAEA,YAA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ACjC,QAAQ;GACR,QAAQ;GACT;EACD,MAAM,SAAS,MAAM,KAAK,oBAAoB,QAAQ,QAAQ;AAC9D,aAAW,MAAM,QAAQ,QAAQ;GAC/B,MAAM,SAAS,MAAM,QAAQ;AAC7B,OAAI,CAAC,OACH;GAEF,MAAM,QAAQ,IAAI,gBAAgB;IAChC,MAAM,OAAO;IACb,gBAAgB,EACd,cAAc,OAAO,eACtB;IACF,CAAC;AACF,SAAM;AAED,eAAY,kBAAkB,MAAM,QAAQ,GAAG;;AAEtD,MAAI,QAAQ,QAAQ,QAClB,OAAM,IAAI,MAAM,aAAa;;CAoBjC,MAAM,oBACJ,SAGA,SAGA;EACA,MAAM,iBAAiB,KAAK,kBAAkB,QAAQ;AACtD,SAAO,KAAK,OAAO,KAAK,YAAY;AAClC,OAAI;AAKF,WAJY,MAAM,KAAK,OAAO,YAAY,OACxC,SACA,eACD;YAEM,GAAG;AAEV,UADc,sBAAsB,EAAE;;IAGxC;;;;;;;;CASJ,kBACE,SAC0B;AAC1B,MAAI,CAAC,KAAK,QAAQ;GAKhB,MAAM,WAAW,YAJkC,EACjD,SAAS,KAAK,aAAa,SAC5B,CAEiD;GAElD,MAAM,SAAS;IACb,GAAG,KAAK;IACR,SAAS;IACT,SAAS,KAAK;IACd,YAAY;IACb;AAED,OAAI,CAAC,OAAO,QACV,QAAO,OAAO;AAGhB,UAAO,iBAAiB,wBAAwB,OAAO,eAAe;AAEtE,QAAK,SAAS,IAAIC,OAAa,OAAO;;AAMxC,SAJuB;GACrB,GAAG,KAAK;GACR,GAAG;GACJ;;CAIH,WAAW;AACT,SAAO"}
|
|
1
|
+
{"version":3,"file":"llms.js","names":["OpenAI","OpenAIClient"],"sources":["../src/llms.ts"],"sourcesContent":["import type { TiktokenModel } from \"js-tiktoken/lite\";\nimport { type ClientOptions, OpenAI as OpenAIClient } from \"openai\";\nimport { calculateMaxTokens } from \"@langchain/core/language_models/base\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport { GenerationChunk, type LLMResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseLLM,\n type BaseLLMParams,\n} from \"@langchain/core/language_models/llms\";\nimport { chunkArray } from \"@langchain/core/utils/chunk_array\";\nimport type {\n OpenAIApiKey,\n OpenAICallOptions,\n OpenAICoreRequestOptions,\n OpenAIInput,\n} from \"./types.js\";\nimport {\n OpenAIEndpointConfig,\n getEndpoint,\n getHeadersWithUserAgent,\n} from \"./utils/azure.js\";\nimport { wrapOpenAIClientError } from \"./utils/client.js\";\n\nexport type { OpenAICallOptions, OpenAIInput };\n\n/**\n * Interface for tracking token usage in OpenAI calls.\n */\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\n/**\n * Wrapper around OpenAI large language models.\n *\n * To use you should have the `openai` package installed, with the\n * `OPENAI_API_KEY` environment variable set.\n *\n * To use with Azure, import the `AzureOpenAI` class.\n *\n * @remarks\n * Any parameters that are valid to be passed to {@link\n * https://platform.openai.com/docs/api-reference/completions/create |\n * `openai.createCompletion`} can be passed through {@link modelKwargs}, even\n * if not explicitly available on this class.\n * @example\n * ```typescript\n * const model = new OpenAI({\n * modelName: \"gpt-4\",\n * temperature: 0.7,\n * maxTokens: 1000,\n * maxRetries: 5,\n * });\n *\n * const res = await model.invoke(\n * \"Question: What would be a good company name for a company that makes colorful socks?\\nAnswer:\"\n * );\n * console.log({ res });\n * ```\n */\nexport class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>\n extends BaseLLM<CallOptions>\n implements Partial<OpenAIInput>\n{\n static lc_name() {\n return \"OpenAI\";\n }\n\n get callKeys() {\n return [...super.callKeys, \"options\"];\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n openAIApiKey: \"OPENAI_API_KEY\",\n apiKey: \"OPENAI_API_KEY\",\n organization: \"OPENAI_ORGANIZATION\",\n };\n }\n\n get lc_aliases(): Record<string, string> {\n return {\n modelName: \"model\",\n openAIApiKey: \"openai_api_key\",\n apiKey: \"openai_api_key\",\n };\n }\n\n temperature?: number;\n\n maxTokens?: number;\n\n topP?: number;\n\n frequencyPenalty?: number;\n\n presencePenalty?: number;\n\n n = 1;\n\n bestOf?: number;\n\n logitBias?: Record<string, number>;\n\n model = \"gpt-3.5-turbo-instruct\";\n\n /** @deprecated Use \"model\" instead */\n modelName: string;\n\n modelKwargs?: OpenAIInput[\"modelKwargs\"];\n\n batchSize = 20;\n\n timeout?: number;\n\n stop?: string[];\n\n stopSequences?: string[];\n\n user?: string;\n\n streaming = false;\n\n openAIApiKey?: OpenAIApiKey;\n\n apiKey?: OpenAIApiKey;\n\n organization?: string;\n\n protected client: OpenAIClient;\n\n protected clientConfig: ClientOptions;\n\n constructor(\n fields?: Partial<OpenAIInput> &\n BaseLLMParams & {\n configuration?: ClientOptions;\n }\n ) {\n super(fields ?? {});\n this._addVersion(\"@langchain/openai\", __PKG_VERSION__);\n\n this.openAIApiKey =\n fields?.apiKey ??\n fields?.openAIApiKey ??\n getEnvironmentVariable(\"OPENAI_API_KEY\");\n this.apiKey = this.openAIApiKey;\n\n this.organization =\n fields?.configuration?.organization ??\n getEnvironmentVariable(\"OPENAI_ORGANIZATION\");\n\n this.model = fields?.model ?? fields?.modelName ?? this.model;\n if (\n (this.model?.startsWith(\"gpt-3.5-turbo\") ||\n this.model?.startsWith(\"gpt-4\") ||\n this.model?.startsWith(\"o1\")) &&\n !this.model?.includes(\"-instruct\")\n ) {\n throw new Error(\n [\n `Your chosen OpenAI model, \"${this.model}\", is a chat model and not a text-in/text-out LLM.`,\n `Passing it into the \"OpenAI\" class is no longer supported.`,\n `Please use the \"ChatOpenAI\" class instead.`,\n \"\",\n `See this page for more information:`,\n \"|\",\n `└> https://js.langchain.com/docs/integrations/chat/openai`,\n ].join(\"\\n\")\n );\n }\n this.modelName = this.model;\n this.modelKwargs = fields?.modelKwargs ?? {};\n this.batchSize = fields?.batchSize ?? this.batchSize;\n this.timeout = fields?.timeout;\n\n this.temperature = fields?.temperature ?? this.temperature;\n this.maxTokens = fields?.maxTokens ?? this.maxTokens;\n this.topP = fields?.topP ?? this.topP;\n this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;\n this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;\n this.n = fields?.n ?? this.n;\n this.bestOf = fields?.bestOf ?? this.bestOf;\n this.logitBias = fields?.logitBias;\n this.stop = fields?.stopSequences ?? fields?.stop;\n this.stopSequences = this.stop;\n this.user = fields?.user;\n\n this.streaming = fields?.streaming ?? false;\n\n if (this.streaming && this.bestOf && this.bestOf > 1) {\n throw new Error(\"Cannot stream results when bestOf > 1\");\n }\n\n this.clientConfig = {\n apiKey: this.apiKey,\n organization: this.organization,\n dangerouslyAllowBrowser: true,\n ...fields?.configuration,\n };\n }\n\n /**\n * Get the parameters used to invoke the model\n */\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> {\n return {\n model: this.model,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n frequency_penalty: this.frequencyPenalty,\n presence_penalty: this.presencePenalty,\n n: this.n,\n best_of: this.bestOf,\n logit_bias: this.logitBias,\n stop: options?.stop ?? this.stopSequences,\n user: this.user,\n stream: this.streaming,\n ...this.modelKwargs,\n };\n }\n\n /** @ignore */\n _identifyingParams(): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> & {\n model_name: string;\n } & ClientOptions {\n return {\n model_name: this.model,\n ...this.invocationParams(),\n ...this.clientConfig,\n };\n }\n\n /**\n * Get the identifying parameters for the model\n */\n identifyingParams(): Omit<OpenAIClient.CompletionCreateParams, \"prompt\"> & {\n model_name: string;\n } & ClientOptions {\n return this._identifyingParams();\n }\n\n /**\n * Call out to OpenAI's endpoint with k unique prompts\n *\n * @param [prompts] - The prompts to pass into the model.\n * @param [options] - Optional list of stop words to use when generating.\n * @param [runManager] - Optional callback manager to use when generating.\n *\n * @returns The full LLM output.\n *\n * @example\n * ```ts\n * import { OpenAI } from \"langchain/llms/openai\";\n * const openai = new OpenAI();\n * const response = await openai.generate([\"Tell me a joke.\"]);\n * ```\n */\n async _generate(\n prompts: string[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<LLMResult> {\n const subPrompts = chunkArray(prompts, this.batchSize);\n const choices: OpenAIClient.CompletionChoice[] = [];\n const tokenUsage: TokenUsage = {};\n\n const params = this.invocationParams(options);\n\n if (params.max_tokens === -1) {\n if (prompts.length !== 1) {\n throw new Error(\n \"max_tokens set to -1 not supported for multiple inputs\"\n );\n }\n params.max_tokens = await calculateMaxTokens({\n prompt: prompts[0],\n // Cast here to allow for other models that may not fit the union\n modelName: this.model as TiktokenModel,\n });\n }\n\n for (let i = 0; i < subPrompts.length; i += 1) {\n const data = params.stream\n ? await (async () => {\n const choices: OpenAIClient.CompletionChoice[] = [];\n let response: Omit<OpenAIClient.Completion, \"choices\"> | undefined;\n const stream = await this.completionWithRetry(\n {\n ...params,\n stream: true,\n prompt: subPrompts[i],\n },\n options\n );\n for await (const message of stream) {\n // on the first message set the response properties\n if (!response) {\n response = {\n id: message.id,\n object: message.object,\n created: message.created,\n model: message.model,\n };\n }\n\n // on all messages, update choice\n for (const part of message.choices) {\n if (!choices[part.index]) {\n choices[part.index] = part;\n } else {\n const choice = choices[part.index];\n choice.text += part.text;\n choice.finish_reason = part.finish_reason;\n choice.logprobs = part.logprobs;\n }\n // oxlint-disable-next-line no-void\n void runManager?.handleLLMNewToken(part.text, {\n prompt: Math.floor(part.index / this.n),\n completion: part.index % this.n,\n });\n }\n }\n if (options.signal?.aborted) {\n throw new Error(\"AbortError\");\n }\n return { ...response, choices };\n })()\n : await this.completionWithRetry(\n {\n ...params,\n stream: false,\n prompt: subPrompts[i],\n },\n {\n signal: options.signal,\n ...options.options,\n }\n );\n\n choices.push(...data.choices);\n const {\n completion_tokens: completionTokens,\n prompt_tokens: promptTokens,\n total_tokens: totalTokens,\n } = data.usage\n ? data.usage\n : {\n completion_tokens: undefined,\n prompt_tokens: undefined,\n total_tokens: undefined,\n };\n\n if (completionTokens) {\n tokenUsage.completionTokens =\n (tokenUsage.completionTokens ?? 0) + completionTokens;\n }\n\n if (promptTokens) {\n tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;\n }\n\n if (totalTokens) {\n tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;\n }\n }\n\n const generations = chunkArray(choices, this.n).map((promptChoices) =>\n promptChoices.map((choice) => ({\n text: choice.text ?? \"\",\n generationInfo: {\n finishReason: choice.finish_reason,\n logprobs: choice.logprobs,\n },\n }))\n );\n return {\n generations,\n llmOutput: { tokenUsage },\n };\n }\n\n // TODO(jacoblee): Refactor with _generate(..., {stream: true}) implementation?\n async *_streamResponseChunks(\n input: string,\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<GenerationChunk> {\n const params = {\n ...this.invocationParams(options),\n prompt: input,\n stream: true as const,\n };\n const stream = await this.completionWithRetry(params, options);\n for await (const data of stream) {\n const choice = data?.choices[0];\n if (!choice) {\n continue;\n }\n const chunk = new GenerationChunk({\n text: choice.text,\n generationInfo: {\n finishReason: choice.finish_reason,\n },\n });\n yield chunk;\n // oxlint-disable-next-line no-void\n void runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n if (options.signal?.aborted) {\n throw new Error(\"AbortError\");\n }\n }\n\n /**\n * Calls the OpenAI API with retry logic in case of failures.\n * @param request The request to send to the OpenAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the OpenAI API.\n */\n async completionWithRetry(\n request: OpenAIClient.CompletionCreateParamsStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<AsyncIterable<OpenAIClient.Completion>>;\n\n async completionWithRetry(\n request: OpenAIClient.CompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<OpenAIClient.Completions.Completion>;\n\n async completionWithRetry(\n request:\n | OpenAIClient.CompletionCreateParamsStreaming\n | OpenAIClient.CompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<\n AsyncIterable<OpenAIClient.Completion> | OpenAIClient.Completions.Completion\n > {\n const requestOptions = this._getClientOptions(options);\n return this.caller.call(async () => {\n try {\n const res = await this.client.completions.create(\n request,\n requestOptions\n );\n return res;\n } catch (e) {\n const error = wrapOpenAIClientError(e);\n throw error;\n }\n });\n }\n\n /**\n * Calls the OpenAI API with retry logic in case of failures.\n * @param request The request to send to the OpenAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the OpenAI API.\n */\n protected _getClientOptions(\n options: OpenAICoreRequestOptions | undefined\n ): OpenAICoreRequestOptions {\n if (!this.client) {\n const openAIEndpointConfig: OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n\n if (!params.baseURL) {\n delete params.baseURL;\n }\n\n params.defaultHeaders = getHeadersWithUserAgent(params.defaultHeaders);\n\n this.client = new OpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as OpenAICoreRequestOptions;\n return requestOptions;\n }\n\n _llmType() {\n return \"openai\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAaA,WAAb,cACU,QAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,WAAW;AACb,SAAO,CAAC,GAAG,MAAM,UAAU,UAAU;;CAGvC,kBAAkB;CAElB,IAAI,aAAoD;AACtD,SAAO;GACL,cAAc;GACd,QAAQ;GACR,cAAc;GACf;;CAGH,IAAI,aAAqC;AACvC,SAAO;GACL,WAAW;GACX,cAAc;GACd,QAAQ;GACT;;CAGH;CAEA;CAEA;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sBACL,OACA,SACA,YACiC;EACjC,MAAM,SAAS;GACb,GAAG,KAAK,iBAAiB,QAAQ;GACjC,QAAQ;GACR,QAAQ;GACT;EACD,MAAM,SAAS,MAAM,KAAK,oBAAoB,QAAQ,QAAQ;AAC9D,aAAW,MAAM,QAAQ,QAAQ;GAC/B,MAAM,SAAS,MAAM,QAAQ;AAC7B,OAAI,CAAC,OACH;GAEF,MAAM,QAAQ,IAAI,gBAAgB;IAChC,MAAM,OAAO;IACb,gBAAgB,EACd,cAAc,OAAO,eACtB;IACF,CAAC;AACF,SAAM;AAED,eAAY,kBAAkB,MAAM,QAAQ,GAAG;;AAEtD,MAAI,QAAQ,QAAQ,QAClB,OAAM,IAAI,MAAM,aAAa;;CAoBjC,MAAM,oBACJ,SAGA,SAGA;EACA,MAAM,iBAAiB,KAAK,kBAAkB,QAAQ;AACtD,SAAO,KAAK,OAAO,KAAK,YAAY;AAClC,OAAI;AAKF,WAJY,MAAM,KAAK,OAAO,YAAY,OACxC,SACA,eACD;YAEM,GAAG;AAEV,UADc,sBAAsB,EAAE;;IAGxC;;;;;;;;CASJ,kBACE,SAC0B;AAC1B,MAAI,CAAC,KAAK,QAAQ;GAKhB,MAAM,WAAW,YAJkC,EACjD,SAAS,KAAK,aAAa,SAC5B,CAEiD;GAElD,MAAM,SAAS;IACb,GAAG,KAAK;IACR,SAAS;IACT,SAAS,KAAK;IACd,YAAY;IACb;AAED,OAAI,CAAC,OAAO,QACV,QAAO,OAAO;AAGhB,UAAO,iBAAiB,wBAAwB,OAAO,eAAe;AAEtE,QAAK,SAAS,IAAIC,OAAa,OAAO;;AAMxC,SAJuB;GACrB,GAAG,KAAK;GACR,GAAG;GACJ;;CAIH,WAAW;AACT,SAAO"}
|
|
@@ -13,6 +13,18 @@ type ApplyPatchDeleteFileOperation = OpenAI.Responses.ResponseApplyPatchToolCall
|
|
|
13
13
|
* Union type of all apply patch operations from OpenAI SDK.
|
|
14
14
|
*/
|
|
15
15
|
type ApplyPatchOperation = NonNullable<OpenAI.Responses.ResponseApplyPatchToolCall["operation"]>;
|
|
16
|
+
declare const ApplyPatchOperationSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
17
|
+
type: z.ZodLiteral<"create_file">;
|
|
18
|
+
path: z.ZodString;
|
|
19
|
+
diff: z.ZodString;
|
|
20
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
21
|
+
type: z.ZodLiteral<"update_file">;
|
|
22
|
+
path: z.ZodString;
|
|
23
|
+
diff: z.ZodString;
|
|
24
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
25
|
+
type: z.ZodLiteral<"delete_file">;
|
|
26
|
+
path: z.ZodString;
|
|
27
|
+
}, z.core.$strip>]>;
|
|
16
28
|
/**
|
|
17
29
|
* Options for the Apply Patch tool.
|
|
18
30
|
*/
|
|
@@ -130,18 +142,7 @@ type ApplyPatchTool = OpenAI.Responses.ApplyPatchTool;
|
|
|
130
142
|
* - Consider backing up files before applying patches
|
|
131
143
|
* - Implement "all-or-nothing" semantics if atomicity is required
|
|
132
144
|
*/
|
|
133
|
-
declare function applyPatch(options: ApplyPatchOptions): DynamicStructuredTool<
|
|
134
|
-
type: z.ZodLiteral<"create_file">;
|
|
135
|
-
path: z.ZodString;
|
|
136
|
-
diff: z.ZodString;
|
|
137
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
138
|
-
type: z.ZodLiteral<"update_file">;
|
|
139
|
-
path: z.ZodString;
|
|
140
|
-
diff: z.ZodString;
|
|
141
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
142
|
-
type: z.ZodLiteral<"delete_file">;
|
|
143
|
-
path: z.ZodString;
|
|
144
|
-
}, z.core.$strip>]>, ApplyPatchOperation, unknown, string, unknown, string>;
|
|
145
|
+
declare function applyPatch(options: ApplyPatchOptions): DynamicStructuredTool<typeof ApplyPatchOperationSchema, ApplyPatchOperation, unknown, string>;
|
|
145
146
|
//#endregion
|
|
146
147
|
export { ApplyPatchCreateFileOperation, ApplyPatchDeleteFileOperation, ApplyPatchOperation, ApplyPatchOptions, ApplyPatchTool, ApplyPatchUpdateFileOperation, applyPatch };
|
|
147
148
|
//# sourceMappingURL=applyPatch.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyPatch.d.cts","names":[],"sources":["../../src/tools/applyPatch.ts"],"mappings":";;;;;;;AAOA;KAAY,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;;;;KAKxC,mBAAA,GAAsB,WAAA,CAChC,MAAA,CAAa,SAAA,CAAU,0BAAA
|
|
1
|
+
{"version":3,"file":"applyPatch.d.cts","names":[],"sources":["../../src/tools/applyPatch.ts"],"mappings":";;;;;;;AAOA;KAAY,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;;;;KAKxC,mBAAA,GAAsB,WAAA,CAChC,MAAA,CAAa,SAAA,CAAU,0BAAA;AAAA,cAsBZ,yBAAA,EAAyB,CAAA,CAAA,QAAA,WAAA,CAAA,CAAA,SAAA;;;;;;;;;;;;;;;UASrB,iBAAA;EA/BkC;;AAsBnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0CE,OAAA,GAAU,SAAA,EAAW,mBAAA,cAAiC,OAAA;AAAA;;;;KAM5C,cAAA,GAAiB,MAAA,CAAa,SAAA,CAAU,cAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAvCpD;;;;;;;;;;AAuCA;;;;;;;;;AA+EA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,UAAA,CAAW,OAAA,EAAS,iBAAA,GAed,qBAAA,QACX,yBAAA,EACP,mBAAA"}
|
|
@@ -13,6 +13,18 @@ type ApplyPatchDeleteFileOperation = OpenAI.Responses.ResponseApplyPatchToolCall
|
|
|
13
13
|
* Union type of all apply patch operations from OpenAI SDK.
|
|
14
14
|
*/
|
|
15
15
|
type ApplyPatchOperation = NonNullable<OpenAI.Responses.ResponseApplyPatchToolCall["operation"]>;
|
|
16
|
+
declare const ApplyPatchOperationSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
17
|
+
type: z.ZodLiteral<"create_file">;
|
|
18
|
+
path: z.ZodString;
|
|
19
|
+
diff: z.ZodString;
|
|
20
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
21
|
+
type: z.ZodLiteral<"update_file">;
|
|
22
|
+
path: z.ZodString;
|
|
23
|
+
diff: z.ZodString;
|
|
24
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
25
|
+
type: z.ZodLiteral<"delete_file">;
|
|
26
|
+
path: z.ZodString;
|
|
27
|
+
}, z.core.$strip>]>;
|
|
16
28
|
/**
|
|
17
29
|
* Options for the Apply Patch tool.
|
|
18
30
|
*/
|
|
@@ -130,18 +142,7 @@ type ApplyPatchTool = OpenAI.Responses.ApplyPatchTool;
|
|
|
130
142
|
* - Consider backing up files before applying patches
|
|
131
143
|
* - Implement "all-or-nothing" semantics if atomicity is required
|
|
132
144
|
*/
|
|
133
|
-
declare function applyPatch(options: ApplyPatchOptions): DynamicStructuredTool<
|
|
134
|
-
type: z.ZodLiteral<"create_file">;
|
|
135
|
-
path: z.ZodString;
|
|
136
|
-
diff: z.ZodString;
|
|
137
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
138
|
-
type: z.ZodLiteral<"update_file">;
|
|
139
|
-
path: z.ZodString;
|
|
140
|
-
diff: z.ZodString;
|
|
141
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
142
|
-
type: z.ZodLiteral<"delete_file">;
|
|
143
|
-
path: z.ZodString;
|
|
144
|
-
}, z.core.$strip>]>, ApplyPatchOperation, unknown, string, unknown, string>;
|
|
145
|
+
declare function applyPatch(options: ApplyPatchOptions): DynamicStructuredTool<typeof ApplyPatchOperationSchema, ApplyPatchOperation, unknown, string>;
|
|
145
146
|
//#endregion
|
|
146
147
|
export { ApplyPatchCreateFileOperation, ApplyPatchDeleteFileOperation, ApplyPatchOperation, ApplyPatchOptions, ApplyPatchTool, ApplyPatchUpdateFileOperation, applyPatch };
|
|
147
148
|
//# sourceMappingURL=applyPatch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyPatch.d.ts","names":[],"sources":["../../src/tools/applyPatch.ts"],"mappings":";;;;;;;AAOA;KAAY,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;;;;KAKxC,mBAAA,GAAsB,WAAA,CAChC,MAAA,CAAa,SAAA,CAAU,0BAAA
|
|
1
|
+
{"version":3,"file":"applyPatch.d.ts","names":[],"sources":["../../src/tools/applyPatch.ts"],"mappings":";;;;;;;AAOA;KAAY,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;AAAA,KACxC,6BAAA,GACV,MAAA,CAAa,SAAA,CAAU,0BAAA,CAA2B,UAAA;;;;KAKxC,mBAAA,GAAsB,WAAA,CAChC,MAAA,CAAa,SAAA,CAAU,0BAAA;AAAA,cAsBZ,yBAAA,EAAyB,CAAA,CAAA,QAAA,WAAA,CAAA,CAAA,SAAA;;;;;;;;;;;;;;;UASrB,iBAAA;EA/BkC;;AAsBnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0CE,OAAA,GAAU,SAAA,EAAW,mBAAA,cAAiC,OAAA;AAAA;;;;KAM5C,cAAA,GAAiB,MAAA,CAAa,SAAA,CAAU,cAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAvCpD;;;;;;;;;;AAuCA;;;;;;;;;AA+EA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,UAAA,CAAW,OAAA,EAAS,iBAAA,GAed,qBAAA,QACX,yBAAA,EACP,mBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computerUse.cjs","names":["z","ToolMessage"],"sources":["../../src/tools/computerUse.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { tool, type DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { type ToolRuntime } from \"@langchain/core/tools\";\nimport {\n ToolMessage,\n type AIMessage,\n type BaseMessage,\n} from \"@langchain/core/messages\";\n\n/**\n * The type of computer environment to control.\n */\nexport type ComputerUseEnvironment =\n | \"browser\"\n | \"mac\"\n | \"windows\"\n | \"linux\"\n | \"ubuntu\";\n\n/**\n * Re-export action types from OpenAI SDK for convenience.\n */\nexport type ComputerUseClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Click;\nexport type ComputerUseDoubleClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.DoubleClick;\nexport type ComputerUseDragAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Drag;\nexport type ComputerUseKeypressAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Keypress;\nexport type ComputerUseMoveAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Move;\nexport type ComputerUseScreenshotAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Screenshot;\nexport type ComputerUseScrollAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Scroll;\nexport type ComputerUseTypeAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Type;\nexport type ComputerUseWaitAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Wait;\n\n/**\n * Union type of all computer use actions from OpenAI SDK.\n */\nexport type ComputerUseAction =\n OpenAIClient.Responses.ResponseComputerToolCall[\"action\"];\n\n// Zod schemas for computer use actions\nconst ComputerUseScreenshotActionSchema = z.object({\n type: z.literal(\"screenshot\"),\n});\n\nconst ComputerUseClickActionSchema = z.object({\n type: z.literal(\"click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDoubleClickActionSchema = z.object({\n type: z.literal(\"double_click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDragActionSchema = z.object({\n type: z.literal(\"drag\"),\n path: z.array(z.object({ x: z.number(), y: z.number() })),\n});\n\nconst ComputerUseKeypressActionSchema = z.object({\n type: z.literal(\"keypress\"),\n keys: z.array(z.string()),\n});\n\nconst ComputerUseMoveActionSchema = z.object({\n type: z.literal(\"move\"),\n x: z.number(),\n y: z.number(),\n});\n\nconst ComputerUseScrollActionSchema = z.object({\n type: z.literal(\"scroll\"),\n x: z.number(),\n y: z.number(),\n scroll_x: z.number(),\n scroll_y: z.number(),\n});\n\nconst ComputerUseTypeActionSchema = z.object({\n type: z.literal(\"type\"),\n text: z.string(),\n});\n\nconst ComputerUseWaitActionSchema = z.object({\n type: z.literal(\"wait\"),\n duration: z.number().optional(),\n});\n\n// Union schema for individual action types\nconst ComputerUseActionUnionSchema = z.union([\n ComputerUseScreenshotActionSchema,\n ComputerUseClickActionSchema,\n ComputerUseDoubleClickActionSchema,\n ComputerUseDragActionSchema,\n ComputerUseKeypressActionSchema,\n ComputerUseMoveActionSchema,\n ComputerUseScrollActionSchema,\n ComputerUseTypeActionSchema,\n ComputerUseWaitActionSchema,\n]);\n\n// Schema for the input structure received from parseComputerCall\n// The action is wrapped in an `action` property: { action: { type: 'screenshot' } }\nexport const ComputerUseActionSchema = z.object({\n action: ComputerUseActionUnionSchema,\n});\n\n// TypeScript types derived from Zod schemas\nexport type ComputerUseScreenshotActionType = z.infer<\n typeof ComputerUseScreenshotActionSchema\n>;\nexport type ComputerUseClickActionType = z.infer<\n typeof ComputerUseClickActionSchema\n>;\nexport type ComputerUseDoubleClickActionType = z.infer<\n typeof ComputerUseDoubleClickActionSchema\n>;\nexport type ComputerUseDragActionType = z.infer<\n typeof ComputerUseDragActionSchema\n>;\nexport type ComputerUseKeypressActionType = z.infer<\n typeof ComputerUseKeypressActionSchema\n>;\nexport type ComputerUseMoveActionType = z.infer<\n typeof ComputerUseMoveActionSchema\n>;\nexport type ComputerUseScrollActionType = z.infer<\n typeof ComputerUseScrollActionSchema\n>;\nexport type ComputerUseTypeActionType = z.infer<\n typeof ComputerUseTypeActionSchema\n>;\nexport type ComputerUseWaitActionType = z.infer<\n typeof ComputerUseWaitActionSchema\n>;\n\n/**\n * Input structure for the Computer Use tool.\n * The action is wrapped in an `action` property.\n */\nexport interface ComputerUseInput {\n action: ComputerUseAction;\n}\n\nexport type ComputerUseReturnType =\n | string\n | Promise<string>\n | ToolMessage<any>\n | Promise<ToolMessage<any>>;\n\n/**\n * Options for the Computer Use tool.\n */\nexport interface ComputerUseOptions {\n /**\n * The width of the computer display in pixels.\n */\n displayWidth: number;\n\n /**\n * The height of the computer display in pixels.\n */\n displayHeight: number;\n\n /**\n * The type of computer environment to control.\n * - `browser`: Browser automation (recommended for most use cases)\n * - `mac`: macOS environment\n * - `windows`: Windows environment\n * - `linux`: Linux environment\n * - `ubuntu`: Ubuntu environment\n */\n environment: ComputerUseEnvironment;\n\n /**\n * Execute function that handles computer action execution.\n * This function receives the action input and should return a base64-encoded\n * screenshot of the result.\n */\n execute: (\n action: ComputerUseAction,\n runtime: ToolRuntime<any, any>\n ) => ComputerUseReturnType;\n}\n\n/**\n * OpenAI Computer Use tool type for the Responses API.\n */\nexport type ComputerUseTool = OpenAIClient.Responses.ComputerUsePreviewTool;\n\nconst TOOL_NAME = \"computer_use\";\n\n/**\n * Creates a Computer Use tool that allows models to control computer interfaces\n * and perform tasks by simulating mouse clicks, keyboard input, scrolling, and more.\n *\n * **Computer Use** is a practical application of OpenAI's Computer-Using Agent (CUA)\n * model (`computer-use-preview`), which combines vision capabilities with advanced\n * reasoning to simulate controlling computer interfaces.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends computer actions (click, type, scroll, etc.)\n * 2. Your code executes these actions in a controlled environment\n * 3. You capture a screenshot of the result\n * 4. Send the screenshot back to the model\n * 5. Repeat until the task is complete\n *\n * **Important**: Computer use is in beta and requires careful consideration:\n * - Use in sandboxed environments only\n * - Do not use for high-stakes or authenticated tasks\n * - Always implement human-in-the-loop for important decisions\n * - Handle safety checks appropriately\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-computer-use | OpenAI Computer Use Documentation}\n *\n * @param options - Configuration options for the Computer Use tool\n * @returns A Computer Use tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"computer-use-preview\" });\n *\n * // With execute callback for automatic action handling\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * execute: async (action) => {\n * if (action.type === \"screenshot\") {\n * return captureScreenshot();\n * }\n * if (action.type === \"click\") {\n * await page.mouse.click(action.x, action.y, { button: action.button });\n * return captureScreenshot();\n * }\n * if (action.type === \"type\") {\n * await page.keyboard.type(action.text);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * return captureScreenshot();\n * },\n * });\n *\n * const llmWithComputer = model.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Check the latest news on bing.com\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Without execute callback (manual action handling)\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * });\n *\n * const response = await model.invoke(\"Check the news\", {\n * tools: [computer],\n * });\n *\n * // Access the computer call from the response\n * const computerCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"computer_call\"\n * );\n * if (computerCall) {\n * console.log(\"Action to execute:\", computerCall.action);\n * // Execute the action manually, then send back a screenshot\n * }\n * ```\n *\n * @example\n * ```typescript\n * // For macOS desktop automation with Docker\n * const computer = tools.computerUse({\n * displayWidth: 1920,\n * displayHeight: 1080,\n * environment: \"mac\",\n * execute: async (action) => {\n * if (action.type === \"click\") {\n * await dockerExec(\n * `DISPLAY=:99 xdotool mousemove ${action.x} ${action.y} click 1`,\n * containerName\n * );\n * }\n * // Capture screenshot from container\n * return await getDockerScreenshot(containerName);\n * },\n * });\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Requires `computer-use-preview` model\n * - Actions include: click, double_click, drag, keypress, move, screenshot, scroll, type, wait\n * - Safety checks may be returned that require acknowledgment before proceeding\n * - Use `truncation: \"auto\"` parameter when making requests\n * - Recommended to use with `reasoning.summary` for debugging\n */\nexport function computerUse(options: ComputerUseOptions) {\n const computerTool = tool(\n async (\n input: ComputerUseInput,\n runtime: ToolRuntime<{ messages: BaseMessage[] }>\n ) => {\n /**\n * get computer_use call id from runtime\n */\n const aiMessage = runtime.state?.messages.at(-1) as AIMessage | undefined;\n const computerToolCall = aiMessage?.tool_calls?.find(\n (tc) => tc.name === \"computer_use\"\n );\n const computerToolCallId = computerToolCall?.id;\n if (!computerToolCallId) {\n throw new Error(\"Computer use call id not found\");\n }\n\n const result = await options.execute(input.action, runtime);\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n if (typeof result === \"string\") {\n return new ToolMessage({\n content: result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n },\n });\n }\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n return new ToolMessage({\n ...result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n ...result.additional_kwargs,\n },\n });\n },\n {\n name: TOOL_NAME,\n description:\n \"Control a computer interface by executing mouse clicks, keyboard input, scrolling, and other actions.\",\n schema: ComputerUseActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_use_preview\",\n display_width: options.displayWidth,\n display_height: options.displayHeight,\n environment: options.environment,\n } satisfies ComputerUseTool,\n };\n\n /**\n * return as typed {@link DynamicStructuredTool} so we don't get any type\n * errors like \"can't export tool without reference\"\n */\n return computerTool as DynamicStructuredTool<\n typeof ComputerUseActionSchema,\n ComputerUseInput,\n unknown,\n ComputerUseReturnType\n >;\n}\n"],"mappings":";;;;AAkDA,MAAM,oCAAoCA,OAAAA,EAAE,OAAO,EACjD,MAAMA,OAAAA,EAAE,QAAQ,aAAa,EAC9B,CAAC;AAEF,MAAM,+BAA+BA,OAAAA,EAAE,OAAO;CAC5C,MAAMA,OAAAA,EAAE,QAAQ,QAAQ;CACxB,GAAGA,OAAAA,EAAE,QAAQ;CACb,GAAGA,OAAAA,EAAE,QAAQ;CACb,QAAQA,OAAAA,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,qCAAqCA,OAAAA,EAAE,OAAO;CAClD,MAAMA,OAAAA,EAAE,QAAQ,eAAe;CAC/B,GAAGA,OAAAA,EAAE,QAAQ;CACb,GAAGA,OAAAA,EAAE,QAAQ;CACb,QAAQA,OAAAA,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,8BAA8BA,OAAAA,EAAE,OAAO;CAC3C,MAAMA,OAAAA,EAAE,QAAQ,OAAO;CACvB,MAAMA,OAAAA,EAAE,MAAMA,OAAAA,EAAE,OAAO;EAAE,GAAGA,OAAAA,EA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CACvD,MAAM,gBAAA,GAAA,sBAAA,MACJ,OACE,OACA,YACG;EAQH,MAAM,uBAJY,QAAQ,OAAO,SAAS,GAAG,GAAG,GACZ,YAAY,MAC7C,OAAO,GAAG,SAAS,eACrB,GAC4C;AAC7C,MAAI,CAAC,mBACH,OAAM,IAAI,MAAM,iCAAiC;EAGnD,MAAM,SAAS,MAAM,QAAQ,QAAQ,MAAM,QAAQ,QAAQ;;;;AAK3D,MAAI,OAAO,WAAW,SACpB,QAAO,IAAIC,yBAAAA,YAAY;GACrB,SAAS;GACT,cAAc;GACd,mBAAmB,EACjB,MAAM,wBACP;GACF,CAAC;;;;AAMJ,SAAO,IAAIA,yBAAAA,YAAY;GACrB,GAAG;GACH,cAAc;GACd,mBAAmB;IACjB,MAAM;IACN,GAAG,OAAO;IACX;GACF,CAAC;IAEJ;EACE,MAAM;EACN,aACE;EACF,QAAQ;EACT,CACF;AAED,cAAa,SAAS;EACpB,GAAI,aAAa,UAAU,EAAE;EAC7B,wBAAwB;GACtB,MAAM;GACN,eAAe,QAAQ;GACvB,gBAAgB,QAAQ;GACxB,aAAa,QAAQ;GACtB;EACF;;;;;AAMD,QAAO"}
|
|
1
|
+
{"version":3,"file":"computerUse.cjs","names":["z","ToolMessage"],"sources":["../../src/tools/computerUse.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { tool, type DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { type ToolRuntime } from \"@langchain/core/tools\";\nimport {\n ToolMessage,\n type AIMessage,\n type BaseMessage,\n} from \"@langchain/core/messages\";\n\n/**\n * The type of computer environment to control.\n */\nexport type ComputerUseEnvironment =\n | \"browser\"\n | \"mac\"\n | \"windows\"\n | \"linux\"\n | \"ubuntu\";\n\n/**\n * Re-export action types from OpenAI SDK for convenience.\n */\nexport type ComputerUseClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Click;\nexport type ComputerUseDoubleClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.DoubleClick;\nexport type ComputerUseDragAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Drag;\nexport type ComputerUseKeypressAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Keypress;\nexport type ComputerUseMoveAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Move;\nexport type ComputerUseScreenshotAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Screenshot;\nexport type ComputerUseScrollAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Scroll;\nexport type ComputerUseTypeAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Type;\nexport type ComputerUseWaitAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Wait;\n\n/**\n * Union type of all computer use actions from OpenAI SDK.\n */\nexport type ComputerUseAction =\n OpenAIClient.Responses.ResponseComputerToolCall[\"action\"];\n\n// Zod schemas for computer use actions\nconst ComputerUseScreenshotActionSchema = z.object({\n type: z.literal(\"screenshot\"),\n});\n\nconst ComputerUseClickActionSchema = z.object({\n type: z.literal(\"click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDoubleClickActionSchema = z.object({\n type: z.literal(\"double_click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDragActionSchema = z.object({\n type: z.literal(\"drag\"),\n path: z.array(z.object({ x: z.number(), y: z.number() })),\n});\n\nconst ComputerUseKeypressActionSchema = z.object({\n type: z.literal(\"keypress\"),\n keys: z.array(z.string()),\n});\n\nconst ComputerUseMoveActionSchema = z.object({\n type: z.literal(\"move\"),\n x: z.number(),\n y: z.number(),\n});\n\nconst ComputerUseScrollActionSchema = z.object({\n type: z.literal(\"scroll\"),\n x: z.number(),\n y: z.number(),\n scroll_x: z.number(),\n scroll_y: z.number(),\n});\n\nconst ComputerUseTypeActionSchema = z.object({\n type: z.literal(\"type\"),\n text: z.string(),\n});\n\nconst ComputerUseWaitActionSchema = z.object({\n type: z.literal(\"wait\"),\n duration: z.number().optional(),\n});\n\n// Union schema for individual action types\nconst ComputerUseActionUnionSchema = z.union([\n ComputerUseScreenshotActionSchema,\n ComputerUseClickActionSchema,\n ComputerUseDoubleClickActionSchema,\n ComputerUseDragActionSchema,\n ComputerUseKeypressActionSchema,\n ComputerUseMoveActionSchema,\n ComputerUseScrollActionSchema,\n ComputerUseTypeActionSchema,\n ComputerUseWaitActionSchema,\n]);\n\n// Schema for the input structure received from parseComputerCall\n// The action is wrapped in an `action` property: { action: { type: 'screenshot' } }\nexport const ComputerUseActionSchema = z.object({\n action: ComputerUseActionUnionSchema,\n});\n\n// TypeScript types derived from Zod schemas\nexport type ComputerUseScreenshotActionType = z.infer<\n typeof ComputerUseScreenshotActionSchema\n>;\nexport type ComputerUseClickActionType = z.infer<\n typeof ComputerUseClickActionSchema\n>;\nexport type ComputerUseDoubleClickActionType = z.infer<\n typeof ComputerUseDoubleClickActionSchema\n>;\nexport type ComputerUseDragActionType = z.infer<\n typeof ComputerUseDragActionSchema\n>;\nexport type ComputerUseKeypressActionType = z.infer<\n typeof ComputerUseKeypressActionSchema\n>;\nexport type ComputerUseMoveActionType = z.infer<\n typeof ComputerUseMoveActionSchema\n>;\nexport type ComputerUseScrollActionType = z.infer<\n typeof ComputerUseScrollActionSchema\n>;\nexport type ComputerUseTypeActionType = z.infer<\n typeof ComputerUseTypeActionSchema\n>;\nexport type ComputerUseWaitActionType = z.infer<\n typeof ComputerUseWaitActionSchema\n>;\n\n/**\n * Input structure for the Computer Use tool.\n * The action is wrapped in an `action` property.\n */\nexport interface ComputerUseInput {\n action: ComputerUseAction;\n}\n\nexport type ComputerUseReturnType =\n | string\n | Promise<string>\n | ToolMessage<any>\n | Promise<ToolMessage<any>>;\n\n/**\n * Options for the Computer Use tool.\n */\nexport interface ComputerUseOptions {\n /**\n * The width of the computer display in pixels.\n */\n displayWidth: number;\n\n /**\n * The height of the computer display in pixels.\n */\n displayHeight: number;\n\n /**\n * The type of computer environment to control.\n * - `browser`: Browser automation (recommended for most use cases)\n * - `mac`: macOS environment\n * - `windows`: Windows environment\n * - `linux`: Linux environment\n * - `ubuntu`: Ubuntu environment\n */\n environment: ComputerUseEnvironment;\n\n /**\n * Execute function that handles computer action execution.\n * This function receives the action input and should return a base64-encoded\n * screenshot of the result.\n */\n execute: (\n action: ComputerUseAction,\n runtime: ToolRuntime<any, any>\n ) => ComputerUseReturnType;\n}\n\n/**\n * OpenAI Computer Use tool type for the Responses API.\n */\nexport type ComputerUseTool = OpenAIClient.Responses.ComputerUsePreviewTool;\n\nconst TOOL_NAME = \"computer_use\";\n\n/**\n * Creates a Computer Use tool that allows models to control computer interfaces\n * and perform tasks by simulating mouse clicks, keyboard input, scrolling, and more.\n *\n * **Computer Use** is a practical application of OpenAI's Computer-Using Agent (CUA)\n * model (`computer-use-preview`), which combines vision capabilities with advanced\n * reasoning to simulate controlling computer interfaces.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends computer actions (click, type, scroll, etc.)\n * 2. Your code executes these actions in a controlled environment\n * 3. You capture a screenshot of the result\n * 4. Send the screenshot back to the model\n * 5. Repeat until the task is complete\n *\n * **Important**: Computer use is in beta and requires careful consideration:\n * - Use in sandboxed environments only\n * - Do not use for high-stakes or authenticated tasks\n * - Always implement human-in-the-loop for important decisions\n * - Handle safety checks appropriately\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-computer-use | OpenAI Computer Use Documentation}\n *\n * @param options - Configuration options for the Computer Use tool\n * @returns A Computer Use tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"computer-use-preview\" });\n *\n * // With execute callback for automatic action handling\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * execute: async (action) => {\n * if (action.type === \"screenshot\") {\n * return captureScreenshot();\n * }\n * if (action.type === \"click\") {\n * await page.mouse.click(action.x, action.y, { button: action.button });\n * return captureScreenshot();\n * }\n * if (action.type === \"type\") {\n * await page.keyboard.type(action.text);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * return captureScreenshot();\n * },\n * });\n *\n * const llmWithComputer = model.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Check the latest news on bing.com\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Without execute callback (manual action handling)\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * });\n *\n * const response = await model.invoke(\"Check the news\", {\n * tools: [computer],\n * });\n *\n * // Access the computer call from the response\n * const computerCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"computer_call\"\n * );\n * if (computerCall) {\n * console.log(\"Action to execute:\", computerCall.action);\n * // Execute the action manually, then send back a screenshot\n * }\n * ```\n *\n * @example\n * ```typescript\n * // For macOS desktop automation with Docker\n * const computer = tools.computerUse({\n * displayWidth: 1920,\n * displayHeight: 1080,\n * environment: \"mac\",\n * execute: async (action) => {\n * if (action.type === \"click\") {\n * await dockerExec(\n * `DISPLAY=:99 xdotool mousemove ${action.x} ${action.y} click 1`,\n * containerName\n * );\n * }\n * // Capture screenshot from container\n * return await getDockerScreenshot(containerName);\n * },\n * });\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Requires `computer-use-preview` model\n * - Actions include: click, double_click, drag, keypress, move, screenshot, scroll, type, wait\n * - Safety checks may be returned that require acknowledgment before proceeding\n * - Use `truncation: \"auto\"` parameter when making requests\n * - Recommended to use with `reasoning.summary` for debugging\n */\nexport function computerUse(options: ComputerUseOptions) {\n const computerTool = tool(\n async (\n input: ComputerUseInput,\n runtime: ToolRuntime<{ messages: BaseMessage[] }>\n ) => {\n /**\n * get computer_use call id from runtime\n */\n const aiMessage = runtime.state?.messages.at(-1) as AIMessage | undefined;\n const computerToolCall = aiMessage?.tool_calls?.find(\n (tc) => tc.name === \"computer_use\"\n );\n const computerToolCallId = computerToolCall?.id;\n if (!computerToolCallId) {\n throw new Error(\"Computer use call id not found\");\n }\n\n const result = await options.execute(input.action, runtime);\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n if (typeof result === \"string\") {\n return new ToolMessage({\n content: result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n },\n });\n }\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n return new ToolMessage({\n ...result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n ...result.additional_kwargs,\n },\n });\n },\n {\n name: TOOL_NAME,\n description:\n \"Control a computer interface by executing mouse clicks, keyboard input, scrolling, and other actions.\",\n schema: ComputerUseActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_use_preview\",\n display_width: options.displayWidth,\n display_height: options.displayHeight,\n environment: options.environment,\n } satisfies ComputerUseTool,\n };\n\n /**\n * return as typed {@link DynamicStructuredTool} so we don't get any type\n * errors like \"can't export tool without reference\"\n */\n return computerTool as DynamicStructuredTool<\n typeof ComputerUseActionSchema,\n ComputerUseInput,\n unknown,\n ComputerUseReturnType\n >;\n}\n"],"mappings":";;;;AAkDA,MAAM,oCAAoCA,OAAAA,EAAE,OAAO,EACjD,MAAMA,OAAAA,EAAE,QAAQ,aAAa,EAC9B,CAAC;AAEF,MAAM,+BAA+BA,OAAAA,EAAE,OAAO;CAC5C,MAAMA,OAAAA,EAAE,QAAQ,QAAQ;CACxB,GAAGA,OAAAA,EAAE,QAAQ;CACb,GAAGA,OAAAA,EAAE,QAAQ;CACb,QAAQA,OAAAA,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,M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;CACvB,UAAUA,OAAAA,EAAE,QAAQ,CAAC,UAAU;CAChC,CAAC;AAGF,MAAM,+BAA+BA,OAAAA,EAAE,MAAM;CAC3C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAIF,MAAa,0BAA0BA,OAAAA,EAAE,OAAO,EAC9C,QAAQ,8BACT,CAAC;AAqFF,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkHlB,SAAgB,YAAY,SAA6B;CACvD,MAAM,gBAAA,GAAA,sBAAA,MACJ,OACE,OACA,YACG;EAQH,MAAM,uBAJY,QAAQ,OAAO,SAAS,GAAG,GAAG,GACZ,YAAY,MAC7C,OAAO,GAAG,SAAS,eACrB,GAC4C;AAC7C,MAAI,CAAC,mBACH,OAAM,IAAI,MAAM,iCAAiC;EAGnD,MAAM,SAAS,MAAM,QAAQ,QAAQ,MAAM,QAAQ,QAAQ;;;;AAK3D,MAAI,OAAO,WAAW,SACpB,QAAO,IAAIC,yBAAAA,YAAY;GACrB,SAAS;GACT,cAAc;GACd,mBAAmB,EACjB,MAAM,wBACP;GACF,CAAC;;;;AAMJ,SAAO,IAAIA,yBAAAA,YAAY;GACrB,GAAG;GACH,cAAc;GACd,mBAAmB;IACjB,MAAM;IACN,GAAG,OAAO;IACX;GACF,CAAC;IAEJ;EACE,MAAM;EACN,aACE;EACF,QAAQ;EACT,CACF;AAED,cAAa,SAAS;EACpB,GAAI,aAAa,UAAU,EAAE;EAC7B,wBAAwB;GACtB,MAAM;GACN,eAAe,QAAQ;GACvB,gBAAgB,QAAQ;GACxB,aAAa,QAAQ;GACtB;EACF;;;;;AAMD,QAAO"}
|
|
@@ -24,6 +24,58 @@ type ComputerUseWaitAction = OpenAI.Responses.ResponseComputerToolCall.Wait;
|
|
|
24
24
|
* Union type of all computer use actions from OpenAI SDK.
|
|
25
25
|
*/
|
|
26
26
|
type ComputerUseAction = OpenAI.Responses.ResponseComputerToolCall["action"];
|
|
27
|
+
declare const ComputerUseActionSchema: z.ZodObject<{
|
|
28
|
+
action: z.ZodUnion<readonly [z.ZodObject<{
|
|
29
|
+
type: z.ZodLiteral<"screenshot">;
|
|
30
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
31
|
+
type: z.ZodLiteral<"click">;
|
|
32
|
+
x: z.ZodNumber;
|
|
33
|
+
y: z.ZodNumber;
|
|
34
|
+
button: z.ZodDefault<z.ZodEnum<{
|
|
35
|
+
back: "back";
|
|
36
|
+
forward: "forward";
|
|
37
|
+
left: "left";
|
|
38
|
+
right: "right";
|
|
39
|
+
wheel: "wheel";
|
|
40
|
+
}>>;
|
|
41
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
42
|
+
type: z.ZodLiteral<"double_click">;
|
|
43
|
+
x: z.ZodNumber;
|
|
44
|
+
y: z.ZodNumber;
|
|
45
|
+
button: z.ZodDefault<z.ZodEnum<{
|
|
46
|
+
back: "back";
|
|
47
|
+
forward: "forward";
|
|
48
|
+
left: "left";
|
|
49
|
+
right: "right";
|
|
50
|
+
wheel: "wheel";
|
|
51
|
+
}>>;
|
|
52
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
53
|
+
type: z.ZodLiteral<"drag">;
|
|
54
|
+
path: z.ZodArray<z.ZodObject<{
|
|
55
|
+
x: z.ZodNumber;
|
|
56
|
+
y: z.ZodNumber;
|
|
57
|
+
}, z.core.$strip>>;
|
|
58
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
59
|
+
type: z.ZodLiteral<"keypress">;
|
|
60
|
+
keys: z.ZodArray<z.ZodString>;
|
|
61
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
62
|
+
type: z.ZodLiteral<"move">;
|
|
63
|
+
x: z.ZodNumber;
|
|
64
|
+
y: z.ZodNumber;
|
|
65
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
66
|
+
type: z.ZodLiteral<"scroll">;
|
|
67
|
+
x: z.ZodNumber;
|
|
68
|
+
y: z.ZodNumber;
|
|
69
|
+
scroll_x: z.ZodNumber;
|
|
70
|
+
scroll_y: z.ZodNumber;
|
|
71
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
72
|
+
type: z.ZodLiteral<"type">;
|
|
73
|
+
text: z.ZodString;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
type: z.ZodLiteral<"wait">;
|
|
76
|
+
duration: z.ZodOptional<z.ZodNumber>;
|
|
77
|
+
}, z.core.$strip>]>;
|
|
78
|
+
}, z.core.$strip>;
|
|
27
79
|
/**
|
|
28
80
|
* Input structure for the Computer Use tool.
|
|
29
81
|
* The action is wrapped in an `action` property.
|
|
@@ -176,58 +228,7 @@ type ComputerUseTool = OpenAI.Responses.ComputerUsePreviewTool;
|
|
|
176
228
|
* - Use `truncation: "auto"` parameter when making requests
|
|
177
229
|
* - Recommended to use with `reasoning.summary` for debugging
|
|
178
230
|
*/
|
|
179
|
-
declare function computerUse(options: ComputerUseOptions): DynamicStructuredTool<
|
|
180
|
-
action: z.ZodUnion<readonly [z.ZodObject<{
|
|
181
|
-
type: z.ZodLiteral<"screenshot">;
|
|
182
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
183
|
-
type: z.ZodLiteral<"click">;
|
|
184
|
-
x: z.ZodNumber;
|
|
185
|
-
y: z.ZodNumber;
|
|
186
|
-
button: z.ZodDefault<z.ZodEnum<{
|
|
187
|
-
back: "back";
|
|
188
|
-
forward: "forward";
|
|
189
|
-
left: "left";
|
|
190
|
-
right: "right";
|
|
191
|
-
wheel: "wheel";
|
|
192
|
-
}>>;
|
|
193
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
194
|
-
type: z.ZodLiteral<"double_click">;
|
|
195
|
-
x: z.ZodNumber;
|
|
196
|
-
y: z.ZodNumber;
|
|
197
|
-
button: z.ZodDefault<z.ZodEnum<{
|
|
198
|
-
back: "back";
|
|
199
|
-
forward: "forward";
|
|
200
|
-
left: "left";
|
|
201
|
-
right: "right";
|
|
202
|
-
wheel: "wheel";
|
|
203
|
-
}>>;
|
|
204
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
205
|
-
type: z.ZodLiteral<"drag">;
|
|
206
|
-
path: z.ZodArray<z.ZodObject<{
|
|
207
|
-
x: z.ZodNumber;
|
|
208
|
-
y: z.ZodNumber;
|
|
209
|
-
}, z.core.$strip>>;
|
|
210
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
211
|
-
type: z.ZodLiteral<"keypress">;
|
|
212
|
-
keys: z.ZodArray<z.ZodString>;
|
|
213
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
214
|
-
type: z.ZodLiteral<"move">;
|
|
215
|
-
x: z.ZodNumber;
|
|
216
|
-
y: z.ZodNumber;
|
|
217
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
218
|
-
type: z.ZodLiteral<"scroll">;
|
|
219
|
-
x: z.ZodNumber;
|
|
220
|
-
y: z.ZodNumber;
|
|
221
|
-
scroll_x: z.ZodNumber;
|
|
222
|
-
scroll_y: z.ZodNumber;
|
|
223
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
224
|
-
type: z.ZodLiteral<"type">;
|
|
225
|
-
text: z.ZodString;
|
|
226
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
227
|
-
type: z.ZodLiteral<"wait">;
|
|
228
|
-
duration: z.ZodOptional<z.ZodNumber>;
|
|
229
|
-
}, z.core.$strip>]>;
|
|
230
|
-
}, z.core.$strip>, ComputerUseInput, unknown, ComputerUseReturnType, unknown, string>;
|
|
231
|
+
declare function computerUse(options: ComputerUseOptions): DynamicStructuredTool<typeof ComputerUseActionSchema, ComputerUseInput, unknown, ComputerUseReturnType>;
|
|
231
232
|
//#endregion
|
|
232
233
|
export { ComputerUseAction, ComputerUseClickAction, ComputerUseDoubleClickAction, ComputerUseDragAction, ComputerUseEnvironment, ComputerUseInput, ComputerUseKeypressAction, ComputerUseMoveAction, ComputerUseOptions, ComputerUseScreenshotAction, ComputerUseScrollAction, ComputerUseTool, ComputerUseTypeAction, ComputerUseWaitAction, computerUse };
|
|
233
234
|
//# sourceMappingURL=computerUse.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computerUse.d.cts","names":[],"sources":["../../src/tools/computerUse.ts"],"mappings":";;;;;;;AAcA;;KAAY,sBAAA;;;AAUZ;KAAY,sBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,KAAA;AAAA,KACtC,4BAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,WAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,yBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,QAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,2BAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,UAAA;AAAA,KACtC,uBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,MAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAflD;;;AAAA,KAoBY,iBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA
|
|
1
|
+
{"version":3,"file":"computerUse.d.cts","names":[],"sources":["../../src/tools/computerUse.ts"],"mappings":";;;;;;;AAcA;;KAAY,sBAAA;;;AAUZ;KAAY,sBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,KAAA;AAAA,KACtC,4BAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,WAAA;AAAA,KACtC,qBAAA,GACV,MA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Action = OpenAI.Responses.ResponseComputerToolCall.Wait;
|
|
|
24
24
|
* Union type of all computer use actions from OpenAI SDK.
|
|
25
25
|
*/
|
|
26
26
|
type ComputerUseAction = OpenAI.Responses.ResponseComputerToolCall["action"];
|
|
27
|
+
declare const ComputerUseActionSchema: z.ZodObject<{
|
|
28
|
+
action: z.ZodUnion<readonly [z.ZodObject<{
|
|
29
|
+
type: z.ZodLiteral<"screenshot">;
|
|
30
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
31
|
+
type: z.ZodLiteral<"click">;
|
|
32
|
+
x: z.ZodNumber;
|
|
33
|
+
y: z.ZodNumber;
|
|
34
|
+
button: z.ZodDefault<z.ZodEnum<{
|
|
35
|
+
back: "back";
|
|
36
|
+
forward: "forward";
|
|
37
|
+
left: "left";
|
|
38
|
+
right: "right";
|
|
39
|
+
wheel: "wheel";
|
|
40
|
+
}>>;
|
|
41
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
42
|
+
type: z.ZodLiteral<"double_click">;
|
|
43
|
+
x: z.ZodNumber;
|
|
44
|
+
y: z.ZodNumber;
|
|
45
|
+
button: z.ZodDefault<z.ZodEnum<{
|
|
46
|
+
back: "back";
|
|
47
|
+
forward: "forward";
|
|
48
|
+
left: "left";
|
|
49
|
+
right: "right";
|
|
50
|
+
wheel: "wheel";
|
|
51
|
+
}>>;
|
|
52
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
53
|
+
type: z.ZodLiteral<"drag">;
|
|
54
|
+
path: z.ZodArray<z.ZodObject<{
|
|
55
|
+
x: z.ZodNumber;
|
|
56
|
+
y: z.ZodNumber;
|
|
57
|
+
}, z.core.$strip>>;
|
|
58
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
59
|
+
type: z.ZodLiteral<"keypress">;
|
|
60
|
+
keys: z.ZodArray<z.ZodString>;
|
|
61
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
62
|
+
type: z.ZodLiteral<"move">;
|
|
63
|
+
x: z.ZodNumber;
|
|
64
|
+
y: z.ZodNumber;
|
|
65
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
66
|
+
type: z.ZodLiteral<"scroll">;
|
|
67
|
+
x: z.ZodNumber;
|
|
68
|
+
y: z.ZodNumber;
|
|
69
|
+
scroll_x: z.ZodNumber;
|
|
70
|
+
scroll_y: z.ZodNumber;
|
|
71
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
72
|
+
type: z.ZodLiteral<"type">;
|
|
73
|
+
text: z.ZodString;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
type: z.ZodLiteral<"wait">;
|
|
76
|
+
duration: z.ZodOptional<z.ZodNumber>;
|
|
77
|
+
}, z.core.$strip>]>;
|
|
78
|
+
}, z.core.$strip>;
|
|
27
79
|
/**
|
|
28
80
|
* Input structure for the Computer Use tool.
|
|
29
81
|
* The action is wrapped in an `action` property.
|
|
@@ -176,58 +228,7 @@ type ComputerUseTool = OpenAI.Responses.ComputerUsePreviewTool;
|
|
|
176
228
|
* - Use `truncation: "auto"` parameter when making requests
|
|
177
229
|
* - Recommended to use with `reasoning.summary` for debugging
|
|
178
230
|
*/
|
|
179
|
-
declare function computerUse(options: ComputerUseOptions): DynamicStructuredTool<
|
|
180
|
-
action: z.ZodUnion<readonly [z.ZodObject<{
|
|
181
|
-
type: z.ZodLiteral<"screenshot">;
|
|
182
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
183
|
-
type: z.ZodLiteral<"click">;
|
|
184
|
-
x: z.ZodNumber;
|
|
185
|
-
y: z.ZodNumber;
|
|
186
|
-
button: z.ZodDefault<z.ZodEnum<{
|
|
187
|
-
back: "back";
|
|
188
|
-
forward: "forward";
|
|
189
|
-
left: "left";
|
|
190
|
-
right: "right";
|
|
191
|
-
wheel: "wheel";
|
|
192
|
-
}>>;
|
|
193
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
194
|
-
type: z.ZodLiteral<"double_click">;
|
|
195
|
-
x: z.ZodNumber;
|
|
196
|
-
y: z.ZodNumber;
|
|
197
|
-
button: z.ZodDefault<z.ZodEnum<{
|
|
198
|
-
back: "back";
|
|
199
|
-
forward: "forward";
|
|
200
|
-
left: "left";
|
|
201
|
-
right: "right";
|
|
202
|
-
wheel: "wheel";
|
|
203
|
-
}>>;
|
|
204
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
205
|
-
type: z.ZodLiteral<"drag">;
|
|
206
|
-
path: z.ZodArray<z.ZodObject<{
|
|
207
|
-
x: z.ZodNumber;
|
|
208
|
-
y: z.ZodNumber;
|
|
209
|
-
}, z.core.$strip>>;
|
|
210
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
211
|
-
type: z.ZodLiteral<"keypress">;
|
|
212
|
-
keys: z.ZodArray<z.ZodString>;
|
|
213
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
214
|
-
type: z.ZodLiteral<"move">;
|
|
215
|
-
x: z.ZodNumber;
|
|
216
|
-
y: z.ZodNumber;
|
|
217
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
218
|
-
type: z.ZodLiteral<"scroll">;
|
|
219
|
-
x: z.ZodNumber;
|
|
220
|
-
y: z.ZodNumber;
|
|
221
|
-
scroll_x: z.ZodNumber;
|
|
222
|
-
scroll_y: z.ZodNumber;
|
|
223
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
224
|
-
type: z.ZodLiteral<"type">;
|
|
225
|
-
text: z.ZodString;
|
|
226
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
227
|
-
type: z.ZodLiteral<"wait">;
|
|
228
|
-
duration: z.ZodOptional<z.ZodNumber>;
|
|
229
|
-
}, z.core.$strip>]>;
|
|
230
|
-
}, z.core.$strip>, ComputerUseInput, unknown, ComputerUseReturnType, unknown, string>;
|
|
231
|
+
declare function computerUse(options: ComputerUseOptions): DynamicStructuredTool<typeof ComputerUseActionSchema, ComputerUseInput, unknown, ComputerUseReturnType>;
|
|
231
232
|
//#endregion
|
|
232
233
|
export { ComputerUseAction, ComputerUseClickAction, ComputerUseDoubleClickAction, ComputerUseDragAction, ComputerUseEnvironment, ComputerUseInput, ComputerUseKeypressAction, ComputerUseMoveAction, ComputerUseOptions, ComputerUseScreenshotAction, ComputerUseScrollAction, ComputerUseTool, ComputerUseTypeAction, ComputerUseWaitAction, computerUse };
|
|
233
234
|
//# sourceMappingURL=computerUse.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computerUse.d.ts","names":[],"sources":["../../src/tools/computerUse.ts"],"mappings":";;;;;;;AAcA;;KAAY,sBAAA;;;AAUZ;KAAY,sBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,KAAA;AAAA,KACtC,4BAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,WAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,yBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,QAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,2BAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,UAAA;AAAA,KACtC,uBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,MAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAAA,KACtC,qBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,IAAA;AAflD;;;AAAA,KAoBY,iBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA
|
|
1
|
+
{"version":3,"file":"computerUse.d.ts","names":[],"sources":["../../src/tools/computerUse.ts"],"mappings":";;;;;;;AAcA;;KAAY,sBAAA;;;AAUZ;KAAY,sBAAA,GACV,MAAA,CAAa,SAAA,CAAU,wBAAA,CAAyB,KAAA;AAAA,KACtC,4BAAA,GACV,MAAA,C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EACP,gBAAA,WAEA,qBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computerUse.js","names":[],"sources":["../../src/tools/computerUse.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { tool, type DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { type ToolRuntime } from \"@langchain/core/tools\";\nimport {\n ToolMessage,\n type AIMessage,\n type BaseMessage,\n} from \"@langchain/core/messages\";\n\n/**\n * The type of computer environment to control.\n */\nexport type ComputerUseEnvironment =\n | \"browser\"\n | \"mac\"\n | \"windows\"\n | \"linux\"\n | \"ubuntu\";\n\n/**\n * Re-export action types from OpenAI SDK for convenience.\n */\nexport type ComputerUseClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Click;\nexport type ComputerUseDoubleClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.DoubleClick;\nexport type ComputerUseDragAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Drag;\nexport type ComputerUseKeypressAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Keypress;\nexport type ComputerUseMoveAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Move;\nexport type ComputerUseScreenshotAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Screenshot;\nexport type ComputerUseScrollAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Scroll;\nexport type ComputerUseTypeAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Type;\nexport type ComputerUseWaitAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Wait;\n\n/**\n * Union type of all computer use actions from OpenAI SDK.\n */\nexport type ComputerUseAction =\n OpenAIClient.Responses.ResponseComputerToolCall[\"action\"];\n\n// Zod schemas for computer use actions\nconst ComputerUseScreenshotActionSchema = z.object({\n type: z.literal(\"screenshot\"),\n});\n\nconst ComputerUseClickActionSchema = z.object({\n type: z.literal(\"click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDoubleClickActionSchema = z.object({\n type: z.literal(\"double_click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDragActionSchema = z.object({\n type: z.literal(\"drag\"),\n path: z.array(z.object({ x: z.number(), y: z.number() })),\n});\n\nconst ComputerUseKeypressActionSchema = z.object({\n type: z.literal(\"keypress\"),\n keys: z.array(z.string()),\n});\n\nconst ComputerUseMoveActionSchema = z.object({\n type: z.literal(\"move\"),\n x: z.number(),\n y: z.number(),\n});\n\nconst ComputerUseScrollActionSchema = z.object({\n type: z.literal(\"scroll\"),\n x: z.number(),\n y: z.number(),\n scroll_x: z.number(),\n scroll_y: z.number(),\n});\n\nconst ComputerUseTypeActionSchema = z.object({\n type: z.literal(\"type\"),\n text: z.string(),\n});\n\nconst ComputerUseWaitActionSchema = z.object({\n type: z.literal(\"wait\"),\n duration: z.number().optional(),\n});\n\n// Union schema for individual action types\nconst ComputerUseActionUnionSchema = z.union([\n ComputerUseScreenshotActionSchema,\n ComputerUseClickActionSchema,\n ComputerUseDoubleClickActionSchema,\n ComputerUseDragActionSchema,\n ComputerUseKeypressActionSchema,\n ComputerUseMoveActionSchema,\n ComputerUseScrollActionSchema,\n ComputerUseTypeActionSchema,\n ComputerUseWaitActionSchema,\n]);\n\n// Schema for the input structure received from parseComputerCall\n// The action is wrapped in an `action` property: { action: { type: 'screenshot' } }\nexport const ComputerUseActionSchema = z.object({\n action: ComputerUseActionUnionSchema,\n});\n\n// TypeScript types derived from Zod schemas\nexport type ComputerUseScreenshotActionType = z.infer<\n typeof ComputerUseScreenshotActionSchema\n>;\nexport type ComputerUseClickActionType = z.infer<\n typeof ComputerUseClickActionSchema\n>;\nexport type ComputerUseDoubleClickActionType = z.infer<\n typeof ComputerUseDoubleClickActionSchema\n>;\nexport type ComputerUseDragActionType = z.infer<\n typeof ComputerUseDragActionSchema\n>;\nexport type ComputerUseKeypressActionType = z.infer<\n typeof ComputerUseKeypressActionSchema\n>;\nexport type ComputerUseMoveActionType = z.infer<\n typeof ComputerUseMoveActionSchema\n>;\nexport type ComputerUseScrollActionType = z.infer<\n typeof ComputerUseScrollActionSchema\n>;\nexport type ComputerUseTypeActionType = z.infer<\n typeof ComputerUseTypeActionSchema\n>;\nexport type ComputerUseWaitActionType = z.infer<\n typeof ComputerUseWaitActionSchema\n>;\n\n/**\n * Input structure for the Computer Use tool.\n * The action is wrapped in an `action` property.\n */\nexport interface ComputerUseInput {\n action: ComputerUseAction;\n}\n\nexport type ComputerUseReturnType =\n | string\n | Promise<string>\n | ToolMessage<any>\n | Promise<ToolMessage<any>>;\n\n/**\n * Options for the Computer Use tool.\n */\nexport interface ComputerUseOptions {\n /**\n * The width of the computer display in pixels.\n */\n displayWidth: number;\n\n /**\n * The height of the computer display in pixels.\n */\n displayHeight: number;\n\n /**\n * The type of computer environment to control.\n * - `browser`: Browser automation (recommended for most use cases)\n * - `mac`: macOS environment\n * - `windows`: Windows environment\n * - `linux`: Linux environment\n * - `ubuntu`: Ubuntu environment\n */\n environment: ComputerUseEnvironment;\n\n /**\n * Execute function that handles computer action execution.\n * This function receives the action input and should return a base64-encoded\n * screenshot of the result.\n */\n execute: (\n action: ComputerUseAction,\n runtime: ToolRuntime<any, any>\n ) => ComputerUseReturnType;\n}\n\n/**\n * OpenAI Computer Use tool type for the Responses API.\n */\nexport type ComputerUseTool = OpenAIClient.Responses.ComputerUsePreviewTool;\n\nconst TOOL_NAME = \"computer_use\";\n\n/**\n * Creates a Computer Use tool that allows models to control computer interfaces\n * and perform tasks by simulating mouse clicks, keyboard input, scrolling, and more.\n *\n * **Computer Use** is a practical application of OpenAI's Computer-Using Agent (CUA)\n * model (`computer-use-preview`), which combines vision capabilities with advanced\n * reasoning to simulate controlling computer interfaces.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends computer actions (click, type, scroll, etc.)\n * 2. Your code executes these actions in a controlled environment\n * 3. You capture a screenshot of the result\n * 4. Send the screenshot back to the model\n * 5. Repeat until the task is complete\n *\n * **Important**: Computer use is in beta and requires careful consideration:\n * - Use in sandboxed environments only\n * - Do not use for high-stakes or authenticated tasks\n * - Always implement human-in-the-loop for important decisions\n * - Handle safety checks appropriately\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-computer-use | OpenAI Computer Use Documentation}\n *\n * @param options - Configuration options for the Computer Use tool\n * @returns A Computer Use tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"computer-use-preview\" });\n *\n * // With execute callback for automatic action handling\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * execute: async (action) => {\n * if (action.type === \"screenshot\") {\n * return captureScreenshot();\n * }\n * if (action.type === \"click\") {\n * await page.mouse.click(action.x, action.y, { button: action.button });\n * return captureScreenshot();\n * }\n * if (action.type === \"type\") {\n * await page.keyboard.type(action.text);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * return captureScreenshot();\n * },\n * });\n *\n * const llmWithComputer = model.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Check the latest news on bing.com\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Without execute callback (manual action handling)\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * });\n *\n * const response = await model.invoke(\"Check the news\", {\n * tools: [computer],\n * });\n *\n * // Access the computer call from the response\n * const computerCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"computer_call\"\n * );\n * if (computerCall) {\n * console.log(\"Action to execute:\", computerCall.action);\n * // Execute the action manually, then send back a screenshot\n * }\n * ```\n *\n * @example\n * ```typescript\n * // For macOS desktop automation with Docker\n * const computer = tools.computerUse({\n * displayWidth: 1920,\n * displayHeight: 1080,\n * environment: \"mac\",\n * execute: async (action) => {\n * if (action.type === \"click\") {\n * await dockerExec(\n * `DISPLAY=:99 xdotool mousemove ${action.x} ${action.y} click 1`,\n * containerName\n * );\n * }\n * // Capture screenshot from container\n * return await getDockerScreenshot(containerName);\n * },\n * });\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Requires `computer-use-preview` model\n * - Actions include: click, double_click, drag, keypress, move, screenshot, scroll, type, wait\n * - Safety checks may be returned that require acknowledgment before proceeding\n * - Use `truncation: \"auto\"` parameter when making requests\n * - Recommended to use with `reasoning.summary` for debugging\n */\nexport function computerUse(options: ComputerUseOptions) {\n const computerTool = tool(\n async (\n input: ComputerUseInput,\n runtime: ToolRuntime<{ messages: BaseMessage[] }>\n ) => {\n /**\n * get computer_use call id from runtime\n */\n const aiMessage = runtime.state?.messages.at(-1) as AIMessage | undefined;\n const computerToolCall = aiMessage?.tool_calls?.find(\n (tc) => tc.name === \"computer_use\"\n );\n const computerToolCallId = computerToolCall?.id;\n if (!computerToolCallId) {\n throw new Error(\"Computer use call id not found\");\n }\n\n const result = await options.execute(input.action, runtime);\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n if (typeof result === \"string\") {\n return new ToolMessage({\n content: result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n },\n });\n }\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n return new ToolMessage({\n ...result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n ...result.additional_kwargs,\n },\n });\n },\n {\n name: TOOL_NAME,\n description:\n \"Control a computer interface by executing mouse clicks, keyboard input, scrolling, and other actions.\",\n schema: ComputerUseActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_use_preview\",\n display_width: options.displayWidth,\n display_height: options.displayHeight,\n environment: options.environment,\n } satisfies ComputerUseTool,\n };\n\n /**\n * return as typed {@link DynamicStructuredTool} so we don't get any type\n * errors like \"can't export tool without reference\"\n */\n return computerTool as DynamicStructuredTool<\n typeof ComputerUseActionSchema,\n ComputerUseInput,\n unknown,\n ComputerUseReturnType\n >;\n}\n"],"mappings":";;;;AAkDA,MAAM,oCAAoC,EAAE,OAAO,EACjD,MAAM,EAAE,QAAQ,aAAa,EAC9B,CAAC;AAEF,MAAM,+BAA+B,EAAE,OAAO;CAC5C,MAAM,EAAE,QAAQ,QAAQ;CACxB,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACb,QAAQ,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,qCAAqC,EAAE,OAAO;CAClD,MAAM,EAAE,QAAQ,eAAe;CAC/B,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACb,QAAQ,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,8BAA8B,EAAE,OAAO;CAC3C,MAAM,EAAE,QAAQ,OAAO;CACvB,MAAM,EAAE,MAAM,EAAE,OAAO;EAAE,GAAG,EAAE,QAAQ;EAAE,GAAG,EAAE,QAAQ;EAAE,CAAC,CAAC;CAC1D,CAAC;AAEF,MAAM,kCAAkC,EAAE,OAAO;CAC/C,MAAM,EAAE,QAAQ,WAAW;CAC3B,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAEF,MAAM,8BAA8B,EAAE,OAAO;CAC3C,MAAM,EAAE,QAAQ,OAAO;CACvB,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACd,CAAC;AAEF,MAAM,gCAAgC,EAAE,OAAO;CAC7C,MAAM,EAAE,QAAQ,SAAS;CACzB,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QA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;GACvB,gBAAgB,QAAQ;GACxB,aAAa,QAAQ;GACtB;EACF;;;;;AAMD,QAAO"}
|
|
1
|
+
{"version":3,"file":"computerUse.js","names":[],"sources":["../../src/tools/computerUse.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { tool, type DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { type ToolRuntime } from \"@langchain/core/tools\";\nimport {\n ToolMessage,\n type AIMessage,\n type BaseMessage,\n} from \"@langchain/core/messages\";\n\n/**\n * The type of computer environment to control.\n */\nexport type ComputerUseEnvironment =\n | \"browser\"\n | \"mac\"\n | \"windows\"\n | \"linux\"\n | \"ubuntu\";\n\n/**\n * Re-export action types from OpenAI SDK for convenience.\n */\nexport type ComputerUseClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Click;\nexport type ComputerUseDoubleClickAction =\n OpenAIClient.Responses.ResponseComputerToolCall.DoubleClick;\nexport type ComputerUseDragAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Drag;\nexport type ComputerUseKeypressAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Keypress;\nexport type ComputerUseMoveAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Move;\nexport type ComputerUseScreenshotAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Screenshot;\nexport type ComputerUseScrollAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Scroll;\nexport type ComputerUseTypeAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Type;\nexport type ComputerUseWaitAction =\n OpenAIClient.Responses.ResponseComputerToolCall.Wait;\n\n/**\n * Union type of all computer use actions from OpenAI SDK.\n */\nexport type ComputerUseAction =\n OpenAIClient.Responses.ResponseComputerToolCall[\"action\"];\n\n// Zod schemas for computer use actions\nconst ComputerUseScreenshotActionSchema = z.object({\n type: z.literal(\"screenshot\"),\n});\n\nconst ComputerUseClickActionSchema = z.object({\n type: z.literal(\"click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDoubleClickActionSchema = z.object({\n type: z.literal(\"double_click\"),\n x: z.number(),\n y: z.number(),\n button: z.enum([\"left\", \"right\", \"wheel\", \"back\", \"forward\"]).default(\"left\"),\n});\n\nconst ComputerUseDragActionSchema = z.object({\n type: z.literal(\"drag\"),\n path: z.array(z.object({ x: z.number(), y: z.number() })),\n});\n\nconst ComputerUseKeypressActionSchema = z.object({\n type: z.literal(\"keypress\"),\n keys: z.array(z.string()),\n});\n\nconst ComputerUseMoveActionSchema = z.object({\n type: z.literal(\"move\"),\n x: z.number(),\n y: z.number(),\n});\n\nconst ComputerUseScrollActionSchema = z.object({\n type: z.literal(\"scroll\"),\n x: z.number(),\n y: z.number(),\n scroll_x: z.number(),\n scroll_y: z.number(),\n});\n\nconst ComputerUseTypeActionSchema = z.object({\n type: z.literal(\"type\"),\n text: z.string(),\n});\n\nconst ComputerUseWaitActionSchema = z.object({\n type: z.literal(\"wait\"),\n duration: z.number().optional(),\n});\n\n// Union schema for individual action types\nconst ComputerUseActionUnionSchema = z.union([\n ComputerUseScreenshotActionSchema,\n ComputerUseClickActionSchema,\n ComputerUseDoubleClickActionSchema,\n ComputerUseDragActionSchema,\n ComputerUseKeypressActionSchema,\n ComputerUseMoveActionSchema,\n ComputerUseScrollActionSchema,\n ComputerUseTypeActionSchema,\n ComputerUseWaitActionSchema,\n]);\n\n// Schema for the input structure received from parseComputerCall\n// The action is wrapped in an `action` property: { action: { type: 'screenshot' } }\nexport const ComputerUseActionSchema = z.object({\n action: ComputerUseActionUnionSchema,\n});\n\n// TypeScript types derived from Zod schemas\nexport type ComputerUseScreenshotActionType = z.infer<\n typeof ComputerUseScreenshotActionSchema\n>;\nexport type ComputerUseClickActionType = z.infer<\n typeof ComputerUseClickActionSchema\n>;\nexport type ComputerUseDoubleClickActionType = z.infer<\n typeof ComputerUseDoubleClickActionSchema\n>;\nexport type ComputerUseDragActionType = z.infer<\n typeof ComputerUseDragActionSchema\n>;\nexport type ComputerUseKeypressActionType = z.infer<\n typeof ComputerUseKeypressActionSchema\n>;\nexport type ComputerUseMoveActionType = z.infer<\n typeof ComputerUseMoveActionSchema\n>;\nexport type ComputerUseScrollActionType = z.infer<\n typeof ComputerUseScrollActionSchema\n>;\nexport type ComputerUseTypeActionType = z.infer<\n typeof ComputerUseTypeActionSchema\n>;\nexport type ComputerUseWaitActionType = z.infer<\n typeof ComputerUseWaitActionSchema\n>;\n\n/**\n * Input structure for the Computer Use tool.\n * The action is wrapped in an `action` property.\n */\nexport interface ComputerUseInput {\n action: ComputerUseAction;\n}\n\nexport type ComputerUseReturnType =\n | string\n | Promise<string>\n | ToolMessage<any>\n | Promise<ToolMessage<any>>;\n\n/**\n * Options for the Computer Use tool.\n */\nexport interface ComputerUseOptions {\n /**\n * The width of the computer display in pixels.\n */\n displayWidth: number;\n\n /**\n * The height of the computer display in pixels.\n */\n displayHeight: number;\n\n /**\n * The type of computer environment to control.\n * - `browser`: Browser automation (recommended for most use cases)\n * - `mac`: macOS environment\n * - `windows`: Windows environment\n * - `linux`: Linux environment\n * - `ubuntu`: Ubuntu environment\n */\n environment: ComputerUseEnvironment;\n\n /**\n * Execute function that handles computer action execution.\n * This function receives the action input and should return a base64-encoded\n * screenshot of the result.\n */\n execute: (\n action: ComputerUseAction,\n runtime: ToolRuntime<any, any>\n ) => ComputerUseReturnType;\n}\n\n/**\n * OpenAI Computer Use tool type for the Responses API.\n */\nexport type ComputerUseTool = OpenAIClient.Responses.ComputerUsePreviewTool;\n\nconst TOOL_NAME = \"computer_use\";\n\n/**\n * Creates a Computer Use tool that allows models to control computer interfaces\n * and perform tasks by simulating mouse clicks, keyboard input, scrolling, and more.\n *\n * **Computer Use** is a practical application of OpenAI's Computer-Using Agent (CUA)\n * model (`computer-use-preview`), which combines vision capabilities with advanced\n * reasoning to simulate controlling computer interfaces.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends computer actions (click, type, scroll, etc.)\n * 2. Your code executes these actions in a controlled environment\n * 3. You capture a screenshot of the result\n * 4. Send the screenshot back to the model\n * 5. Repeat until the task is complete\n *\n * **Important**: Computer use is in beta and requires careful consideration:\n * - Use in sandboxed environments only\n * - Do not use for high-stakes or authenticated tasks\n * - Always implement human-in-the-loop for important decisions\n * - Handle safety checks appropriately\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-computer-use | OpenAI Computer Use Documentation}\n *\n * @param options - Configuration options for the Computer Use tool\n * @returns A Computer Use tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"computer-use-preview\" });\n *\n * // With execute callback for automatic action handling\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * execute: async (action) => {\n * if (action.type === \"screenshot\") {\n * return captureScreenshot();\n * }\n * if (action.type === \"click\") {\n * await page.mouse.click(action.x, action.y, { button: action.button });\n * return captureScreenshot();\n * }\n * if (action.type === \"type\") {\n * await page.keyboard.type(action.text);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * return captureScreenshot();\n * },\n * });\n *\n * const llmWithComputer = model.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Check the latest news on bing.com\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Without execute callback (manual action handling)\n * const computer = tools.computerUse({\n * displayWidth: 1024,\n * displayHeight: 768,\n * environment: \"browser\",\n * });\n *\n * const response = await model.invoke(\"Check the news\", {\n * tools: [computer],\n * });\n *\n * // Access the computer call from the response\n * const computerCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"computer_call\"\n * );\n * if (computerCall) {\n * console.log(\"Action to execute:\", computerCall.action);\n * // Execute the action manually, then send back a screenshot\n * }\n * ```\n *\n * @example\n * ```typescript\n * // For macOS desktop automation with Docker\n * const computer = tools.computerUse({\n * displayWidth: 1920,\n * displayHeight: 1080,\n * environment: \"mac\",\n * execute: async (action) => {\n * if (action.type === \"click\") {\n * await dockerExec(\n * `DISPLAY=:99 xdotool mousemove ${action.x} ${action.y} click 1`,\n * containerName\n * );\n * }\n * // Capture screenshot from container\n * return await getDockerScreenshot(containerName);\n * },\n * });\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Requires `computer-use-preview` model\n * - Actions include: click, double_click, drag, keypress, move, screenshot, scroll, type, wait\n * - Safety checks may be returned that require acknowledgment before proceeding\n * - Use `truncation: \"auto\"` parameter when making requests\n * - Recommended to use with `reasoning.summary` for debugging\n */\nexport function computerUse(options: ComputerUseOptions) {\n const computerTool = tool(\n async (\n input: ComputerUseInput,\n runtime: ToolRuntime<{ messages: BaseMessage[] }>\n ) => {\n /**\n * get computer_use call id from runtime\n */\n const aiMessage = runtime.state?.messages.at(-1) as AIMessage | undefined;\n const computerToolCall = aiMessage?.tool_calls?.find(\n (tc) => tc.name === \"computer_use\"\n );\n const computerToolCallId = computerToolCall?.id;\n if (!computerToolCallId) {\n throw new Error(\"Computer use call id not found\");\n }\n\n const result = await options.execute(input.action, runtime);\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n if (typeof result === \"string\") {\n return new ToolMessage({\n content: result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n },\n });\n }\n\n /**\n * make sure {@link ToolMessage} is returned with the correct additional kwargs\n */\n return new ToolMessage({\n ...result,\n tool_call_id: computerToolCallId,\n additional_kwargs: {\n type: \"computer_call_output\",\n ...result.additional_kwargs,\n },\n });\n },\n {\n name: TOOL_NAME,\n description:\n \"Control a computer interface by executing mouse clicks, keyboard input, scrolling, and other actions.\",\n schema: ComputerUseActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_use_preview\",\n display_width: options.displayWidth,\n display_height: options.displayHeight,\n environment: options.environment,\n } satisfies ComputerUseTool,\n };\n\n /**\n * return as typed {@link DynamicStructuredTool} so we don't get any type\n * errors like \"can't export tool without reference\"\n */\n return computerTool as DynamicStructuredTool<\n typeof ComputerUseActionSchema,\n ComputerUseInput,\n unknown,\n ComputerUseReturnType\n >;\n}\n"],"mappings":";;;;AAkDA,MAAM,oCAAoC,EAAE,OAAO,EACjD,MAAM,EAAE,QAAQ,aAAa,EAC9B,CAAC;AAEF,MAAM,+BAA+B,EAAE,OAAO;CAC5C,MAAM,EAAE,QAAQ,QAAQ;CACxB,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACb,QAAQ,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,qCAAqC,EAAE,OAAO;CAClD,MAAM,EAAE,QAAQ,eAAe;CAC/B,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACb,QAAQ,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAS;EAAQ;EAAU,CAAC,CAAC,QAAQ,OAAO;CAC9E,CAAC;AAEF,MAAM,8BAA8B,EAAE,OAAO;CAC3C,MAAM,EAAE,QAAQ,OAAO;CACvB,MAAM,EAAE,MAAM,EAAE,OAAO;EAAE,GAAG,EAAE,QAAQ;EAAE,GAAG,EAAE,QAAQ;EAAE,CAAC,CAAC;CAC1D,CAAC;AAEF,MAAM,kCAAkC,EAAE,OAAO;CAC/C,MAAM,EAAE,QAAQ,WAAW;CAC3B,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAEF,MAAM,8BAA8B,EAAE,OAAO;CAC3C,MAAM,EAAE,QAAQ,OAAO;CACvB,GAAG,EAAE,QAAQ;CACb,GAAG,EAAE,QAAQ;CACd,CAAC;AAEF,MAAM,gCAAgC,EAAE,OAAO;CAC7C,MAAM,EAAE,QAAQ,SAAS;CACzB,GAAG,EAAE,Q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tB,MAAM;GACN,eAAe,QAAQ;GACvB,gBAAgB,QAAQ;GACxB,aAAa,QAAQ;GACtB;EACF;;;;;AAMD,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.cjs","names":["DynamicTool","AsyncLocalStorageProviderSingleton"],"sources":["../../src/tools/custom.ts"],"sourcesContent":["import {\n patchConfig,\n pickRunnableConfigKeys,\n RunnableFunc,\n} from \"@langchain/core/runnables\";\nimport { AsyncLocalStorageProviderSingleton } from \"@langchain/core/singletons\";\nimport { DynamicTool, ToolRunnableConfig } from \"@langchain/core/tools\";\nimport OpenAI from \"openai\";\n\nexport type CustomToolFields = Omit<OpenAI.Responses.CustomTool, \"type\">;\n\nexport function customTool(\n func: RunnableFunc<string, string, ToolRunnableConfig>,\n fields: CustomToolFields\n): DynamicTool<string> {\n return new DynamicTool({\n ...fields,\n description: \"\",\n metadata: {\n customTool: fields,\n },\n func: async (input, runManager, config) =>\n new Promise<string>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n //
|
|
1
|
+
{"version":3,"file":"custom.cjs","names":["DynamicTool","AsyncLocalStorageProviderSingleton"],"sources":["../../src/tools/custom.ts"],"sourcesContent":["import {\n patchConfig,\n pickRunnableConfigKeys,\n RunnableFunc,\n} from \"@langchain/core/runnables\";\nimport { AsyncLocalStorageProviderSingleton } from \"@langchain/core/singletons\";\nimport { DynamicTool, ToolRunnableConfig } from \"@langchain/core/tools\";\nimport OpenAI from \"openai\";\n\nexport type CustomToolFields = Omit<OpenAI.Responses.CustomTool, \"type\">;\n\nexport function customTool(\n func: RunnableFunc<string, string, ToolRunnableConfig>,\n fields: CustomToolFields\n): DynamicTool<string> {\n return new DynamicTool({\n ...fields,\n description: \"\",\n metadata: {\n customTool: fields,\n },\n func: async (input, runManager, config) =>\n new Promise<string>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n resolve(func(input, childConfig));\n } catch (e) {\n reject(e);\n }\n }\n );\n }),\n });\n}\n"],"mappings":";;;;AAWA,SAAgB,WACd,MACA,QACqB;AACrB,QAAO,IAAIA,sBAAAA,YAAY;EACrB,GAAG;EACH,aAAa;EACb,UAAU,EACR,YAAY,QACb;EACD,MAAM,OAAO,OAAO,YAAY,WAC9B,IAAI,SAAiB,SAAS,WAAW;GACvC,MAAM,eAAA,GAAA,0BAAA,aAA0B,QAAQ,EACtC,WAAW,YAAY,UAAU,EAClC,CAAC;AAEGC,8BAAAA,mCAAmC,eAAA,GAAA,0BAAA,wBACf,YAAY,EACnC,YAAY;AACV,QAAI;AACF,aAAQ,KAAK,OAAO,YAAY,CAAC;aAC1B,GAAG;AACV,YAAO,EAAE;;KAGd;IACD;EACL,CAAC"}
|
package/dist/tools/custom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.js","names":[],"sources":["../../src/tools/custom.ts"],"sourcesContent":["import {\n patchConfig,\n pickRunnableConfigKeys,\n RunnableFunc,\n} from \"@langchain/core/runnables\";\nimport { AsyncLocalStorageProviderSingleton } from \"@langchain/core/singletons\";\nimport { DynamicTool, ToolRunnableConfig } from \"@langchain/core/tools\";\nimport OpenAI from \"openai\";\n\nexport type CustomToolFields = Omit<OpenAI.Responses.CustomTool, \"type\">;\n\nexport function customTool(\n func: RunnableFunc<string, string, ToolRunnableConfig>,\n fields: CustomToolFields\n): DynamicTool<string> {\n return new DynamicTool({\n ...fields,\n description: \"\",\n metadata: {\n customTool: fields,\n },\n func: async (input, runManager, config) =>\n new Promise<string>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n //
|
|
1
|
+
{"version":3,"file":"custom.js","names":[],"sources":["../../src/tools/custom.ts"],"sourcesContent":["import {\n patchConfig,\n pickRunnableConfigKeys,\n RunnableFunc,\n} from \"@langchain/core/runnables\";\nimport { AsyncLocalStorageProviderSingleton } from \"@langchain/core/singletons\";\nimport { DynamicTool, ToolRunnableConfig } from \"@langchain/core/tools\";\nimport OpenAI from \"openai\";\n\nexport type CustomToolFields = Omit<OpenAI.Responses.CustomTool, \"type\">;\n\nexport function customTool(\n func: RunnableFunc<string, string, ToolRunnableConfig>,\n fields: CustomToolFields\n): DynamicTool<string> {\n return new DynamicTool({\n ...fields,\n description: \"\",\n metadata: {\n customTool: fields,\n },\n func: async (input, runManager, config) =>\n new Promise<string>((resolve, reject) => {\n const childConfig = patchConfig(config, {\n callbacks: runManager?.getChild(),\n });\n // oxlint-disable-next-line no-void\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n pickRunnableConfigKeys(childConfig),\n async () => {\n try {\n resolve(func(input, childConfig));\n } catch (e) {\n reject(e);\n }\n }\n );\n }),\n });\n}\n"],"mappings":";;;;AAWA,SAAgB,WACd,MACA,QACqB;AACrB,QAAO,IAAI,YAAY;EACrB,GAAG;EACH,aAAa;EACb,UAAU,EACR,YAAY,QACb;EACD,MAAM,OAAO,OAAO,YAAY,WAC9B,IAAI,SAAiB,SAAS,WAAW;GACvC,MAAM,cAAc,YAAY,QAAQ,EACtC,WAAW,YAAY,UAAU,EAClC,CAAC;AAEG,sCAAmC,cACtC,uBAAuB,YAAY,EACnC,YAAY;AACV,QAAI;AACF,aAAQ,KAAK,OAAO,YAAY,CAAC;aAC1B,GAAG;AACV,YAAO,EAAE;;KAGd;IACD;EACL,CAAC"}
|