sheetnext 0.2.11 → 0.2.12

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.
@@ -1,284 +1,284 @@
1
- # AI Request Contract
2
-
3
- > Manual supplemental document. This file is not generated by `scripts/doc-scanner.js`.
4
-
5
- ## Purpose
6
-
7
- This document describes the request data built by the SheetNext front end and the response format that the configured `AI_URL` endpoint must accept.
8
-
9
- It does not define your internal server implementation.
10
-
11
- The exact client behavior is based on `src/core/AI/AI.js`.
12
-
13
- ## Client Configuration
14
-
15
- Configure the AI endpoint when creating `SheetNext`:
16
-
17
- ```js
18
- const SN = new SheetNext(container, {
19
- AI_URL: 'https://your-server.example.com/api/ai',
20
- AI_TOKEN: 'optional-token'
21
- })
22
- ```
23
-
24
- - `AI_URL`: required if you want to use the built-in AI runtime
25
- - `AI_TOKEN`: optional; when present, the browser sends `Authorization: Bearer <token>`
26
-
27
- ## What the Front End Sends
28
-
29
- The front end sends an HTTP `POST` request to `AI_URL` with:
30
-
31
- - `Content-Type: application/json`
32
- - optional `Authorization: Bearer <token>`
33
-
34
- Request body:
35
-
36
- ```json
37
- {
38
- "messages": [
39
- {
40
- "role": "system",
41
- "content": "System prompt text"
42
- },
43
- {
44
- "role": "user",
45
- "content": "User instruction"
46
- }
47
- ],
48
- "tools": [
49
- {
50
- "type": "function",
51
- "function": {
52
- "name": "exampleTool",
53
- "description": "Tool description",
54
- "parameters": {
55
- "type": "object",
56
- "properties": {}
57
- }
58
- }
59
- }
60
- ],
61
- "isUserStart": true
62
- }
63
- ```
64
-
65
- ## Request Field Meaning
66
-
67
- ### `messages`
68
-
69
- This is the main Chat Completions-style message array built by the client.
70
-
71
- The front end assembles it from:
72
-
73
- - a system prompt
74
- - previous conversation history
75
- - optional attachment messages
76
- - a final system snapshot describing current workbook context
77
-
78
- Each message uses the usual chat structure:
79
-
80
- ```json
81
- {
82
- "role": "user",
83
- "content": "plain text"
84
- }
85
- ```
86
-
87
- For attachment cases, `content` can also be an array of parts:
88
-
89
- ```json
90
- {
91
- "role": "user",
92
- "content": [
93
- {
94
- "type": "text",
95
- "text": "User uploaded attachments:"
96
- },
97
- {
98
- "type": "image_url",
99
- "image_url": {
100
- "url": "data:image/png;base64,..."
101
- }
102
- }
103
- ]
104
- }
105
- ```
106
-
107
- So from the server perspective:
108
-
109
- - `messages` should be accepted as-is
110
- - `content` may be either a string or a multimodal content array
111
-
112
- ### `tools`
113
-
114
- This is the built-in tool definition array generated by SheetNext and sent together with the messages.
115
-
116
- The server should accept it as-is.
117
-
118
- If your model provider supports tool/function calling, you can forward these definitions directly or map them to your provider's equivalent format.
119
-
120
- ### `isUserStart`
121
-
122
- Boolean flag:
123
-
124
- - `true`: this request starts a new user turn
125
- - `false`: this request is a follow-up turn in the current workflow
126
-
127
- The front end sends it for server-side flow control if needed.
128
-
129
- ## What the Server Must Accept
130
-
131
- At minimum, the configured `AI_URL` endpoint should accept:
132
-
133
- ```json
134
- {
135
- "messages": "Chat Completions-style message array",
136
- "tools": "SheetNext tool definitions",
137
- "isUserStart": true
138
- }
139
- ```
140
-
141
- In practical terms:
142
-
143
- - `messages`: required
144
- - `tools`: required by the current client contract
145
- - `isUserStart`: required by the current client contract
146
-
147
- ## What the Front End Accepts Back
148
-
149
- The current client expects a streaming response and parses it as SSE-style `data:` lines.
150
-
151
- Recommended response header:
152
-
153
- ```text
154
- Content-Type: text/event-stream
155
- ```
156
-
157
- Stream example:
158
-
159
- ```text
160
- data: {"type":"text","delta":"Hello"}
161
-
162
- data: {"type":"text","delta":" world"}
163
-
164
- data: [DONE]
165
- ```
166
-
167
- Each event should be one `data: ...` line followed by a blank line.
168
-
169
- ## Supported Response Chunks
170
-
171
- ### `text`
172
-
173
- Append assistant text to the UI.
174
-
175
- ```json
176
- {
177
- "type": "text",
178
- "delta": "Hello"
179
- }
180
- ```
181
-
182
- ### `tool_call`
183
-
184
- Incrementally stream a tool call. `arguments` may be partial.
185
-
186
- ```json
187
- {
188
- "type": "tool_call",
189
- "tool_call": {
190
- "index": 0,
191
- "id": "call_1",
192
- "type": "function",
193
- "function": {
194
- "name": "setCellValue",
195
- "arguments": "{\"range\":\"A1\""
196
- }
197
- }
198
- }
199
- ```
200
-
201
- ### `tool_call_complete`
202
-
203
- Provide the completed tool call payload.
204
-
205
- ```json
206
- {
207
- "type": "tool_call_complete",
208
- "tool_call": {
209
- "index": 0,
210
- "id": "call_1",
211
- "type": "function",
212
- "function": {
213
- "name": "setCellValue",
214
- "arguments": "{\"range\":\"A1\",\"value\":123}"
215
- }
216
- }
217
- }
218
- ```
219
-
220
- ### `usage`
221
-
222
- Optional usage information.
223
-
224
- ```json
225
- {
226
- "type": "usage",
227
- "usage": {
228
- "input_tokens": 1200,
229
- "output_tokens": 260,
230
- "total_tokens": 1460
231
- }
232
- }
233
- ```
234
-
235
- ### `web_search`
236
-
237
- Optional status chunk recognized by the current client.
238
-
239
- ```json
240
- {
241
- "type": "web_search",
242
- "status": "in_progress"
243
- }
244
- ```
245
-
246
- or:
247
-
248
- ```json
249
- {
250
- "type": "web_search",
251
- "status": "completed"
252
- }
253
- ```
254
-
255
- ### `error`
256
-
257
- Structured stream error.
258
-
259
- ```json
260
- {
261
- "error": {
262
- "message": "Model request failed"
263
- }
264
- }
265
- ```
266
-
267
- ## Error Handling
268
-
269
- - non-2xx HTTP responses are treated as request failures
270
- - a 2xx response can still fail by streaming an `error` payload
271
- - plain text or HTML error bodies are shown as error content
272
- - unknown chunk types are ignored by the current client
273
-
274
- ## Recommended Server Strategy
275
-
276
- The simplest integration is:
277
-
278
- 1. receive the JSON body from SheetNext
279
- 2. read `messages`, `tools`, and `isUserStart`
280
- 3. call your model service
281
- 4. convert the model output into the SSE chunk format above
282
- 5. stream it back to the browser
283
-
284
- As long as the server accepts the request body above and returns the chunk format above, the current SheetNext AI front end can work with it.
1
+ # AI Request Contract
2
+
3
+ > Manual supplemental document. This file is not generated by `scripts/doc-scanner.js`.
4
+
5
+ ## Purpose
6
+
7
+ This document describes the request data built by the SheetNext front end and the response format that the configured `AI_URL` endpoint must accept.
8
+
9
+ It does not define your internal server implementation.
10
+
11
+ The exact client behavior is based on `src/core/AI/AI.js`.
12
+
13
+ ## Client Configuration
14
+
15
+ Configure the AI endpoint when creating `SheetNext`:
16
+
17
+ ```js
18
+ const SN = new SheetNext(container, {
19
+ AI_URL: 'https://your-server.example.com/api/ai',
20
+ AI_TOKEN: 'optional-token'
21
+ })
22
+ ```
23
+
24
+ - `AI_URL`: required if you want to use the built-in AI runtime
25
+ - `AI_TOKEN`: optional; when present, the browser sends `Authorization: Bearer <token>`
26
+
27
+ ## What the Front End Sends
28
+
29
+ The front end sends an HTTP `POST` request to `AI_URL` with:
30
+
31
+ - `Content-Type: application/json`
32
+ - optional `Authorization: Bearer <token>`
33
+
34
+ Request body:
35
+
36
+ ```json
37
+ {
38
+ "messages": [
39
+ {
40
+ "role": "system",
41
+ "content": "System prompt text"
42
+ },
43
+ {
44
+ "role": "user",
45
+ "content": "User instruction"
46
+ }
47
+ ],
48
+ "tools": [
49
+ {
50
+ "type": "function",
51
+ "function": {
52
+ "name": "exampleTool",
53
+ "description": "Tool description",
54
+ "parameters": {
55
+ "type": "object",
56
+ "properties": {}
57
+ }
58
+ }
59
+ }
60
+ ],
61
+ "isUserStart": true
62
+ }
63
+ ```
64
+
65
+ ## Request Field Meaning
66
+
67
+ ### `messages`
68
+
69
+ This is the main Chat Completions-style message array built by the client.
70
+
71
+ The front end assembles it from:
72
+
73
+ - a system prompt
74
+ - previous conversation history
75
+ - optional attachment messages
76
+ - a final system snapshot describing current workbook context
77
+
78
+ Each message uses the usual chat structure:
79
+
80
+ ```json
81
+ {
82
+ "role": "user",
83
+ "content": "plain text"
84
+ }
85
+ ```
86
+
87
+ For attachment cases, `content` can also be an array of parts:
88
+
89
+ ```json
90
+ {
91
+ "role": "user",
92
+ "content": [
93
+ {
94
+ "type": "text",
95
+ "text": "User uploaded attachments:"
96
+ },
97
+ {
98
+ "type": "image_url",
99
+ "image_url": {
100
+ "url": "data:image/png;base64,..."
101
+ }
102
+ }
103
+ ]
104
+ }
105
+ ```
106
+
107
+ So from the server perspective:
108
+
109
+ - `messages` should be accepted as-is
110
+ - `content` may be either a string or a multimodal content array
111
+
112
+ ### `tools`
113
+
114
+ This is the built-in tool definition array generated by SheetNext and sent together with the messages.
115
+
116
+ The server should accept it as-is.
117
+
118
+ If your model provider supports tool/function calling, you can forward these definitions directly or map them to your provider's equivalent format.
119
+
120
+ ### `isUserStart`
121
+
122
+ Boolean flag:
123
+
124
+ - `true`: this request starts a new user turn
125
+ - `false`: this request is a follow-up turn in the current workflow
126
+
127
+ The front end sends it for server-side flow control if needed.
128
+
129
+ ## What the Server Must Accept
130
+
131
+ At minimum, the configured `AI_URL` endpoint should accept:
132
+
133
+ ```json
134
+ {
135
+ "messages": "Chat Completions-style message array",
136
+ "tools": "SheetNext tool definitions",
137
+ "isUserStart": true
138
+ }
139
+ ```
140
+
141
+ In practical terms:
142
+
143
+ - `messages`: required
144
+ - `tools`: required by the current client contract
145
+ - `isUserStart`: required by the current client contract
146
+
147
+ ## What the Front End Accepts Back
148
+
149
+ The current client expects a streaming response and parses it as SSE-style `data:` lines.
150
+
151
+ Recommended response header:
152
+
153
+ ```text
154
+ Content-Type: text/event-stream
155
+ ```
156
+
157
+ Stream example:
158
+
159
+ ```text
160
+ data: {"type":"text","delta":"Hello"}
161
+
162
+ data: {"type":"text","delta":" world"}
163
+
164
+ data: [DONE]
165
+ ```
166
+
167
+ Each event should be one `data: ...` line followed by a blank line.
168
+
169
+ ## Supported Response Chunks
170
+
171
+ ### `text`
172
+
173
+ Append assistant text to the UI.
174
+
175
+ ```json
176
+ {
177
+ "type": "text",
178
+ "delta": "Hello"
179
+ }
180
+ ```
181
+
182
+ ### `tool_call`
183
+
184
+ Incrementally stream a tool call. `arguments` may be partial.
185
+
186
+ ```json
187
+ {
188
+ "type": "tool_call",
189
+ "tool_call": {
190
+ "index": 0,
191
+ "id": "call_1",
192
+ "type": "function",
193
+ "function": {
194
+ "name": "setCellValue",
195
+ "arguments": "{\"range\":\"A1\""
196
+ }
197
+ }
198
+ }
199
+ ```
200
+
201
+ ### `tool_call_complete`
202
+
203
+ Provide the completed tool call payload.
204
+
205
+ ```json
206
+ {
207
+ "type": "tool_call_complete",
208
+ "tool_call": {
209
+ "index": 0,
210
+ "id": "call_1",
211
+ "type": "function",
212
+ "function": {
213
+ "name": "setCellValue",
214
+ "arguments": "{\"range\":\"A1\",\"value\":123}"
215
+ }
216
+ }
217
+ }
218
+ ```
219
+
220
+ ### `usage`
221
+
222
+ Optional usage information.
223
+
224
+ ```json
225
+ {
226
+ "type": "usage",
227
+ "usage": {
228
+ "input_tokens": 1200,
229
+ "output_tokens": 260,
230
+ "total_tokens": 1460
231
+ }
232
+ }
233
+ ```
234
+
235
+ ### `web_search`
236
+
237
+ Optional status chunk recognized by the current client.
238
+
239
+ ```json
240
+ {
241
+ "type": "web_search",
242
+ "status": "in_progress"
243
+ }
244
+ ```
245
+
246
+ or:
247
+
248
+ ```json
249
+ {
250
+ "type": "web_search",
251
+ "status": "completed"
252
+ }
253
+ ```
254
+
255
+ ### `error`
256
+
257
+ Structured stream error.
258
+
259
+ ```json
260
+ {
261
+ "error": {
262
+ "message": "Model request failed"
263
+ }
264
+ }
265
+ ```
266
+
267
+ ## Error Handling
268
+
269
+ - non-2xx HTTP responses are treated as request failures
270
+ - a 2xx response can still fail by streaming an `error` payload
271
+ - plain text or HTML error bodies are shown as error content
272
+ - unknown chunk types are ignored by the current client
273
+
274
+ ## Recommended Server Strategy
275
+
276
+ The simplest integration is:
277
+
278
+ 1. receive the JSON body from SheetNext
279
+ 2. read `messages`, `tools`, and `isUserStart`
280
+ 3. call your model service
281
+ 4. convert the model output into the SSE chunk format above
282
+ 5. stream it back to the browser
283
+
284
+ As long as the server accepts the request body above and returns the chunk format above, the current SheetNext AI front end can work with it.