sarvam-agents-client 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/dist/compat/core/agents.d.ts +53 -0
  2. package/dist/compat/core/agents.js +338 -0
  3. package/dist/compat/core/data.d.ts +10 -0
  4. package/dist/compat/core/data.js +275 -0
  5. package/dist/compat/core/errors.d.ts +37 -0
  6. package/dist/compat/core/errors.js +155 -0
  7. package/dist/compat/core/events.d.ts +107 -0
  8. package/dist/compat/core/events.js +160 -0
  9. package/dist/compat/core/files.d.ts +65 -0
  10. package/dist/compat/core/files.js +851 -0
  11. package/dist/compat/core/history.d.ts +9 -0
  12. package/dist/compat/core/history.js +112 -0
  13. package/dist/compat/core/http.d.ts +112 -0
  14. package/dist/compat/core/http.js +753 -0
  15. package/dist/compat/core/ids.d.ts +4 -0
  16. package/dist/compat/core/ids.js +36 -0
  17. package/dist/compat/core/index.d.ts +14 -0
  18. package/dist/compat/core/index.js +30 -0
  19. package/dist/compat/core/models.d.ts +20 -0
  20. package/dist/compat/core/models.js +75 -0
  21. package/dist/compat/core/paging.d.ts +14 -0
  22. package/dist/compat/core/paging.js +124 -0
  23. package/dist/compat/core/refusals.d.ts +9 -0
  24. package/dist/compat/core/refusals.js +25 -0
  25. package/dist/compat/core/sessions.d.ts +65 -0
  26. package/dist/compat/core/sessions.js +317 -0
  27. package/dist/compat/core/toolResults.d.ts +43 -0
  28. package/dist/compat/core/toolResults.js +208 -0
  29. package/dist/compat/core/turns.d.ts +104 -0
  30. package/dist/compat/core/turns.js +504 -0
  31. package/dist/compat/core/warnings.d.ts +6 -0
  32. package/dist/compat/core/warnings.js +26 -0
  33. package/dist/compat/index.d.ts +3 -0
  34. package/dist/compat/index.js +9 -0
  35. package/dist/compat/openai/client.d.ts +62 -0
  36. package/dist/compat/openai/client.js +108 -0
  37. package/dist/compat/openai/errors.d.ts +72 -0
  38. package/dist/compat/openai/errors.js +150 -0
  39. package/dist/compat/openai/index.d.ts +14 -0
  40. package/dist/compat/openai/index.js +42 -0
  41. package/dist/compat/openai/pages.d.ts +19 -0
  42. package/dist/compat/openai/pages.js +39 -0
  43. package/dist/compat/openai/resources/agents.d.ts +36 -0
  44. package/dist/compat/openai/resources/agents.js +549 -0
  45. package/dist/compat/openai/resources/events.d.ts +23 -0
  46. package/dist/compat/openai/resources/events.js +485 -0
  47. package/dist/compat/openai/resources/sessions.d.ts +33 -0
  48. package/dist/compat/openai/resources/sessions.js +283 -0
  49. package/dist/compat/openai/stream.d.ts +40 -0
  50. package/dist/compat/openai/stream.js +585 -0
  51. package/dist/compat/openai/types.d.ts +125 -0
  52. package/dist/compat/openai/types.js +98 -0
  53. package/dist/compose/agent.d.ts +2 -0
  54. package/dist/compose/agent.js +27 -0
  55. package/dist/compose/functionTool.d.ts +2 -0
  56. package/dist/compose/functionTool.js +46 -0
  57. package/dist/compose/output.d.ts +2 -0
  58. package/dist/compose/output.js +15 -7
  59. package/dist/esm/compat/core/agents.d.ts +53 -0
  60. package/dist/esm/compat/core/agents.js +334 -0
  61. package/dist/esm/compat/core/data.d.ts +10 -0
  62. package/dist/esm/compat/core/data.js +272 -0
  63. package/dist/esm/compat/core/errors.d.ts +37 -0
  64. package/dist/esm/compat/core/errors.js +146 -0
  65. package/dist/esm/compat/core/events.d.ts +107 -0
  66. package/dist/esm/compat/core/events.js +156 -0
  67. package/dist/esm/compat/core/files.d.ts +65 -0
  68. package/dist/esm/compat/core/files.js +829 -0
  69. package/dist/esm/compat/core/history.d.ts +9 -0
  70. package/dist/esm/compat/core/history.js +109 -0
  71. package/dist/esm/compat/core/http.d.ts +112 -0
  72. package/dist/esm/compat/core/http.js +746 -0
  73. package/dist/esm/compat/core/ids.d.ts +4 -0
  74. package/dist/esm/compat/core/ids.js +30 -0
  75. package/dist/esm/compat/core/index.d.ts +14 -0
  76. package/dist/esm/compat/core/index.js +14 -0
  77. package/dist/esm/compat/core/models.d.ts +20 -0
  78. package/dist/esm/compat/core/models.js +71 -0
  79. package/dist/esm/compat/core/paging.d.ts +14 -0
  80. package/dist/esm/compat/core/paging.js +118 -0
  81. package/dist/esm/compat/core/refusals.d.ts +9 -0
  82. package/dist/esm/compat/core/refusals.js +21 -0
  83. package/dist/esm/compat/core/sessions.d.ts +65 -0
  84. package/dist/esm/compat/core/sessions.js +312 -0
  85. package/dist/esm/compat/core/toolResults.d.ts +43 -0
  86. package/dist/esm/compat/core/toolResults.js +203 -0
  87. package/dist/esm/compat/core/turns.d.ts +104 -0
  88. package/dist/esm/compat/core/turns.js +497 -0
  89. package/dist/esm/compat/core/warnings.d.ts +6 -0
  90. package/dist/esm/compat/core/warnings.js +20 -0
  91. package/dist/esm/compat/index.d.ts +3 -0
  92. package/dist/esm/compat/index.js +3 -0
  93. package/dist/esm/compat/openai/client.d.ts +62 -0
  94. package/dist/esm/compat/openai/client.js +104 -0
  95. package/dist/esm/compat/openai/errors.d.ts +72 -0
  96. package/dist/esm/compat/openai/errors.js +130 -0
  97. package/dist/esm/compat/openai/index.d.ts +14 -0
  98. package/dist/esm/compat/openai/index.js +17 -0
  99. package/dist/esm/compat/openai/pages.d.ts +19 -0
  100. package/dist/esm/compat/openai/pages.js +35 -0
  101. package/dist/esm/compat/openai/resources/agents.d.ts +36 -0
  102. package/dist/esm/compat/openai/resources/agents.js +540 -0
  103. package/dist/esm/compat/openai/resources/events.d.ts +23 -0
  104. package/dist/esm/compat/openai/resources/events.js +479 -0
  105. package/dist/esm/compat/openai/resources/sessions.d.ts +33 -0
  106. package/dist/esm/compat/openai/resources/sessions.js +278 -0
  107. package/dist/esm/compat/openai/stream.d.ts +40 -0
  108. package/dist/esm/compat/openai/stream.js +579 -0
  109. package/dist/esm/compat/openai/types.d.ts +125 -0
  110. package/dist/esm/compat/openai/types.js +91 -0
  111. package/dist/esm/compose/agent.d.ts +2 -0
  112. package/dist/esm/compose/agent.js +28 -2
  113. package/dist/esm/compose/functionTool.d.ts +2 -0
  114. package/dist/esm/compose/functionTool.js +45 -0
  115. package/dist/esm/compose/output.d.ts +2 -0
  116. package/dist/esm/compose/output.js +14 -7
  117. package/dist/esm/index.d.ts +2 -0
  118. package/dist/esm/index.js +2 -0
  119. package/dist/index.d.ts +2 -0
  120. package/dist/index.js +5 -1
  121. package/package.json +53 -11
  122. package/src/compat/core/agents.ts +437 -0
  123. package/src/compat/core/data.ts +283 -0
  124. package/src/compat/core/errors.ts +162 -0
  125. package/src/compat/core/events.ts +198 -0
  126. package/src/compat/core/files.ts +913 -0
  127. package/src/compat/core/history.ts +116 -0
  128. package/src/compat/core/http.ts +827 -0
  129. package/src/compat/core/ids.ts +35 -0
  130. package/src/compat/core/index.ts +14 -0
  131. package/src/compat/core/models.ts +85 -0
  132. package/src/compat/core/paging.ts +119 -0
  133. package/src/compat/core/refusals.ts +31 -0
  134. package/src/compat/core/sessions.ts +358 -0
  135. package/src/compat/core/toolResults.ts +235 -0
  136. package/src/compat/core/turns.ts +622 -0
  137. package/src/compat/core/warnings.ts +23 -0
  138. package/src/compat/index.ts +3 -0
  139. package/src/compat/openai/client.ts +177 -0
  140. package/src/compat/openai/errors.ts +165 -0
  141. package/src/compat/openai/index.ts +65 -0
  142. package/src/compat/openai/pages.ts +55 -0
  143. package/src/compat/openai/resources/agents.ts +603 -0
  144. package/src/compat/openai/resources/events.ts +523 -0
  145. package/src/compat/openai/resources/sessions.ts +369 -0
  146. package/src/compat/openai/stream.ts +599 -0
  147. package/src/compat/openai/types.ts +217 -0
  148. package/src/compose/agent.ts +30 -3
  149. package/src/compose/functionTool.ts +44 -0
  150. package/src/compose/output.ts +14 -8
  151. package/src/index.ts +3 -0
@@ -0,0 +1,283 @@
1
+ // AUTO-GENERATED from shared/compat/refusals.json by scripts/syncCompatData.mjs. Do not edit manually.
2
+ export interface RefusalSpec {
3
+ provider: string;
4
+ path: string;
5
+ param: string;
6
+ reason: string;
7
+ hint: string;
8
+ needs: string | null;
9
+ feature: string;
10
+ }
11
+
12
+ export const REFUSALS: readonly RefusalSpec[] = [
13
+ {
14
+ "provider": "openai",
15
+ "path": "environment.type=self_hosted",
16
+ "param": "environment.type",
17
+ "reason": "Sarvam runs agents in a managed sandbox only",
18
+ "hint": "Use environment={'type': 'openai_hosted'} (sandbox on) or {'type': 'none'} (sandbox off).",
19
+ "needs": null,
20
+ "feature": "openai.environment.self_hosted"
21
+ },
22
+ {
23
+ "provider": "openai",
24
+ "path": "environment.vault_ids",
25
+ "param": "environment.vault_ids",
26
+ "reason": "OpenAI vaults are not supported",
27
+ "hint": "Configure agent apps or custom APIs via sarvam_config={'apps': [...]}.",
28
+ "needs": null,
29
+ "feature": "openai.environment.vault_ids"
30
+ },
31
+ {
32
+ "provider": "openai",
33
+ "path": "environment.spend_control",
34
+ "param": "environment.spend_control",
35
+ "reason": "spend_control is not supported",
36
+ "hint": "Use sarvam_config={'limits': {'max_turns': ..., 'max_tokens_per_turn': ...}} instead.",
37
+ "needs": null,
38
+ "feature": "openai.environment.spend_control"
39
+ },
40
+ {
41
+ "provider": "openai",
42
+ "path": "environment.contradicts_agent",
43
+ "param": "environment.type",
44
+ "reason": "sandbox setting is configured per agent on Sarvam and contradicts the existing agent",
45
+ "hint": "Match the agent's sandbox setting or update the agent first.",
46
+ "needs": null,
47
+ "feature": "openai.environment.contradicts_agent"
48
+ },
49
+ {
50
+ "provider": "openai",
51
+ "path": "tools[type=mcp]",
52
+ "param": "tools",
53
+ "reason": "ad-hoc MCP servers by URL are not supported yet",
54
+ "hint": "Bind installed apps via sarvam_config={'apps': [...]} instead.",
55
+ "needs": "panja: MCP by URL (harness phase 4)",
56
+ "feature": "openai.tools.mcp"
57
+ },
58
+ {
59
+ "provider": "openai",
60
+ "path": "tools[type=computer_use]",
61
+ "param": "tools",
62
+ "reason": "computer use is not supported",
63
+ "hint": "",
64
+ "needs": null,
65
+ "feature": "openai.tools.computer_use"
66
+ },
67
+ {
68
+ "provider": "openai",
69
+ "path": "tools[type=web_search].options",
70
+ "param": "tools",
71
+ "reason": "only domain filters (allowed_domains or blocked_domains) are supported on web_search",
72
+ "hint": "Pass filters={'allowed_domains': [...]} without extra web_search options.",
73
+ "needs": null,
74
+ "feature": "openai.tools.web_search.options"
75
+ },
76
+ {
77
+ "provider": "openai",
78
+ "path": "tools[type=programmatic_tool_calling].no_sandbox",
79
+ "param": "tools",
80
+ "reason": "programmatic_tool_calling requires the managed sandbox",
81
+ "hint": "Use environment={'type': 'openai_hosted'} when programmatic_tool_calling is enabled.",
82
+ "needs": null,
83
+ "feature": "openai.tools.programmatic_tool_calling.no_sandbox"
84
+ },
85
+ {
86
+ "provider": "openai",
87
+ "path": "multi_agent.max_concurrent_subagents",
88
+ "param": "multi_agent.max_concurrent_subagents",
89
+ "reason": "sub-agent concurrency cap is not configurable yet",
90
+ "hint": "Omit max_concurrent_subagents to use the platform default.",
91
+ "needs": "panja: sub-agent concurrency (harness phase 9)",
92
+ "feature": "openai.multi_agent.max_concurrent_subagents"
93
+ },
94
+ {
95
+ "provider": "openai",
96
+ "path": "text.verbosity",
97
+ "param": "text.verbosity",
98
+ "reason": "text verbosity is not configurable",
99
+ "hint": "Omit text.verbosity to use the default.",
100
+ "needs": null,
101
+ "feature": "openai.text.verbosity"
102
+ },
103
+ {
104
+ "provider": "openai",
105
+ "path": "service_tier",
106
+ "param": "service_tier",
107
+ "reason": "service_tier is not supported",
108
+ "hint": "Omit service_tier to use the default.",
109
+ "needs": null,
110
+ "feature": "openai.service_tier"
111
+ },
112
+ {
113
+ "provider": "openai",
114
+ "path": "experimental",
115
+ "param": "experimental",
116
+ "reason": "experimental provider flags are not supported",
117
+ "hint": "",
118
+ "needs": null,
119
+ "feature": "openai.experimental"
120
+ },
121
+ {
122
+ "provider": "openai",
123
+ "path": "compaction",
124
+ "param": "compaction",
125
+ "reason": "compaction is automatic and cannot be customized",
126
+ "hint": "Omit compaction to use automatic compaction.",
127
+ "needs": null,
128
+ "feature": "openai.compaction"
129
+ },
130
+ {
131
+ "provider": "openai",
132
+ "path": "tool_choice",
133
+ "param": "tool_choice",
134
+ "reason": "only 'auto' and 'none' are supported for tool_choice",
135
+ "hint": "Use tool_choice='auto' or tool_choice='none'.",
136
+ "needs": null,
137
+ "feature": "openai.tool_choice"
138
+ },
139
+ {
140
+ "provider": "openai",
141
+ "path": "model",
142
+ "param": "model",
143
+ "reason": "unknown model is not in the Sarvam model catalog",
144
+ "hint": "Pass a Sarvam model ID, configure model_map / SARVAM_MODEL_MAP, or disable strict_models.",
145
+ "needs": null,
146
+ "feature": "openai.model.unknown"
147
+ },
148
+ {
149
+ "provider": "openai",
150
+ "path": "sessions.update.model",
151
+ "param": "model",
152
+ "reason": "per-session model overrides are not supported yet",
153
+ "hint": "Update the agent instead.",
154
+ "needs": "panja: per-conversation overrides (harness phase 11)",
155
+ "feature": "openai.sessions.update.overrides"
156
+ },
157
+ {
158
+ "provider": "openai",
159
+ "path": "sessions.update.reasoning",
160
+ "param": "reasoning",
161
+ "reason": "per-session reasoning overrides are not supported yet",
162
+ "hint": "Update the agent instead.",
163
+ "needs": "panja: per-conversation overrides (harness phase 11)",
164
+ "feature": "openai.sessions.update.overrides"
165
+ },
166
+ {
167
+ "provider": "openai",
168
+ "path": "sessions.update.tools",
169
+ "param": "tools",
170
+ "reason": "per-session tool overrides are not supported yet",
171
+ "hint": "Update the agent instead.",
172
+ "needs": "panja: per-conversation overrides (harness phase 11)",
173
+ "feature": "openai.sessions.update.overrides"
174
+ },
175
+ {
176
+ "provider": "openai",
177
+ "path": "sessions.update.agent",
178
+ "param": "agent",
179
+ "reason": "per-session agent overrides are not supported yet",
180
+ "hint": "Update the agent instead.",
181
+ "needs": "panja: per-conversation overrides (harness phase 11)",
182
+ "feature": "openai.sessions.update.overrides"
183
+ },
184
+ {
185
+ "provider": "openai",
186
+ "path": "sessions.create.pin_agent_version",
187
+ "param": "pin_agent_version",
188
+ "reason": "pinning a session to an agent configuration snapshot is not supported yet",
189
+ "hint": "",
190
+ "needs": "panja: session version pinning",
191
+ "feature": "openai.sessions.pin_version"
192
+ },
193
+ {
194
+ "provider": "openai",
195
+ "path": "sessions.create.idempotency_key",
196
+ "param": "idempotency_key",
197
+ "reason": "idempotent session creation is not supported by Panja yet",
198
+ "hint": "Omit idempotencyKey / Idempotency-Key on sessions.create; Panja does not deduplicate POST /v1/conversations yet.",
199
+ "needs": "panja: Idempotency-Key on POST /v1/conversations",
200
+ "feature": "openai.sessions.create.idempotency_key"
201
+ },
202
+ {
203
+ "provider": "openai",
204
+ "path": "agents.create.idempotency_key",
205
+ "param": "idempotency_key",
206
+ "reason": "idempotent agent creation is not supported by Panja yet",
207
+ "hint": "Omit idempotencyKey / Idempotency-Key on agents.create; Panja does not deduplicate POST /v2/agents yet.",
208
+ "needs": "panja: Idempotency-Key on POST /v2/agents",
209
+ "feature": "openai.agents.create.idempotency_key"
210
+ },
211
+ {
212
+ "provider": "openai",
213
+ "path": "input.input_image.detail",
214
+ "param": "input",
215
+ "reason": "input_image.detail is not supported on agent sessions",
216
+ "hint": "Omit detail on input_image blocks; Sarvam ignores image detail tiers.",
217
+ "needs": null,
218
+ "feature": "openai.input.image.detail"
219
+ },
220
+ {
221
+ "provider": "openai",
222
+ "path": "beta.agents.environments",
223
+ "param": "beta.agents.environments",
224
+ "reason": "managed environments resource is not supported",
225
+ "hint": "Pass environment={'type': 'openai_hosted'} or {'type': 'none'} to sessions.create.",
226
+ "needs": null,
227
+ "feature": "openai.resource.environments"
228
+ },
229
+ {
230
+ "provider": "openai",
231
+ "path": "beta.agents.templates",
232
+ "param": "beta.agents.templates",
233
+ "reason": "environment templates are not supported",
234
+ "hint": "",
235
+ "needs": null,
236
+ "feature": "openai.resource.templates"
237
+ },
238
+ {
239
+ "provider": "openai",
240
+ "path": "beta.agents.files",
241
+ "param": "beta.agents.files",
242
+ "reason": "agent files resource is not supported",
243
+ "hint": "",
244
+ "needs": null,
245
+ "feature": "openai.resource.files"
246
+ },
247
+ {
248
+ "provider": "openai",
249
+ "path": "beta.agents.vaults",
250
+ "param": "beta.agents.vaults",
251
+ "reason": "vaults are not supported",
252
+ "hint": "",
253
+ "needs": null,
254
+ "feature": "openai.resource.vaults"
255
+ },
256
+ {
257
+ "provider": "openai",
258
+ "path": "beta.agents.credentials",
259
+ "param": "beta.agents.credentials",
260
+ "reason": "credentials resource is not supported",
261
+ "hint": "",
262
+ "needs": null,
263
+ "feature": "openai.resource.credentials"
264
+ },
265
+ {
266
+ "provider": "openai",
267
+ "path": "beta.agents.sessions.traces",
268
+ "param": "beta.agents.sessions.traces",
269
+ "reason": "session traces resource is not supported",
270
+ "hint": "",
271
+ "needs": null,
272
+ "feature": "openai.resource.traces"
273
+ },
274
+ {
275
+ "provider": "openai",
276
+ "path": "extra_body",
277
+ "param": "extra_body",
278
+ "reason": "extra_body keys other than 'sarvam_config' are not supported",
279
+ "hint": "Pass Sarvam extensions under sarvam_config={...} or extra_body={'sarvam_config': {...}}.",
280
+ "needs": null,
281
+ "feature": "openai.extra_body"
282
+ }
283
+ ] as const;
@@ -0,0 +1,162 @@
1
+ /** Neutral error types for sarvam-agents-client/compat/core. */
2
+
3
+ export interface CoreErrorOptions {
4
+ status?: number | null;
5
+ code?: string | null;
6
+ param?: string | null;
7
+ requestId?: string | null;
8
+ body?: unknown;
9
+ headers?: Record<string, string>;
10
+ }
11
+
12
+ export class CoreAPIError extends Error {
13
+ readonly status: number | null;
14
+ readonly code: string | null;
15
+ readonly param: string | null;
16
+ readonly requestId: string | null;
17
+ readonly body: unknown;
18
+ readonly headers: Record<string, string>;
19
+
20
+ constructor(message: string, opts: CoreErrorOptions = {}) {
21
+ super(message);
22
+ this.name = "CoreAPIError";
23
+ this.status = opts.status ?? null;
24
+ this.code = opts.code ?? null;
25
+ this.param = opts.param ?? null;
26
+ this.requestId = opts.requestId ?? null;
27
+ this.body = opts.body ?? null;
28
+ this.headers = { ...(opts.headers ?? {}) };
29
+ }
30
+ }
31
+
32
+ export class CoreConnectionError extends CoreAPIError {
33
+ constructor(message: string, opts: CoreErrorOptions = {}) {
34
+ super(message, opts);
35
+ this.name = "CoreConnectionError";
36
+ }
37
+ }
38
+
39
+ export class CoreTimeoutError extends CoreConnectionError {
40
+ constructor(message: string, opts: CoreErrorOptions = {}) {
41
+ super(message, opts);
42
+ this.name = "CoreTimeoutError";
43
+ }
44
+ }
45
+
46
+ export class CoreUserAbortError extends CoreAPIError {
47
+ constructor(message = "Request was aborted.", opts: CoreErrorOptions = {}) {
48
+ super(message, { code: "user_abort", ...opts });
49
+ this.name = "CoreUserAbortError";
50
+ }
51
+ }
52
+
53
+ export class Unsupported extends CoreAPIError {
54
+ readonly feature: string;
55
+ readonly needs: string | null;
56
+
57
+ constructor(
58
+ message: string,
59
+ opts: { param: string | null; feature: string; needs?: string | null },
60
+ ) {
61
+ const body = {
62
+ error: {
63
+ message,
64
+ type: "invalid_request_error",
65
+ param: opts.param,
66
+ code: "unsupported_by_sarvam",
67
+ },
68
+ };
69
+ super(message, {
70
+ status: 400,
71
+ code: "unsupported_by_sarvam",
72
+ param: opts.param,
73
+ requestId: null,
74
+ body,
75
+ headers: {},
76
+ });
77
+ this.name = "Unsupported";
78
+ this.feature = opts.feature;
79
+ this.needs = opts.needs ?? null;
80
+ }
81
+ }
82
+
83
+ function getHeader(headers: Record<string, string>, name: string): string | null {
84
+ const low = name.toLowerCase();
85
+ for (const [k, v] of Object.entries(headers)) {
86
+ if (k.toLowerCase() === low) return v;
87
+ }
88
+ return null;
89
+ }
90
+
91
+ function parseBody(raw: unknown): unknown {
92
+ if (raw === null || raw === undefined) return null;
93
+ if (typeof raw === "object") return raw;
94
+ if (typeof raw === "string" && raw.trim().length > 0) {
95
+ try {
96
+ return JSON.parse(raw);
97
+ } catch {
98
+ return raw;
99
+ }
100
+ }
101
+ return null;
102
+ }
103
+
104
+ export function mapPanjaError(
105
+ status: number,
106
+ rawBody: unknown,
107
+ headers: Record<string, string> = {},
108
+ ): CoreAPIError {
109
+ const requestId = getHeader(headers, "x-request-id");
110
+ const body = parseBody(rawBody);
111
+ let code: string | null = null;
112
+ let param: string | null = null;
113
+ let message = `HTTP ${status}`;
114
+
115
+ if (body && typeof body === "object" && !Array.isArray(body)) {
116
+ const obj = body as Record<string, unknown>;
117
+ const err = obj["error"];
118
+ const detail = obj["detail"];
119
+ if (err && typeof err === "object" && !Array.isArray(err)) {
120
+ const e = err as Record<string, unknown>;
121
+ if (typeof e["code"] === "string") code = e["code"];
122
+ if (typeof e["message"] === "string" && e["message"]) message = e["message"];
123
+ const details = e["details"];
124
+ if (Array.isArray(details) && details.length > 0 && details[0] && typeof details[0] === "object") {
125
+ const first = details[0] as Record<string, unknown>;
126
+ if (first["reason"]) code = String(first["reason"]);
127
+ if (first["param"]) param = String(first["param"]);
128
+ else if (first["path"]) param = String(first["path"]);
129
+ }
130
+ } else if (Array.isArray(detail) && detail.length > 0 && detail[0] && typeof detail[0] === "object") {
131
+ const first = detail[0] as Record<string, unknown>;
132
+ const loc = first["loc"];
133
+ if (Array.isArray(loc)) {
134
+ const parts = loc.map(String).filter((p) => p !== "body" && p !== "query" && p !== "path");
135
+ param = parts.length > 0 ? parts.join(".") : null;
136
+ }
137
+ if (typeof first["msg"] === "string" && first["msg"]) message = first["msg"];
138
+ code = "invalid_request_error";
139
+ } else if (detail && typeof detail === "object" && !Array.isArray(detail)) {
140
+ const d = detail as Record<string, unknown>;
141
+ if (d["code"]) code = String(d["code"]);
142
+ else if (d["reason"]) code = String(d["reason"]);
143
+ if (typeof d["message"] === "string" && d["message"]) message = d["message"];
144
+ if (d["param"]) param = String(d["param"]);
145
+ } else if (typeof detail === "string" && detail) {
146
+ message = detail;
147
+ }
148
+ } else if (typeof body === "string" && body) {
149
+ message = body;
150
+ }
151
+
152
+ if (status === 422 && code === null) code = "invalid_request_error";
153
+
154
+ return new CoreAPIError(message, {
155
+ status,
156
+ code,
157
+ param,
158
+ requestId,
159
+ body,
160
+ headers,
161
+ });
162
+ }
@@ -0,0 +1,198 @@
1
+ /** Neutral turn event model built from Panja /v1/responses UI-message chunks. */
2
+ import { fromUiChunk, type FunctionCallRequest } from "../../compose/events.js";
3
+ import { itemId as makeItemId } from "./ids.js";
4
+
5
+ const SANDBOX_TOOLS = new Set(["bash", "code_execute", "run_program"]);
6
+ const BUILTIN_TOOLS = new Set([
7
+ "web_search", "web_fetch", "bash", "code_execute", "run_program", "read_file",
8
+ "write_file", "patch", "search_files", "todo", "cron", "clarify", "delegate_task",
9
+ "share_file", "list_artifacts", "skill_view",
10
+ ]);
11
+
12
+ export interface Usage {
13
+ inputTokens: number;
14
+ cachedTokens: number;
15
+ outputTokens: number;
16
+ reasoningTokens: number;
17
+ totalTokens: number;
18
+ }
19
+
20
+ export interface AttachmentRef {
21
+ fileId: string;
22
+ filename: string;
23
+ mediaType: string;
24
+ }
25
+
26
+ export type TurnEvent =
27
+ | { kind: "turn_started"; responseId: string }
28
+ | { kind: "text_started"; itemId: string }
29
+ | { kind: "text_delta"; itemId: string; delta: string }
30
+ | { kind: "text_done"; itemId: string; text: string; attachments?: AttachmentRef[] }
31
+ | { kind: "reasoning_started"; itemId: string }
32
+ | { kind: "reasoning_delta"; itemId: string; delta: string }
33
+ | { kind: "reasoning_done"; itemId: string; text: string }
34
+ | { kind: "tool_called"; itemId: string; callId: string; name: string; input: unknown; toolKind: "builtin" | "app" }
35
+ | { kind: "tool_output"; itemId: string; callId: string; output: unknown; isSandbox: boolean }
36
+ | { kind: "function_calls_requested"; requestId: string; calls: FunctionCallRequest[]; itemIds: string[] }
37
+ | { kind: "clarify_requested"; requestId: string; questions: Record<string, unknown>[] }
38
+ | { kind: "output_ready"; output: unknown; skipped?: string; mode?: string }
39
+ | { kind: "output_mismatch"; errors: string[]; rawText: string }
40
+ | { kind: "turn_limit_reached"; capKind: string; limit?: string; value?: number }
41
+ | { kind: "file_shared"; data: Record<string, unknown> }
42
+ | { kind: "stopped"; reason: string }
43
+ | { kind: "turn_failed"; code: string; message: string }
44
+ | { kind: "turn_finished"; usage: Usage }
45
+ | { kind: "unknown"; raw: Record<string, unknown> };
46
+
47
+ function isObj(v: unknown): v is Record<string, unknown> {
48
+ return v !== null && typeof v === "object" && !Array.isArray(v);
49
+ }
50
+
51
+ function parseUsage(chunk: Record<string, unknown>): Usage {
52
+ const meta = chunk["messageMetadata"];
53
+ const raw = isObj(meta) && isObj(meta["usage"]) ? meta["usage"] : null;
54
+ if (!raw) return { inputTokens: 0, cachedTokens: 0, outputTokens: 0, reasoningTokens: 0, totalTokens: 0 };
55
+ const inputTokens = Number(raw["inputTokens"] ?? raw["input_tokens"] ?? 0);
56
+ const outputTokens = Number(raw["outputTokens"] ?? raw["output_tokens"] ?? 0);
57
+ const cachedTokens = Number(raw["cacheReadTokens"] ?? raw["cached_tokens"] ?? 0);
58
+ const reasoningTokens = Number(raw["reasoningTokens"] ?? raw["reasoning_tokens"] ?? 0);
59
+ const totalTokens = Number(raw["totalTokens"] ?? raw["total_tokens"] ?? inputTokens + outputTokens);
60
+ return { inputTokens, cachedTokens, outputTokens, reasoningTokens, totalTokens };
61
+ }
62
+
63
+ export class TurnEvents {
64
+ responseId: string;
65
+ private partIndex = 0;
66
+ private textItemId: string | null = null;
67
+ private textBuf: string[] = [];
68
+ private reasoningItemId: string | null = null;
69
+ private reasoningBuf: string[] = [];
70
+ private toolNames = new Map<string, string>();
71
+ private toolItems = new Map<string, string>();
72
+
73
+ constructor(responseId = "") {
74
+ this.responseId = responseId;
75
+ }
76
+
77
+ private nextItemId(): string {
78
+ const idx = this.partIndex++;
79
+ return makeItemId(this.responseId || "turn", idx);
80
+ }
81
+
82
+ private closeReasoning(out: TurnEvent[]): void {
83
+ if (this.reasoningItemId !== null) {
84
+ out.push({ kind: "reasoning_done", itemId: this.reasoningItemId, text: this.reasoningBuf.join("") });
85
+ this.reasoningItemId = null;
86
+ this.reasoningBuf = [];
87
+ }
88
+ }
89
+
90
+ feed(chunk: unknown): TurnEvent[] {
91
+ const out: TurnEvent[] = [];
92
+ if (!isObj(chunk)) {
93
+ this.closeReasoning(out);
94
+ out.push({ kind: "unknown", raw: { value: chunk } });
95
+ return out;
96
+ }
97
+ const type = chunk["type"];
98
+ if (type === "start") {
99
+ const msgId = typeof chunk["messageId"] === "string" && chunk["messageId"] ? chunk["messageId"] : this.responseId;
100
+ if (msgId) this.responseId = msgId;
101
+ this.partIndex = 0;
102
+ out.push({ kind: "turn_started", responseId: this.responseId });
103
+ return out;
104
+ }
105
+ if (type === "data-file-shared") {
106
+ this.closeReasoning(out);
107
+ const data = isObj(chunk["data"]) ? { ...chunk["data"] } : {};
108
+ out.push({ kind: "file_shared", data });
109
+ return out;
110
+ }
111
+ const ev = fromUiChunk(chunk);
112
+ if (ev === null) return out;
113
+
114
+ if (ev.kind === "reasoning_delta") {
115
+ if (this.reasoningItemId === null) {
116
+ this.reasoningItemId = this.nextItemId();
117
+ out.push({ kind: "reasoning_started", itemId: this.reasoningItemId });
118
+ }
119
+ this.reasoningBuf.push(ev.delta);
120
+ out.push({ kind: "reasoning_delta", itemId: this.reasoningItemId, delta: ev.delta });
121
+ return out;
122
+ }
123
+
124
+ this.closeReasoning(out);
125
+
126
+ switch (ev.kind) {
127
+ case "text_start": {
128
+ const iid = this.nextItemId();
129
+ this.textItemId = iid;
130
+ this.textBuf = [];
131
+ out.push({ kind: "text_started", itemId: iid });
132
+ break;
133
+ }
134
+ case "text_delta": {
135
+ if (this.textItemId === null) {
136
+ this.textItemId = this.nextItemId();
137
+ this.textBuf = [];
138
+ out.push({ kind: "text_started", itemId: this.textItemId });
139
+ }
140
+ this.textBuf.push(ev.delta);
141
+ out.push({ kind: "text_delta", itemId: this.textItemId, delta: ev.delta });
142
+ break;
143
+ }
144
+ case "text_end": {
145
+ const iid = this.textItemId ?? this.nextItemId();
146
+ const text = this.textBuf.join("");
147
+ this.textItemId = null;
148
+ this.textBuf = [];
149
+ out.push({ kind: "text_done", itemId: iid, text });
150
+ break;
151
+ }
152
+ case "tool_called": {
153
+ const iid = this.nextItemId();
154
+ this.toolNames.set(ev.callId, ev.name);
155
+ this.toolItems.set(ev.callId, iid);
156
+ const toolKind = BUILTIN_TOOLS.has(ev.name) ? "builtin" : "app";
157
+ out.push({ kind: "tool_called", itemId: iid, callId: ev.callId, name: ev.name, input: ev.arguments, toolKind });
158
+ break;
159
+ }
160
+ case "tool_output": {
161
+ const iid = this.toolItems.get(ev.callId) ?? this.nextItemId();
162
+ const isSandbox = SANDBOX_TOOLS.has(this.toolNames.get(ev.callId) ?? "");
163
+ out.push({ kind: "tool_output", itemId: iid, callId: ev.callId, output: ev.output, isSandbox });
164
+ break;
165
+ }
166
+ case "function_calls_requested": {
167
+ const itemIds = ev.calls.map(() => this.nextItemId());
168
+ out.push({ kind: "function_calls_requested", requestId: ev.requestId, calls: [...ev.calls], itemIds });
169
+ break;
170
+ }
171
+ case "clarify_requested":
172
+ out.push({ kind: "clarify_requested", requestId: ev.requestId, questions: ev.questions as Record<string, unknown>[] });
173
+ break;
174
+ case "output":
175
+ out.push({ kind: "output_ready", output: ev.output, skipped: ev.skipped, mode: ev.mode });
176
+ break;
177
+ case "output_error":
178
+ out.push({ kind: "output_mismatch", errors: [...ev.errors], rawText: ev.rawText });
179
+ break;
180
+ case "turn_limit":
181
+ out.push({ kind: "turn_limit_reached", capKind: ev.capKind, limit: ev.limit, value: ev.value });
182
+ break;
183
+ case "stopped":
184
+ out.push({ kind: "stopped", reason: ev.reason });
185
+ break;
186
+ case "turn_failed":
187
+ out.push({ kind: "turn_failed", code: ev.code, message: ev.message });
188
+ break;
189
+ case "turn_finished":
190
+ out.push({ kind: "turn_finished", usage: parseUsage(chunk) });
191
+ break;
192
+ case "unknown":
193
+ out.push({ kind: "unknown", raw: isObj(ev.chunk) ? ev.chunk : { value: ev.chunk } });
194
+ break;
195
+ }
196
+ return out;
197
+ }
198
+ }