-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcode-interpreter.ts
More file actions
176 lines (135 loc) · 4.58 KB
/
Copy pathcode-interpreter.ts
File metadata and controls
176 lines (135 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../../core/resource';
import * as SessionsAPI from './sessions';
import { SessionListResponse, Sessions } from './sessions';
import { APIPromise } from '../../core/api-promise';
import { RequestOptions } from '../../internal/request-options';
export class CodeInterpreter extends APIResource {
sessions: SessionsAPI.Sessions = new SessionsAPI.Sessions(this._client);
/**
* Executes the given code snippet and returns the output. Without a session_id, a
* new session is created to run the code. If you pass a valid session_id, the code
* runs in that session. This is useful for running multiple code snippets in the
* same environment, because dependencies and similar things are persisted between
* calls to the same session.
*
* @example
* ```ts
* const executeResponse =
* await client.codeInterpreter.execute({
* code: "print('Hello, world!')",
* language: 'python',
* });
* ```
*/
execute(body: CodeInterpreterExecuteParams, options?: RequestOptions): APIPromise<ExecuteResponse> {
return this._client.post('/tci/execute', { body, ...options });
}
}
/**
* The result of the execution. If successful, `data` contains the result and
* `errors` is null. If unsuccessful, `data` is null and `errors` contains the
* errors.
*/
export type ExecuteResponse = ExecuteResponse.SuccessfulExecution | ExecuteResponse.FailedExecution;
export namespace ExecuteResponse {
export interface SuccessfulExecution {
data: SuccessfulExecution.Data;
errors: null;
}
export namespace SuccessfulExecution {
export interface Data {
outputs: Array<Data.StreamOutput | Data.Error | Data.DisplayorExecuteOutput>;
/**
* Identifier of the current session. Used to make follow-up calls.
*/
session_id: string;
/**
* Status of the execution. Currently only supports success.
*/
status?: 'success';
}
export namespace Data {
/**
* Outputs that were printed to stdout or stderr
*/
export interface StreamOutput {
data: string;
type: 'stdout' | 'stderr';
}
/**
* Errors and exceptions that occurred. If this output type is present, your code
* did not execute successfully.
*/
export interface Error {
data: string;
type: 'error';
}
export interface DisplayorExecuteOutput {
data: DisplayorExecuteOutput.Data;
type: 'display_data' | 'execute_result';
}
export namespace DisplayorExecuteOutput {
export interface Data {
'application/geo+json'?: { [key: string]: unknown };
'application/javascript'?: string;
'application/json'?: { [key: string]: unknown };
'application/pdf'?: string;
'application/vnd.vega.v5+json'?: { [key: string]: unknown };
'application/vnd.vegalite.v4+json'?: { [key: string]: unknown };
'image/gif'?: string;
'image/jpeg'?: string;
'image/png'?: string;
'image/svg+xml'?: string;
'text/html'?: string;
'text/latex'?: string;
'text/markdown'?: string;
'text/plain'?: string;
}
}
}
}
export interface FailedExecution {
data: null;
errors: Array<string | { [key: string]: unknown }>;
}
}
export interface CodeInterpreterExecuteParams {
/**
* Code snippet to execute.
*/
code: string;
/**
* Programming language for the code to execute. Currently only supports Python.
*/
language: 'python';
/**
* Files to upload to the session. If present, files are uploaded before executing
* the given code.
*/
files?: Array<CodeInterpreterExecuteParams.File>;
/**
* Identifier of the current session. Used to make follow-up calls. Returns an
* error if the session does not belong to the caller or has expired.
*/
session_id?: string;
}
export namespace CodeInterpreterExecuteParams {
export interface File {
content: string;
/**
* Encoding of the file content. Use `string` for text files such as code, and
* `base64` for binary files, such as images.
*/
encoding: 'string' | 'base64';
name: string;
}
}
CodeInterpreter.Sessions = Sessions;
export declare namespace CodeInterpreter {
export {
type ExecuteResponse as ExecuteResponse,
type CodeInterpreterExecuteParams as CodeInterpreterExecuteParams,
};
export { Sessions as Sessions, type SessionListResponse as SessionListResponse };
}